-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(...)<CR> | ||
| call add(g:repeat_calls, a:000)<CR> | ||
:endfunction | ||
:call maktaba#test#Override('repeat#set', 'FakeRepeat') | ||
|
||
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) | ||
|
||
|
||
stylua expects the executable to be installed on your system. | ||
|
||
% function hello()<CR> | ||
% print("world")<CR> | ||
% 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'''] | ||
|