|
| 1 | +" Copyright 2020 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 | +"" |
| 17 | +" @public |
| 18 | +" Format lines in the current buffer via a formatter invoked by {cmd}, which |
| 19 | +" is a system call represented by either a |maktaba.Syscall| or any argument |
| 20 | +" accepted by |maktaba#syscall#Create()|. The command must include any |
| 21 | +" arguments for the explicit range line numbers to use, if any. |
| 22 | +" |
| 23 | +" @throws ShellError if the {cmd} system call fails |
| 24 | +function! codefmt#formatterhelpers#Format(cmd) abort |
| 25 | + let l:lines = getline(1, line('$')) |
| 26 | + let l:input = join(l:lines, "\n") |
| 27 | + |
| 28 | + let l:result = maktaba#syscall#Create(a:cmd).WithStdin(l:input).Call() |
| 29 | + let l:formatted = split(l:result.stdout, "\n") |
| 30 | + |
| 31 | + call maktaba#buffer#Overwrite(1, line('$'), l:formatted) |
| 32 | +endfunction |
| 33 | + |
| 34 | +"" |
| 35 | +" @public |
| 36 | +" Attempt to format a range of lines from {startline} to {endline} in the |
| 37 | +" current buffer via a formatter that doesn't natively support range |
| 38 | +" formatting, which is invoked via {cmd} (a system call represented by either |
| 39 | +" a |maktaba.Syscall| or any argument accepted by |maktaba#syscall#Create()|). |
| 40 | +" It uses a hacky strategy of sending those lines to the formatter in |
| 41 | +" isolation, which gives bad results if the code on those lines isn't |
| 42 | +" a self-contained block of syntax or is part of a larger indent. |
| 43 | +" |
| 44 | +" If invoking this hack, please make sure to file a feature request against |
| 45 | +" the tool for range formatting and post a URL for that feature request above |
| 46 | +" code that calls it. |
| 47 | +" |
| 48 | +" @throws ShellError if the {cmd} system call fails |
| 49 | +function! codefmt#formatterhelpers#AttemptFakeRangeFormatting( |
| 50 | + \ startline, endline, cmd) abort |
| 51 | + call maktaba#ensure#IsNumber(a:startline) |
| 52 | + call maktaba#ensure#IsNumber(a:endline) |
| 53 | + |
| 54 | + let l:lines = getline(1, line('$')) |
| 55 | + let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n") |
| 56 | + |
| 57 | + let l:result = maktaba#syscall#Create(a:cmd).WithStdin(l:input).Call() |
| 58 | + let l:formatted = split(l:result.stdout, "\n") |
| 59 | + " Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right. |
| 60 | + let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : [] |
| 61 | + let l:full_formatted = l:before + l:formatted + l:lines[a:endline :] |
| 62 | + |
| 63 | + call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted) |
| 64 | +endfunction |
0 commit comments