-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathcodefmt.txt
412 lines (331 loc) · 17.8 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
*codefmt.txt* Syntax-aware code formatting for a variety of languages
Google *codefmt*
==============================================================================
CONTENTS *codefmt-contents*
1. Introduction..............................................|codefmt-intro|
2. Formatters...........................................|codefmt-formatters|
3. Configuration............................................|codefmt-config|
4. Commands...............................................|codefmt-commands|
5. Autocommands...........................................|codefmt-autocmds|
6. Mappings...............................................|codefmt-mappings|
1. Recommended zprint mappings.................|codefmt-mappings-zprint|
7. Dictionaries..............................................|codefmt-dicts|
8. Functions.............................................|codefmt-functions|
==============================================================================
INTRODUCTION *codefmt-intro*
Provides a |:FormatCode| command to intelligently reformat code.
==============================================================================
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: cljstyle, zprint
* dart: dartfmt
* fish: fish_indent
* elixir: mixformat
* gn: gn
* go: gofmt
* haskell: ormolu
* java: google-java-format
* javascript, json, html, css: js-beautify
* javascript, html, css, markdown: prettier
* json, jsonnet: jsonnetfmt
* kotlin: ktfmt
* lua: luaformatterfiveone
* nix: nixpkgs-fmt
* ocaml: ocamlformat
* python: autopep8, black, yapf
* rust: rustfmt
* sh: shfmt
* swift: swift-format
==============================================================================
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:mix_executable*
The path to the mix executable for Elixir.
Default: 'mix' `
*codefmt:yapf_executable*
The path to the yapf executable.
Default: 'yapf' `
*codefmt:black_executable*
The path to the black executable.
Default: 'black' `
*codefmt:isort_executable*
The path to the isort executable.
Default: 'isort' `
*codefmt:gn_executable*
The path to the gn executable.
Default: 'gn' `
*codefmt:buildifier_executable*
The path to the buildifier executable.
Default: 'buildifier' `
*codefmt:buildifier_lint_mode*
The lint_mode for buildifier. passed to buildifier --lint parameter.
Options:
"" (empty): Use default from buildifier.
"Off": Do not fix issues.
"Fix": Fix issues automatically during formatting.
"Warn": Format only if there are no issues; if there are issues, it will cause
an error and do no formatting.
Default: '' `
*codefmt:buildifier_warnings*
The warnings options passed to buildifier to modify the defaults. Whatever is
specified is added to the commandline after "--warnings=". For example, if you
add this to your config:
>
Glaive codefmt buildifier_warnings='-module-docstring,+unsorted-dict-items'
<
Then buildifier will omit the "module-docstring" warning, but add
"unsorted-dict-items" (which is ignored by default). This works also in
fix-mode, in which case dictionary items will be resorted upon buffer save.
Options:
"" (empty): Use default warnings from buildifier.
"-some-warning": Remove 'some-warning' from the warning set.
"+some-warning": Add 'some-warning' to the warning set.
Default: '' `
*codefmt:jsonnetfmt_executable*
The path to the jsonnetfmt executable.
Default: 'jsonnetfmt' `
*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:ktfmt_executable*
The path to the ktfmt executable with args, as a list. The default value
assumes there is a wrapper script named `ktfmt`. Without such a script, this
will generally have the form:
`ktfmt_executable=java,-jar,/path/to/ktfmt-VERSION-jar-with-dependencies.jar`
Note that range formatting is not fully supported, with a feature request at
https://github.com/facebookincubator/ktfmt/issues/218. ktfmt will align a
formatted range to column 1, requiring a manual reindent to match the
surrounding blocks.
Default: ['ktfmt'] `
*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:swift_format_executable*
The path to the swift-format executable.
Default: 'swift-format' `
*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:nixpkgs_fmt_executable*
The path to the nixpkgs-fmt executable.
Default: 'nixpkgs-fmt' `
*codefmt:luaformatterfiveone_executable*
The path to the luaformatterfiveone executable.
Default: 'luaformatterfiveone' `
*codefmt:cljstyle_executable*
The path to the cljstyle executable.
Default: 'cljstyle' `
*codefmt:ormolu_executable*
The path to the ormolu executable.
Default: 'ormolu' `
*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.
==============================================================================
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|.
==============================================================================
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
<
==============================================================================
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.
==============================================================================
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#FiletypeMatches({filetype}, {expected})
*codefmt#formatterhelpers#FiletypeMatches()*
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 ERROR(WrongType)
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}, {ignoreerrors}, {skipfirstnlines}) *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
Throws ERROR(WrongType)
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
vim:tw=78:ts=8:ft=help:norl: