Skip to content

Commit e603c47

Browse files
authored
Implement codefmt#formatterhelpers#FiletypeMatches. (#213)
1 parent 3f5ddc6 commit e603c47

26 files changed

+81
-32
lines changed

autoload/codefmt/autopep8.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function! codefmt#autopep8#GetFormatter() abort
3838
endfunction
3939

4040
function l:formatter.AppliesToBuffer() abort
41-
return &filetype is# 'python'
41+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'python')
4242
endfunction
4343

4444
""

autoload/codefmt/black.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function! codefmt#black#GetFormatter() abort
3030
endfunction
3131

3232
function l:formatter.AppliesToBuffer() abort
33-
return &filetype is# 'python'
33+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'python')
3434
endfunction
3535

3636
""

autoload/codefmt/clangformat.vim

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,14 @@ function! codefmt#clangformat#GetFormatter() abort
115115
endfunction
116116

117117
function l:formatter.AppliesToBuffer() abort
118-
if &filetype is# 'c' || &filetype is# 'cpp' ||
119-
\ &filetype is# 'proto' || &filetype is# 'javascript' ||
120-
\ &filetype is# 'objc' || &filetype is# 'objcpp' ||
121-
\ &filetype is# 'typescript' || &filetype is# 'arduino' ||
122-
\ &filetype is# 'cuda'
118+
if codefmt#formatterhelpers#FiletypeMatches(
119+
\ &filetype,
120+
\ ['c', 'cpp', 'cuda', 'proto', 'javascript', 'objc', 'objcpp', 'typescript', 'arduino'])
123121
return 1
124122
endif
125123
" Version 3.6 adds support for java
126124
" http://llvm.org/releases/3.6.0/tools/clang/docs/ReleaseNotes.html
127-
return &filetype is# 'java' && s:ClangFormatHasAtLeastVersion([3, 6])
125+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'java') && s:ClangFormatHasAtLeastVersion([3, 6])
128126
endfunction
129127

130128
""

autoload/codefmt/cljstyle.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function! codefmt#cljstyle#GetFormatter() abort
3131
endfunction
3232

3333
function l:formatter.AppliesToBuffer() abort
34-
return &filetype is# 'clojure'
34+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'clojure')
3535
endfunction
3636

3737
""

autoload/codefmt/dartfmt.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function! codefmt#dartfmt#GetFormatter() abort
3030
endfunction
3131

3232
function l:formatter.AppliesToBuffer() abort
33-
return &filetype is# 'dart'
33+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'dart')
3434
endfunction
3535

3636
""

autoload/codefmt/fish_indent.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function! codefmt#fish_indent#GetFormatter() abort
2727
endfunction
2828

2929
function l:formatter.AppliesToBuffer() abort
30-
return &filetype is# 'fish'
30+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'fish')
3131
endfunction
3232

3333
""

autoload/codefmt/formatterhelpers.vim

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@
1616
let s:plugin = maktaba#plugin#Get('codefmt')
1717

1818

19+
" TODO(google/vim-maktaba#255): Use maktaba's when dropping support for 1.16.0.
20+
function! s:ValueAsList(Value_or_values) abort
21+
return maktaba#value#IsList(a:Value_or_values) ?
22+
\ a:Value_or_values : [a:Value_or_values]
23+
endfunction
24+
25+
26+
""
27+
" @public
28+
" Checks if the given {filetype} matches {expected} filetype(s).
29+
"
30+
" Usage examples: >
31+
" if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c')
32+
" < >
33+
" if codefmt#formatterhelpers#FiletypeMatches(&filetype, ['c', 'cpp'])
34+
" <
35+
" @throws WrongType
36+
function! codefmt#formatterhelpers#FiletypeMatches(filetype, expected) abort
37+
call maktaba#ensure#TypeMatchesOneOf(a:expected, ['', ['']])
38+
" TODO(#212): Support dot-separated filetype names.
39+
let l:expected = s:ValueAsList(a:expected)
40+
" TODO(google/vim-maktaba#256): Drop this check when redundant with above.
41+
for l:expected_ft in l:expected
42+
call maktaba#ensure#IsString(l:expected_ft)
43+
endfor
44+
return index(l:expected, a:filetype) >= 0
45+
endfunction
46+
47+
1948
""
2049
" @public
2150
" Format lines in the current buffer via a formatter invoked by {cmd}, which
@@ -50,6 +79,7 @@ endfunction
5079
" code that calls it.
5180
"
5281
" @throws ShellError if the {cmd} system call fails
82+
" @throws WrongType
5383
function! codefmt#formatterhelpers#AttemptFakeRangeFormatting(
5484
\ startline, endline, cmd) abort
5585
call maktaba#ensure#IsNumber(a:startline)

autoload/codefmt/gn.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function! codefmt#gn#GetFormatter() abort
3131
endfunction
3232

3333
function l:formatter.AppliesToBuffer() abort
34-
return &filetype is# 'gn'
34+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'gn')
3535
endfunction
3636

3737
""

autoload/codefmt/gofmt.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function! codefmt#gofmt#GetFormatter() abort
3030
endfunction
3131

3232
function l:formatter.AppliesToBuffer() abort
33-
return &filetype is# 'go'
33+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'go')
3434
endfunction
3535

3636
""

autoload/codefmt/googlejava.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function! codefmt#googlejava#GetFormatter() abort
4141
endfunction
4242

4343
function l:formatter.AppliesToBuffer() abort
44-
return &filetype is# 'java'
44+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'java')
4545
endfunction
4646

4747
""

0 commit comments

Comments
 (0)