Skip to content

Commit a3aaa2e

Browse files
committed
Merge branch 'formatexpr'
2 parents 009df09 + c7738e0 commit a3aaa2e

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Format your C++ code [![Build Status](https://travis-ci.org/rhysd/vim-clang-format.png?branch=master)](https://travis-ci.org/rhysd/vim-clang-format)
1+
## Format your C, C++ and Objective-C code [![Build Status](https://travis-ci.org/rhysd/vim-clang-format.png?branch=master)](https://travis-ci.org/rhysd/vim-clang-format)
22

33
This plugin formats your C++ code with specific coding style using [clang](http://clang.llvm.org/).
44

@@ -84,6 +84,11 @@ Formatting is executed on `BufWritePre` event.
8484
When the value is 1, inserted lines are automatically formatted on leaving insert mode.
8585
Formatting is executed on `InsertLeave` event.
8686

87+
- `g:clang_format#auto_formatexpr`
88+
89+
When the value is 1, `formatexpr` option is set by vim-clang-format automatically in C, C++ and ObjC codes.
90+
Vim's format mappings (e.g. `gq`) get to use `clang-format` to format.
91+
8792
### Vimrc Example
8893

8994
```vim

autoload/clang_format.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ let g:clang_format#style_options = s:getg('clang_format#style_options', {})
119119
let g:clang_format#detect_style_file = s:getg('clang_format#detect_style_file', 1)
120120
let g:clang_format#auto_format = s:getg('clang_format#auto_format', 0)
121121
let g:clang_format#auto_format_on_insert_leave = s:getg('clang_format#auto_format_on_insert_leave', 0)
122+
let g:clang_format#auto_formatexpr = s:getg('clang_format#auto_formatexpr', 0)
122123
" }}}
123124

124125
" format codes {{{

doc/clang-format.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ g:clang_format#auto_format_on_insert_leave
164164

165165

166166

167+
g:clang_format#auto_formatexpr *g:clang_format#auto_formatexpr*
168+
169+
When the value is 1, 'formatexpr' option is set automatically in |c|, |cpp|
170+
and |objc| codes. Vim's format mappings (e.g. |gq|) get to use |clang-format|
171+
to format.
172+
173+
174+
167175
==============================================================================
168176
SETUP EXAMPLE *vim-clang-format-setup-example*
169177

plugin/clang_format.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ augroup plugin-clang-format-auto-format
1616
autocmd!
1717
autocmd BufWritePre * if &ft =~# '^\%(c\|cpp\|objc\)$' && g:clang_format#auto_format && clang_format#check_cmd() ==# '' | call clang_format#replace(1, line('$')) | endif
1818
autocmd FileType c,cpp,objc if g:clang_format#auto_format_on_insert_leave && clang_format#check_cmd() ==# '' | call clang_format#enable_format_on_insert() | endif
19+
autocmd FileType c,cpp,objc if g:clang_format#auto_formatexpr && clang_format#check_cmd() ==# '' | setlocal formatexpr=clang_format#replace(v:lnum,v:lnum+v:count-1) | endif
1920
augroup END
2021

2122
let g:loaded_clang_format = 1

t/clang_format_spec.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,25 @@ describe 'g:clang_format#auto_format_on_insert_leave'
297297
end
298298

299299
" }}}
300+
301+
" test for auto 'formatexpr' setting feature
302+
303+
describe 'g:clang_format#auto_formatexpr'
304+
before
305+
let g:clang_format#auto_formatexpr = 1
306+
new
307+
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
308+
end
309+
310+
after
311+
bdelete!
312+
end
313+
314+
it 'formats the text object using gq operator'
315+
doautocmd Filetype cpp
316+
let expected = ClangFormat(1, line('$'))
317+
normal ggVGgq
318+
let actual = GetBuffer()
319+
Expect expected ==# actual
320+
end
321+
end

0 commit comments

Comments
 (0)