|
| 1 | +" Copyright 2023 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 | +let s:plugin = maktaba#plugin#Get('codefmt') |
| 16 | + |
| 17 | + |
| 18 | +"" |
| 19 | +" @private |
| 20 | +" Formatter: perltidy |
| 21 | +function! codefmt#perltidy#GetFormatter() abort |
| 22 | + let l:formatter = { |
| 23 | + \ 'name': 'perltidy', |
| 24 | + \ 'setup_instructions': 'Install perltidy ' . |
| 25 | + \ '(https://perltidy.sourceforge.net/INSTALL.html).'} |
| 26 | + |
| 27 | + function l:formatter.IsAvailable() abort |
| 28 | + return executable(s:plugin.Flag('perltidy_executable')) |
| 29 | + endfunction |
| 30 | + |
| 31 | + function l:formatter.AppliesToBuffer() abort |
| 32 | + return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'perl') |
| 33 | + endfunction |
| 34 | + |
| 35 | + "" |
| 36 | + " Reformat the current buffer with perltidy or the binary named in |
| 37 | + " @flag(perltidy_executable), only targeting the range between {startline} and |
| 38 | + " {endline}. |
| 39 | + " @throws ShellError |
| 40 | + function l:formatter.FormatRange(startline, endline) abort |
| 41 | + let l:executable = s:plugin.Flag('perltidy_executable') |
| 42 | + |
| 43 | + call maktaba#ensure#IsNumber(a:startline) |
| 44 | + call maktaba#ensure#IsNumber(a:endline) |
| 45 | + |
| 46 | + " Perltidy does not support range formatting. |
| 47 | + call codefmt#formatterhelpers#AttemptFakeRangeFormatting( |
| 48 | + \ a:startline, |
| 49 | + \ a:endline, |
| 50 | + \ [l:executable, '-']) |
| 51 | + endfunction |
| 52 | + |
| 53 | + return l:formatter |
| 54 | +endfunction |
0 commit comments