Skip to content

Commit 47a10a7

Browse files
committed
Make style lookup consistent with clang-format
The current version was using -style=file option only, causing failure if pwd != file's directory. 2 kinds of failures occur: - If no .clang-format is present in PWD, and one is present in the file's directory, then clang-format fails for lack of style file, and dump its error message directly into the buffer - If a .clang-format is present in PWD, and one is present in the file's directory, then the wrong .clang-format from PWD is used This commit makes the behavior fully consistent with clang-format: - lookup .clang_format in the file's directory and recursively go up its parent directory until found. If no style file is found, defaults to g:clang_format#style_options
1 parent d90af92 commit 47a10a7

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Diff for: autoload/clang_format.vim

+4-3
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,16 @@ let g:clang_format#auto_formatexpr = s:getg('clang_format#auto_formatexpr', 0)
148148
function! s:detect_style_file()
149149
let dirname = expand('%:p:h')
150150
let style_file_name = has('win32') || has('win64') ? '_clang-format' : '.clang-format'
151-
return findfile(style_file_name, dirname.';') != ''
151+
return findfile(style_file_name, dirname.';')
152152
endfunction
153153

154154
function! clang_format#format(line1, line2)
155155
let args = printf(" -lines=%d:%d", a:line1, a:line2)
156-
if ! (g:clang_format#detect_style_file && s:detect_style_file())
156+
let style_file = s:detect_style_file()
157+
if ! (g:clang_format#detect_style_file && style_file != '')
157158
let args .= printf(" -style=%s ", s:make_style_options())
158159
else
159-
let args .= " -style=file "
160+
let args .= " -style=file --assume-filename=".style_file
160161
endif
161162
let args .= g:clang_format#extra_args
162163
let clang_format = printf("%s %s --", g:clang_format#command, args)

0 commit comments

Comments
 (0)