From 92bfedef0b1bb85bfc505173d82b84b8339cd1d6 Mon Sep 17 00:00:00 2001 From: Lam Chau Date: Mon, 8 Jul 2024 01:57:29 -0700 Subject: [PATCH] add lua formatter --- README.md | 3 +- autoload/codefmt/stylua.vim | 57 +++++++++++++++++++++++++++++++++++++ instant/flags.vim | 5 ++++ plugin/register.vim | 3 +- vroom/stylua.vroom | 55 +++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 autoload/codefmt/stylua.vim create mode 100644 vroom/stylua.vroom diff --git a/README.md b/README.md index 2fbc7e8..e18110f 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ codefmt` if codefmt is installed (and helptags have been generated). * Julia ([JuliaFormatter](https://github.com/domluna/JuliaFormatter.jl)) * Kotlin ([ktfmt](https://github.com/facebookincubator/ktfmt)) * Lua - ([FormatterFiveOne](https://luarocks.org/modules/ElPiloto/formatterfiveone) + ([FormatterFiveOne](https://luarocks.org/modules/ElPiloto/formatterfiveone), + [StyLua](https://github.com/JohnnyMorganz/StyLua)) * Markdown (prettier) * Nix (nixpkgs-fmt) * OCaml ([ocamlformat](https://github.com/ocaml-ppx/ocamlformat)) diff --git a/autoload/codefmt/stylua.vim b/autoload/codefmt/stylua.vim new file mode 100644 index 0000000..e53bdce --- /dev/null +++ b/autoload/codefmt/stylua.vim @@ -0,0 +1,57 @@ +let s:plugin = maktaba#plugin#Get('codefmt') + + +"" +" @private +" +" Formatter provider for lua files using stylua. +function! codefmt#stylua#GetFormatter() abort + let l:formatter = { + \ 'name': 'stylua', + \ 'setup_instructions': 'Install stylua (https://github.com/JohnnyMorganz/StyLua).'} + + function l:formatter.IsAvailable() abort + return executable(s:plugin.Flag('stylua_executable')) + endfunction + + function l:formatter.AppliesToBuffer() abort + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'lua') + endfunction + + "" + " Reformat the current buffer with stylua or the binary named in + " @flag(stylua_executable) + " @throws ShellError + function l:formatter.Format() abort + let l:cmd = [s:plugin.Flag('stylua_executable')] + " Specify we are sending input through stdin + let l:cmd += ['--stdin-filepath', expand('%:p'), '-'] + + 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 stylua 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 cb9da01..a615b9c 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -246,6 +246,11 @@ call s:plugin.Flag('nixpkgs_fmt_executable', 'nixpkgs-fmt') "" " The path to the luaformatterfiveone executable. call s:plugin.Flag('luaformatterfiveone_executable', 'luaformatterfiveone') +" +"" +" The path to the stylua executable. +call s:plugin.Flag('stylua_executable', 'stylua') + "" " The path to the cljstyle executable. diff --git a/plugin/register.vim b/plugin/register.vim index ec2ac49..c686340 100644 --- a/plugin/register.vim +++ b/plugin/register.vim @@ -41,7 +41,7 @@ " * json, jsonnet: jsonnetfmt " * julia: JuliaFormatter " * kotlin: ktfmt -" * lua: luaformatterfiveone +" * lua: luaformatterfiveone, stylua " * nix: nixpkgs-fmt " * ocaml: ocamlformat " * python: autopep8, black, yapf @@ -78,6 +78,7 @@ call s:registry.AddExtension(codefmt#prettier#GetFormatter()) call s:registry.AddExtension(codefmt#juliaformatter#GetFormatter()) call s:registry.AddExtension(codefmt#ktfmt#GetFormatter()) call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter()) +call s:registry.AddExtension(codefmt#stylua#GetFormatter()) call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter()) call s:registry.AddExtension(codefmt#autopep8#GetFormatter()) call s:registry.AddExtension(codefmt#isort#GetFormatter()) diff --git a/vroom/stylua.vroom b/vroom/stylua.vroom new file mode 100644 index 0000000..0358c96 --- /dev/null +++ b/vroom/stylua.vroom @@ -0,0 +1,55 @@ +stylua is a formatter that knows how to format all lua 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) + + +stylua expects the executable to be installed on your system. + + % function hello() + % print("world") + % end + :FormatCode stylua + ! stylua -i 2> .* + $ function hello() + $ print("world") + $ end + +The name or path of the stylua executable can be configured via the +stylua_executable flag if the default of "buildifier" doesn't work. + + :Glaive codefmt stylua_executable='mystylua' + :FormatCode stylua + ! mystylelua -i 2> .* + $ function hello() + $ print("world") + $ end + :Glaive codefmt stylua_executable='stylua' + +Errors are reported using the quickfix list. + + @clear + % 13() + :FormatCode stylua + ! stylua -i 2> (.*) + $ 1 (status) + $ echo >\1 ' (command) + |stylua:Unable to format stdin:\n + |[string "isCodeValid"]:1: unexpected symbol near '"'13'" + ~ (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'''] +