Skip to content

Commit

Permalink
add feature to format automatically on InsertLeave
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
rhysd committed Jul 4, 2014
1 parent 71f9eb9 commit c83f840
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -77,7 +77,16 @@ When this variable's value is `1`, vim-clang-format automatically detects the st
- `g:clang_format#auto_format`

When the value is 1, a current buffer is automatically formatted on saving the buffer.
Formatting is executed at `BufWritePre` event.
Formatting is executed on `BufWritePre` event.

- `g:clang_format#auto_format_on_insert_leave`

When the value is 1, inserted lines are automatically formatted on leaving insert mode.
Formatting is executed on `InsertLeave` event.

![auto formatting on InsertLeave](http://gifzo.net/CPrVbHnKMe.gif)

In above screenshot, inserted 'if' statement are automatically formated on leaving insert mode.

### Vimrc Example

Expand Down
22 changes: 22 additions & 0 deletions autoload/clang_format.vim
Expand Up @@ -106,6 +106,7 @@ let g:clang_format#style_options = s:getg('clang_format#style_options', {})

let g:clang_format#detect_style_file = s:getg('clang_format#detect_style_file', 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)
" }}}

" check version of clang-format "{{{
Expand Down Expand Up @@ -159,5 +160,26 @@ function! clang_format#replace(line1, line2)
endfunction
" }}}

" auto formatting on insert leave {{{
let s:pos_on_insertenter = []

function! s:format_inserted_area()
let pos = getpos('.')
" When in the same buffer
if &modified && ! empty(s:pos_on_insertenter) && s:pos_on_insertenter[0] == pos[0]
call clang_format#replace(s:pos_on_insertenter[1], line('.'))
let s:pos_on_insertenter = []
endif
endfunction

function! clang_format#enable_format_on_insert()
augroup plugin-clang-format-auto-format-insert
autocmd!
autocmd InsertEnter <buffer> let s:pos_on_insertenter = getpos('.')
autocmd InsertLeave <buffer> call s:format_inserted_area()
augroup END
endfunction
" }}}

let &cpo = s:save_cpo
unlet s:save_cpo
1 change: 1 addition & 0 deletions plugin/clang_format.vim
Expand Up @@ -15,6 +15,7 @@ command! -range=% -nargs=0 ClangFormatEchoFormattedCode echo clang_format#format
augroup plugin-clang-format-auto-format
autocmd!
autocmd BufWritePre * if &ft ==# 'cpp' && g:clang_format#auto_format | call clang_format#replace(1, line('$')) | endif
autocmd FileType c,cpp,objc if 1 || g:clang_format#auto_format_on_insert_leave | call clang_format#enable_format_on_insert() | endif
augroup END

let g:loaded_clang_format = 1
26 changes: 26 additions & 0 deletions t/clang_format_spec.vim
Expand Up @@ -67,6 +67,8 @@ describe 'default settings'
Expect exists('g:clang_format#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#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
Expect g:clang_format#code_style ==# 'google'
Expect g:clang_format#style_options to_be_empty
Expand Down Expand Up @@ -235,3 +237,27 @@ describe 'g:clang_format#auto_format'
end
end
" }}}

" test for auto formatting on insert leave {{{

describe 'g:clang_format#auto_format_on_insert_leave'

before
let g:clang_format#auto_format_on_insert_leave = 1
new
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
end

after
bdelete!
end

it 'formats a inserted area on InsertLeave if the value is 1'
SKIP because somehow InsertEnter and InsertLeave events aren't fired
execute 10
execute 'normal' "iif(1+2)return 4;\<Esc>"
Expect getline('.') ==# ' if (1 + 2) return 4;'
end
end

" }}}

0 comments on commit c83f840

Please sign in to comment.