-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathcodefmt.txt
310 lines (247 loc) · 14.1 KB
/
codefmt.txt
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
*codefmt.txt* Syntax-aware code formatting for a variety of languages
Google *codefmt*
==============================================================================
CONTENTS *codefmt-contents*
1. Introduction..............................................|codefmt-intro|
2. Configuration............................................|codefmt-config|
3. Formatters...........................................|codefmt-formatters|
4. Dictionaries..............................................|codefmt-dicts|
5. Commands...............................................|codefmt-commands|
6. Autocommands...........................................|codefmt-autocmds|
7. Functions.............................................|codefmt-functions|
8. Mappings...............................................|codefmt-mappings|
1. Recommended zprint mappings.................|codefmt-mappings-zprint|
==============================================================================
INTRODUCTION *codefmt-intro*
Provides a |:FormatCode| command to intelligently reformat code.
==============================================================================
CONFIGURATION *codefmt-config*
This plugin uses maktaba flags for configuration. Install Glaive
(https://github.com/google/glaive) and use the |:Glaive| command to configure
them.
*codefmt:autopep8_executable*
The path to the autopep8 executable.
Default: 'autopep8' `
*codefmt:clang_format_executable*
The path to the clang-format executable. String, list, or callable that takes
no args and returns a string or a list.
Default: 'clang-format' `
*codefmt:clang_format_style*
Formatting style for clang-format to use. Either a string or callable that
takes no args and returns a style name for the current buffer. See
http://clang.llvm.org/docs/ClangFormatStyleOptions.html for details.
Default: 'file' `
*codefmt:gofmt_executable*
The path to the gofmt executable. For example, this can be changed to
"goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to
additionally adjust imports when formatting.
Default: 'gofmt' `
*codefmt:dartfmt_executable*
The path to the dartfmt executable.
Default: 'dartfmt' `
*codefmt:js_beautify_executable*
The path to the js-beautify executable.
Default: 'js-beautify' `
*codefmt:yapf_executable*
The path to the yapf executable.
Default: 'yapf' `
*codefmt:black_executable*
The path to the black executable.
Default: 'black' `
*codefmt:gn_executable*
The path to the gn executable.
Default: 'gn' `
*codefmt:buildifier_executable*
The path to the buildifier executable.
Default: 'buildifier' `
*codefmt:google_java_executable*
The path to the google-java executable. Generally, this should have the form:
`java -jar /path/to/google-java`
Default: 'google-java-format' `
*codefmt:shfmt_options*
Command line arguments to feed shfmt. Either a list or callable that takes no
args and returns a list with command line arguments. By default, uses the
Google's style. See https://github.com/mvdan/sh for details.
Default: ['-i', '2', '-sr', '-ci'] `
*codefmt:shfmt_executable*
The path to the shfmt executable. String, list, or callable that takes no args
and returns a string or a list.
Default: 'shfmt' `
*codefmt:prettier_options*
Command line arguments to feed prettier. Either a list or callable that takes
no args and returns a list with command line arguments.
Default: [] `
*codefmt:prettier_executable*
The path to the prettier executable. String, list, or callable that takes no
args and returns a string or a list. The default uses npx if available, so
that the repository-local prettier will have priority.
Default: function('s:LookupPrettierExecutable') `
*codefmt:rustfmt_options*
Command line arguments to feed rustfmt. Either a list or callable that takes
no args and returns a list with command line arguments.
Default: [] `
*codefmt:rustfmt_executable*
The path to the rustfmt executable.
Default: 'rustfmt' `
*codefmt:zprint_options*
Command line arguments to feed zprint. Either a list or callable that takes no
args and returns a list with command line arguments. The default configures
zprint with Vim's textwidth.
Default: function('s:ZprintOptions') `
*codefmt:zprint_executable*
The path to the zprint executable. Typically this is one of the native images
(zprintl or zprintm) from https://github.com/kkinnear/zprint/releases
installed as zprint.
Default: 'zprint' `
*codefmt:fish_indent_executable*
The path to the fish_indent executable.
Default: 'fish_indent' `
*codefmt:ocamlformat_executable*
The path to the ocamlformat executable.
Default: 'ocamlformat' `
*codefmt:plugin[autocmds]*
Configures whether plugin/autocmds.vim should be loaded.
Default: 1 `
*codefmt:plugin[commands]*
Configures whether plugin/commands.vim should be loaded.
Default: 1 `
*codefmt:plugin[mappings]*
Configures whether plugin/mappings.vim should be loaded.
Default: 0 `
*codefmt:plugin[register]*
Configures whether plugin/register.vim should be loaded.
Default: 1 `
*b:codefmt_formatter*
You can override the default formatter by defining this variable. For
instance, to explicitly select the clang-format formatter for Java, add
>
autocmd FileType java let b:codefmt_formatter = 'clang-format'
<
to your vimrc. You can also set the value to an empty string to disable all
formatting.
==============================================================================
FORMATTERS *codefmt-formatters*
This plugin has three built-in formatters: clang-format, gofmt, and autopep8.
More formatters can be registered by other plugins that integrate with
codefmt.
DEFAULT FORMATTERS
Codefmt will automatically use a default formatter for certain filetypes if
none is explicitly supplied via an explicit arg to |:FormatCode| or the
|b:codefmt_formatter| variable. The default formatter may also depend on what
plugins are enabled or what other software is installed on your system.
The current list of defaults by filetype is:
* bzl (Bazel): buildifier
* c, cpp, proto, javascript, typescript: clang-format
* clojure: zprint
* dart: dartfmt
* fish: fish_indent
* gn: gn
* go: gofmt
* ocaml: ocamlformat
* python: autopep8, black, yapf
==============================================================================
DICTIONARIES *codefmt-dicts*
*codefmt.Formatter*
Interface for applying formatting to lines of code. Formatters are registered
with codefmt using maktaba's standard extension registry:
>
let l:codefmt_registry = maktaba#extension#GetRegistry('codefmt')
call l:codefmt_registry.AddExtension(l:formatter)
<
Formatters define these fields:
* name (string): The formatter name that will be exposed to users.
* setup_instructions (string, optional): A string explaining to users how to
make the plugin available if not already available.
and these functions:
* IsAvailable() -> boolean: Whether the formatter is fully functional with
all dependencies available. Returns 0 only if setup_instructions have not
been followed.
* AppliesToBuffer() -> boolean: Whether the current buffer is of a type
normally formatted by this formatter. Normally based on 'filetype', but
could depend on buffer name or other properties.
and should implement at least one of the following functions:
* Format(): Formats the current buffer directly.
* FormatRange({startline}, {endline}): Formats the current buffer, focusing
on the range of lines from {startline} to {endline}.
* FormatRanges({ranges}): Formats the current buffer, focusing on the given
ranges of lines. Each range should be a 2-item list of
[startline,endline].
Formatters should implement the most specific format method that is supported.
==============================================================================
COMMANDS *codefmt-commands*
:[range]FormatLines [formatter] *:FormatLines*
Format the current line or range using [formatter].
[formatter] is the default formatter associated with the current buffer if
omitted.
:FormatCode [formatter] *:FormatCode*
Format the whole buffer using [formatter]. See |codefmt-formatters| for list
of valid formatters.
[formatter] is the default formatter associated with the current buffer if
omitted.
:AutoFormatBuffer [formatter] *:AutoFormatBuffer*
Enables format on save for this buffer using [formatter]. Also configures
[formatter] as the default formatter for this buffer via the
|b:codefmt_formatter| variable.
[formatter] is the default formatter associated with the current buffer if
omitted.
:NoAutoFormatBuffer *:NoAutoFormatBuffer*
Disables format on save for this buffer.
==============================================================================
AUTOCOMMANDS *codefmt-autocmds*
You can enable automatic formatting on a buffer using |:AutoFormatBuffer|.
==============================================================================
FUNCTIONS *codefmt-functions*
codefmt#FormatMap({type}) *codefmt#FormatMap()*
Suitable for use as 'operatorfunc'; see |g@| for details. The type is
ignored since formatting only works on complete lines.
codefmt#formatterhelpers#Format({cmd}) *codefmt#formatterhelpers#Format()*
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 ERROR(ShellError) if the {cmd} system call fails
codefmt#formatterhelpers#AttemptFakeRangeFormatting({startline}, {endline},
{cmd}) *codefmt#formatterhelpers#AttemptFakeRangeFormatting()*
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.
Throws ERROR(ShellError) if the {cmd} system call fails
codefmt#formatterhelpers#ResolveFlagToArray({flag_name})
*codefmt#formatterhelpers#ResolveFlagToArray()*
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 ERROR(WrongType) if the flag doesn't resolve to a string or array
==============================================================================
MAPPINGS *codefmt-mappings*
This plugin provides default mappings that can be enabled via the
plugin[mappings] flag. You can enable them under the default prefix of
<Leader>= (<Leader> being "\" by default) or set the plugin[mappings] flag to
an explicit prefix to use. Or you can define your own custom mappings; see
plugin/mappings.vim for inspiration.
To format the whole buffer, use <PREFIX>b.
Some formatters also support formatting ranges. There are several mappings for
formatting ranges that mimic vim's built-in |operator|s:
* Format the current line with the <PREFIX>= mapping.
* <PREFIX> by itself acts as an |operator|. Use <PREFIX><MOTION> to format
over any motion. For instance, <PREFIX>i{ will format all lines inside the
enclosing curly braces.
* In visual mode, <PREFIX> will format the visual selection.
==============================================================================
RECOMMENDED ZPRINT MAPPINGS *codefmt-mappings-zprint*
Since zprint only works on top-level Clojure forms, it doesn't make sense to
format line ranges that aren't complete forms. If you're using vim-sexp
(https://github.com/guns/vim-sexp), the following mapping replaces the default
"format the current line" with "format the current top-level form."
>
autocmd FileType clojure nmap <buffer> <silent> <leader>== <leader>=iF
<
vim:tw=78:ts=8:ft=help:norl: