From 8d5cdbba07f7cae3c7bb8947933737795792fe09 Mon Sep 17 00:00:00 2001 From: David Barnett Date: Sat, 18 Mar 2023 17:07:33 -0600 Subject: [PATCH] Implement codefmt#formatterhelpers#FiletypeMatches. --- autoload/codefmt/autopep8.vim | 2 +- autoload/codefmt/black.vim | 2 +- autoload/codefmt/clangformat.vim | 10 ++++---- autoload/codefmt/cljstyle.vim | 2 +- autoload/codefmt/dartfmt.vim | 2 +- autoload/codefmt/fish_indent.vim | 2 +- autoload/codefmt/formatterhelpers.vim | 30 ++++++++++++++++++++++++ autoload/codefmt/gn.vim | 2 +- autoload/codefmt/gofmt.vim | 2 +- autoload/codefmt/googlejava.vim | 2 +- autoload/codefmt/isort.vim | 2 +- autoload/codefmt/jsbeautify.vim | 2 +- autoload/codefmt/jsonnetfmt.vim | 7 ++++-- autoload/codefmt/ktfmt.vim | 2 +- autoload/codefmt/luaformatterfiveone.vim | 2 +- autoload/codefmt/mixformat.vim | 4 ++-- autoload/codefmt/nixpkgs_fmt.vim | 2 +- autoload/codefmt/ocamlformat.vim | 2 +- autoload/codefmt/ormolu.vim | 2 +- autoload/codefmt/prettier.vim | 7 ++++-- autoload/codefmt/rustfmt.vim | 2 +- autoload/codefmt/shfmt.vim | 2 +- autoload/codefmt/swiftformat.vim | 2 +- autoload/codefmt/yapf.vim | 2 +- autoload/codefmt/zprint.vim | 2 +- doc/codefmt.txt | 15 ++++++++++++ 26 files changed, 81 insertions(+), 32 deletions(-) diff --git a/autoload/codefmt/autopep8.vim b/autoload/codefmt/autopep8.vim index 6b42b8c..f9ba8a5 100644 --- a/autoload/codefmt/autopep8.vim +++ b/autoload/codefmt/autopep8.vim @@ -38,7 +38,7 @@ function! codefmt#autopep8#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'python' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'python') endfunction "" diff --git a/autoload/codefmt/black.vim b/autoload/codefmt/black.vim index a8f0628..697554b 100644 --- a/autoload/codefmt/black.vim +++ b/autoload/codefmt/black.vim @@ -30,7 +30,7 @@ function! codefmt#black#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'python' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'python') endfunction "" diff --git a/autoload/codefmt/clangformat.vim b/autoload/codefmt/clangformat.vim index b5d2f8f..afa6fd2 100644 --- a/autoload/codefmt/clangformat.vim +++ b/autoload/codefmt/clangformat.vim @@ -115,16 +115,14 @@ function! codefmt#clangformat#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - if &filetype is# 'c' || &filetype is# 'cpp' || - \ &filetype is# 'proto' || &filetype is# 'javascript' || - \ &filetype is# 'objc' || &filetype is# 'objcpp' || - \ &filetype is# 'typescript' || &filetype is# 'arduino' || - \ &filetype is# 'cuda' + if codefmt#formatterhelpers#FiletypeMatches( + \ &filetype, + \ ['c', 'cpp', 'cuda', 'proto', 'javascript', 'objc', 'objcpp', 'typescript', 'arduino']) return 1 endif " Version 3.6 adds support for java " http://llvm.org/releases/3.6.0/tools/clang/docs/ReleaseNotes.html - return &filetype is# 'java' && s:ClangFormatHasAtLeastVersion([3, 6]) + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'java') && s:ClangFormatHasAtLeastVersion([3, 6]) endfunction "" diff --git a/autoload/codefmt/cljstyle.vim b/autoload/codefmt/cljstyle.vim index bd97e90..0e9fca5 100644 --- a/autoload/codefmt/cljstyle.vim +++ b/autoload/codefmt/cljstyle.vim @@ -31,7 +31,7 @@ function! codefmt#cljstyle#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'clojure' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'clojure') endfunction "" diff --git a/autoload/codefmt/dartfmt.vim b/autoload/codefmt/dartfmt.vim index f2c0022..6bd98a1 100644 --- a/autoload/codefmt/dartfmt.vim +++ b/autoload/codefmt/dartfmt.vim @@ -30,7 +30,7 @@ function! codefmt#dartfmt#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'dart' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'dart') endfunction "" diff --git a/autoload/codefmt/fish_indent.vim b/autoload/codefmt/fish_indent.vim index 795b3b4..7b0c78d 100644 --- a/autoload/codefmt/fish_indent.vim +++ b/autoload/codefmt/fish_indent.vim @@ -27,7 +27,7 @@ function! codefmt#fish_indent#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'fish' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'fish') endfunction "" diff --git a/autoload/codefmt/formatterhelpers.vim b/autoload/codefmt/formatterhelpers.vim index eedfd2f..ebc5d1c 100644 --- a/autoload/codefmt/formatterhelpers.vim +++ b/autoload/codefmt/formatterhelpers.vim @@ -16,6 +16,35 @@ let s:plugin = maktaba#plugin#Get('codefmt') +" TODO(google/vim-maktaba#255): Use maktaba's when dropping support for 1.16.0. +function! s:ValueAsList(Value_or_values) abort + return maktaba#value#IsList(a:Value_or_values) ? + \ a:Value_or_values : [a:Value_or_values] +endfunction + + +"" +" @public +" Checks if the given {filetype} matches {expected} filetype(s). +" +" Usage examples: > +" if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c') +" < > +" if codefmt#formatterhelpers#FiletypeMatches(&filetype, ['c', 'cpp']) +" < +" @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 +endfunction + + "" " @public " Format lines in the current buffer via a formatter invoked by {cmd}, which @@ -50,6 +79,7 @@ endfunction " code that calls it. " " @throws ShellError if the {cmd} system call fails +" @throws WrongType function! codefmt#formatterhelpers#AttemptFakeRangeFormatting( \ startline, endline, cmd) abort call maktaba#ensure#IsNumber(a:startline) diff --git a/autoload/codefmt/gn.vim b/autoload/codefmt/gn.vim index 3cfd9db..7d3353d 100644 --- a/autoload/codefmt/gn.vim +++ b/autoload/codefmt/gn.vim @@ -31,7 +31,7 @@ function! codefmt#gn#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'gn' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'gn') endfunction "" diff --git a/autoload/codefmt/gofmt.vim b/autoload/codefmt/gofmt.vim index da4aad6..723e594 100644 --- a/autoload/codefmt/gofmt.vim +++ b/autoload/codefmt/gofmt.vim @@ -30,7 +30,7 @@ function! codefmt#gofmt#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'go' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'go') endfunction "" diff --git a/autoload/codefmt/googlejava.vim b/autoload/codefmt/googlejava.vim index 94fba8e..2981821 100644 --- a/autoload/codefmt/googlejava.vim +++ b/autoload/codefmt/googlejava.vim @@ -41,7 +41,7 @@ function! codefmt#googlejava#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'java' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'java') endfunction "" diff --git a/autoload/codefmt/isort.vim b/autoload/codefmt/isort.vim index 40bf2f0..2e8aa2a 100644 --- a/autoload/codefmt/isort.vim +++ b/autoload/codefmt/isort.vim @@ -30,7 +30,7 @@ function! codefmt#isort#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'python' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'python') endfunction "" diff --git a/autoload/codefmt/jsbeautify.vim b/autoload/codefmt/jsbeautify.vim index 7ea08c4..b90ef74 100644 --- a/autoload/codefmt/jsbeautify.vim +++ b/autoload/codefmt/jsbeautify.vim @@ -70,7 +70,7 @@ function! codefmt#jsbeautify#GetFormatter() abort " 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 index(l:filetypes, l:filetype) >= 0 + if codefmt#formatterhelpers#FiletypeMatches(l:filetype, l:filetypes) return l:format_name endif endfor diff --git a/autoload/codefmt/jsonnetfmt.vim b/autoload/codefmt/jsonnetfmt.vim index f3068b3..a56fe3b 100644 --- a/autoload/codefmt/jsonnetfmt.vim +++ b/autoload/codefmt/jsonnetfmt.vim @@ -1,6 +1,9 @@ let s:plugin = maktaba#plugin#Get('codefmt') -let s:SUPPORTED_FILETYPES = ['json', 'jsonnet'] +if !exists('s:SUPPORTED_FILETYPES') + let s:SUPPORTED_FILETYPES = ['json', 'jsonnet'] + lockvar! s:SUPPORTED_FILETYPES +endif "" @@ -17,7 +20,7 @@ function! codefmt#jsonnetfmt#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return index(s:SUPPORTED_FILETYPES, &filetype) >= 0 + return codefmt#formatterhelpers#FiletypeMatches(&filetype, s:SUPPORTED_FILETYPES) endfunction "" diff --git a/autoload/codefmt/ktfmt.vim b/autoload/codefmt/ktfmt.vim index 88681eb..c250e4f 100644 --- a/autoload/codefmt/ktfmt.vim +++ b/autoload/codefmt/ktfmt.vim @@ -71,7 +71,7 @@ function! codefmt#ktfmt#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'kotlin' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'kotlin') endfunction "" diff --git a/autoload/codefmt/luaformatterfiveone.vim b/autoload/codefmt/luaformatterfiveone.vim index 01a8530..732e661 100644 --- a/autoload/codefmt/luaformatterfiveone.vim +++ b/autoload/codefmt/luaformatterfiveone.vim @@ -31,7 +31,7 @@ function! codefmt#luaformatterfiveone#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'lua' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'lua') endfunction "" diff --git a/autoload/codefmt/mixformat.vim b/autoload/codefmt/mixformat.vim index a24a7ab..d1b6c4b 100644 --- a/autoload/codefmt/mixformat.vim +++ b/autoload/codefmt/mixformat.vim @@ -35,8 +35,8 @@ function! codefmt#mixformat#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'elixir' || &filetype is# 'eelixir' - \ || &filetype is# 'heex' + return codefmt#formatterhelpers#FiletypeMatches( + \ &filetype, ['elixir', 'eelixir', 'heex']) endfunction "" diff --git a/autoload/codefmt/nixpkgs_fmt.vim b/autoload/codefmt/nixpkgs_fmt.vim index 2046a6b..940a15b 100644 --- a/autoload/codefmt/nixpkgs_fmt.vim +++ b/autoload/codefmt/nixpkgs_fmt.vim @@ -31,7 +31,7 @@ function! codefmt#nixpkgs_fmt#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'nix' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'nix') endfunction "" diff --git a/autoload/codefmt/ocamlformat.vim b/autoload/codefmt/ocamlformat.vim index 156af9e..01663f7 100644 --- a/autoload/codefmt/ocamlformat.vim +++ b/autoload/codefmt/ocamlformat.vim @@ -30,7 +30,7 @@ function! codefmt#ocamlformat#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'ocaml' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'ocaml') endfunction "" diff --git a/autoload/codefmt/ormolu.vim b/autoload/codefmt/ormolu.vim index dcdb424..e9be408 100644 --- a/autoload/codefmt/ormolu.vim +++ b/autoload/codefmt/ormolu.vim @@ -30,7 +30,7 @@ function! codefmt#ormolu#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'haskell' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'haskell') endfunction "" diff --git a/autoload/codefmt/prettier.vim b/autoload/codefmt/prettier.vim index f310dc3..add1f63 100644 --- a/autoload/codefmt/prettier.vim +++ b/autoload/codefmt/prettier.vim @@ -16,8 +16,11 @@ let s:plugin = maktaba#plugin#Get('codefmt') " See https://prettier.io for a list of supported file types. -let s:supported_filetypes = ['javascript', 'markdown', 'html', 'css', 'yaml', +if !exists('s:SUPPORTED_FILETYPES') + let s:SUPPORTED_FILETYPES = ['javascript', 'markdown', 'html', 'css', 'yaml', \ 'jsx', 'less', 'scss', 'mdx', 'vue'] + lockvar! s:SUPPORTED_FILETYPES +endif "" @@ -56,7 +59,7 @@ function! codefmt#prettier#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return index(s:supported_filetypes, &filetype) >= 0 + return codefmt#formatterhelpers#FiletypeMatches(&filetype, s:SUPPORTED_FILETYPES) endfunction "" diff --git a/autoload/codefmt/rustfmt.vim b/autoload/codefmt/rustfmt.vim index 2c0789f..0d7e38a 100644 --- a/autoload/codefmt/rustfmt.vim +++ b/autoload/codefmt/rustfmt.vim @@ -31,7 +31,7 @@ function! codefmt#rustfmt#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'rust' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'rust') endfunction "" diff --git a/autoload/codefmt/shfmt.vim b/autoload/codefmt/shfmt.vim index c016d35..114263d 100644 --- a/autoload/codefmt/shfmt.vim +++ b/autoload/codefmt/shfmt.vim @@ -36,7 +36,7 @@ function! codefmt#shfmt#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'sh' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'sh') endfunction "" diff --git a/autoload/codefmt/swiftformat.vim b/autoload/codefmt/swiftformat.vim index 3aa3502..744acc9 100644 --- a/autoload/codefmt/swiftformat.vim +++ b/autoload/codefmt/swiftformat.vim @@ -29,7 +29,7 @@ function! codefmt#swiftformat#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'swift' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'swift') endfunction "" diff --git a/autoload/codefmt/yapf.vim b/autoload/codefmt/yapf.vim index a415414..1225d00 100644 --- a/autoload/codefmt/yapf.vim +++ b/autoload/codefmt/yapf.vim @@ -30,7 +30,7 @@ function! codefmt#yapf#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'python' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'python') endfunction "" diff --git a/autoload/codefmt/zprint.vim b/autoload/codefmt/zprint.vim index caf734c..f20a500 100644 --- a/autoload/codefmt/zprint.vim +++ b/autoload/codefmt/zprint.vim @@ -44,7 +44,7 @@ function! codefmt#zprint#GetFormatter() abort endfunction function l:formatter.AppliesToBuffer() abort - return &filetype is# 'clojure' + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'clojure') endfunction "" diff --git a/doc/codefmt.txt b/doc/codefmt.txt index d34c2ee..ce523a5 100644 --- a/doc/codefmt.txt +++ b/doc/codefmt.txt @@ -357,6 +357,20 @@ codefmt#FormatMap({type}) *codefmt#FormatMap()* Suitable for use as 'operatorfunc'; see |g@| for details. The type is ignored since formatting only works on complete lines. +codefmt#formatterhelpers#FiletypeMatches({filetype}, {expected}) + *codefmt#formatterhelpers#FiletypeMatches()* + Checks if the given {filetype} matches {expected} filetype(s). + + Usage examples: +> + if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c') +< + +> + if codefmt#formatterhelpers#FiletypeMatches(&filetype, ['c', 'cpp']) +< + Throws ERROR(WrongType) + codefmt#formatterhelpers#Format({cmd}) *codefmt#formatterhelpers#Format()* Format lines in the current buffer via a formatter invoked by {cmd}, which is a system call represented by either a |maktaba.Syscall| or any argument @@ -380,6 +394,7 @@ codefmt#formatterhelpers#AttemptFakeRangeFormatting({startline}, {endline}, code that calls it. Throws ERROR(ShellError) if the {cmd} system call fails + Throws ERROR(WrongType) codefmt#formatterhelpers#ResolveFlagToArray({flag_name}) *codefmt#formatterhelpers#ResolveFlagToArray()*