Skip to content

Commit 009df09

Browse files
committed
a refactoring for command checking
1 parent f6fdeaa commit 009df09

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

autoload/clang_format.vim

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,23 @@ function! clang_format#get_version()
7676
endtry
7777
endfunction
7878

79-
function! clang_format#is_invalid()
79+
function! clang_format#check_cmd()
8080
if !exists('s:command_available')
8181
if ! executable(g:clang_format#command)
82-
return 1
82+
return 'clang-format is not found. check g:clang_format#command.'
8383
endif
8484
let s:command_available = 1
8585
endif
8686

8787
if !exists('s:version')
8888
let v = clang_format#get_version()
8989
if v[0] < 3 || (v[0] == 3 && v[1] < 4)
90-
return 2
90+
return 'clang-format 3.3 or earlier is not supported for the lack of aruguments.'
9191
endif
9292
let s:version = v
9393
endif
9494

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
95+
return ''
10596
endfunction
10697
" }}}
10798

@@ -153,7 +144,10 @@ endfunction
153144
" replace buffer {{{
154145
function! clang_format#replace(line1, line2)
155146

156-
call s:verify_command()
147+
let err_msg = clang_format#check_cmd()
148+
if err_msg !=# ''
149+
echoerr err_msg
150+
endif
157151

158152
let pos_save = getpos('.')
159153
let sel_save = &l:selection

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 =~# '^\%(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
17+
autocmd BufWritePre * if &ft =~# '^\%(c\|cpp\|objc\)$' && g:clang_format#auto_format && clang_format#check_cmd() ==# '' | call clang_format#replace(1, line('$')) | endif
18+
autocmd FileType c,cpp,objc if g:clang_format#auto_format_on_insert_leave && clang_format#check_cmd() ==# '' | call clang_format#enable_format_on_insert() | endif
1919
augroup END
2020

2121
let g:loaded_clang_format = 1

0 commit comments

Comments
 (0)