Skip to content

Add formatter for lua 5.1 #167

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
Dec 18, 2020
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
1 change: 1 addition & 0 deletions autoload/codefmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
" * fish: fish_indent
" * gn: gn
" * go: gofmt
" * lua: luaformatterfiveone
" * python: autopep8, black, yapf


Expand Down
74 changes: 74 additions & 0 deletions autoload/codefmt/luaformatterfiveone.vim
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
1 change: 1 addition & 0 deletions plugin/register.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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())
53 changes: 53 additions & 0 deletions vroom/luaformatterfiveone.vroom
Original file line number Diff line number Diff line change
@@ -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(...)<CR>
| call add(g:repeat_calls, a:000)<CR>
: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''']