|
| 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: 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)'} |
| 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), only targeting the range between {startline} and |
| 39 | + " {endline}. |
| 40 | + function l:formatter.FormatRange(startline, endline) abort |
| 41 | + let l:cmd = [ s:plugin.Flag('ocamlformat_executable'), '-' ] |
| 42 | + let l:fname = expand('%:p') |
| 43 | + if !empty(l:fname) |
| 44 | + let l:cmd += ['--name', l:fname] |
| 45 | + let l:fname_pattern = '"' . escape(l:fname, '\') . '"' |
| 46 | + else |
| 47 | + " assume it's an OCaml implementation file (.ml) if no file name is |
| 48 | + " provided |
| 49 | + let l:cmd += ['--impl'] |
| 50 | + let l:fname_pattern = '\<standard input\>' |
| 51 | + end |
| 52 | + try |
| 53 | + " NOTE: ocamlformat does not support range formatting. |
| 54 | + " See https://github.com/ocaml-ppx/ocamlformat/pull/1188 |
| 55 | + call codefmt#formatterhelpers#AttemptFakeRangeFormatting( |
| 56 | + \ a:startline, a:endline, l:cmd) |
| 57 | + catch /ERROR(ShellError):/ |
| 58 | + " Parse all the errors and stick them in the quickfix list. |
| 59 | + let l:errors = [] |
| 60 | + let l:matchidx = 1 |
| 61 | + while 1 |
| 62 | + let l:tokens = matchlist(v:exception, |
| 63 | + \ '\vFile ' . l:fname_pattern . ', line (\d+), characters (\d+)-\d+:\n(.*)\n', 0, l:matchidx) |
| 64 | + if empty(l:tokens) |
| 65 | + break |
| 66 | + endif |
| 67 | + call add(l:errors, { |
| 68 | + \ 'filename': @%, |
| 69 | + \ 'lnum': l:tokens[1] + a:startline - 1, |
| 70 | + \ 'col': l:tokens[2], |
| 71 | + \ 'text': l:tokens[3]}) |
| 72 | + let l:matchidx = l:matchidx + 1 |
| 73 | + endwhile |
| 74 | + |
| 75 | + if empty(l:errors) |
| 76 | + " Couldn't parse ocamlformat error format; display it all. |
| 77 | + call maktaba#error#Shout('Error formatting file: %s', v:exception) |
| 78 | + else |
| 79 | + call setqflist(l:errors, 'r') |
| 80 | + cc 1 |
| 81 | + endif |
| 82 | + endtry |
| 83 | + endfunction |
| 84 | + |
| 85 | + return l:formatter |
| 86 | +endfunction |
0 commit comments