Skip to content

Commit 0fbe1b6

Browse files
authored
Consolidate jsbeautify supported formats and allow for sass/scss/less (#173)
Deduplicates per-filetype logic in AppliesToBuffer & FormatRange to use a shared _supported_formats definition. In the process, updates AppliesToBuffer to accept sass/scss/less filetypes, where #114 had only fixed them to work via explicit `:FormatCode jsbeautify` but not via `:FormatCode`.
1 parent 3494825 commit 0fbe1b6

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

autoload/codefmt/jsbeautify.vim

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,35 @@ function! codefmt#jsbeautify#GetFormatter() abort
2525
\ 'setup_instructions': 'Install js-beautify ' .
2626
\ '(https://www.npmjs.com/package/js-beautify).'}
2727

28-
function l:formatter.IsAvailable() abort
28+
" Mapping of jsbeautify type to vim filetype name.
29+
" TODO: Support jsx and other variants?
30+
let l:formatter._supported_formats = {
31+
\ 'js': ['javascript', 'json'],
32+
\ 'css': ['css', 'sass', 'scss', 'less'],
33+
\ 'html': ['html']}
34+
35+
function l:formatter.IsAvailable() dict abort
2936
return executable(s:plugin.Flag('js_beautify_executable'))
3037
endfunction
3138

32-
function l:formatter.AppliesToBuffer() abort
33-
return &filetype is# 'css' || &filetype is# 'html' ||
34-
\ &filetype =~# '\mhtml\.' || &filetype is# 'json' ||
35-
\ &filetype is# 'javascript'
39+
function l:formatter.AppliesToBuffer() dict abort
40+
return self._GetSupportedFormatName(&filetype) isnot 0
3641
endfunction
3742

3843
""
3944
" Reformat the current buffer with js-beautify or the binary named in
4045
" @flag(js_beautify_executable), only targeting the range between {startline} and
4146
" {endline}.
4247
" @throws ShellError
43-
function l:formatter.FormatRange(startline, endline) abort
48+
function l:formatter.FormatRange(startline, endline) dict abort
4449
let l:cmd = [s:plugin.Flag('js_beautify_executable'), '-f', '-']
45-
if &filetype is# 'javascript' || &filetype is# 'json'
46-
let l:cmd = l:cmd + ['--type', 'js']
47-
elseif &filetype is# 'sass' || &filetype is# 'scss' || &filetype is# 'less'
48-
let l:cmd = l:cmd + ['--type', 'css']
49-
elseif &filetype =~# '\mhtml\.'
50-
let l:cmd = l:cmd + ['--type', 'html']
51-
elseif &filetype != ""
52-
let l:cmd = l:cmd + ['--type', &filetype]
50+
" Add --type if known
51+
if !empty(&filetype)
52+
let l:format_name = self._GetSupportedFormatName(&filetype)
53+
if l:format_name is 0
54+
let l:format_name = &filetype
55+
endif
56+
let l:cmd += ['--type', l:format_name]
5357
endif
5458

5559
call maktaba#ensure#IsNumber(a:startline)
@@ -61,6 +65,18 @@ function! codefmt#jsbeautify#GetFormatter() abort
6165
\ a:startline, a:endline, l:cmd)
6266
endfunction
6367

68+
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', '')
72+
for [l:format_name, l:filetypes] in items(self._supported_formats)
73+
if index(l:filetypes, l:filetype) >= 0
74+
return l:format_name
75+
endif
76+
endfor
77+
return 0
78+
endfunction
79+
6480
return l:formatter
6581
endfunction
6682

0 commit comments

Comments
 (0)