Skip to content

Commit d5f5cc5

Browse files
committed
Merge branch 'fix/check_command'
2 parents 8a14b70 + 3ea2160 commit d5f5cc5

File tree

4 files changed

+74
-16
lines changed

4 files changed

+74
-16
lines changed

autoload/clang_format.vim

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,34 @@ function! clang_format#get_version()
7575
endif
7676
endtry
7777
endfunction
78+
79+
function! clang_format#is_invalid()
80+
if !exists('s:command_available')
81+
if ! executable(g:clang_format#command)
82+
return 1
83+
endif
84+
let s:command_available = 1
85+
endif
86+
87+
if !exists('s:version')
88+
let v = clang_format#get_version()
89+
if v[0] < 3 || (v[0] == 3 && v[1] < 4)
90+
return 2
91+
endif
92+
let s:version = v
93+
endif
94+
95+
return 0
96+
endfunction
97+
98+
function! s:verify_command()
99+
let invalidity = clang_format#is_invalid()
100+
if invalidity == 1
101+
echoerr "clang-format is not found. check g:clang_format#command."
102+
elseif invalidity == 2
103+
echoerr 'clang-format 3.3 or earlier is not supported for the lack of aruguments'
104+
endif
105+
endfunction
78106
" }}}
79107

80108
" variable definitions {{{
@@ -89,13 +117,6 @@ function! s:getg(name, default)
89117
endfunction
90118

91119
let g:clang_format#command = s:getg('clang_format#command', 'clang-format')
92-
if ! executable(g:clang_format#command)
93-
echoerr "clang-format is not found. check g:clang_format#command."
94-
let &cpo = s:save_cpo
95-
unlet s:save_cpo
96-
finish
97-
endif
98-
99120
let g:clang_format#extra_args = s:getg('clang_format#extra_args', "")
100121
if type(g:clang_format#extra_args) == type([])
101122
let g:clang_format#extra_args = join(g:clang_format#extra_args, " ")
@@ -109,13 +130,6 @@ let g:clang_format#auto_format = s:getg('clang_format#auto_format', 0)
109130
let g:clang_format#auto_format_on_insert_leave = s:getg('clang_format#auto_format_on_insert_leave', 0)
110131
" }}}
111132

112-
" check version of clang-format "{{{
113-
let s:version = clang_format#get_version()
114-
if s:version[0] < 3 || (s:version[0] == 3 && s:version[1] < 4)
115-
echoerr 'clang-format 3.3 or earlier is not supported for the lack of aruguments'
116-
endif
117-
"}}}
118-
119133
" format codes {{{
120134
function! s:detect_style_file()
121135
let dirname = expand('%:p:h')
@@ -138,6 +152,9 @@ endfunction
138152

139153
" replace buffer {{{
140154
function! clang_format#replace(line1, line2)
155+
156+
call s:verify_command()
157+
141158
let pos_save = getpos('.')
142159
let sel_save = &l:selection
143160
let &l:selection = "inclusive"
@@ -157,6 +174,8 @@ function! clang_format#replace(line1, line2)
157174
let &l:selection = sel_save
158175
call setpos('.', pos_save)
159176
endtry
177+
178+
return 1
160179
endfunction
161180
" }}}
162181

plugin/clang_format.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ command! -range=% -nargs=0 ClangFormatEchoFormattedCode echo clang_format#format
1414

1515
augroup plugin-clang-format-auto-format
1616
autocmd!
17-
autocmd BufWritePre * if &ft ==# 'cpp' && g:clang_format#auto_format | call clang_format#replace(1, line('$')) | endif
18-
autocmd FileType c,cpp,objc if g:clang_format#auto_format_on_insert_leave | call clang_format#enable_format_on_insert() | endif
17+
autocmd BufWritePre * if &ft =~# '^\%(c\|cpp\|objc\)$' && g:clang_format#auto_format && !clang_format#is_invalid() | call clang_format#replace(1, line('$')) | endif
18+
autocmd FileType c,cpp,objc if g:clang_format#auto_format_on_insert_leave && !clang_format#is_invalid() | call clang_format#enable_format_on_insert() | endif
1919
augroup END
2020

2121
let g:loaded_clang_format = 1

t/clang-format-dummy.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
echo 'Ubuntu clang-format version 3.2.0-1~exp1 (trunk) (based on LLVM 3.2.0)'

t/clang_format_spec.vim

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ set rtp +=~/.vim/bundle/vim-operator-user
4444
runtime! plugin/clang_format.vim
4545

4646
call vspec#customize_matcher('to_be_empty', function('empty'))
47+
48+
function! RaisesException(cmd)
49+
try
50+
execute a:cmd
51+
return 0
52+
catch
53+
return 1
54+
endtry
55+
endfunction
56+
57+
call vspec#customize_matcher('to_throw_exception', function('RaisesException'))
58+
4759
"}}}
4860

4961
" test for default settings {{{
@@ -131,6 +143,30 @@ describe 'clang_format#format()'
131143
Expect pos == getpos('.')
132144
end
133145
end
146+
147+
describe 'clang_format#replace()'
148+
before
149+
let g:clang_format#detect_style_file = 0
150+
new
151+
execute 'silent' 'edit!' './'.s:root_dir.'t/test.cpp'
152+
let s:cmd_tmp = g:clang_format#command
153+
end
154+
155+
after
156+
bdelete!
157+
let g:clang_format#command = s:cmd_tmp
158+
end
159+
160+
it 'throws an error when command is not found'
161+
let g:clang_format#command = "clang_format_not_exist"
162+
Expect "call clang_format#replace(1, line('$'))" to_throw_exception
163+
end
164+
165+
it 'throws an error when command is not found'
166+
let g:clang_format#command = './' . s:root_dir . 't/clang-format-dummy.sh'
167+
Expect "call clang_format#replace(1, line('$'))" to_throw_exception
168+
end
169+
end
134170
" }}}
135171

136172
" test for <Plug>(operator-clang-format) {{{

0 commit comments

Comments
 (0)