Skip to content

Commit 276a9e1

Browse files
committed
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.
1 parent 9bf2a62 commit 276a9e1

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ When the value is 0, `-fallback-style=none` option is added on executing clang-f
116116
It means that vim-clang-format does nothing when `.clang-format` is not found.
117117
The default value is 1.
118118

119+
- `g:clang_format#praise`
120+
121+
When the value is 1, you will be praised for already having well-formatted code.
122+
Some users might not want/need praise, thus providing this variable.
123+
The default value is 1.
124+
119125
## Vimrc Example
120126

121127
```vim

autoload/clang_format.vim

+8
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ let g:clang_format#filetype_style_options = s:getg('clang_format#filetype_style_
191191

192192
let g:clang_format#detect_style_file = s:getg('clang_format#detect_style_file', 1)
193193
let g:clang_format#enable_fallback_style = s:getg('clang_format#enable_fallback_style', 1)
194+
let g:clang_format#praise = s:getg('clang_format#praise', 1)
194195

195196
let g:clang_format#auto_format = s:getg('clang_format#auto_format', 0)
196197
let g:clang_format#auto_format_on_insert_leave = s:getg('clang_format#auto_format_on_insert_leave', 0)
@@ -238,6 +239,13 @@ function! clang_format#replace(line1, line2, ...) abort
238239

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

242250
silent! undojoin
243251
if line('$') > len(splitted)

doc/clang-format.txt

+7
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ g:clang_format#enable_fallback_style *g:clang_format#enable_fallback_style*
206206
".clang-format" is not found.
207207
The default value is 1.
208208

209+
g:clang_format#praise *g:clang_format#praise*
210+
211+
When the value is 1, |vim-clang-format| will praise you for already having
212+
well-formatted code. Some users might not want/need praise, thus
213+
providing this variable.
214+
The default value is 1.
215+
209216

210217

211218
==============================================================================

test/t/clang_format_spec.vim

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ describe 'default settings'
9999
Expect exists('g:clang_format#filetype_style_options') to_be_true
100100
Expect exists('g:clang_format#command') to_be_true
101101
Expect exists('g:clang_format#detect_style_file') to_be_true
102+
Expect exists('g:clang_format#praise') to_be_true
102103
Expect exists('g:clang_format#auto_format') to_be_true
103104
Expect exists('g:clang_format#auto_format_on_insert_leave') to_be_true
104105
Expect g:clang_format#extra_args to_be_empty
@@ -107,6 +108,7 @@ describe 'default settings'
107108
Expect g:clang_format#filetype_style_options to_be_empty
108109
Expect executable(g:clang_format#command) to_be_true
109110
Expect g:clang_format#detect_style_file to_be_true
111+
Expect g:clang_format#praise to_be_true
110112
end
111113

112114
it 'provide commands'

0 commit comments

Comments
 (0)