Skip to content

feat: add luaformatter formatter #54

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
49 changes: 48 additions & 1 deletion autoload/codefmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function! codefmt#GetJsBeautifyFormatter() abort
" {endline}.
" @throws ShellError
function l:formatter.FormatRange(startline, endline) abort
let l:cmd = [s:plugin.Flag('js_beautify_executable'), '-f', '-']
let l:cmd = [s:plugin.Flag('js_beautify_executable'), '-s', string(&shiftwidth), '-f', '-']
if &filetype != ""
let l:cmd = l:cmd + ['--type', &filetype]
endif
Expand Down Expand Up @@ -399,6 +399,53 @@ function! codefmt#GetAutopep8Formatter() abort
endfunction


""
" @private
" Formatter: luaformatter
function! codefmt#GetLuaFormatter() abort
let l:formatter = {
\ 'name': 'luaformatter',
\ 'setup_instructions': 'Install luaformatter ' .
\ '(https://luarocks.org/modules/luarocks/formatter).'}

function l:formatter.IsAvailable() abort
return executable(s:plugin.Flag('luaformatter_executable'))
endfunction

function l:formatter.AppliesToBuffer() abort
return &filetype is# 'lua'
endfunction

""
" Reformat the current buffer with js-beautify or the binary named in
" @flag(js_beautify_executable), only targeting the range between {startline} and
" {endline}.
" @throws ShellError
function l:formatter.FormatRange(startline, endline) abort
" Use temporary file because luaformatter don't accept stdin.
let l:tempfile = tempname()
let l:cmd = [s:plugin.Flag('luaformatter_executable'), '-s', string(&shiftwidth), l:tempfile ]

call maktaba#ensure#IsNumber(a:startline)
call maktaba#ensure#IsNumber(a:endline)

let l:lines = getline(1, line('$'))
" Hack range formatting by formatting range individually, ignoring context.
call writefile(l:lines[a:startline - 1 : a:endline - 1], l:tempfile)

let l:result = maktaba#syscall#Create(l:cmd).Call()
let l:formatted = split(l:result.stdout, "\n")
" Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right.
let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : []
let l:full_formatted = l:before + l:formatted + l:lines[a:endline :]

call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted)
endfunction

return l:formatter
endfunction


""
" Checks whether {formatter} is available.
" NOTE: If IsAvailable checks are disabled via
Expand Down
4 changes: 4 additions & 0 deletions doc/codefmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Default: 'gofmt' `
The path to the js-beautify executable.
Default: 'js-beautify' `

*codefmt:luaformatter_executable*
The path to the luaformatter executable.
Default: 'luaformatter' `

*codefmt:plugin[autocmds]*
Configures whether plugin/autocmds.vim should be loaded.
Default: 1 `
Expand Down
4 changes: 4 additions & 0 deletions instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ call s:plugin.Flag('gofmt_executable', 'gofmt')
""
" The path to the js-beautify executable.
call s:plugin.Flag('js_beautify_executable', 'js-beautify')

""
" The path to the luaformatter executable.
call s:plugin.Flag('luaformatter_executable', 'luaformatter')
1 change: 1 addition & 0 deletions plugin/register.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ call s:registry.AddExtension(codefmt#GetJsBeautifyFormatter())
call s:registry.AddExtension(codefmt#GetClangFormatFormatter())
call s:registry.AddExtension(codefmt#GetGofmtFormatter())
call s:registry.AddExtension(codefmt#GetAutopep8Formatter())
call s:registry.AddExtension(codefmt#GetLuaFormatter())
123 changes: 123 additions & 0 deletions vroom/luaformatter.vroom
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
The built-in luaformatter formatter knows how to format lua code.
If you aren't familiar with basic codefmt usage yet, see main.vroom first.

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)


The luaformatter formatter expects the luaformatter executable to be installed
on your system.

% f()
:FormatCode luaformatter
! luaformatter .*
$ f()

The name or path of the luaformatter executable can be configured via the
luaformatter_executable flag if the default of "luaformatter" doesn't work.

:Glaive codefmt luaformatter_executable='myjsb'
:FormatCode luaformatter
! myjsb .*
$ f()
:Glaive codefmt luaformatter_executable='luaformatter'


You can format any buffer with luaformatter specifying the formatter explicitly.

@clear
% for i = 0,max do<CR>
|a=1234 + 5678 + i<CR>
| b = 1234*5678+i<CR>
| c=1234/2+i<CR>
| end


:FormatCode luaformatter
! luaformatter .*2>.*
$ for i = 0,max do
$ a=1234 + 5678 + i
$ b = 1234*5678+i
$ c=1234/2+i
$ end
for i = 0,max do
a=1234 + 5678 + i
b = 1234*5678+i
c=1234/2+i
end
@end

NOTE: unfortunately luaformatter don't format punctuation.

Lua filetype will use the luaformatter formatter by default:

@clear
% f();

:set filetype=lua
:FormatCode
! luaformatter .*
$ f();

:set filetype=


It can format specific line ranges of code using :FormatLines.

@clear
% for i = 0,max do<CR>
|a=1234 + 5678 + i<CR>
| b = 1234*5678+i<CR>
| c=1234/2+i<CR>
| end

:2,2FormatLines luaformatter
! luaformatter .*2>.*
$ a=1234 + 5678 + i
$ b = 1234*5678+i
for i = 0,max do
a=1234 + 5678 + i
b = 1234*5678+i
c=1234/2+i
end
@end

NOTE: the luaformatter formatter does not natively support range formatting,
so there are certain limitations like not being able to format misaligned
braces.


Shitftwidth will be used for indentation:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate typo here.


@clear
% for i = 0,max do<CR>
|a=1234 + 5678 + i<CR>
| b = 1234*5678+i<CR>
| c=1234/2+i<CR>
| end


:sw=8
:FormatCode luaformatter
! luaformatter .*2>.*
$ for i = 0,max do
$ a=1234 + 5678 + i
$ b = 1234*5678+i
$ c=1234/2+i
$ end
for i = 0,max do
a=1234 + 5678 + i
b = 1234*5678+i
c=1234/2+i
end
@end