From dc0e9ddead2bc358dfd426f045f5a12e40cd8fd2 Mon Sep 17 00:00:00 2001 From: actionless Date: Mon, 31 Aug 2015 10:29:10 +0200 Subject: [PATCH 1/4] feat: add luaformatter formatter --- autoload/codefmt.vim | 47 +++++++++++++++++++ doc/codefmt.txt | 4 ++ instant/flags.vim | 4 ++ plugin/register.vim | 1 + vroom/luaformatter.vroom | 97 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 vroom/luaformatter.vroom diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index ad7bfc7..4672adc 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -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'), 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 diff --git a/doc/codefmt.txt b/doc/codefmt.txt index 81d37c5..352e787 100644 --- a/doc/codefmt.txt +++ b/doc/codefmt.txt @@ -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 ` diff --git a/instant/flags.vim b/instant/flags.vim index 69ec789..0e0eafa 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -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') diff --git a/plugin/register.vim b/plugin/register.vim index c16ad03..00b980d 100644 --- a/plugin/register.vim +++ b/plugin/register.vim @@ -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()) diff --git a/vroom/luaformatter.vroom b/vroom/luaformatter.vroom new file mode 100644 index 0000000..d0d888e --- /dev/null +++ b/vroom/luaformatter.vroom @@ -0,0 +1,97 @@ +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(...) + | call add(g:repeat_calls, a:000) + :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 + |a=1234 + 5678 + i + | b = 1234*5678+i + | c=1234/2+i + | 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 + |a=1234 + 5678 + i + | b = 1234*5678+i + | c=1234/2+i + | 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. From c82c38a7e6a2ae7312604203fc83fa37cb23a6f5 Mon Sep 17 00:00:00 2001 From: actionless Date: Mon, 31 Aug 2015 10:56:30 +0200 Subject: [PATCH 2/4] feat(formatters: js,lua): honor 'shiftwidth' --- autoload/codefmt.vim | 4 ++-- vroom/luaformatter.vroom | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index 4672adc..f677eb2 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -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 @@ -424,7 +424,7 @@ function! codefmt#GetLuaFormatter() abort 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'), l:tempfile] + 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) diff --git a/vroom/luaformatter.vroom b/vroom/luaformatter.vroom index d0d888e..2e1a5f5 100644 --- a/vroom/luaformatter.vroom +++ b/vroom/luaformatter.vroom @@ -85,7 +85,7 @@ It can format specific line ranges of code using :FormatLines. ! luaformatter .*2>.* $ a=1234 + 5678 + i $ b = 1234*5678+i - for i = 0,max do + for i = 0,max do a=1234 + 5678 + i b = 1234*5678+i c=1234/2+i @@ -95,3 +95,29 @@ It can format specific line ranges of code using :FormatLines. 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: + + @clear + % for i = 0,max do + |a=1234 + 5678 + i + | b = 1234*5678+i + | c=1234/2+i + | 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 From 10426ab315857d6d6027c8d5afafdca2a5e726e8 Mon Sep 17 00:00:00 2001 From: actionless Date: Wed, 2 Sep 2015 09:31:10 +0200 Subject: [PATCH 3/4] fix(vroom: luaformatter): fix range and shiftwidth tests --- vroom/luaformatter.vroom | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vroom/luaformatter.vroom b/vroom/luaformatter.vroom index 2e1a5f5..ff47bf6 100644 --- a/vroom/luaformatter.vroom +++ b/vroom/luaformatter.vroom @@ -81,7 +81,7 @@ It can format specific line ranges of code using :FormatLines. | c=1234/2+i | end - :2,2FormatLines luaformatter + :2,3FormatLines luaformatter ! luaformatter .*2>.* $ a=1234 + 5678 + i $ b = 1234*5678+i @@ -97,7 +97,7 @@ so there are certain limitations like not being able to format misaligned braces. -Shitftwidth will be used for indentation: +Shiftwidth will be used for indentation: @clear % for i = 0,max do @@ -107,7 +107,7 @@ Shitftwidth will be used for indentation: | end - :sw=8 + :set shiftwidth=8 :FormatCode luaformatter ! luaformatter .*2>.* $ for i = 0,max do From e2983b326ac1f55c8d64b10636834586d4b487af Mon Sep 17 00:00:00 2001 From: actionless Date: Wed, 2 Sep 2015 09:31:51 +0200 Subject: [PATCH 4/4] test(formatters: jsbeautify): add shiftwidth --- vroom/jsbeautify.vroom | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/vroom/jsbeautify.vroom b/vroom/jsbeautify.vroom index e7022ed..4f6ac00 100644 --- a/vroom/jsbeautify.vroom +++ b/vroom/jsbeautify.vroom @@ -106,3 +106,25 @@ It can format specific line ranges of code using :FormatLines. NOTE: the js-beautify formatter does not natively support range formatting, so there are certain limitations like not being able to format misaligned braces. + + +Shiftwidth will be used for indentation: + + @clear + % for (i = 0; i < max; i++) { + |a=1234 + 5678 + i; b = 1234*5678+i; c=1234/2+i;} + + :set shiftwidth=8 + :FormatCode js-beautify + ! js-beautify .*2>.* + $ for (i = 0; i < max; i++) { + $ a = 1234 + 5678 + i; + $ b = 1234 * 5678 + i; + $ c = 1234 / 2 + i; + $ } + for (i = 0; i < max; i++) { + a = 1234 + 5678 + i; + b = 1234 * 5678 + i; + c = 1234 / 2 + i; + } + @end