Skip to content

Commit cd271bb

Browse files
committed
Make style lookup consistent with clang-format
The current version was only - looking up .clang_format in the file's directory. - using -style=file option only, causing failure if pwd != file's directory 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 cd271bb

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

autoload/clang_format.vim

+14-4
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,28 @@ let g:clang_format#auto_formatexpr = s:getg('clang_format#auto_formatexpr', 0)
145145
" }}}
146146

147147
" format codes {{{
148+
function! s:detect_style_file_recursive(dir_modifier, style_file_name)
149+
let dirname = expand(a:dir_modifier)
150+
let found = findfile(a:style_file_name, dirname.';')
151+
if(found == '' && dirname != '/')
152+
execute s:detect_style_file_recursive(a:dir_modifier.':h', a:style_file_name)
153+
else
154+
return found
155+
end
156+
endfunction
157+
148158
function! s:detect_style_file()
149-
let dirname = expand('%:p:h')
150159
let style_file_name = has('win32') || has('win64') ? '_clang-format' : '.clang-format'
151-
return findfile(style_file_name, dirname.';') != ''
160+
return s:detect_style_file_recursive('%:p:h', style_file_name)
152161
endfunction
153162

154163
function! clang_format#format(line1, line2)
155164
let args = printf(" -lines=%d:%d", a:line1, a:line2)
156-
if ! (g:clang_format#detect_style_file && s:detect_style_file())
165+
let style_file = s:detect_style_file()
166+
if ! (g:clang_format#detect_style_file && style_file != '')
157167
let args .= printf(" -style=%s ", s:make_style_options())
158168
else
159-
let args .= " -style=file "
169+
let args .= " -style=file --assume-filename=".style_file
160170
endif
161171
let args .= g:clang_format#extra_args
162172
let clang_format = printf("%s %s --", g:clang_format#command, args)

0 commit comments

Comments
 (0)