|
| 1 | +" Copyright 2018 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: prettier |
| 22 | +function! codefmt#prettier#GetFormatter() abort |
| 23 | + let l:formatter = { |
| 24 | + \ 'name': 'prettier', |
| 25 | + \ 'setup_instructions': 'Install prettier (https://prettier.io/) ' . |
| 26 | + \ 'and configure the prettier_executable flag'} |
| 27 | + |
| 28 | + function l:formatter.IsAvailable() abort |
| 29 | + return executable(s:plugin.Flag('prettier_executable')) |
| 30 | + endfunction |
| 31 | + |
| 32 | + function l:formatter.AppliesToBuffer() abort |
| 33 | + return &filetype is# 'javascript' |
| 34 | + endfunction |
| 35 | + |
| 36 | + "" |
| 37 | + " Reformat the current buffer with prettier or the binary named in |
| 38 | + " @flag(prettier_executable), only targeting the range between {startline} and |
| 39 | + " {endline}. |
| 40 | + function l:formatter.FormatRange(startline, endline) abort |
| 41 | + let l:Prettier_options = s:plugin.Flag('prettier_options') |
| 42 | + if type(l:Prettier_options) is# type([]) |
| 43 | + let l:prettier_options = l:Prettier_options |
| 44 | + elseif maktaba#value#IsCallable(l:Prettier_options) |
| 45 | + let l:prettier_options = maktaba#function#Call(l:Prettier_options) |
| 46 | + else |
| 47 | + throw maktaba#error#WrongType( |
| 48 | + \ 'prettier_options flag must be list or callable. Found %s', |
| 49 | + \ string(l:Prettier_options)) |
| 50 | + endif |
| 51 | + let l:cmd = [s:plugin.Flag('prettier_executable'), '--stdin', '--no-color'] |
| 52 | + if @% == "" |
| 53 | + call extend(l:cmd, ['--parser', 'babylon']) |
| 54 | + else |
| 55 | + call extend(l:cmd, ['--stdin-filepath', @%]) |
| 56 | + endif |
| 57 | + call maktaba#ensure#IsNumber(a:startline) |
| 58 | + call maktaba#ensure#IsNumber(a:endline) |
| 59 | + |
| 60 | + let l:lines = getline(1, line('$')) |
| 61 | + let l:input = join(l:lines, "\n") |
| 62 | + if a:startline > 1 |
| 63 | + let l:lines_start = join(l:lines[0 : a:startline - 1], "\n") |
| 64 | + call extend(l:cmd, ['--range-start', string(strchars(l:lines_start))]) |
| 65 | + endif |
| 66 | + let l:lines_end = join(l:lines[0 : a:endline - 1], "\n") |
| 67 | + call extend(l:cmd, ['--range-end', string(strchars(l:lines_end))]) |
| 68 | + |
| 69 | + call extend(l:cmd, l:prettier_options) |
| 70 | + try |
| 71 | + let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() |
| 72 | + let l:formatted = split(l:result.stdout, "\n") |
| 73 | + call maktaba#buffer#Overwrite(1, line('$'), l:formatted) |
| 74 | + catch /ERROR(ShellError):/ |
| 75 | + " Parse all the errors and stick them in the quickfix list. |
| 76 | + let l:errors = [] |
| 77 | + for l:line in split(v:exception, "\n") |
| 78 | + let l:tokens = matchlist(l:line, |
| 79 | + \ '\C\v^\[error\] stdin: (.*) \((\d+):(\d+)\).*') |
| 80 | + if !empty(l:tokens) |
| 81 | + call add(l:errors, { |
| 82 | + \ 'filename': @%, |
| 83 | + \ 'lnum': l:tokens[2], |
| 84 | + \ 'col': l:tokens[3], |
| 85 | + \ 'text': l:tokens[1]}) |
| 86 | + endif |
| 87 | + endfor |
| 88 | + |
| 89 | + if empty(l:errors) |
| 90 | + " Couldn't parse prettier error format; display it all. |
| 91 | + call maktaba#error#Shout('Error formatting file: %s', v:exception) |
| 92 | + else |
| 93 | + call setqflist(l:errors, 'r') |
| 94 | + cc 1 |
| 95 | + endif |
| 96 | + endtry |
| 97 | + endfunction |
| 98 | + |
| 99 | + return l:formatter |
| 100 | +endfunction |
0 commit comments