|
| 1 | +" Copyright 2022 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 | +let s:cmdAvailable = {} |
| 17 | + |
| 18 | +"" |
| 19 | +" @private |
| 20 | +" Formatter: mixformat |
| 21 | +function! codefmt#mixformat#GetFormatter() abort |
| 22 | + let l:formatter = { |
| 23 | + \ 'name': 'mixformat', |
| 24 | + \ 'setup_instructions': 'mix is usually installed with Elixir ' . |
| 25 | + \ '(https://elixir-lang.org/install.html). ' . |
| 26 | + \ "If mix is not in your path, configure it in .vimrc:\n" . |
| 27 | + \ 'Glaive codefmt mix_executable=/path/to/mix' } |
| 28 | + |
| 29 | + function l:formatter.IsAvailable() abort |
| 30 | + let l:cmd = codefmt#formatterhelpers#ResolveFlagToArray('mix_executable') |
| 31 | + if codefmt#ShouldPerformIsAvailableChecks() && !executable(l:cmd[0]) |
| 32 | + return 0 |
| 33 | + endif |
| 34 | + return 1 |
| 35 | + endfunction |
| 36 | + |
| 37 | + function l:formatter.AppliesToBuffer() abort |
| 38 | + return &filetype is# 'elixir' || &filetype is# 'eelixir' |
| 39 | + \ || &filetype is# 'heex' |
| 40 | + endfunction |
| 41 | + |
| 42 | + "" |
| 43 | + " Reformat the current buffer using mix format, only targeting {ranges}. |
| 44 | + function l:formatter.FormatRange(startline, endline) abort |
| 45 | + let l:filename = expand('%:p') |
| 46 | + if empty(l:filename) |
| 47 | + let l:dir = getcwd() |
| 48 | + " Default filename per https://hexdocs.pm/mix/Mix.Tasks.Format.html |
| 49 | + let l:filename = 'stdin.exs' |
| 50 | + else |
| 51 | + let l:dir = s:findMixDir(l:filename) |
| 52 | + endif |
| 53 | + " mix format docs: https://hexdocs.pm/mix/main/Mix.Tasks.Format.html |
| 54 | + let l:cmd = codefmt#formatterhelpers#ResolveFlagToArray('mix_executable') |
| 55 | + " Specify stdin as the file |
| 56 | + let l:cmd = l:cmd + ['format', '--stdin-filename=' . l:filename, '-'] |
| 57 | + let l:syscall = maktaba#syscall#Create(l:cmd).WithCwd(l:dir) |
| 58 | + try |
| 59 | + " mix format doesn't have a line-range option, but does a reasonable job |
| 60 | + " (except for leading indent) when given a full valid expression |
| 61 | + call codefmt#formatterhelpers#AttemptFakeRangeFormatting( |
| 62 | + \ a:startline, a:endline, l:syscall) |
| 63 | + catch /ERROR(ShellError):/ |
| 64 | + " Parse all the errors and stick them in the quickfix list. |
| 65 | + let l:errors = [] |
| 66 | + for l:line in split(v:exception, "\n") |
| 67 | + " Example output: |
| 68 | + " ** (SyntaxError) foo.exs:57:28: unexpected reserved word: end |
| 69 | + " (blank line) |
| 70 | + " HINT: it looks like the "end" on line 56 does not have a matching "do" defined before it |
| 71 | + " (blank line), (stack trace with 4-space indent) |
| 72 | + " TODO gather additional details between error message and stack trace |
| 73 | + let l:tokens = matchlist(l:line, |
| 74 | + \ printf('\v^\*\* (\(\k+\)) [^:]+:(\d+):(\d+):\s*(.*)')) |
| 75 | + if !empty(l:tokens) |
| 76 | + call add(l:errors, { |
| 77 | + \ 'filename': @%, |
| 78 | + \ 'lnum': l:tokens[2] + a:startline - 1, |
| 79 | + \ 'col': l:tokens[3], |
| 80 | + \ 'text': printf('%s %s', l:tokens[1], l:tokens[4])}) |
| 81 | + endif |
| 82 | + endfor |
| 83 | + if empty(l:errors) |
| 84 | + " Couldn't parse mix error format; display it all. |
| 85 | + call maktaba#error#Shout('Error formatting range: %s', v:exception) |
| 86 | + else |
| 87 | + call setqflist(l:errors, 'r') |
| 88 | + cc 1 |
| 89 | + endif |
| 90 | + endtry |
| 91 | + endfunction |
| 92 | + |
| 93 | + return l:formatter |
| 94 | +endfunction |
| 95 | + |
| 96 | +" Finds the directory to run mix from. Looks for a mix.exs file first; if that |
| 97 | +" is not found looks for a .formatter.exs file, falling back to the parent of |
| 98 | +" filepath. |
| 99 | +function! s:findMixDir(filepath) abort |
| 100 | + let l:path = empty(a:filepath) ? getcwd() : fnamemodify(a:filepath, ':h') |
| 101 | + let l:root = findfile('mix.exs', l:path . ';') |
| 102 | + if empty(l:root) |
| 103 | + let l:root = findfile('.formatter.exs', l:path . ';') |
| 104 | + endif |
| 105 | + if empty(l:root) |
| 106 | + let l:root = l:path |
| 107 | + else |
| 108 | + let l:root = fnamemodify(l:root, ':h') |
| 109 | + endif |
| 110 | + return l:root |
| 111 | +endfunction |
0 commit comments