Skip to content

Commit 152627f

Browse files
committed
Merge branch 'add/auto_format'
2 parents c989d84 + 5577292 commit 152627f

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ You can specify more extra options in `g:clang_format#extra_args` as String or L
7474
When this variable's value is `1`, vim-clang-format automatically detects the style file like
7575
`.clang-format` or `_clang-format` and applies the style to formatting.
7676

77+
- `g:clang_format#auto_format`
78+
79+
When the value is 1, a current buffer is automatically formatted on saving the buffer.
80+
Formatting is executed at `BufWritePre` event.
81+
7782
### Vimrc Example
7883

7984
```vim

autoload/clang_format.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ let g:clang_format#code_style = s:getg('clang_format#code_style', 'google')
105105
let g:clang_format#style_options = s:getg('clang_format#style_options', {})
106106

107107
let g:clang_format#detect_style_file = s:getg('clang_format#detect_style_file', 1)
108+
let g:clang_format#auto_format = s:getg('clang_format#auto_format', 0)
108109
" }}}
109110

110111
" check version of clang-format "{{{

plugin/clang_format.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ command! -range=% -nargs=0 ClangFormatEchoFormattedCode echo clang_format#format
1414

1515
augroup plugin-clang-format-auto-format
1616
autocmd!
17-
autocmd BufWritePre * if &ft ==# 'cpp' && get(g:, 'clang_format#auto_format', 0) | call clang_format#replace(1, line('$')) | endif
17+
autocmd BufWritePre * if &ft ==# 'cpp' && g:clang_format#auto_format | call clang_format#replace(1, line('$')) | endif
1818
augroup END
1919

2020
let g:loaded_clang_format = 1

t/clang_format_spec.vim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,30 @@ describe ':ClangFormat'
212212

213213
end
214214
" }}}
215+
216+
217+
" test for auto formatting {{{
218+
describe 'g:clang_format#auto_format'
219+
220+
before
221+
let g:clang_format#auto_format = 1
222+
new
223+
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
224+
end
225+
226+
after
227+
bdelete!
228+
if filereadable('./'.s:root_dir.'t/tmp.cpp')
229+
call delete('./'.s:root_dir.'t/tmp.cpp')
230+
endif
231+
end
232+
233+
it 'formats a current buffer on BufWritePre if the value is 1'
234+
SKIP because somehow BufWritePre event isn't fired
235+
let formatted = clang_format#format(1, line('$'))
236+
doautocmd BufWritePre
237+
let auto_formatted = join(getline(1, line('$')), "\n")
238+
Expect auto_formatted ==# formatted
239+
end
240+
end
241+
" }}}

0 commit comments

Comments
 (0)