Skip to content

Commit 92bfede

Browse files
committed
add lua formatter
1 parent 6fa1616 commit 92bfede

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ codefmt` if codefmt is installed (and helptags have been generated).
3131
* Julia ([JuliaFormatter](https://github.com/domluna/JuliaFormatter.jl))
3232
* Kotlin ([ktfmt](https://github.com/facebookincubator/ktfmt))
3333
* Lua
34-
([FormatterFiveOne](https://luarocks.org/modules/ElPiloto/formatterfiveone)
34+
([FormatterFiveOne](https://luarocks.org/modules/ElPiloto/formatterfiveone),
35+
[StyLua](https://github.com/JohnnyMorganz/StyLua))
3536
* Markdown (prettier)
3637
* Nix (nixpkgs-fmt)
3738
* OCaml ([ocamlformat](https://github.com/ocaml-ppx/ocamlformat))

autoload/codefmt/stylua.vim

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
let s:plugin = maktaba#plugin#Get('codefmt')
2+
3+
4+
""
5+
" @private
6+
"
7+
" Formatter provider for lua files using stylua.
8+
function! codefmt#stylua#GetFormatter() abort
9+
let l:formatter = {
10+
\ 'name': 'stylua',
11+
\ 'setup_instructions': 'Install stylua (https://github.com/JohnnyMorganz/StyLua).'}
12+
13+
function l:formatter.IsAvailable() abort
14+
return executable(s:plugin.Flag('stylua_executable'))
15+
endfunction
16+
17+
function l:formatter.AppliesToBuffer() abort
18+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'lua')
19+
endfunction
20+
21+
""
22+
" Reformat the current buffer with stylua or the binary named in
23+
" @flag(stylua_executable)
24+
" @throws ShellError
25+
function l:formatter.Format() abort
26+
let l:cmd = [s:plugin.Flag('stylua_executable')]
27+
" Specify we are sending input through stdin
28+
let l:cmd += ['--stdin-filepath', expand('%:p'), '-']
29+
30+
try
31+
call codefmt#formatterhelpers#Format(l:cmd)
32+
catch
33+
" Parse all the errors and stick them in the quickfix list.
34+
let l:errors = []
35+
for line in split(v:exception, "\n")
36+
let l:fname_pattern = 'stdin'
37+
let l:tokens = matchlist(line, '\C\v^\[string "isCodeValid"\]:(\d+): (.*)')
38+
if !empty(l:tokens)
39+
call add(l:errors, {
40+
\ "filename": @%,
41+
\ "lnum": l:tokens[1],
42+
\ "text": l:tokens[2]})
43+
endif
44+
endfor
45+
46+
if empty(l:errors)
47+
" Couldn't parse stylua error format; display it all.
48+
call maktaba#error#Shout('Error formatting file: %s', v:exception)
49+
else
50+
call setqflist(l:errors, 'r')
51+
cc 1
52+
endif
53+
endtry
54+
endfunction
55+
56+
return l:formatter
57+
endfunction

instant/flags.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ call s:plugin.Flag('nixpkgs_fmt_executable', 'nixpkgs-fmt')
246246
""
247247
" The path to the luaformatterfiveone executable.
248248
call s:plugin.Flag('luaformatterfiveone_executable', 'luaformatterfiveone')
249+
"
250+
""
251+
" The path to the stylua executable.
252+
call s:plugin.Flag('stylua_executable', 'stylua')
253+
249254

250255
""
251256
" The path to the cljstyle executable.

plugin/register.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
" * json, jsonnet: jsonnetfmt
4242
" * julia: JuliaFormatter
4343
" * kotlin: ktfmt
44-
" * lua: luaformatterfiveone
44+
" * lua: luaformatterfiveone, stylua
4545
" * nix: nixpkgs-fmt
4646
" * ocaml: ocamlformat
4747
" * python: autopep8, black, yapf
@@ -78,6 +78,7 @@ call s:registry.AddExtension(codefmt#prettier#GetFormatter())
7878
call s:registry.AddExtension(codefmt#juliaformatter#GetFormatter())
7979
call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())
8080
call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter())
81+
call s:registry.AddExtension(codefmt#stylua#GetFormatter())
8182
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())
8283
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
8384
call s:registry.AddExtension(codefmt#isort#GetFormatter())

vroom/stylua.vroom

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
stylua is a formatter that knows how to format all lua 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+
stylua expects the executable to be installed on your system.
19+
20+
% function hello()<CR>
21+
% print("world")<CR>
22+
% end
23+
:FormatCode stylua
24+
! stylua -i 2> .*
25+
$ function hello()
26+
$ print("world")
27+
$ end
28+
29+
The name or path of the stylua executable can be configured via the
30+
stylua_executable flag if the default of "buildifier" doesn't work.
31+
32+
:Glaive codefmt stylua_executable='mystylua'
33+
:FormatCode stylua
34+
! mystylelua -i 2> .*
35+
$ function hello()
36+
$ print("world")
37+
$ end
38+
:Glaive codefmt stylua_executable='stylua'
39+
40+
Errors are reported using the quickfix list.
41+
42+
@clear
43+
% 13()
44+
:FormatCode stylua
45+
! stylua -i 2> (.*)
46+
$ 1 (status)
47+
$ echo >\1 ' (command)
48+
|stylua:Unable to format stdin:\n
49+
|[string "isCodeValid"]:1: unexpected symbol near '"'13'"
50+
~ (1 of 1): unexpected symbol near '13'
51+
:echomsg line('.') . ',' . col('.')
52+
~ 1,1
53+
:echomsg string(map(getqflist(), 'v:val.lnum . "," . v:val.text'))
54+
~ ['1,unexpected symbol near ''13''']
55+

0 commit comments

Comments
 (0)