From c1d68a8105412ba86c8061119d8ea71df0c19a24 Mon Sep 17 00:00:00 2001 From: Mathias Jonsson Date: Fri, 17 Apr 2020 20:38:00 +0200 Subject: [PATCH 1/4] Fix small typo in s:verify_command() --- autoload/clang_format.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/clang_format.vim b/autoload/clang_format.vim index ac9c49a..b97b2d0 100644 --- a/autoload/clang_format.vim +++ b/autoload/clang_format.vim @@ -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 From 9bf2a62a06c3cf62aa27c79997adcfacdb504a65 Mon Sep 17 00:00:00 2001 From: Mathias Jonsson Date: Fri, 17 Apr 2020 20:40:23 +0200 Subject: [PATCH 2/4] Expand error message upon -style arg parse error When the user adds invalid arguments to the -style switch of clang-format, the error message may yield different responses depending on the type of failure. Expand error message given to the user to not only be verbose on 'key' errors. An example: let g:clang_format#style_options = { "Standard" : "go" } Will result in: Error detected while processing function clang_format#replace[6]..77_error_message: line 1: clang-format has failed to format. YAML:1:63: error: unknown enumerated scalar {BasedOnStyle: LLVM, IndentWidth: 4, UseTab: false, Standard: 'go'} ^~~~ Error parsing -style: Invalid argument --- autoload/clang_format.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/clang_format.vim b/autoload/clang_format.vim index b97b2d0..c107c26 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 From 2476aa4a2c6f0e06b97f2d1f7c54a062fba1066e Mon Sep 17 00:00:00 2001 From: Mathias Jonsson Date: Fri, 17 Apr 2020 21:00:58 +0200 Subject: [PATCH 3/4] Introduce g:clang_format#praise and avoid changes Avoid add unnecessary items to the change-list if the code is already formatted according to clang-format. Add a cheerful echo to praise the user (positive nudging?). However, some users might not be pleased by that, so introduce a way to disable the joyful echo. --- README.md | 6 ++++++ autoload/clang_format.vim | 13 +++++++++++++ doc/clang-format.txt | 7 +++++++ test/t/clang_format_spec.vim | 2 ++ 4 files changed, 28 insertions(+) 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 c107c26..4c64cb9 100644 --- a/autoload/clang_format.vim +++ b/autoload/clang_format.vim @@ -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) @@ -238,6 +239,18 @@ function! clang_format#replace(line1, line2, ...) abort 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 + + if g:clang_format#praise + " Reset any previous praise + echo "" + endif silent! undojoin if line('$') > len(splitted) 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/t/clang_format_spec.vim b/test/t/clang_format_spec.vim index 2616061..c0cdc69 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' From c6cc7d2e7f98dd2e6b37ff6783a3d57ede8dcc37 Mon Sep 17 00:00:00 2001 From: Mathias Jonsson Date: Fri, 17 Apr 2020 21:20:50 +0200 Subject: [PATCH 4/4] Avoid 2x undo (#98) Kind of hard to follow the history of this, but currently (NVIM v0.5.0-427-g1f56f9a4b) it seems like this undojoin makes some unexpected behaviour for the user. Unable to tell what-else is unlocked by removing it, so let's try without it for a while. --- autoload/clang_format.vim | 3 +-- test/VimFlavor.lock | 2 +- test/t/clang_format_spec.vim | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/autoload/clang_format.vim b/autoload/clang_format.vim index 4c64cb9..00acd2e 100644 --- a/autoload/clang_format.vim +++ b/autoload/clang_format.vim @@ -237,7 +237,6 @@ 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 @@ -252,7 +251,7 @@ function! clang_format#replace(line1, line2, ...) abort echo "" endif - silent! undojoin + let winview = winsaveview() if line('$') > len(splitted) execute len(splitted) .',$delete' '_' endif 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 c0cdc69..cfb498b 100644 --- a/test/t/clang_format_spec.vim +++ b/test/t/clang_format_spec.vim @@ -459,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