diff --git a/README.md b/README.md index 4fce884..d2399da 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/autoload/clang_format.vim b/autoload/clang_format.vim index ac9c49a..00acd2e 100644 --- a/autoload/clang_format.vim +++ b/autoload/clang_format.vim @@ -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 @@ -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 @@ -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) @@ -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 - 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 diff --git a/doc/clang-format.txt b/doc/clang-format.txt index 7dccce8..b35be85 100644 --- a/doc/clang-format.txt +++ b/doc/clang-format.txt @@ -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. + ============================================================================== diff --git a/test/VimFlavor.lock b/test/VimFlavor.lock index b4062e6..0cb992c 100644 --- a/test/VimFlavor.lock +++ b/test/VimFlavor.lock @@ -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) diff --git a/test/t/clang_format_spec.vim b/test/t/clang_format_spec.vim index 2616061..cfb498b 100644 --- a/test/t/clang_format_spec.vim +++ b/test/t/clang_format_spec.vim @@ -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 @@ -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' @@ -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 + 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