diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index 7edab59..417fe50 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -33,6 +33,7 @@ " * fish: fish_indent " * gn: gn " * go: gofmt +" * lua: luaformatterfiveone " * python: autopep8, black, yapf diff --git a/autoload/codefmt/luaformatterfiveone.vim b/autoload/codefmt/luaformatterfiveone.vim new file mode 100644 index 0000000..01a8530 --- /dev/null +++ b/autoload/codefmt/luaformatterfiveone.vim @@ -0,0 +1,74 @@ +" Copyright 2017 Google Inc. All rights reserved. +" +" Licensed under the Apache License, Version 2.0 (the "License"); +" you may not use this file except in compliance with the License. +" You may obtain a copy of the License at +" +" http://www.apache.org/licenses/LICENSE-2.0 +" +" Unless required by applicable law or agreed to in writing, software +" distributed under the License is distributed on an "AS IS" BASIS, +" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +" See the License for the specific language governing permissions and +" limitations under the License. + + +let s:plugin = maktaba#plugin#Get('codefmt') + + +"" +" @private +" +" Formatter provider for lua files using luaformatterfiveone. +function! codefmt#luaformatterfiveone#GetFormatter() abort + let l:formatter = { + \ 'name': 'luaformatterfiveone', + \ 'setup_instructions': 'Install luaformatterfiveone with luarocks. ' . + \ '(https://luarocks.org/modules/ElPiloto/formatterfiveone).'} + + function l:formatter.IsAvailable() abort + return executable(s:plugin.Flag('luaformatterfiveone_executable')) + endfunction + + function l:formatter.AppliesToBuffer() abort + return &filetype is# 'lua' + endfunction + + "" + " Reformat the current buffer with luaformatterfiveone or the binary named in + " @flag(luaformatterfiveone_executable) + " @throws ShellError + function l:formatter.Format() abort + let l:cmd = [ s:plugin.Flag('luaformatterfiveone_executable')] + " Specify we are sending input through stdin + let l:cmd += ['-i'] + + try + call codefmt#formatterhelpers#Format(l:cmd) + catch + " Parse all the errors and stick them in the quickfix list. + let l:errors = [] + for line in split(v:exception, "\n") + let l:fname_pattern = 'stdin' + let l:tokens = matchlist(line, + \ '\C\v^\[string "isCodeValid"\]:(\d+): (.*)') + if !empty(l:tokens) + call add(l:errors, { + \ "filename": @%, + \ "lnum": l:tokens[1], + \ "text": l:tokens[2]}) + endif + endfor + + if empty(l:errors) + " Couldn't parse buildifier error format; display it all. + call maktaba#error#Shout('Error formatting file: %s', v:exception) + else + call setqflist(l:errors, 'r') + cc 1 + endif + endtry + endfunction + + return l:formatter +endfunction diff --git a/instant/flags.vim b/instant/flags.vim index 680059e..6fec774 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -172,3 +172,7 @@ call s:plugin.Flag('fish_indent_executable', 'fish_indent') "" " The path to the nixpkgs-fmt executable. call s:plugin.Flag('nixpkgs_fmt_executable', 'nixpkgs-fmt') + +"" +" The path to the luaformatterfiveone executable. +call s:plugin.Flag('luaformatterfiveone_executable', 'luaformatterfiveone') diff --git a/plugin/register.vim b/plugin/register.vim index 6a3176f..ca2fa6a 100644 --- a/plugin/register.vim +++ b/plugin/register.vim @@ -39,3 +39,4 @@ call s:registry.AddExtension(codefmt#shfmt#GetFormatter()) call s:registry.AddExtension(codefmt#zprint#GetFormatter()) call s:registry.AddExtension(codefmt#fish_indent#GetFormatter()) call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter()) +call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter()) diff --git a/vroom/luaformatterfiveone.vroom b/vroom/luaformatterfiveone.vroom new file mode 100644 index 0000000..5a6a4fc --- /dev/null +++ b/vroom/luaformatterfiveone.vroom @@ -0,0 +1,53 @@ +luaformatterfiveone" formatter only knows how to format lua 5.1 code. +If you aren't familiar with basic codefmt usage yet, see main.vroom + +We'll set up codefmt and configure the vroom environment, then jump into some +examples. + + :source $VROOMDIR/setupvroom.vim + + :let g:repeat_calls = [] + :function FakeRepeat(...) + | call add(g:repeat_calls, a:000) + :endfunction + :call maktaba#test#Override('repeat#set', 'FakeRepeat') + + :call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) + + +luaformatterfiveone expects the lua formatterfiveone executable to be installed +on your system. + + % function hello() + % print("world") + % end + :FormatCode luaformatterfiveone + ! luaformatterfiveone -i 2> .* + $ function hello() + $ print("world") + $ end + +The name or path of the luaformatterfiveone executable can be configured via the +luaformatterfiveone_executable flag if the default of "buildifier" doesn't work. + + :Glaive codefmt luaformatterfiveone_executable='myluaformatterfiveone' + :FormatCode luaformatterfiveone + ! myluaformatterfiveone -i 2> .* + $ function hello() + $ print("world") + $ end + :Glaive codefmt luaformatterfiveone_executable='luaformatterfiveone' + +Errors are reported using the quickfix list. + + @clear + % 13() + :FormatCode luaformatterfiveone + ! luaformatterfiveone -i 2> .* + ~ (1 of 1): unexpected symbol near '13' + :echomsg line('.') . ',' . col('.') + ~ 1,1 + :echomsg string(map(getqflist(), + |'v:val.lnum . "," . v:val.text')) + ~ ['1,unexpected symbol near ''13'''] +