|
| 1 | +" Copyright 2019 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 | +" @section Recommended zprint mappings, mappings-zprint |
| 17 | +" @parentsection mappings |
| 18 | +" |
| 19 | +" Since zprint only works on top-level Clojure forms, it doesn't make sense to |
| 20 | +" format line ranges that aren't complete forms. If you're using vim-sexp |
| 21 | +" (https://github.com/guns/vim-sexp), the following mapping replaces the default |
| 22 | +" "format the current line" with "format the current top-level form." > |
| 23 | +" autocmd FileType clojure nmap <buffer> <silent> <leader>== <leader>=iF |
| 24 | +" < |
| 25 | + |
| 26 | + |
| 27 | +let s:plugin = maktaba#plugin#Get('codefmt') |
| 28 | + |
| 29 | + |
| 30 | +"" |
| 31 | +" @private |
| 32 | +" Formatter: zprint |
| 33 | +function! codefmt#zprint#GetFormatter() abort |
| 34 | + let l:formatter = { |
| 35 | + \ 'name': 'zprint', |
| 36 | + \ 'setup_instructions': |
| 37 | + \ 'Install zprint filter (https://github.com/kkinnear/zprint) ' . |
| 38 | + \ 'and configure the zprint_executable flag'} |
| 39 | + |
| 40 | + function l:formatter.IsAvailable() abort |
| 41 | + return executable(s:plugin.Flag('zprint_executable')) |
| 42 | + endfunction |
| 43 | + |
| 44 | + function l:formatter.AppliesToBuffer() abort |
| 45 | + return &filetype is# 'clojure' |
| 46 | + endfunction |
| 47 | + |
| 48 | + "" |
| 49 | + " Reformat the current buffer with zprint or the binary named in |
| 50 | + " @flag(zprint_executable), only targeting the range between {startline} and |
| 51 | + " {endline}. |
| 52 | + function l:formatter.FormatRange(startline, endline) abort |
| 53 | + " Must be upper-cased to call as a function |
| 54 | + let l:ZprintOptions = s:plugin.Flag('zprint_options') |
| 55 | + if type(l:ZprintOptions) is# type([]) |
| 56 | + " Assign upper-case to lower-case |
| 57 | + let l:zprint_options = l:ZprintOptions |
| 58 | + elseif maktaba#value#IsCallable(l:ZprintOptions) |
| 59 | + " Call upper-case to assign lower-case |
| 60 | + let l:zprint_options = maktaba#function#Call(l:ZprintOptions) |
| 61 | + else |
| 62 | + throw maktaba#error#WrongType( |
| 63 | + \ 'zprint_options flag must be list or callable. Found %s', |
| 64 | + \ string(l:ZprintOptions)) |
| 65 | + endif |
| 66 | + let l:cmd = [s:plugin.Flag('zprint_executable')] |
| 67 | + call extend(l:cmd, l:zprint_options) |
| 68 | + |
| 69 | + call maktaba#ensure#IsNumber(a:startline) |
| 70 | + call maktaba#ensure#IsNumber(a:endline) |
| 71 | + let l:lines = getline(1, line('$')) |
| 72 | + |
| 73 | + " zprint doesn't support formatting a range of lines, so format the range |
| 74 | + " individually, ignoring context. This works well for top-level forms, although it's |
| 75 | + " not ideal for inner forms because it loses the indentation. |
| 76 | + let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n") |
| 77 | + |
| 78 | + " Prepare the syscall, changing to the containing directory in case the user |
| 79 | + " has configured {:search-config? true} in ~/.zprintrc |
| 80 | + let l:result = maktaba#syscall#Create(l:cmd).WithCwd(expand('%:p:h')).WithStdin(l:input).Call() |
| 81 | + let l:formatted = split(l:result.stdout, "\n") |
| 82 | + |
| 83 | + " Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right. |
| 84 | + let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : [] |
| 85 | + let l:full_formatted = l:before + l:formatted + l:lines[a:endline :] |
| 86 | + |
| 87 | + call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted) |
| 88 | + endfunction |
| 89 | + |
| 90 | + return l:formatter |
| 91 | +endfunction |
0 commit comments