Skip to content

register jsonnetfmt using buildifier as a template #206

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 3 commits into from
Dec 3, 2022
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
47 changes: 47 additions & 0 deletions autoload/codefmt/jsonnetfmt.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
let s:plugin = maktaba#plugin#Get('codefmt')


""
" @private
"
" Formatter provider for jsonnet files using jsonnetfmt
function! codefmt#jsonnetfmt#GetFormatter() abort
let l:formatter = {
\ 'name': 'jsonnetfmt',
\ 'setup_instructions': 'Install jsonnet. ' .
\ '(https://jsonnet.org/).'}

function l:formatter.IsAvailable() abort
return executable(s:plugin.Flag('jsonnetfmt_executable'))
endfunction

let l:supported_filetypes = ['json','jsonnet']

function l:formatter.AppliesToBuffer() abort
return index(l:supported_filetypes, &filetype) >= 0
endfunction

""
" Reformat the current buffer with jsonnetfmt or the binary named in
" @flag(jsonnetfmt_executable)
" @throws ShellError
function l:formatter.Format() abort
let l:cmd = [ s:plugin.Flag('jsonnetfmt_executable') ]
let l:fname = expand('%:p')
if !empty(l:fname)
let l:cmd += ['-path', l:fname]
endif

try
" NOTE: Ignores any line ranges given and formats entire buffer.
" jsonnetfmt does not support range formatting.
call codefmt#formatterhelpers#Format(l:cmd)
catch
" TODO: Parse all the errors and stick them in the quickfix list.
" currently just echoes the errors.
call maktaba#error#Shout('Error formatting file: %s', v:exception)
endtry
endfunction

return l:formatter
endfunction
4 changes: 4 additions & 0 deletions instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ call s:plugin.Flag('buildifier_lint_mode', '')
" - "+some-warning": Add 'some-warning' to the warning set.
call s:plugin.Flag('buildifier_warnings', '')

""
" The path to the jsonnetfmt executable.
call s:plugin.Flag('jsonnetfmt_executable', 'jsonnetfmt')

""
" The path to the google-java executable. Generally, this should have the
" form:
Expand Down
1 change: 1 addition & 0 deletions plugin/register.vim
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ call s:registry.AddExtension(codefmt#fish_indent#GetFormatter())
call s:registry.AddExtension(codefmt#gn#GetFormatter())
call s:registry.AddExtension(codefmt#gofmt#GetFormatter())
call s:registry.AddExtension(codefmt#googlejava#GetFormatter())
call s.registry.AddExtension(codefmt#jsonnetfmt#GetFormatter())
call s:registry.AddExtension(codefmt#jsbeautify#GetFormatter())
call s:registry.AddExtension(codefmt#prettier#GetFormatter())
call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())
Expand Down
33 changes: 33 additions & 0 deletions vroom/jsonnetfmt.vroom
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

:source $VROOMDIR/setupvroom.vim

:let g:repeat_calls = []
:function FakeRepeat(...)<CR>
| call add(g:repeat_calls, a:000)<CR>
:endfunction
:call maktaba#test#Override('repeat#set', 'FakeRepeat')

:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)


The jsonnetfmt formatter expects the jsonnetfmt executable to be installed on
your system.

% {foo:'bar'}
:FormatCode jsonnetfmt
! jsonnetfmt .*
$ { foo: 'foo' }


The name or path of the jsonnetfmt executable can be configured via the
jsonnetfmt_executable flag if the default of "jsonnetfmt" doesn't work.

:Glaive codefmt jsonnetfmt_executable='myjsonnetfmt'
:FormatCode jsonnetfmt
! myjsonnetfmt .*
$ foo_library(
$ name = "foo",
$ srcs = ["bar.js"],
$ )
:Glaive codefmt jsonnetfmt_executable='jsonnetfmt'