-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathformatterhelpers.vim
163 lines (142 loc) · 6.15 KB
/
formatterhelpers.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
" Copyright 2020 Google Inc. All rights reserved.
"
" Licensed under the Apache License, Version 2.0 (the "License");
" you may not use this file except in compliance with the License.
" You may obtain a copy of the License at
"
" http://www.apache.org/licenses/LICENSE-2.0
"
" Unless required by applicable law or agreed to in writing, software
" distributed under the License is distributed on an "AS IS" BASIS,
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
" See the License for the specific language governing permissions and
" limitations under the License.
let s:plugin = maktaba#plugin#Get('codefmt')
" TODO(google/vim-maktaba#255): Use maktaba's when dropping support for 1.16.0.
function! s:ValueAsList(Value_or_values) abort
return maktaba#value#IsList(a:Value_or_values) ?
\ a:Value_or_values : [a:Value_or_values]
endfunction
""
" @public
" Checks if the given {filetype} matches {expected} filetype(s).
"
" When checking a dotted filetype name (like "c.doxygen"), returns true if any
" piece matches expected filetype(s).
"
" Usage examples: >
" if codefmt#formatterhelpers#FiletypeMatches(&filetype, 'c')
" < >
" if codefmt#formatterhelpers#FiletypeMatches(&filetype, ['c', 'cpp'])
" <
" @throws WrongType
function! codefmt#formatterhelpers#FiletypeMatches(filetype, expected) abort
call maktaba#ensure#TypeMatchesOneOf(a:expected, ['', ['']])
let l:expected = s:ValueAsList(a:expected)
" TODO(google/vim-maktaba#256): Drop this check when redundant with above.
for l:expected_ft in l:expected
call maktaba#ensure#IsString(l:expected_ft)
endfor
" Check if filetypes match expected (splitting & looping to help support
" dot-separated filetype names).
for l:filetype in split(a:filetype, '\m\.', 0)
if index(l:expected, l:filetype) >= 0
return 1
endif
endfor
return 0
endfunction
""
" @public
" Format lines in the current buffer via a formatter invoked by {cmd}, which
" is a system call represented by either a |maktaba.Syscall| or any argument
" accepted by |maktaba#syscall#Create()|. The command must include any
" arguments for the explicit range line numbers to use, if any.
"
" @throws ShellError if the {cmd} system call fails
function! codefmt#formatterhelpers#Format(cmd) abort
let l:lines = getline(1, line('$'))
let l:input = join(l:lines, "\n")
let l:result = maktaba#syscall#Create(a:cmd).WithStdin(l:input).Call()
let l:formatted = split(l:result.stdout, "\n")
call maktaba#buffer#Overwrite(1, line('$'), l:formatted)
endfunction
""
" @public
" @usage startline endline cmd \[ignoreerrors] \[skipfirstnlines]
" Attempt to format a range of lines from {startline} to {endline} in the
" current buffer via a formatter that doesn't natively support range
" formatting, which is invoked via {cmd} (a system call represented by either
" a |maktaba.Syscall| or any argument accepted by |maktaba#syscall#Create()|).
" It uses a hacky strategy of sending those lines to the formatter in
" isolation, which gives bad results if the code on those lines isn't
" a self-contained block of syntax or is part of a larger indent.
"
" If invoking this hack, please make sure to file a feature request against
" the tool for range formatting and post a URL for that feature request above
" code that calls it.
"
" If [ignoreerrors] is nonzero, the syscall ignores errors. This can be helpful
" for formatters that return nonzero results for reasons unrelated to
" formatting. If [skipfirstnlines] is set to a nonzero number N, the first
" N lines of the formatter output are trimmed. This can be used to trim
" always-present headers.
"
" @throws ShellError if the {cmd} system call fails (and [ignoreerrors] is 0)
" @throws WrongType
function! codefmt#formatterhelpers#AttemptFakeRangeFormatting(
\ startline, endline, cmd, ...) abort
call maktaba#ensure#IsNumber(a:startline)
call maktaba#ensure#IsNumber(a:endline)
let l:ignoreerrors = a:0 >= 1 ? a:1 : 0
let l:skipfirstnlines = a:0 >= 2 ? a:2 : 0
call maktaba#ensure#IsNumber(l:ignoreerrors)
call maktaba#ensure#IsNumber(l:skipfirstnlines)
let l:lines = getline(1, line('$'))
let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n")
let l:result =
\ maktaba#syscall#Create(a:cmd).WithStdin(l:input).Call(!l:ignoreerrors)
let l:formatted = split(l:result.stdout, "\n")
" Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right.
let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : []
let l:full_formatted = l:before + l:formatted[l:skipfirstnlines :]
\ + l:lines[a:endline :]
call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted)
endfunction
""
" @public
" Resolve a flag (function, string or array) to a normalized array, with special
" handling to convert a spaceless string to a single-element array. This is the
" common case for executables, and more importantly, is backward-compatible for
" existing user settings.
"
" @throws WrongType if the flag doesn't resolve to a string or array
function! codefmt#formatterhelpers#ResolveFlagToArray(flag_name) abort
let l:FlagFn = s:plugin.Flag(a:flag_name)
if maktaba#value#IsFuncref(l:FlagFn)
let l:value = maktaba#function#Call(l:FlagFn)
else
let l:value = l:FlagFn
endif
" After (conditionally) calling the function, the resulting value should be
" either a list that we can use directly, or a string that we can treat as
" a single-element list, mainly for backward compatibility.
if maktaba#value#IsString(l:value)
if l:value =~ '\s'
" Uh oh, there are spaces in the string. Rather than guessing user intent
" with shell quoting and word splitting, handle this (hopefully unusual)
" case by telling them to update their configuration.
throw maktaba#error#WrongType(
\ '%s flag is a string with spaces, please make it a list. ' .
\ 'Resolved value was: %s',
\ a:flag_name, l:value)
endif
" Convert spaceless string to single-element list.
return [l:value]
elseif maktaba#value#IsList(l:value)
return l:value
endif
throw maktaba#error#WrongType(
\ '%s flag should be a list after calling. Found %s',
\ a:flag_name, maktaba#value#TypeName(l:value))
endfunction