Skip to content

Commit f7aea5c

Browse files
committed
Add ktfmt support. Fixes google#176.
Unfortunately, ktfmt doesn't yet support a --lines option to format a range. See facebook/ktfmt#218 for the feature request.
1 parent 793d816 commit f7aea5c

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via
2323
* Java (google-java-format or clang-format)
2424
* JavaScript (clang-format or [prettier](https://prettier.io))
2525
* JSON (js-beautify)
26+
* Kotlin ([ktfmt](https://github.com/facebookincubator/ktfmt))
2627
* Proto (clang-format)
2728
* Python (Autopep8, Black, or YAPF)
2829
* Rust ([rustfmt](https://github.com/rust-lang/rustfmt))

autoload/codefmt/ktfmt.vim

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
" Copyright 2017 Google Inc. All rights reserved.
2+
"
3+
" Licensed under the Apache License, Version 2.0 (the "License");
4+
" you may not use this file except in compliance with the License.
5+
" You may obtain a copy of the License at
6+
"
7+
" http://www.apache.org/licenses/LICENSE-2.0
8+
"
9+
" Unless required by applicable law or agreed to in writing, software
10+
" distributed under the License is distributed on an "AS IS" BASIS,
11+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
" See the License for the specific language governing permissions and
13+
" limitations under the License.
14+
15+
16+
let s:plugin = maktaba#plugin#Get('codefmt')
17+
18+
""
19+
" @private
20+
" Formatter: ktfmt
21+
function! codefmt#ktfmt#GetFormatter() abort
22+
let l:formatter = {
23+
\ 'name': 'ktfmt',
24+
\ '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' }
29+
30+
function l:formatter.IsAvailable() abort
31+
let l:exec = s:plugin.Flag('ktfmt_executable')
32+
if executable(l:exec)
33+
return 1
34+
elseif !empty(l:exec) && l:exec isnot# 'ktfmt'
35+
" The user has specified a custom formatter command. Hope it works.
36+
return 1
37+
else
38+
return 0
39+
endif
40+
endfunction
41+
42+
function l:formatter.AppliesToBuffer() abort
43+
return &filetype is# 'kotlin'
44+
endfunction
45+
46+
""
47+
" Reformat the current buffer using ktfmt, only targeting {ranges}.
48+
function l:formatter.FormatRange(startline, endline) abort
49+
" Split the command on spaces, except when there's a proceeding \
50+
let l:cmd = split(s:plugin.Flag('ktfmt_executable'), '\\\@<! ')
51+
try
52+
" TODO(tstone) Switch to using --lines once that arg is added, see
53+
" https://github.com/facebookincubator/ktfmt/issues/218
54+
call codefmt#formatterhelpers#AttemptFakeRangeFormatting(
55+
\ a:startline, a:endline, l:cmd)
56+
catch /ERROR(ShellError):/
57+
" Parse all the errors and stick them in the quickfix list.
58+
let l:errors = []
59+
for l:line in split(v:exception, "\n")
60+
let l:tokens = matchlist(l:line, '\C\v^<stdin>:(\d+):(\d+):\s*(.*)')
61+
if !empty(l:tokens)
62+
call add(l:errors, {
63+
\ 'filename': @%,
64+
\ 'lnum': l:tokens[1] + a:startline - 1,
65+
\ 'col': l:tokens[2],
66+
\ 'text': l:tokens[3]})
67+
endif
68+
endfor
69+
if empty(l:errors)
70+
" Couldn't parse ktfmt error format; display it all.
71+
call maktaba#error#Shout('Error formatting range: %s', v:exception)
72+
else
73+
call setqflist(l:errors, 'r')
74+
cc 1
75+
endif
76+
endtry
77+
endfunction
78+
79+
return l:formatter
80+
endfunction
81+
82+

instant/flags.vim

+5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ call s:plugin.Flag('buildifier_executable', 'buildifier')
102102
" `java -jar /path/to/google-java`
103103
call s:plugin.Flag('google_java_executable', 'google-java-format')
104104

105+
""
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')
109+
105110
""
106111
" Command line arguments to feed shfmt. Either a list or callable that
107112
" takes no args and returns a list with command line arguments. By default, uses

plugin/register.vim

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ call s:registry.AddExtension(codefmt#gofmt#GetFormatter())
6565
call s:registry.AddExtension(codefmt#googlejava#GetFormatter())
6666
call s:registry.AddExtension(codefmt#jsbeautify#GetFormatter())
6767
call s:registry.AddExtension(codefmt#prettier#GetFormatter())
68+
call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())
6869
call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter())
6970
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())
7071
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())

vroom/ktfmt.vroom

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
The built-in ktfmt formatter knows how to format Java BUILD files. If you
2+
aren't familiar with basic codefmt usage yet, see main.vroom first.
3+
4+
We'll set up codefmt and configure the vroom environment, then jump into some
5+
examples.
6+
7+
:source $VROOMDIR/setupvroom.vim
8+
9+
:let g:repeat_calls = []
10+
:function FakeRepeat(...)<CR>
11+
| call add(g:repeat_calls, a:000)<CR>
12+
:endfunction
13+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
14+
15+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
16+
17+
18+
The ktfmt formatter expects a ktfmt executable to be installed on your system.
19+
20+
% class Foo { public bar() : String { return "bar"; } }
21+
:FormatCode ktfmt
22+
! ktfmt .*
23+
$ class Foo {
24+
$ public bar(): String {
25+
$ return "bar"
26+
$ }
27+
$ }
28+
29+
The name or path of the ktmft executable can be configured via the
30+
ktfmt_executable flag if the default of "ktmft" doesn't work.
31+
32+
:Glaive codefmt ktfmt_executable='java -jar /path/to/ktfmt.jar'
33+
:FormatCode ktfmt
34+
! java -jar /path/to/ktfmt.jar .*
35+
$ class Foo {
36+
$ public bar(): String {
37+
$ return "bar"
38+
$ }
39+
$ }
40+
:Glaive codefmt ktfmt_executable='ktfmt'

0 commit comments

Comments
 (0)