Skip to content

Implement codefmt#formatterhelpers#FiletypeMatches. #213

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 21, 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
2 changes: 1 addition & 1 deletion autoload/codefmt/autopep8.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/black.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
10 changes: 4 additions & 6 deletions autoload/codefmt/clangformat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/cljstyle.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/dartfmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/fish_indent.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
30 changes: 30 additions & 0 deletions autoload/codefmt/formatterhelpers.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/gn.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/gofmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/googlejava.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/isort.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/jsbeautify.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions autoload/codefmt/jsonnetfmt.vim
Original file line number Diff line number Diff line change
@@ -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


""
Expand All @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/ktfmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/luaformatterfiveone.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
4 changes: 2 additions & 2 deletions autoload/codefmt/mixformat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/nixpkgs_fmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/ocamlformat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/ormolu.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
7 changes: 5 additions & 2 deletions autoload/codefmt/prettier.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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


""
Expand Down Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/rustfmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/shfmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/swiftformat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/yapf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
2 changes: 1 addition & 1 deletion autoload/codefmt/zprint.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
15 changes: 15 additions & 0 deletions doc/codefmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()*
Expand Down