|
| 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 | +let s:plugin = maktaba#plugin#Get('codefmt') |
| 17 | + |
| 18 | + |
| 19 | +"" |
| 20 | +" @private |
| 21 | +" Formatter: black |
| 22 | +function! codefmt#black#GetFormatter() abort |
| 23 | + let l:formatter = { |
| 24 | + \ 'name': 'black', |
| 25 | + \ 'setup_instructions': 'Install black ' . |
| 26 | + \ '(https://pypi.python.org/pypi/black/).'} |
| 27 | + |
| 28 | + function l:formatter.IsAvailable() abort |
| 29 | + return executable(s:plugin.Flag('black_executable')) |
| 30 | + endfunction |
| 31 | + |
| 32 | + function l:formatter.AppliesToBuffer() abort |
| 33 | + return &filetype is# 'python' |
| 34 | + endfunction |
| 35 | + |
| 36 | + "" |
| 37 | + " Reformat the current buffer with black or the binary named in |
| 38 | + " @flag(black_executable) |
| 39 | + " |
| 40 | + " We implement Format(), and not FormatRange{,s}(), because black doesn't |
| 41 | + " provide a hook for formatting a range |
| 42 | + function l:formatter.Format() abort |
| 43 | + let l:executable = s:plugin.Flag('black_executable') |
| 44 | + |
| 45 | + call codefmt#formatterhelpers#Format([ |
| 46 | + \ l:executable, |
| 47 | + \ '-']) |
| 48 | + endfunction |
| 49 | + |
| 50 | + return l:formatter |
| 51 | +endfunction |
0 commit comments