Skip to content

Commit 46a41ce

Browse files
committed
escaping -style
This fixes following error on Windows when .clang-format is not found: error: -offset, -length and -lines can only be used for single file. Also added undocumented feature - g:clang_format#enable_fallback_style for those who do not want any auto-formatting from clang if .clang-format is not found.
1 parent 661e7b3 commit 46a41ce

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

autoload/clang_format.vim

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,22 @@ function! s:stringize_options(opts) abort
5050
endfunction
5151

5252
function! s:build_extra_options() abort
53-
let extra_options = ''
54-
5553
let opts = copy(g:clang_format#style_options)
5654
if has_key(g:clang_format#filetype_style_options, &ft)
5755
call extend(opts, g:clang_format#filetype_style_options[&ft])
5856
endif
5957

60-
let extra_options .= ', ' . s:stringize_options(opts)
58+
let extra_options = s:stringize_options(opts)
59+
if !empty(extra_options)
60+
let extra_options = ', ' . extra_options
61+
endif
6162

6263
return extra_options
6364
endfunction
6465

6566
function! s:make_style_options() abort
6667
let extra_options = s:build_extra_options()
67-
return printf("'{BasedOnStyle: %s, IndentWidth: %d, UseTab: %s%s}'",
68+
return printf("{BasedOnStyle: %s, IndentWidth: %d, UseTab: %s%s}",
6869
\ g:clang_format#code_style,
6970
\ (exists('*shiftwidth') ? shiftwidth() : &l:shiftwidth),
7071
\ &l:expandtab==1 ? 'false' : 'true',
@@ -193,6 +194,8 @@ let g:clang_format#style_options = s:getg('clang_format#style_options', {})
193194
let g:clang_format#filetype_style_options = s:getg('clang_format#filetype_style_options', {})
194195

195196
let g:clang_format#detect_style_file = s:getg('clang_format#detect_style_file', 1)
197+
let g:clang_format#enable_fallback_style = s:getg('clang_format#enable_fallback_style', 1)
198+
196199
let g:clang_format#auto_format = s:getg('clang_format#auto_format', 0)
197200
let g:clang_format#auto_format_on_insert_leave = s:getg('clang_format#auto_format_on_insert_leave', 0)
198201
let g:clang_format#auto_formatexpr = s:getg('clang_format#auto_formatexpr', 0)
@@ -207,7 +210,11 @@ endfunction
207210
function! clang_format#format(line1, line2) abort
208211
let args = printf(' -lines=%d:%d', a:line1, a:line2)
209212
if ! (g:clang_format#detect_style_file && s:detect_style_file())
210-
let args .= printf(' -style=%s ', s:make_style_options())
213+
if g:clang_format#enable_fallback_style
214+
let args .= ' ' . s:shellescape(printf('-style=%s', s:make_style_options())) . ' '
215+
else
216+
let args .= ' -fallback-style=none '
217+
endif
211218
else
212219
let args .= ' -style=file '
213220
endif

t/clang_format_spec.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function! GetBuffer() abort
3535
endfunction
3636

3737
function! ClangFormat(line1, line2, ...) abort
38-
let opt = printf("-lines=%d:%d -style='{BasedOnStyle: Google, IndentWidth: %d, UseTab: %s", a:line1, a:line2, &l:shiftwidth, &l:expandtab==1 ? "false" : "true")
38+
let opt = printf("-lines=%d:%d '-style={BasedOnStyle: Google, IndentWidth: %d, UseTab: %s", a:line1, a:line2, &l:shiftwidth, &l:expandtab==1 ? "false" : "true")
3939
let file = 'test.cpp'
4040

4141
for a in a:000

0 commit comments

Comments
 (0)