Skip to content

Commit 0817773

Browse files
committed
Fancier ktfmt IsAvailable check and minor fixes
1 parent 148e774 commit 0817773

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed

autoload/codefmt/ktfmt.vim

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,57 @@
1515

1616
let s:plugin = maktaba#plugin#Get('codefmt')
1717

18+
let s:cmdAvailable = {}
19+
1820
""
1921
" @private
2022
" Formatter: ktfmt
2123
function! codefmt#ktfmt#GetFormatter() abort
2224
let l:formatter = {
2325
\ 'name': 'ktfmt',
2426
\ 'setup_instructions': 'Install ktfmt ' .
25-
\ "(https://github.com/facebookincubator/ktfmt).\n" .
26-
\ 'Enable with "Glaive codefmt ktfmt_executable=' .
27-
\ '"java -jar /path/to/ktfmt-<VERSION>-jar-with-dependencies.jar" ' .
28-
\ 'in your .vimrc' }
27+
\ '(https://github.com/facebookincubator/ktfmt). ' .
28+
\ "Enable with\nGlaive codefmt ktfmt_executable=" .
29+
\ 'java,-jar,/path/to/ktfmt-<VERSION>-jar-with-dependencies.jar ' .
30+
\ "\nin your .vimrc or create a shell script named 'ktfmt'" }
2931

3032
function l:formatter.IsAvailable() abort
31-
let l:exec = split(s:plugin.Flag('ktfmt_executable'), '\\\@<! ')
32-
if empty(l:exec)
33+
let l:cmd = codefmt#formatterhelpers#ResolveFlagToArray('ktfmt_executable')
34+
if empty(l:cmd)
3335
return 0
3436
endif
35-
if executable(l:exec[0])
36-
return 1
37-
elseif !empty(l:exec[0]) && l:exec[0] isnot# 'ktfmt'
38-
" The user has specified a custom formatter command. Hope it works.
39-
return 1
37+
let l:joined = join(l:cmd, ' ')
38+
if has_key(s:cmdAvailable, l:joined)
39+
return s:cmdAvailable[l:joined]
40+
endif
41+
if executable(l:cmd[0])
42+
if l:cmd[0] is# 'java' || l:cmd[0] =~# '/java$'
43+
" Even if java is executable, jar path might be wrong, so run a simple
44+
" command. There's no --version flag, so format an empty file.
45+
let l:success = 0
46+
try
47+
let l:result = maktaba#syscall#Create(l:cmd + ['-']).Call()
48+
let l:success = v:shell_error != 0
49+
catch /ERROR(ShellError)/
50+
call maktaba#error#Shout(
51+
\ 'ktfmt unavailable, check jar file in %s -: %s',
52+
\ l:joined,
53+
\ v:exception)
54+
endtry
55+
let s:cmdAvailable[l:joined] = l:success
56+
else
57+
" command is executable and doesn't look like 'java' so assume yes
58+
let s:cmdAvailable[l:joined] = 1
59+
endif
4060
else
61+
if l:cmd[0] =~# ','
62+
call maktaba#error#Warn(
63+
\ 'ktfmt_executable is a string "%s" but looks like a list. '
64+
\ . 'Try not quoting the comma-separated value',
65+
\ l:cmd[0])
66+
endif
67+
return s:cmdAvailable[l:joined]
68+
" don't cache unavailability, in case user installs the command
4169
return 0
4270
endif
4371
endfunction
@@ -49,10 +77,9 @@ function! codefmt#ktfmt#GetFormatter() abort
4977
""
5078
" Reformat the current buffer using ktfmt, only targeting {ranges}.
5179
function l:formatter.FormatRange(startline, endline) abort
52-
" Split the command on spaces, except when there's a proceeding \
53-
let l:cmd = split(s:plugin.Flag('ktfmt_executable'), '\\\@<! ')
5480
" ktfmt requires '-' as a filename arg to read stdin
55-
let l:cmd = add(l:cmd, '-')
81+
let l:cmd = codefmt#formatterhelpers#ResolveFlagToArray('ktfmt_executable')
82+
\ + ['-']
5683
try
5784
" TODO(tstone) Switch to using --lines once that arg is added, see
5885
" https://github.com/facebookincubator/ktfmt/issues/218

