Skip to content

Commit 6548022

Browse files
authored
register jsonnetfmt using buildifier as a template (#206)
1 parent 0fbe1b6 commit 6548022

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

autoload/codefmt/jsonnetfmt.vim

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
let s:plugin = maktaba#plugin#Get('codefmt')
2+
3+
4+
""
5+
" @private
6+
"
7+
" Formatter provider for jsonnet files using jsonnetfmt
8+
function! codefmt#jsonnetfmt#GetFormatter() abort
9+
let l:formatter = {
10+
\ 'name': 'jsonnetfmt',
11+
\ 'setup_instructions': 'Install jsonnet. ' .
12+
\ '(https://jsonnet.org/).'}
13+
14+
function l:formatter.IsAvailable() abort
15+
return executable(s:plugin.Flag('jsonnetfmt_executable'))
16+
endfunction
17+
18+
let l:supported_filetypes = ['json','jsonnet']
19+
20+
function l:formatter.AppliesToBuffer() abort
21+
return index(l:supported_filetypes, &filetype) >= 0
22+
endfunction
23+
24+
""
25+
" Reformat the current buffer with jsonnetfmt or the binary named in
26+
" @flag(jsonnetfmt_executable)
27+
" @throws ShellError
28+
function l:formatter.Format() abort
29+
let l:cmd = [ s:plugin.Flag('jsonnetfmt_executable') ]
30+
let l:fname = expand('%:p')
31+
if !empty(l:fname)
32+
let l:cmd += ['-path', l:fname]
33+
endif
34+
35+
try
36+
" NOTE: Ignores any line ranges given and formats entire buffer.
37+
" jsonnetfmt does not support range formatting.
38+
call codefmt#formatterhelpers#Format(l:cmd)
39+
catch
40+
" TODO: Parse all the errors and stick them in the quickfix list.
41+
" currently just echoes the errors.
42+
call maktaba#error#Shout('Error formatting file: %s', v:exception)
43+
endtry
44+
endfunction
45+
46+
return l:formatter
47+
endfunction

instant/flags.vim

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ call s:plugin.Flag('buildifier_lint_mode', '')
131131
" - "+some-warning": Add 'some-warning' to the warning set.
132132
call s:plugin.Flag('buildifier_warnings', '')
133133

134+
""
135+
" The path to the jsonnetfmt executable.
136+
call s:plugin.Flag('jsonnetfmt_executable', 'jsonnetfmt')
137+
134138
""
135139
" The path to the google-java executable. Generally, this should have the
136140
" form:

plugin/register.vim

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ call s:registry.AddExtension(codefmt#fish_indent#GetFormatter())
6969
call s:registry.AddExtension(codefmt#gn#GetFormatter())
7070
call s:registry.AddExtension(codefmt#gofmt#GetFormatter())
7171
call s:registry.AddExtension(codefmt#googlejava#GetFormatter())
72+
call s.registry.AddExtension(codefmt#jsonnetfmt#GetFormatter())
7273
call s:registry.AddExtension(codefmt#jsbeautify#GetFormatter())
7374
call s:registry.AddExtension(codefmt#prettier#GetFormatter())
7475
call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())

vroom/jsonnetfmt.vroom

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
:source $VROOMDIR/setupvroom.vim
3+
4+
:let g:repeat_calls = []
5+
:function FakeRepeat(...)<CR>
6+
| call add(g:repeat_calls, a:000)<CR>
7+
:endfunction
8+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
9+
10+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
11+
12+
13+
The jsonnetfmt formatter expects the jsonnetfmt executable to be installed on
14+
your system.
15+
16+
% {foo:'bar'}
17+
:FormatCode jsonnetfmt
18+
! jsonnetfmt .*
19+
$ { foo: 'foo' }
20+
21+
22+
The name or path of the jsonnetfmt executable can be configured via the
23+
jsonnetfmt_executable flag if the default of "jsonnetfmt" doesn't work.
24+
25+
:Glaive codefmt jsonnetfmt_executable='myjsonnetfmt'
26+
:FormatCode jsonnetfmt
27+
! myjsonnetfmt .*
28+
$ foo_library(
29+
$ name = "foo",
30+
$ srcs = ["bar.js"],
31+
$ )
32+
:Glaive codefmt jsonnetfmt_executable='jsonnetfmt'
33+

0 commit comments

Comments
 (0)