Skip to content

Commit 901a43d

Browse files
ElPilotoElPiloto
ElPiloto
authored and
ElPiloto
committed
Add formatter for lua 5.1
luaformatterfiveone is a formatter for Lua 5.1. It is readily available through standard via `luarocks install formatterfiveone`.
1 parent 6761a21 commit 901a43d

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

Diff for: autoload/codefmt/luaformatterfiveone.vim

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
" Copyright 2017 Google Inc. All rights reserved.
2+
"
3+
" Licensed under the Apache License, Version 2.0 (the "License");
4+
" you may not use this file except in compliance with the License.
5+
" You may obtain a copy of the License at
6+
"
7+
" http://www.apache.org/licenses/LICENSE-2.0
8+
"
9+
" Unless required by applicable law or agreed to in writing, software
10+
" distributed under the License is distributed on an "AS IS" BASIS,
11+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
" See the License for the specific language governing permissions and
13+
" limitations under the License.
14+
15+
16+
let s:plugin = maktaba#plugin#Get('codefmt')
17+
18+
19+
""
20+
" @private
21+
"
22+
" Formatter provider for lua files using luaformatterfiveone.
23+
function! codefmt#luaformatterfiveone#GetFormatter() abort
24+
let l:formatter = {
25+
\ 'name': 'luaformatterfiveone',
26+
\ 'setup_instructions': 'Install luaformatterfiveone with luarocks. ' .
27+
\ '(https://luarocks.org/modules/ElPiloto/formatterfiveone).'}
28+
29+
function l:formatter.IsAvailable() abort
30+
return executable(s:plugin.Flag('luaformatterfiveone_executable'))
31+
endfunction
32+
33+
function l:formatter.AppliesToBuffer() abort
34+
return &filetype is# 'lua'
35+
endfunction
36+
37+
""
38+
" Reformat the current buffer with luaformatterfiveone or the binary named in
39+
" @flag(luaformatterfiveone)
40+
" @throws ShellError
41+
function l:formatter.Format() abort
42+
let l:cmd = [ s:plugin.Flag('luaformatterfiveone_executable')]
43+
" Specify we are sending input through stdin
44+
let l:cmd += ['-i']
45+
46+
try
47+
call codefmt#formatterhelpers#Format(l:cmd)
48+
catch
49+
" Parse all the errors and stick them in the quickfix list.
50+
let l:errors = []
51+
for line in split(v:exception, "\n")
52+
let l:fname_pattern = 'stdin'
53+
let l:tokens = matchlist(line,
54+
\ '\C\v^\[string "isCodeValid"\]:(\d+): (.*)')
55+
if !empty(l:tokens)
56+
call add(l:errors, {
57+
\ "filename": @%,
58+
\ "lnum": l:tokens[1],
59+
\ "text": l:tokens[2]})
60+
endif
61+
"\ "col": l:tokens[2],
62+
endfor
63+
64+
if empty(l:errors)
65+
" Couldn't parse buildifier error format; display it all.
66+
call maktaba#error#Shout('Error formatting file: %s', v:exception)
67+
else
68+
call setqflist(l:errors, 'r')
69+
cc 1
70+
endif
71+
endtry
72+
endfunction
73+
74+
return l:formatter
75+
endfunction

Diff for: instant/flags.vim

+4
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,7 @@ call s:plugin.Flag('fish_indent_executable', 'fish_indent')
172172
""
173173
" The path to the nixpkgs-fmt executable.
174174
call s:plugin.Flag('nixpkgs_fmt_executable', 'nixpkgs-fmt')
175+
176+
""
177+
" The path to the luaformatterfiveone executable.
178+
call s:plugin.Flag('luaformatterfiveone_executable', 'luaformatterfiveone')

Diff for: plugin/register.vim

+1
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ call s:registry.AddExtension(codefmt#shfmt#GetFormatter())
3939
call s:registry.AddExtension(codefmt#zprint#GetFormatter())
4040
call s:registry.AddExtension(codefmt#fish_indent#GetFormatter())
4141
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())
42+
call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter())

Diff for: vroom/luaformatterfiveone.vroom

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
luaformatterfiveone" formatter only knows how to format lua 5.1 code.
2+
If you aren't familiar with basic codefmt usage yet, see main.vroom
3+
4+
We'll set up codefmt and configure the vroom environment, then jump into some
5+
examples.
6+
7+
:source $VROOMDIR/setupvroom.vim
8+
9+
:let g:repeat_calls = []
10+
:function FakeRepeat(...)<CR>
11+
| call add(g:repeat_calls, a:000)<CR>
12+
:endfunction
13+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
14+
15+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
16+
17+
18+
luaformatterfiveone expects the lua formatterfiveone executable to be installed
19+
on your system.
20+
21+
% function hello()
22+
% print("world")
23+
% end
24+
:FormatCode luaformatterfiveone
25+
! luaformatterfiveone -i 2> .*
26+
$ function hello()
27+
$ print("world")
28+
$ end
29+
30+
The name or path of the luaformatterfiveone executable can be configured via the
31+
luaformatterfiveone_executable flag if the default of "buildifier" doesn't work.
32+
33+
:Glaive codefmt luaformatterfiveone_executable='myluaformatterfiveone'
34+
:FormatCode luaformatterfiveone
35+
! myluaformatterfiveone -i 2> .*
36+
$ function hello()
37+
$ print("world")
38+
$ end
39+
:Glaive codefmt luaformatterfiveone_executable='luaformatterfiveone'
40+
41+
Errors are reported using the quickfix list.
42+
43+
@clear
44+
% 13()
45+
:FormatCode luaformatterfiveone
46+
! luaformatterfiveone -i 2> .*
47+
~ (1 of 1): unexpected symbol near '13'
48+
:echomsg line('.') . ',' . col('.')
49+
~ 1,1
50+
:echomsg string(map(getqflist(),
51+
|'v:val.lnum . "," . v:val.text'))
52+
~ ['1,unexpected symbol near ''13''']
53+

0 commit comments

Comments
 (0)