doc/codefmt.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The current list of defaults by filetype is:
4242
* java: google-java-format
4343
* javascript, json, html, css: js-beautify
4444
* javascript, html, css, markdown: prettier
45+
* kotlin: ktfmt
4546
* lua: luaformatterfiveone
4647
* nix: nixpkgs-fmt
4748
* python: autopep8, black, yapf
@@ -106,9 +107,16 @@ The path to the google-java executable. Generally, this should have the form:
106107
Default: 'google-java-format' `
107108

108109
*codefmt:ktfmt_executable*
109-
The path to the ktfmt executable. Generally, this should have the form: `java
110-
-jar /path/to/ktfmt-VERSION-jar-with-dependencies.jar`
111-
Default: 'ktfmt' `
110+
The path to the ktfmt executable with args, as a list. The default value
111+
assumes there is a wrapper script named `ktfmt`. Without such a script, this
112+
will generally have the form:
113+
`ktfmt_executable=java,-jar,/path/to/ktfmt-VERSION-jar-with-dependencies.jar`
114+
115+
Note that range formatting is not fully supported, with a feature request at
116+
https://github.com/facebookincubator/ktfmt/issues/218. ktfmt will align a
117+
formatted range to column 1, requiring a manual reindent to match the
118+
surrounding blocks.
119+
Default: ['ktfmt'] `
112120

113121
*codefmt:shfmt_options*
114122
Command line arguments to feed shfmt. Either a list or callable that takes no

instant/flags.vim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,16 @@ call s:plugin.Flag('buildifier_executable', 'buildifier')
103103
call s:plugin.Flag('google_java_executable', 'google-java-format')
104104

105105
""
106-
" The path to the ktfmt executable. Generally, this should have the form:
107-
" `java -jar /path/to/ktfmt-VERSION-jar-with-dependencies.jar`
108-
call s:plugin.Flag('ktfmt_executable', 'ktfmt')
106+
" The path to the ktfmt executable with args, as a list. The default value
107+
" assumes there is a wrapper script named `ktfmt`. Without such a script, this
108+
" will generally have the form:
109+
" `ktfmt_executable=java,-jar,/path/to/ktfmt-VERSION-jar-with-dependencies.jar`
110+
"
111+
" Note that range formatting is not fully supported, with a feature request at
112+
" https://github.com/facebookincubator/ktfmt/issues/218. ktfmt will align a
113+
" formatted range to column 1, requiring a manual reindent to match the
114+
" surrounding blocks.
115+
call s:plugin.Flag('ktfmt_executable', ['ktfmt'])
109116

110117
""
111118
" Command line arguments to feed shfmt. Either a list or callable that

plugin/register.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
" * java: google-java-format
3737
" * javascript, json, html, css: js-beautify
3838
" * javascript, html, css, markdown: prettier
39+
" * kotlin: ktfmt
3940
" * lua: luaformatterfiveone
4041
" * nix: nixpkgs-fmt
4142
" * python: autopep8, black, yapf

vroom/ktfmt.vroom

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ examples.
1515
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
1616

1717

18-
The ktfmt formatter expects a ktfmt executable to be installed on your system.
18+
The ktfmt formatter expects a ktfmt executable to be installed on your system,
19+
e.g. as a shell wrapper around "java -jar /path/to/ktfmt.jar"..
1920

2021
% class Foo { public bar() : String { return "bar"; } }
2122
:FormatCode ktfmt
@@ -26,10 +27,10 @@ The ktfmt formatter expects a ktfmt executable to be installed on your system.
2627
$ }
2728
$ }
2829

29-
The name or path of the ktmft executable can be configured via the
30+
The name or path of the ktfmt executable can be configured via the
3031
ktfmt_executable flag if the default of "ktmft" doesn't work.
3132

32-
:Glaive codefmt ktfmt_executable='java -jar /path/to/ktfmt.jar'
33+
:Glaive codefmt ktfmt_executable=java,-jar,/path/to/ktfmt.jar
3334
:FormatCode ktfmt
3435
! java -jar /path/to/ktfmt.jar .*
3536
$ class Foo {

0 commit comments

Comments
 (0)