|
| 1 | +" Copyright 2021 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: ocamlformat |
| 22 | +function! codefmt#ocamlformat#GetFormatter() abort |
| 23 | + let l:formatter = { |
| 24 | + \ 'name': 'ocamlformat', |
| 25 | + \ 'setup_instructions': 'Install ocamlformat ' . |
| 26 | + \ '(https://github.com/ocaml-ppx/ocamlformat#installation).'} |
| 27 | + |
| 28 | + function l:formatter.IsAvailable() abort |
| 29 | + return executable(s:plugin.Flag('ocamlformat_executable')) |
| 30 | + endfunction |
| 31 | + |
| 32 | + function l:formatter.AppliesToBuffer() abort |
| 33 | + return &filetype is# 'ocaml' |
| 34 | + endfunction |
| 35 | + |
| 36 | + "" |
| 37 | + " Reformat the current buffer with ocamlformat or the binary named in |
| 38 | + " @flag(ocamlformat_executable) |
| 39 | + " @throws ShellError |
| 40 | + " |
| 41 | + " We implement Format(), and not FormatRange{,s}(), because black doesn't |
| 42 | + " provide a hook for formatting a range |
| 43 | + function l:formatter.Format() abort |
| 44 | + let l:executable = s:plugin.Flag('ocamlformat_executable') |
| 45 | + |
| 46 | + " ocamlformat requires --name, --impl, or --intf when reading from |
| 47 | + " stdin. |
| 48 | + let l:inputflags = ['--name', @%] |
| 49 | + if len(@%) == 0 |
| 50 | + " Assume we're formatting an implementation file. |
| 51 | + let l:inputflags = ['--impl'] |
| 52 | + endif |
| 53 | + " TODO(dimitrije): Catch and handle formatting errors more nicely. See how |
| 54 | + " it's dome in gofmt.vim. |
| 55 | + " For now, an ugly ShellError will be thrown if anything fails here. |
| 56 | + call codefmt#formatterhelpers#Format( |
| 57 | + \ [l:executable] |
| 58 | + \ + l:inputflags |
| 59 | + \ + ['-']) |
| 60 | + endfunction |
| 61 | + |
| 62 | + return l:formatter |
| 63 | +endfunction |
0 commit comments