|
| 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 | +"" |
| 20 | +" @private |
| 21 | +" Formatter: shfmt |
| 22 | +function! codefmt#shfmt#GetFormatter() abort |
| 23 | + let l:formatter = { |
| 24 | + \ 'name': 'shfmt', |
| 25 | + \ 'setup_instructions': 'Install shfmt and configure the ' . |
| 26 | + \ 'shfmt_executable flag'} |
| 27 | + |
| 28 | + function l:formatter.IsAvailable() abort |
| 29 | + return executable(s:plugin.Flag('shfmt_executable')) |
| 30 | + endfunction |
| 31 | + |
| 32 | + function l:formatter.AppliesToBuffer() abort |
| 33 | + return &filetype is# 'sh' |
| 34 | + endfunction |
| 35 | + |
| 36 | + "" |
| 37 | + " Reformat the current buffer with shfmt or the binary named in |
| 38 | + " @flag(shfmt_executable), only targeting the range between {startline} and |
| 39 | + " {endline}. |
| 40 | + function l:formatter.FormatRange(startline, endline) abort |
| 41 | + let l:Shfmt_options = s:plugin.Flag('shfmt_options') |
| 42 | + if type(l:Shfmt_options) is# type([]) |
| 43 | + let l:shfmt_options = l:Shfmt_options |
| 44 | + elseif maktaba#value#IsCallable(l:Shfmt_options) |
| 45 | + let l:shfmt_options = maktaba#function#Call(l:Shfmt_options) |
| 46 | + else |
| 47 | + throw maktaba#error#WrongType( |
| 48 | + \ 'shfmt_options flag must be list or callable. Found %s', |
| 49 | + \ string(l:Shfmt_options)) |
| 50 | + endif |
| 51 | + " Hack range formatting by formatting range individually, ignoring context. |
| 52 | + let l:cmd = [ s:plugin.Flag('shfmt_executable') ] + l:shfmt_options |
| 53 | + call maktaba#ensure#IsNumber(a:startline) |
| 54 | + call maktaba#ensure#IsNumber(a:endline) |
| 55 | + let l:lines = getline(1, line('$')) |
| 56 | + let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n") |
| 57 | + try |
| 58 | + let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() |
| 59 | + let l:formatted = split(l:result.stdout, "\n") |
| 60 | + " Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right. |
| 61 | + let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : [] |
| 62 | + |
| 63 | + let l:full_formatted = l:before + l:formatted + l:lines[a:endline :] |
| 64 | + call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted) |
| 65 | + catch /ERROR(ShellError):/ |
| 66 | + " Parse all the errors and stick them in the quickfix list. |
| 67 | + let l:errors = [] |
| 68 | + for l:line in split(v:exception, "\n") |
| 69 | + let l:tokens = matchlist(l:line, |
| 70 | + \ '\C\v^\<standard input\>:(\d+):(\d+):\s*(.*)') |
| 71 | + if !empty(l:tokens) |
| 72 | + call add(l:errors, { |
| 73 | + \ 'filename': @%, |
| 74 | + \ 'lnum': l:tokens[1] + a:startline - 1, |
| 75 | + \ 'col': l:tokens[2], |
| 76 | + \ 'text': l:tokens[3]}) |
| 77 | + endif |
| 78 | + endfor |
| 79 | + |
| 80 | + if empty(l:errors) |
| 81 | + " Couldn't parse shfmt error format; display it all. |
| 82 | + call maktaba#error#Shout('Error formatting file: %s', v:exception) |
| 83 | + else |
| 84 | + call setqflist(l:errors, 'r') |
| 85 | + cc 1 |
| 86 | + endif |
| 87 | + endtry |
| 88 | + endfunction |
| 89 | + |
| 90 | + return l:formatter |
| 91 | +endfunction |
0 commit comments