Skip to content

Update #formatterhelpers#FiletypeMatches to handle dotted ft names #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions autoload/codefmt/formatterhelpers.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ endfunction
" @public
" Checks if the given {filetype} matches {expected} filetype(s).
"
" When checking a dotted filetype name (like "c.doxygen"), returns true if any
" piece matches expected filetype(s).
"
" Usage examples: >
" if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c')
" < >
Expand All @@ -35,13 +38,19 @@ endfunction
" @throws WrongType
function! codefmt#formatterhelpers#FiletypeMatches(filetype, expected) abort
call maktaba#ensure#TypeMatchesOneOf(a:expected, ['', ['']])
" TODO(#212): Support dot-separated filetype names.
let l:expected = s:ValueAsList(a:expected)
" TODO(google/vim-maktaba#256): Drop this check when redundant with above.
for l:expected_ft in l:expected
call maktaba#ensure#IsString(l:expected_ft)
endfor
return index(l:expected, a:filetype) >= 0
" Check if filetypes match expected (splitting & looping to help support
" dot-separated filetype names).
for l:filetype in split(a:filetype, '\m\.', 0)
if index(l:expected, l:filetype) >= 0
return 1
endif
endfor
return 0
endfunction


Expand Down
5 changes: 1 addition & 4 deletions autoload/codefmt/jsbeautify.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ function! codefmt#jsbeautify#GetFormatter() abort
endfunction

function l:formatter._GetSupportedFormatName(filetype) dict abort
" Simplify compound filetypes like "html.mustache" down to just "html".
" TODO: Support other compound filetypes like "javascript.*" and "css.*"?
let l:filetype = substitute(a:filetype, '\m^html\..*', 'html', '')
for [l:format_name, l:filetypes] in items(self._supported_formats)
if codefmt#formatterhelpers#FiletypeMatches(l:filetype, l:filetypes)
if codefmt#formatterhelpers#FiletypeMatches(a:filetype, l:filetypes)
return l:format_name
endif
endfor
Expand Down
3 changes: 3 additions & 0 deletions doc/codefmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ codefmt#formatterhelpers#FiletypeMatches({filetype}, {expected})
*codefmt#formatterhelpers#FiletypeMatches()*
Checks if the given {filetype} matches {expected} filetype(s).

When checking a dotted filetype name (like "c.doxygen"), returns true if any
piece matches expected filetype(s).

Usage examples:
>
if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c')
Expand Down
14 changes: 14 additions & 0 deletions vroom/main.vroom
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ will be used by default for the go filetype.
! gofmt .*
$ f()

This will correctly detect compound dot-separated filetypes (see vim's
`:help 'filetype'`), so gofmt will still be used for "go.otherft":

@clear
% f()
:set filetype=go.otherft
:FormatCode
! gofmt .*
$ f()

(Be aware though that the order of dotted filetypes doesn't affect which
formatter wins if the multiple filetypes each have their own formatter, so in
that case you may need to explicitly choose one instead of relying on defaults.)

You can also configure which formatter to use on a buffer-by-buffer basis via
b:codefmt_formatter, which will take precedence over the built-in defaulting.

Expand Down