1515
1616let s: plugin = maktaba#plugin#Get (' codefmt' )
1717
18+ let s: cmdAvailable = {}
19+
1820" "
1921" @private
2022" Formatter: ktfmt
2123function ! 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\n Glaive codefmt ktfmt_executable=" .
29+ \ ' java, -jar, /path/to/ktfmt-<VERSION>-jar-with-dependencies.jar ' .
30+ \ " \n in 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
0 commit comments