|
| 1 | +" Copyright 2018 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 | +"" |
| 20 | +" @private |
| 21 | +" Formatter: rustfmt |
| 22 | +function! codefmt#rustfmt#GetFormatter() abort |
| 23 | + let l:formatter = { |
| 24 | + \ 'name': 'rustfmt', |
| 25 | + \ 'setup_instructions': 'Install ' . |
| 26 | + \ 'rustfmt (https://github.com/rust-lang/rustfmt) ' . |
| 27 | + \ 'and configure the rustfmt_executable flag'} |
| 28 | + |
| 29 | + function l:formatter.IsAvailable() abort |
| 30 | + return executable(s:plugin.Flag('rustfmt_executable')) |
| 31 | + endfunction |
| 32 | + |
| 33 | + function l:formatter.AppliesToBuffer() abort |
| 34 | + return &filetype is# 'rust' |
| 35 | + endfunction |
| 36 | + |
| 37 | + "" |
| 38 | + " Reformat the current buffer with rustfmt or the binary named in |
| 39 | + " @flag(rustfmt_executable). |
| 40 | + function l:formatter.FormatRange(startline, endline) abort |
| 41 | + let l:Rustfmt_options = s:plugin.Flag('rustfmt_options') |
| 42 | + if type(l:Rustfmt_options) is# type([]) |
| 43 | + let l:rustfmt_options = l:Rustfmt_options |
| 44 | + elseif maktaba#value#IsCallable(l:Rustfmt_options) |
| 45 | + let l:rustfmt_options = maktaba#function#Call(l:Rustfmt_options) |
| 46 | + else |
| 47 | + throw maktaba#error#WrongType( |
| 48 | + \ 'rustfmt_options flag must be list or callable. Found %s', |
| 49 | + \ string(l:Rustfmt_options)) |
| 50 | + endif |
| 51 | + let l:cmd = [s:plugin.Flag('rustfmt_executable'), '--emit=stdout', '--color=never'] |
| 52 | + |
| 53 | + call extend(l:cmd, l:rustfmt_options) |
| 54 | + try |
| 55 | + let l:lines = getline(1, line('$')) |
| 56 | + let l:input = join(l:lines, "\n") |
| 57 | + let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() |
| 58 | + let l:formatted = split(l:result.stdout, "\n") |
| 59 | + " Even though rustfmt supports formatting ranges through the --file-lines |
| 60 | + " flag, it is not still enabled in the stable binaries. |
| 61 | + call maktaba#buffer#Overwrite(1, line('$'), l:formatted) |
| 62 | + catch /ERROR(ShellError):/ |
| 63 | + " Parse all the errors and stick them in the quickfix list. |
| 64 | + let l:errors = [] |
| 65 | + let l:last_error_text = '' |
| 66 | + for l:line in split(v:exception, "\n") |
| 67 | + let l:error_text_tokens = matchlist(l:line, |
| 68 | + \ '\C\v^error: (.*)') |
| 69 | + if !empty(l:error_text_tokens) |
| 70 | + let l:last_error_text = l:error_text_tokens[1] |
| 71 | + endif |
| 72 | + |
| 73 | + let l:tokens = matchlist(l:line, |
| 74 | + \ '\C\v^.*\<stdin\>:(\d+):(\d+).*') |
| 75 | + if !empty(l:tokens) |
| 76 | + call add(l:errors, { |
| 77 | + \ 'filename': @%, |
| 78 | + \ 'lnum': l:tokens[1], |
| 79 | + \ 'col': l:tokens[2], |
| 80 | + \ 'text': l:last_error_text}) |
| 81 | + endif |
| 82 | + endfor |
| 83 | + |
| 84 | + if empty(l:errors) |
| 85 | + " Couldn't parse rustfmt error format; display it all. |
| 86 | + call maktaba#error#Shout('Error formatting file: %s', v:exception) |
| 87 | + else |
| 88 | + call setqflist(l:errors, 'r') |
| 89 | + cc 1 |
| 90 | + endif |
| 91 | + endtry |
| 92 | + endfunction |
| 93 | + |
| 94 | + return l:formatter |
| 95 | +endfunction |
0 commit comments