Skip to content

Commit 3c8c9bb

Browse files
committed
fix #38: nested YAML configuration
1 parent 2220a53 commit 3c8c9bb

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

autoload/clang_format.vim

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

36+
function! s:create_keyvals(key, val) abort
37+
if type(a:val) == type({})
38+
return a:key . ': {' . s:stringize_options(a:val) . '}'
39+
else
40+
return a:key . ': ' . a:val
41+
endif
42+
endfunction
43+
44+
function! s:stringize_options(opts) abort
45+
let dict_type = type({})
46+
let keyvals = map(items(a:opts), 's:create_keyvals(v:val[0], v:val[1])')
47+
return join(keyvals, ',')
48+
endfunction
49+
3650
function! s:build_extra_options()
3751
let extra_options = ""
3852

@@ -41,9 +55,7 @@ function! s:build_extra_options()
4155
call extend(opts, g:clang_format#filetype_style_options[&ft])
4256
endif
4357

44-
for [key, value] in items(opts)
45-
let extra_options .= printf(", %s: %s", key, value)
46-
endfor
58+
let extra_options .= ', ' . s:stringize_options(opts)
4759

4860
return extra_options
4961
endfunction

t/clang_format_spec.vim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,39 @@ describe 'clang_format#format()'
145145
call s:expect_the_same_output(1, line('$'))
146146
Expect pos == getpos('.')
147147
end
148+
149+
it 'formats following g:clang_format#style_options'
150+
let saved = [g:clang_format#style_options, &expandtab, &shiftwidth]
151+
try
152+
set expandtab
153+
set shiftwidth=4
154+
let g:clang_format#style_options = {'UseTab' : 'false', 'IndentWidth' : 4}
155+
call s:expect_the_same_output(1, line('$'))
156+
finally
157+
let g:clang_format#style_options = saved[0]
158+
let &expandtab = saved[1]
159+
let &shiftwidth = saved[2]
160+
endtry
161+
end
162+
163+
it 'ensures to fix issue #38'
164+
let saved = g:clang_format#style_options
165+
try
166+
let g:clang_format#style_options = {
167+
\ "BraceWrapping" : {
168+
\ "AfterControlStatement" : "true" ,
169+
\ "AfterClass " : "true",
170+
\ },
171+
\ }
172+
try
173+
call clang_format#format(1, line('$'))
174+
catch /^YAML:\d\+:\d\+/
175+
" OK
176+
endtry
177+
finally
178+
let g:clang_format#style_options = saved
179+
endtry
180+
end
148181
end
149182

150183
describe 'clang_format#replace()'

0 commit comments

Comments
 (0)