Skip to content

Commit ee84b1e

Browse files
committed
Merge branch 'javascript'
2 parents 8894970 + c203fef commit ee84b1e

File tree

3 files changed

+98
-37
lines changed

3 files changed

+98
-37
lines changed

autoload/clang_format.vim

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,23 @@ function! s:system(str, ...)
3333
return output
3434
endfunction
3535

36-
function! s:make_style_options()
36+
function! s:build_extra_options()
3737
let extra_options = ""
38-
for [key, value] in items(g:clang_format#style_options)
38+
39+
let opts = copy(g:clang_format#style_options)
40+
if has_key(g:clang_format#filetype_style_options, &ft)
41+
call extend(opts, g:clang_format#filetype_style_options[&ft])
42+
endif
43+
44+
for [key, value] in items(opts)
3945
let extra_options .= printf(", %s: %s", key, value)
4046
endfor
47+
48+
return extra_options
49+
endfunction
50+
51+
function! s:make_style_options()
52+
let extra_options = s:build_extra_options()
4153
return printf("'{BasedOnStyle: %s, IndentWidth: %d, UseTab: %s%s}'",
4254
\ g:clang_format#code_style,
4355
\ (exists('*shiftwidth') ? shiftwidth() : &l:shiftwidth),
@@ -124,6 +136,7 @@ endif
124136

125137
let g:clang_format#code_style = s:getg('clang_format#code_style', 'google')
126138
let g:clang_format#style_options = s:getg('clang_format#style_options', {})
139+
let g:clang_format#filetype_style_options = s:getg('clang_format#filetype_style_options', {})
127140

128141
let g:clang_format#detect_style_file = s:getg('clang_format#detect_style_file', 1)
129142
let g:clang_format#auto_format = s:getg('clang_format#auto_format', 0)

doc/clang-format.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,27 @@ g:clang_format#style_options *g:clang_format#style_options*
139139
CLANG-FORMAT STYLE OPTIONS
140140
http://clang.llvm.org/docs/ClangFormatStyleOptions.html
141141

142+
g:clang_format#filetype_style_options
143+
*g:clang_format#filetype_style_options*
144+
145+
If you want to use language-specific style options, you can use this
146+
variable. This variable is a |Dictionary| and its key is |filetype|. You
147+
can specify the filetype specific style options as a value of the key.
148+
The format to specify options is the same as |g:clang_format#style_options|.
149+
150+
For example, if you want to set "C++11" to "Standard", you can specify it
151+
only when the filetype is "cpp" as below:
152+
>
153+
" Your common style options
154+
let g:clang_format#style_options = {
155+
\ 'AllowShortIfStatementsOnASingleLine' : 'true',
156+
\ }
157+
158+
" Your filetype specific options
159+
let g:clang_format#filetype_style_options = {
160+
\ 'cpp' : {"Standard" : "C++11"},
161+
\ }
162+
<
142163
g:clang_format#extra_args *g:clang_format#extra_args*
143164

144165
Extra arguments for clang-format. This |String| value is passed to

t/clang_format_spec.vim

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ function! GetBuffer()
2929
return join(getline(1, '$'), "\n")
3030
endfunction
3131

32-
function! ClangFormat(line1, line2)
32+
function! ClangFormat(line1, line2, ...)
3333
let opt = printf(" -lines=%d:%d -style='{BasedOnStyle: Google, IndentWidth: %d, UseTab: %s}' ", a:line1, a:line2, &l:shiftwidth, &l:expandtab==1 ? "false" : "true")
34-
let cmd = g:clang_format#command.opt.'./'.s:root_dir.'t/test.cpp --'
34+
let file = a:0 == 0 ? 'test.cpp' : a:1
35+
let cmd = g:clang_format#command.opt.'./'.s:root_dir.'t/' . file . ' --'
3536
return Chomp(system(cmd))
3637
endfunction
3738
"}}}
@@ -77,13 +78,15 @@ describe 'default settings'
7778
Expect exists('g:clang_format#extra_args') to_be_true
7879
Expect exists('g:clang_format#code_style') to_be_true
7980
Expect exists('g:clang_format#style_options') to_be_true
81+
Expect exists('g:clang_format#filetype_style_options') to_be_true
8082
Expect exists('g:clang_format#command') to_be_true
8183
Expect exists('g:clang_format#detect_style_file') to_be_true
8284
Expect exists('g:clang_format#auto_format') to_be_true
8385
Expect exists('g:clang_format#auto_format_on_insert_leave') to_be_true
8486
Expect g:clang_format#extra_args to_be_empty
8587
Expect g:clang_format#code_style ==# 'google'
8688
Expect g:clang_format#style_options to_be_empty
89+
Expect g:clang_format#filetype_style_options to_be_empty
8790
Expect executable(g:clang_format#command) to_be_true
8891
Expect g:clang_format#detect_style_file to_be_true
8992
end
@@ -213,41 +216,65 @@ end
213216
" test for :ClangFormat {{{
214217
describe ':ClangFormat'
215218

216-
before
217-
let g:clang_format#detect_style_file = 0
218-
new
219-
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
220-
end
221-
222-
after
223-
bdelete!
224-
end
225-
226-
it 'formats the whole code in normal mode'
227-
let by_clang_format_command = ClangFormat(1, line('$'))
228-
ClangFormat
229-
let buffer = GetBuffer()
230-
Expect by_clang_format_command ==# buffer
231-
end
219+
describe 'with filetype cpp'
220+
221+
before
222+
let g:clang_format#detect_style_file = 0
223+
new
224+
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
225+
end
226+
227+
after
228+
bdelete!
229+
end
230+
231+
it 'formats the whole code in normal mode'
232+
let by_clang_format_command = ClangFormat(1, line('$'))
233+
ClangFormat
234+
let buffer = GetBuffer()
235+
Expect by_clang_format_command ==# buffer
236+
end
237+
238+
it 'formats selected code in visual mode'
239+
" format for statement
240+
let by_clang_format_command = ClangFormat(11, 13)
241+
" move to for statement block
242+
execute 11
243+
normal! VjjV
244+
'<,'>ClangFormat
245+
let buffer = GetBuffer()
246+
Expect by_clang_format_command ==# buffer
247+
end
248+
249+
it 'doesn''t move cursor'
250+
execute 'normal!' (1+line('$')).'gg'
251+
let pos = getpos('.')
252+
ClangFormat
253+
Expect pos == getpos('.')
254+
end
255+
256+
end
257+
258+
describe 'with filetype javascript'
259+
260+
before
261+
let g:clang_format#detect_style_file = 0
262+
new
263+
execute 'silent' 'edit!' './'.s:root_dir.'t/test.js'
264+
end
265+
266+
after
267+
bdelete!
268+
end
269+
270+
it 'formats the whole code in normal mode'
271+
let by_clang_format_command = ClangFormat(1, line('$'), 'test.js')
272+
ClangFormat
273+
let buffer = GetBuffer()
274+
Expect by_clang_format_command ==# buffer
275+
end
232276

233-
it 'formats selected code in visual mode'
234-
" format for statement
235-
let by_clang_format_command = ClangFormat(11, 13)
236-
" move to for statement block
237-
execute 11
238-
normal! VjjV
239-
'<,'>ClangFormat
240-
let buffer = GetBuffer()
241-
Expect by_clang_format_command ==# buffer
242277
end
243-
244-
it 'doesn''t move cursor'
245-
execute 'normal!' (1+line('$')).'gg'
246-
let pos = getpos('.')
247-
ClangFormat
248-
Expect pos == getpos('.')
249-
end
250-
251278
end
252279
" }}}
253280

0 commit comments

Comments
 (0)