From 4cfc1be1adeda0bb2fa25e576221eff0af82b11a Mon Sep 17 00:00:00 2001 From: David Barnett Date: Sun, 19 Mar 2023 22:00:33 -0600 Subject: [PATCH] Update #formatterhelpers#FiletypeMatches to handle dotted ft names --- autoload/codefmt/formatterhelpers.vim | 13 +++++++++++-- autoload/codefmt/jsbeautify.vim | 5 +---- doc/codefmt.txt | 3 +++ vroom/main.vroom | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/autoload/codefmt/formatterhelpers.vim b/autoload/codefmt/formatterhelpers.vim index ebc5d1c..a1babb8 100644 --- a/autoload/codefmt/formatterhelpers.vim +++ b/autoload/codefmt/formatterhelpers.vim @@ -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') " < > @@ -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 diff --git a/autoload/codefmt/jsbeautify.vim b/autoload/codefmt/jsbeautify.vim index b90ef74..3011284 100644 --- a/autoload/codefmt/jsbeautify.vim +++ b/autoload/codefmt/jsbeautify.vim @@ -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 diff --git a/doc/codefmt.txt b/doc/codefmt.txt index ce523a5..db07692 100644 --- a/doc/codefmt.txt +++ b/doc/codefmt.txt @@ -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') diff --git a/vroom/main.vroom b/vroom/main.vroom index f0f1688..a91721c 100644 --- a/vroom/main.vroom +++ b/vroom/main.vroom @@ -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.