|
| 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 | + |
0 commit comments