Skip to content

Commit 890ca5d

Browse files
committed
fix assumed filename is empty (#58)
1 parent 3b0b1c8 commit 890ca5d

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

autoload/clang_format.vim

+4-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ endfunction
7474
function! s:success(result)
7575
return (s:has_vimproc() ? vimproc#get_last_status() : v:shell_error) == 0
7676
\ && a:result !~# '^YAML:\d\+:\d\+: error: unknown key '
77-
\ && a:result !~# '^\n\?$'
7877
endfunction
7978

8079
function! s:error_message(result)
@@ -193,7 +192,10 @@ function! clang_format#format(line1, line2)
193192
else
194193
let args .= ' -style=file '
195194
endif
196-
let args .= printf('-assume-filename=%s ', s:shellescape(escape(expand('%'), " \t")))
195+
let filename = expand('%')
196+
if filename !=# ''
197+
let args .= printf('-assume-filename=%s ', s:shellescape(escape(filename, " \t")))
198+
endif
197199
let args .= g:clang_format#extra_args
198200
let clang_format = printf('%s %s --', s:shellescape(g:clang_format#command), args)
199201
return s:system(clang_format, join(getline(1, '$'), "\n"))

t/clang_format_spec.vim

+24
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ describe 'g:clang_format#auto_formatexpr'
424424
end
425425
" }}}
426426

427+
" test for undo {{{
427428
describe 'undo formatting text'
428429
before
429430
let g:clang_format#detect_style_file = 0
@@ -443,3 +444,26 @@ describe 'undo formatting text'
443444
Expect prev ==# buf
444445
end
445446
end
447+
" }}}
448+
449+
" test for empty buffer {{{
450+
describe 'empty buffer'
451+
before
452+
let g:clang_format#detect_style_file = 0
453+
new
454+
end
455+
456+
after
457+
bdelete!
458+
end
459+
460+
it 'can format empty buffer'
461+
ClangFormat
462+
end
463+
464+
it 'can format a buffer containing only new lines'
465+
call setline('.', ['', '', ''])
466+
ClangFormat
467+
end
468+
end
469+
" }}}"

0 commit comments

Comments
 (0)