Skip to content

Commit 82af84b

Browse files
committed
Fix broken formatting for buildifier/rustfmt, check Syscall args
Clarifies that the formatterhelper functions expect a maktaba.Syscall, enforces that, and fixes two formatters buildifier and rustfmt that didn't.
1 parent d579038 commit 82af84b

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

autoload/codefmt/buildifier.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function! codefmt#buildifier#GetFormatter() abort
4949
try
5050
" NOTE: Ignores any line ranges given and formats entire buffer.
5151
" buildifier does not support range formatting.
52-
call codefmt#formatterhelpers#Format(l:cmd)
52+
call codefmt#formatterhelpers#Format(maktaba#syscall#Create(l:cmd))
5353
catch
5454
" Parse all the errors and stick them in the quickfix list.
5555
let l:errors = []

autoload/codefmt/formatterhelpers.vim

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,27 @@
1313
" limitations under the License.
1414

1515

16+
function! s:EnsureIsSyscall(Value) abort
17+
if type(a:Value) == type({}) &&
18+
\ has_key(a:Value, 'Call') &&
19+
\ maktaba#function#HasSameName(
20+
\ a:Value.Call, function('maktaba#syscall#Call'))
21+
return a:Value
22+
endif
23+
throw maktaba#error#BadValue(
24+
\ 'Not a valid matkaba.Syscall: %s', string(a:Value))
25+
endfunction
26+
27+
1628
""
1729
" @public
18-
" Format lines in the current buffer via a formatter invoked by {cmd}. The
19-
" command includes the explicit range line numbers to use, if any.
30+
" Format lines in the current buffer via a formatter invoked by {cmd} (a
31+
" |maktaba.Syscall|). The command includes the explicit range line numbers to
32+
" use, if any.
2033
"
2134
" @throws ShellError if the {cmd} system call fails
2235
function! codefmt#formatterhelpers#Format(cmd) abort
36+
call s:EnsureIsSyscall(a:cmd)
2337
let l:lines = getline(1, line('$'))
2438
let l:input = join(l:lines, "\n")
2539

@@ -33,8 +47,8 @@ endfunction
3347
" @public
3448
" Attempt to format a range of lines from {startline} to {endline} in the
3549
" current buffer via a formatter that doesn't natively support range
36-
" formatting (invoked by {cmd}), using a hacky strategy of sending those lines
37-
" to the formatter in isolation.
50+
" formatting (invoked by {cmd}, a |maktaba.Syscall|), using a hacky strategy
51+
" of sending those lines to the formatter in isolation.
3852
"
3953
" If invoking this hack, please make sure to file a feature request against
4054
" the tool for range formatting and post a URL for that feature request above
@@ -45,6 +59,7 @@ function! codefmt#formatterhelpers#AttemptFakeRangeFormatting(
4559
\ startline, endline, cmd) abort
4660
call maktaba#ensure#IsNumber(a:startline)
4761
call maktaba#ensure#IsNumber(a:endline)
62+
call s:EnsureIsSyscall(a:cmd)
4863

4964
let l:lines = getline(1, line('$'))
5065
let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n")

autoload/codefmt/rustfmt.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function! codefmt#rustfmt#GetFormatter() abort
5555
" NOTE: Ignores any line ranges given and formats entire buffer.
5656
" Even though rustfmt supports formatting ranges through the --file-lines
5757
" flag, it is not still enabled in the stable binaries.
58-
call codefmt#formatterhelpers#Format(l:cmd)
58+
call codefmt#formatterhelpers#Format(maktaba#syscall#Create(l:cmd))
5959
catch /ERROR(ShellError):/
6060
" Parse all the errors and stick them in the quickfix list.
6161
let l:errors = []

doc/codefmt.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,18 @@ codefmt#FormatMap({type}) *codefmt#FormatMap()*
226226
ignored since formatting only works on complete lines.
227227

228228
codefmt#formatterhelpers#Format({cmd}) *codefmt#formatterhelpers#Format()*
229-
Format lines in the current buffer via a formatter invoked by {cmd}. The
230-
command includes the explicit range line numbers to use, if any.
229+
Format lines in the current buffer via a formatter invoked by {cmd} (a
230+
|maktaba.Syscall|). The command includes the explicit range line numbers to
231+
use, if any.
231232

232233
Throws ERROR(ShellError) if the {cmd} system call fails
233234

234235
codefmt#formatterhelpers#AttemptFakeRangeFormatting({startline}, {endline},
235236
{cmd}) *codefmt#formatterhelpers#AttemptFakeRangeFormatting()*
236237
Attempt to format a range of lines from {startline} to {endline} in the
237238
current buffer via a formatter that doesn't natively support range
238-
formatting (invoked by {cmd}), using a hacky strategy of sending those lines
239-
to the formatter in isolation.
239+
formatting (invoked by {cmd}, a |maktaba.Syscall|), using a hacky strategy
240+
of sending those lines to the formatter in isolation.
240241

241242
If invoking this hack, please make sure to file a feature request against
242243
the tool for range formatting and post a URL for that feature request above

0 commit comments

Comments
 (0)