Skip to content

Commit c83f840

Browse files
committed
add feature to format automatically on InsertLeave
This feature has some side effects. So you should be careful if you introduce this feature. - Sometime, screen is moved on auto-formatting. - Cursor position history is changed (Issue #8)
1 parent 71f9eb9 commit c83f840

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,16 @@ When this variable's value is `1`, vim-clang-format automatically detects the st
7777
- `g:clang_format#auto_format`
7878

7979
When the value is 1, a current buffer is automatically formatted on saving the buffer.
80-
Formatting is executed at `BufWritePre` event.
80+
Formatting is executed on `BufWritePre` event.
81+
82+
- `g:clang_format#auto_format_on_insert_leave`
83+
84+
When the value is 1, inserted lines are automatically formatted on leaving insert mode.
85+
Formatting is executed on `InsertLeave` event.
86+
87+
![auto formatting on InsertLeave](http://gifzo.net/CPrVbHnKMe.gif)
88+
89+
In above screenshot, inserted 'if' statement are automatically formated on leaving insert mode.
8190

8291
### Vimrc Example
8392

autoload/clang_format.vim

+22
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ 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)
108108
let g:clang_format#auto_format = s:getg('clang_format#auto_format', 0)
109+
let g:clang_format#auto_format_on_insert_leave = s:getg('clang_format#auto_format_on_insert_leave', 0)
109110
" }}}
110111

111112
" check version of clang-format "{{{
@@ -159,5 +160,26 @@ function! clang_format#replace(line1, line2)
159160
endfunction
160161
" }}}
161162

163+
" auto formatting on insert leave {{{
164+
let s:pos_on_insertenter = []
165+
166+
function! s:format_inserted_area()
167+
let pos = getpos('.')
168+
" When in the same buffer
169+
if &modified && ! empty(s:pos_on_insertenter) && s:pos_on_insertenter[0] == pos[0]
170+
call clang_format#replace(s:pos_on_insertenter[1], line('.'))
171+
let s:pos_on_insertenter = []
172+
endif
173+
endfunction
174+
175+
function! clang_format#enable_format_on_insert()
176+
augroup plugin-clang-format-auto-format-insert
177+
autocmd!
178+
autocmd InsertEnter <buffer> let s:pos_on_insertenter = getpos('.')
179+
autocmd InsertLeave <buffer> call s:format_inserted_area()
180+
augroup END
181+
endfunction
182+
" }}}
183+
162184
let &cpo = s:save_cpo
163185
unlet s:save_cpo

plugin/clang_format.vim

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ command! -range=% -nargs=0 ClangFormatEchoFormattedCode echo clang_format#format
1515
augroup plugin-clang-format-auto-format
1616
autocmd!
1717
autocmd BufWritePre * if &ft ==# 'cpp' && g:clang_format#auto_format | call clang_format#replace(1, line('$')) | endif
18+
autocmd FileType c,cpp,objc if 1 || g:clang_format#auto_format_on_insert_leave | call clang_format#enable_format_on_insert() | endif
1819
augroup END
1920

2021
let g:loaded_clang_format = 1

t/clang_format_spec.vim

+26
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ describe 'default settings'
6767
Expect exists('g:clang_format#style_options') to_be_true
6868
Expect exists('g:clang_format#command') to_be_true
6969
Expect exists('g:clang_format#detect_style_file') to_be_true
70+
Expect exists('g:clang_format#auto_format') to_be_true
71+
Expect exists('g:clang_format#auto_format_on_insert_leave') to_be_true
7072
Expect g:clang_format#extra_args to_be_empty
7173
Expect g:clang_format#code_style ==# 'google'
7274
Expect g:clang_format#style_options to_be_empty
@@ -235,3 +237,27 @@ describe 'g:clang_format#auto_format'
235237
end
236238
end
237239
" }}}
240+
241+
" test for auto formatting on insert leave {{{
242+
243+
describe 'g:clang_format#auto_format_on_insert_leave'
244+
245+
before
246+
let g:clang_format#auto_format_on_insert_leave = 1
247+
new
248+
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
249+
end
250+
251+
after
252+
bdelete!
253+
end
254+
255+
it 'formats a inserted area on InsertLeave if the value is 1'
256+
SKIP because somehow InsertEnter and InsertLeave events aren't fired
257+
execute 10
258+
execute 'normal' "iif(1+2)return 4;\<Esc>"
259+
Expect getline('.') ==# ' if (1 + 2) return 4;'
260+
end
261+
end
262+
263+
" }}}

0 commit comments

Comments
 (0)