Skip to content

Commit db7631c

Browse files
authored
Update #formatterhelpers#FiletypeMatches to handle dotted ft names (#214)
Fixes #212.
1 parent e603c47 commit db7631c

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

Diff for: autoload/codefmt/formatterhelpers.vim

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ endfunction
2727
" @public
2828
" Checks if the given {filetype} matches {expected} filetype(s).
2929
"
30+
" When checking a dotted filetype name (like "c.doxygen"), returns true if any
31+
" piece matches expected filetype(s).
32+
"
3033
" Usage examples: >
3134
" if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c')
3235
" < >
@@ -35,13 +38,19 @@ endfunction
3538
" @throws WrongType
3639
function! codefmt#formatterhelpers#FiletypeMatches(filetype, expected) abort
3740
call maktaba#ensure#TypeMatchesOneOf(a:expected, ['', ['']])
38-
" TODO(#212): Support dot-separated filetype names.
3941
let l:expected = s:ValueAsList(a:expected)
4042
" TODO(google/vim-maktaba#256): Drop this check when redundant with above.
4143
for l:expected_ft in l:expected
4244
call maktaba#ensure#IsString(l:expected_ft)
4345
endfor
44-
return index(l:expected, a:filetype) >= 0
46+
" Check if filetypes match expected (splitting & looping to help support
47+
" dot-separated filetype names).
48+
for l:filetype in split(a:filetype, '\m\.', 0)
49+
if index(l:expected, l:filetype) >= 0
50+
return 1
51+
endif
52+
endfor
53+
return 0
4554
endfunction
4655

4756

Diff for: autoload/codefmt/jsbeautify.vim

+1-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ function! codefmt#jsbeautify#GetFormatter() abort
6666
endfunction
6767

6868
function l:formatter._GetSupportedFormatName(filetype) dict abort
69-
" Simplify compound filetypes like "html.mustache" down to just "html".
70-
" TODO: Support other compound filetypes like "javascript.*" and "css.*"?
71-
let l:filetype = substitute(a:filetype, '\m^html\..*', 'html', '')
7269
for [l:format_name, l:filetypes] in items(self._supported_formats)
73-
if codefmt#formatterhelpers#FiletypeMatches(l:filetype, l:filetypes)
70+
if codefmt#formatterhelpers#FiletypeMatches(a:filetype, l:filetypes)
7471
return l:format_name
7572
endif
7673
endfor

Diff for: doc/codefmt.txt

+3
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ codefmt#formatterhelpers#FiletypeMatches({filetype}, {expected})
361361
*codefmt#formatterhelpers#FiletypeMatches()*
362362
Checks if the given {filetype} matches {expected} filetype(s).
363363

364+
When checking a dotted filetype name (like "c.doxygen"), returns true if any
365+
piece matches expected filetype(s).
366+
364367
Usage examples:
365368
>
366369
if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c')

Diff for: vroom/main.vroom

+14
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ will be used by default for the go filetype.
138138
! gofmt .*
139139
$ f()
140140

141+
This will correctly detect compound dot-separated filetypes (see vim's
142+
`:help 'filetype'`), so gofmt will still be used for "go.otherft":
143+
144+
@clear
145+
% f()
146+
:set filetype=go.otherft
147+
:FormatCode
148+
! gofmt .*
149+
$ f()
150+
151+
(Be aware though that the order of dotted filetypes doesn't affect which
152+
formatter wins if the multiple filetypes each have their own formatter, so in
153+
that case you may need to explicitly choose one instead of relying on defaults.)
154+
141155
You can also configure which formatter to use on a buffer-by-buffer basis via
142156
b:codefmt_formatter, which will take precedence over the built-in defaulting.
143157

0 commit comments

Comments
 (0)