Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double undo #100

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ When the value is 0, `-fallback-style=none` option is added on executing clang-f
It means that vim-clang-format does nothing when `.clang-format` is not found.
The default value is 1.

- `g:clang_format#praise`

When the value is 1, you will be praised for already having well-formatted code.
Some users might not want/need praise, thus providing this variable.
The default value is 1.

## Vimrc Example

```vim
Expand Down
22 changes: 17 additions & 5 deletions autoload/clang_format.vim
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ endfunction

function! s:error_message(result) abort
echoerr 'clang-format has failed to format.'
if a:result =~# '^YAML:\d\+:\d\+: error: unknown key '
if a:result =~# '^YAML:\d\+:\d\+: error: '
echohl ErrorMsg
for l in split(a:result, "\n")[0:1]
for l in split(a:result, "\n")
echomsg l
endfor
echohl None
Expand Down Expand Up @@ -147,7 +147,7 @@ function! s:verify_command() abort
if invalidity == 1
echoerr "clang-format is not found. check g:clang_format#command."
elseif invalidity == 2
echoerr 'clang-format 3.3 or earlier is not supported for the lack of aruguments'
echoerr 'clang-format 3.3 or earlier is not supported for the lack of arguments'
endif
endfunction

Expand Down Expand Up @@ -191,6 +191,7 @@ let g:clang_format#filetype_style_options = s:getg('clang_format#filetype_style_

let g:clang_format#detect_style_file = s:getg('clang_format#detect_style_file', 1)
let g:clang_format#enable_fallback_style = s:getg('clang_format#enable_fallback_style', 1)
let g:clang_format#praise = s:getg('clang_format#praise', 1)

let g:clang_format#auto_format = s:getg('clang_format#auto_format', 0)
let g:clang_format#auto_format_on_insert_leave = s:getg('clang_format#auto_format_on_insert_leave', 0)
Expand Down Expand Up @@ -236,10 +237,21 @@ function! clang_format#replace(line1, line2, ...) abort
return
endif

let winview = winsaveview()
let splitted = split(formatted, '\n', 1)
if getline(a:line1, a:line2) ==# splitted[a:line1-1:a:line2-1]
if g:clang_format#praise
echo "No formatting needed, looking fabulous already!"
endif
" Early out, no need to introduce a change
return
endif

Kypert marked this conversation as resolved.
Show resolved Hide resolved
silent! undojoin
if g:clang_format#praise
" Reset any previous praise
echo ""
endif

let winview = winsaveview()
if line('$') > len(splitted)
execute len(splitted) .',$delete' '_'
endif
Expand Down
7 changes: 7 additions & 0 deletions doc/clang-format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ g:clang_format#enable_fallback_style *g:clang_format#enable_fallback_style*
".clang-format" is not found.
The default value is 1.

g:clang_format#praise *g:clang_format#praise*

When the value is 1, |vim-clang-format| will praise you for already having
well-formatted code. Some users might not want/need praise, thus
providing this variable.
The default value is 1.



==============================================================================
Expand Down
2 changes: 1 addition & 1 deletion test/VimFlavor.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kana/vim-operator-user (0.1.0)
kana/vim-vspec (1.6.1)
kana/vim-vspec (1.9.0)
rhysd/vim-vspec-matchers (0.0.4)
21 changes: 21 additions & 0 deletions test/t/clang_format_spec.vim
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe 'default settings'
Expect exists('g:clang_format#filetype_style_options') to_be_true
Expect exists('g:clang_format#command') to_be_true
Expect exists('g:clang_format#detect_style_file') to_be_true
Expect exists('g:clang_format#praise') to_be_true
Expect exists('g:clang_format#auto_format') to_be_true
Expect exists('g:clang_format#auto_format_on_insert_leave') to_be_true
Expect g:clang_format#extra_args to_be_empty
Expand All @@ -107,6 +108,7 @@ describe 'default settings'
Expect g:clang_format#filetype_style_options to_be_empty
Expect executable(g:clang_format#command) to_be_true
Expect g:clang_format#detect_style_file to_be_true
Expect g:clang_format#praise to_be_true
end

it 'provide commands'
Expand Down Expand Up @@ -457,9 +459,28 @@ describe 'undo formatting text'
bdelete!
end

it 'can make a change, format (constitutes as a second change), then undo and keep the first change'
normal! ggOmega
ClangFormat
" When running :ClangFormat from a normal vim instance, setline() does add an entry in the undolist
" For some reason it does not do so when running in this test framework
" Also, seems like it is hard to even add another item to the change list too, always rendering:
" # number changes when saved
" # 9 1 0 seconds ago
Comment on lines +465 to +469
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be happy if someone could enlighten me regarding this comment.

normal! ggOadditional
:Debug GetBuffer()
" TODO: should enable this
" normal! u
Expect 0 != search('mega', 'cw')
undo
Expect 0 == search('mega', 'cw')
end

it 'restores previous text as editing buffer normally'
let prev = GetBuffer()
ClangFormat
let buf = GetBuffer()
Expect prev != buf
undo
let buf = GetBuffer()
Expect prev ==# buf
Expand Down