|
| 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 | +let s:cmdAvailable = {} |
| 19 | + |
| 20 | +"" |
| 21 | +" @private |
| 22 | +" Formatter: ktfmt |
| 23 | +function! codefmt#ktfmt#GetFormatter() abort |
| 24 | + let l:formatter = { |
| 25 | + \ 'name': 'ktfmt', |
| 26 | + \ 'setup_instructions': 'Install ktfmt ' . |
| 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'" } |
| 31 | + |
| 32 | + function l:formatter.IsAvailable() abort |
| 33 | + let l:cmd = codefmt#formatterhelpers#ResolveFlagToArray('ktfmt_executable') |
| 34 | + if empty(l:cmd) |
| 35 | + return 0 |
| 36 | + endif |
| 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#Warn( |
| 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 |
| 60 | + return s:cmdAvailable[l:joined] |
| 61 | + else |
| 62 | + if l:cmd[0] =~# ',' |
| 63 | + call maktaba#error#Warn( |
| 64 | + \ 'ktfmt_executable is a string "%s" but looks like a list. ' |
| 65 | + \ . 'Try not quoting the comma-separated value', |
| 66 | + \ l:cmd[0]) |
| 67 | + endif |
| 68 | + " don't cache unavailability, in case user installs the command |
| 69 | + return 0 |
| 70 | + endif |
| 71 | + endfunction |
| 72 | + |
| 73 | + function l:formatter.AppliesToBuffer() abort |
| 74 | + return &filetype is# 'kotlin' |
| 75 | + endfunction |
| 76 | + |
| 77 | + "" |
| 78 | + " Reformat the current buffer using ktfmt, only targeting {ranges}. |
| 79 | + function l:formatter.FormatRange(startline, endline) abort |
| 80 | + " ktfmt requires '-' as a filename arg to read stdin |
| 81 | + let l:cmd = codefmt#formatterhelpers#ResolveFlagToArray('ktfmt_executable') |
| 82 | + \ + ['-'] |
| 83 | + try |
| 84 | + " TODO(tstone) Switch to using --lines once that arg is added, see |
| 85 | + " https://github.com/facebookincubator/ktfmt/issues/218 |
| 86 | + call codefmt#formatterhelpers#AttemptFakeRangeFormatting( |
| 87 | + \ a:startline, a:endline, l:cmd) |
| 88 | + catch /ERROR(ShellError):/ |
| 89 | + " Parse all the errors and stick them in the quickfix list. |
| 90 | + let l:errors = [] |
| 91 | + for l:line in split(v:exception, "\n") |
| 92 | + let l:tokens = matchlist(l:line, '\C\v^<stdin>:(\d+):(\d+):\s*(.*)') |
| 93 | + if !empty(l:tokens) |
| 94 | + call add(l:errors, { |
| 95 | + \ 'filename': @%, |
| 96 | + \ 'lnum': l:tokens[1] + a:startline - 1, |
| 97 | + \ 'col': l:tokens[2], |
| 98 | + \ 'text': l:tokens[3]}) |
| 99 | + endif |
| 100 | + endfor |
| 101 | + if empty(l:errors) |
| 102 | + " Couldn't parse ktfmt error format; display it all. |
| 103 | + call maktaba#error#Shout('Error formatting range: %s', v:exception) |
| 104 | + else |
| 105 | + call setqflist(l:errors, 'r') |
| 106 | + cc 1 |
| 107 | + endif |
| 108 | + endtry |
| 109 | + endfunction |
| 110 | + |
| 111 | + return l:formatter |
| 112 | +endfunction |
| 113 | + |
| 114 | + |
0 commit comments