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

+1-1
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

+1-1
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

+4-6
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

+1-1
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

+1-1
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

+1-1
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

+30
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

+1-1
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

+1-1
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

+1-1
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
""

autoload/codefmt/isort.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function! codefmt#isort#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/jsbeautify.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function! codefmt#jsbeautify#GetFormatter() abort
7070
" TODO: Support other compound filetypes like "javascript.*" and "css.*"?
7171
let l:filetype = substitute(a:filetype, '\m^html\..*', 'html', '')
7272
for [l:format_name, l:filetypes] in items(self._supported_formats)
73-
if index(l:filetypes, l:filetype) >= 0
73+
if codefmt#formatterhelpers#FiletypeMatches(l:filetype, l:filetypes)
7474
return l:format_name
7575
endif
7676
endfor

autoload/codefmt/jsonnetfmt.vim

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
let s:plugin = maktaba#plugin#Get('codefmt')
22

3-
let s:SUPPORTED_FILETYPES = ['json', 'jsonnet']
3+
if !exists('s:SUPPORTED_FILETYPES')
4+
let s:SUPPORTED_FILETYPES = ['json', 'jsonnet']
5+
lockvar! s:SUPPORTED_FILETYPES
6+
endif
47

58

69
""
@@ -17,7 +20,7 @@ function! codefmt#jsonnetfmt#GetFormatter() abort
1720
endfunction
1821

1922
function l:formatter.AppliesToBuffer() abort
20-
return index(s:SUPPORTED_FILETYPES, &filetype) >= 0
23+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, s:SUPPORTED_FILETYPES)
2124
endfunction
2225

2326
""

autoload/codefmt/ktfmt.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function! codefmt#ktfmt#GetFormatter() abort
7171
endfunction
7272

7373
function l:formatter.AppliesToBuffer() abort
74-
return &filetype is# 'kotlin'
74+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'kotlin')
7575
endfunction
7676

7777
""

autoload/codefmt/luaformatterfiveone.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function! codefmt#luaformatterfiveone#GetFormatter() abort
3131
endfunction
3232

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

3737
""

autoload/codefmt/mixformat.vim

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ function! codefmt#mixformat#GetFormatter() abort
3535
endfunction
3636

3737
function l:formatter.AppliesToBuffer() abort
38-
return &filetype is# 'elixir' || &filetype is# 'eelixir'
39-
\ || &filetype is# 'heex'
38+
return codefmt#formatterhelpers#FiletypeMatches(
39+
\ &filetype, ['elixir', 'eelixir', 'heex'])
4040
endfunction
4141

4242
""

autoload/codefmt/nixpkgs_fmt.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function! codefmt#nixpkgs_fmt#GetFormatter() abort
3131
endfunction
3232

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

3737
""

autoload/codefmt/ocamlformat.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function! codefmt#ocamlformat#GetFormatter() abort
3030
endfunction
3131

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

3636
""

autoload/codefmt/ormolu.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function! codefmt#ormolu#GetFormatter() abort
3030
endfunction
3131

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

3636
""

autoload/codefmt/prettier.vim

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
let s:plugin = maktaba#plugin#Get('codefmt')
1717

1818
" See https://prettier.io for a list of supported file types.
19-
let s:supported_filetypes = ['javascript', 'markdown', 'html', 'css', 'yaml',
19+
if !exists('s:SUPPORTED_FILETYPES')
20+
let s:SUPPORTED_FILETYPES = ['javascript', 'markdown', 'html', 'css', 'yaml',
2021
\ 'jsx', 'less', 'scss', 'mdx', 'vue']
22+
lockvar! s:SUPPORTED_FILETYPES
23+
endif
2124

2225

2326
""
@@ -56,7 +59,7 @@ function! codefmt#prettier#GetFormatter() abort
5659
endfunction
5760

5861
function l:formatter.AppliesToBuffer() abort
59-
return index(s:supported_filetypes, &filetype) >= 0
62+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, s:SUPPORTED_FILETYPES)
6063
endfunction
6164

6265
""

autoload/codefmt/rustfmt.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function! codefmt#rustfmt#GetFormatter() abort
3131
endfunction
3232

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

3737
""

autoload/codefmt/shfmt.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function! codefmt#shfmt#GetFormatter() abort
3636
endfunction
3737

3838
function l:formatter.AppliesToBuffer() abort
39-
return &filetype is# 'sh'
39+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'sh')
4040
endfunction
4141

4242
""

autoload/codefmt/swiftformat.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function! codefmt#swiftformat#GetFormatter() abort
2929
endfunction
3030

3131
function l:formatter.AppliesToBuffer() abort
32-
return &filetype is# 'swift'
32+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'swift')
3333
endfunction
3434

3535
""

autoload/codefmt/yapf.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function! codefmt#yapf#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/zprint.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function! codefmt#zprint#GetFormatter() abort
4444
endfunction
4545

4646
function l:formatter.AppliesToBuffer() abort
47-
return &filetype is# 'clojure'
47+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'clojure')
4848
endfunction
4949

5050
""

doc/codefmt.txt

+15
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,20 @@ codefmt#FormatMap({type}) *codefmt#FormatMap()*
357357
Suitable for use as 'operatorfunc'; see |g@| for details. The type is
358358
ignored since formatting only works on complete lines.
359359

360+
codefmt#formatterhelpers#FiletypeMatches({filetype}, {expected})
361+
*codefmt#formatterhelpers#FiletypeMatches()*
362+
Checks if the given {filetype} matches {expected} filetype(s).
363+
364+
Usage examples:
365+
>
366+
if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c')
367+
<
368+
369+
>
370+
if codefmt#formatterhelpers#FiletypeMatches(&filetype, ['c', 'cpp'])
371+
<
372+
Throws ERROR(WrongType)
373+
360374
codefmt#formatterhelpers#Format({cmd}) *codefmt#formatterhelpers#Format()*
361375
Format lines in the current buffer via a formatter invoked by {cmd}, which
362376
is a system call represented by either a |maktaba.Syscall| or any argument
@@ -380,6 +394,7 @@ codefmt#formatterhelpers#AttemptFakeRangeFormatting({startline}, {endline},
380394
code that calls it.
381395

382396
Throws ERROR(ShellError) if the {cmd} system call fails
397+
Throws ERROR(WrongType)
383398

384399
codefmt#formatterhelpers#ResolveFlagToArray({flag_name})
385400
*codefmt#formatterhelpers#ResolveFlagToArray()*

0 commit comments

Comments
 (0)