blob: 564f3e774a96319eb310fc5253e4213b3b85d35b [file] [log] [blame]
Gregory Andersfc935942023-09-12 13:23:38 -05001*ft_rust.txt* Filetype plugin for Rust
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01002
3==============================================================================
Gregory Andersfc935942023-09-12 13:23:38 -05004CONTENTS *rust*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01005
Gregory Andersfc935942023-09-12 13:23:38 -050061. Introduction |rust-intro|
72. Settings |rust-settings|
83. Commands |rust-commands|
94. Mappings |rust-mappings|
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010010
11==============================================================================
Gregory Andersfc935942023-09-12 13:23:38 -050012INTRODUCTION *rust-intro*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010013
14This plugin provides syntax and supporting functionality for the Rust
Gregory Andersfc935942023-09-12 13:23:38 -050015filetype. It requires Vim 8 or higher for full functionality. Some commands
16will not work on earlier versions.
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010017
18==============================================================================
Gregory Andersfc935942023-09-12 13:23:38 -050019SETTINGS *rust-settings*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010020
21This plugin has a few variables you can define in your vimrc that change the
22behavior of the plugin.
23
Gregory Andersfc935942023-09-12 13:23:38 -050024Some variables can be set buffer local (`:b` prefix), and the buffer local
25will take precedence over the global `g:` counterpart.
26
27 *g:rustc_path*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010028g:rustc_path~
29 Set this option to the path to rustc for use in the |:RustRun| and
30 |:RustExpand| commands. If unset, "rustc" will be located in $PATH: >
Gregory Andersfc935942023-09-12 13:23:38 -050031 let g:rustc_path = $HOME."/bin/rustc"
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010032<
33
Gregory Andersfc935942023-09-12 13:23:38 -050034 *g:rustc_makeprg_no_percent*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010035g:rustc_makeprg_no_percent~
36 Set this option to 1 to have 'makeprg' default to "rustc" instead of
37 "rustc %": >
38 let g:rustc_makeprg_no_percent = 1
39<
40
Gregory Andersfc935942023-09-12 13:23:38 -050041 *g:rust_conceal*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010042g:rust_conceal~
43 Set this option to turn on the basic |conceal| support: >
44 let g:rust_conceal = 1
45<
46
Gregory Andersfc935942023-09-12 13:23:38 -050047 *g:rust_conceal_mod_path*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010048g:rust_conceal_mod_path~
49 Set this option to turn on |conceal| for the path connecting token
50 "::": >
51 let g:rust_conceal_mod_path = 1
52<
53
Gregory Andersfc935942023-09-12 13:23:38 -050054 *g:rust_conceal_pub*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010055g:rust_conceal_pub~
56 Set this option to turn on |conceal| for the "pub" token: >
57 let g:rust_conceal_pub = 1
58<
59
Gregory Andersfc935942023-09-12 13:23:38 -050060 *g:rust_recommended_style*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010061g:rust_recommended_style~
Gregory Andersfc935942023-09-12 13:23:38 -050062 Set this option to enable vim indentation and textwidth settings to
63 conform to style conventions of the rust standard library (i.e. use 4
64 spaces for indents and sets 'textwidth' to 99). This option is enabled
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010065 by default. To disable it: >
66 let g:rust_recommended_style = 0
67<
68
Gregory Andersfc935942023-09-12 13:23:38 -050069 *g:rust_fold*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010070g:rust_fold~
71 Set this option to turn on |folding|: >
72 let g:rust_fold = 1
73<
74 Value Effect ~
75 0 No folding
76 1 Braced blocks are folded. All folds are open by
77 default.
78 2 Braced blocks are folded. 'foldlevel' is left at the
79 global value (all folds are closed by default).
80
Gregory Andersfc935942023-09-12 13:23:38 -050081 *g:rust_bang_comment_leader*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010082g:rust_bang_comment_leader~
83 Set this option to 1 to preserve the leader on multi-line doc comments
84 using the /*! syntax: >
85 let g:rust_bang_comment_leader = 1
86<
87
Gregory Andersfc935942023-09-12 13:23:38 -050088 *g:rust_use_custom_ctags_defs*
89g:rust_use_custom_ctags_defs~
90 Set this option to 1 if you have customized ctags definitions for Rust
91 and do not wish for those included with rust.vim to be used: >
92 let g:rust_use_custom_ctags_defs = 1
93<
94
95 NOTE: rust.vim's built-in definitions are only used for the Tagbar Vim
96 plugin, if you have it installed, AND if Universal Ctags is not
97 detected. This is because Universal Ctags already has built-in
98 support for Rust when used with Tagbar.
99
100 Also, note that when using ctags other than Universal Ctags, it is not
101 automatically used when generating |tags| files that Vim can use to
102 navigate to definitions across different source files. Feel free to
103 copy `rust.vim/ctags/rust.ctags` into your own `~/.ctags` if you wish
104 to generate |tags| files.
105
106
107 *g:ftplugin_rust_source_path*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100108g:ftplugin_rust_source_path~
109 Set this option to a path that should be prepended to 'path' for Rust
110 source files: >
Gregory Andersfc935942023-09-12 13:23:38 -0500111 let g:ftplugin_rust_source_path = $HOME.'/dev/rust'
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100112<
113
Gregory Andersfc935942023-09-12 13:23:38 -0500114 *g:rustfmt_command*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100115g:rustfmt_command~
116 Set this option to the name of the 'rustfmt' executable in your $PATH. If
117 not specified it defaults to 'rustfmt' : >
118 let g:rustfmt_command = 'rustfmt'
119<
Gregory Andersfc935942023-09-12 13:23:38 -0500120 *g:rustfmt_autosave*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100121g:rustfmt_autosave~
122 Set this option to 1 to run |:RustFmt| automatically when saving a
123 buffer. If not specified it defaults to 0 : >
124 let g:rustfmt_autosave = 0
125<
Gregory Andersfc935942023-09-12 13:23:38 -0500126 There is also a buffer-local b:rustfmt_autosave that can be set for
127 the same purpose, and can override the global setting.
128
129 *g:rustfmt_autosave_if_config_present*
130g:rustfmt_autosave_if_config_present~
131 Set this option to 1 to have *b:rustfmt_autosave* be set automatically
132 if a `rustfmt.toml` file is present in any parent directly leading to
133 the file being edited. If not set, default to 0: >
134 let g:rustfmt_autosave_if_config_present = 0
135<
136 This is useful to have `rustfmt` only execute on save, on projects
137 that have `rustfmt.toml` configuration.
138
139 There is also a buffer-local b:rustfmt_autosave_if_config_present
140 that can be set for the same purpose, which can overrides the global
141 setting.
142 *g:rustfmt_fail_silently*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100143g:rustfmt_fail_silently~
144 Set this option to 1 to prevent 'rustfmt' from populating the
145 |location-list| with errors. If not specified it defaults to 0: >
146 let g:rustfmt_fail_silently = 0
147<
Gregory Andersfc935942023-09-12 13:23:38 -0500148 *g:rustfmt_options*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100149g:rustfmt_options~
150 Set this option to a string of options to pass to 'rustfmt'. The
151 write-mode is already set to 'overwrite'. If not specified it
152 defaults to '' : >
153 let g:rustfmt_options = ''
154<
Gregory Andersfc935942023-09-12 13:23:38 -0500155 *g:rustfmt_emit_files*
156g:rustfmt_emit_files~
157 If not specified rust.vim tries to detect the right parameter to
158 pass to rustfmt based on its reported version. Otherwise, it
159 determines whether to run rustfmt with '--emit=files' (when 1 is
160 provided) instead of '--write-mode=overwrite'. >
161 let g:rustfmt_emit_files = 0
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100162
Christian Brabandt6c1afa32024-01-01 20:50:51 +0100163<
Gregory Andersfc935942023-09-12 13:23:38 -0500164 *g:rust_playpen_url*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100165g:rust_playpen_url~
Gregory Andersfc935942023-09-12 13:23:38 -0500166 Set this option to override the url for the playpen to use: >
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100167 let g:rust_playpen_url = 'https://play.rust-lang.org/'
168<
169
Gregory Andersfc935942023-09-12 13:23:38 -0500170 *g:rust_shortener_url*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100171g:rust_shortener_url~
Gregory Andersfc935942023-09-12 13:23:38 -0500172 Set this option to override the url for the url shortener: >
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100173 let g:rust_shortener_url = 'https://is.gd/'
174<
175
Gregory Andersfc935942023-09-12 13:23:38 -0500176 *g:rust_clip_command*
177g:rust_clip_command~
178 Set this option to the command used in your OS to copy the Rust Play
179 url to the clipboard: >
180 let g:rust_clip_command = 'xclip -selection clipboard'
181<
182
183 *g:cargo_makeprg_params*
184g:cargo_makeprg_params~
185 Set this option to the string of parameters to pass to cargo. If not
186 specified it defaults to '$*' : >
187 let g:cargo_makeprg_params = 'build'
188<
189
190 *g:cargo_shell_command_runner*
191g:cargo_shell_command_runner~
192 Set this option to change how to run shell commands for cargo commands
193 |:Cargo|, |:Cbuild|, |:Crun|, ...
194 By default, |:terminal| is used to run shell command in terminal window
195 asynchronously. But if you prefer |:!| for running the commands, it can
196 be specified: >
197 let g:cargo_shell_command_runner = '!'
198<
199
200
201Integration with Syntastic *rust-syntastic*
202--------------------------
203
204This plugin automatically integrates with the Syntastic checker. There are two
205checkers provided: 'rustc', and 'cargo'. The latter invokes 'Cargo' in order to
206build code, and the former delivers a single edited '.rs' file as a compilation
207target directly to the Rust compiler, `rustc`.
208
209Because Cargo is almost exclusively being used for building Rust code these
210days, 'cargo' is the default checker. >
211
212 let g:syntastic_rust_checkers = ['cargo']
213<
214If you would like to change it, you can set `g:syntastic_rust_checkers` to a
215different value.
216 *g:rust_cargo_avoid_whole_workspace*
217 *b:rust_cargo_avoid_whole_workspace*
218g:rust_cargo_avoid_whole_workspace~
219 When editing a crate that is part of a Cargo workspace, and this
220 option is set to 1 (the default), then 'cargo' will be executed
221 directly in that crate directory instead of in the workspace
222 directory. Setting 0 prevents this behavior - however be aware that if
223 you are working in large workspace, Cargo commands may take more time,
224 plus the Syntastic error list may include all the crates in the
225 workspace. >
226 let g:rust_cargo_avoid_whole_workspace = 0
227<
228 *g:rust_cargo_check_all_targets*
229 *b:rust_cargo_check_all_targets*
230g:rust_cargo_check_all_targets~
231 When set to 1, the `--all-targets` option will be passed to cargo when
232 Syntastic executes it, allowing the linting of all targets under the
233 package.
234 The default is 0.
235
236 *g:rust_cargo_check_all_features*
237 *b:rust_cargo_check_all_features*
238g:rust_cargo_check_all_features~
239 When set to 1, the `--all-features` option will be passed to cargo when
240 Syntastic executes it, allowing the linting of all features of the
241 package.
242 The default is 0.
243
244 *g:rust_cargo_check_examples*
245 *b:rust_cargo_check_examples*
246g:rust_cargo_check_examples~
247 When set to 1, the `--examples` option will be passed to cargo when
248 Syntastic executes it, to prevent the exclusion of examples from
249 linting. The examples are normally under the `examples/` directory of
250 the crate.
251 The default is 0.
252
253 *g:rust_cargo_check_tests*
254 *b:rust_cargo_check_tests*
255g:rust_cargo_check_tests~
256 When set to 1, the `--tests` option will be passed to cargo when
257 Syntastic executes it, to prevent the exclusion of tests from linting.
258 The tests are normally under the `tests/` directory of the crate.
259 The default is 0.
260
261 *g:rust_cargo_check_benches*
262 *b:rust_cargo_check_benches*
263g:rust_cargo_check_benches~
264 When set to 1, the `--benches` option will be passed to cargo when
265 Syntastic executes it. The benches are normally under the `benches/`
266 directory of the crate.
267 The default is 0.
268
269Integration with auto-pairs *rust-auto-pairs*
270---------------------------
271
272This plugin automatically configures the auto-pairs plugin not to duplicate
273single quotes, which are used more often for lifetime annotations than for
274single character literals.
275
276 *g:rust_keep_autopairs_default*
277g:rust_keep_autopairs_default~
278
279 Don't override auto-pairs default for the Rust filetype. The default
280 is 0.
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100281
282==============================================================================
Gregory Andersfc935942023-09-12 13:23:38 -0500283COMMANDS *rust-commands*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100284
Gregory Andersfc935942023-09-12 13:23:38 -0500285Invoking Cargo
286--------------
287
288This plug defines very simple shortcuts for invoking Cargo from with Vim.
289
290:Cargo <args> *:Cargo*
291 Runs 'cargo' with the provided arguments.
292
293:Cbuild <args> *:Cbuild*
294 Shortcut for 'cargo build`.
295
296:Cclean <args> *:Cclean*
297 Shortcut for 'cargo clean`.
298
299:Cdoc <args> *:Cdoc*
300 Shortcut for 'cargo doc`.
301
302:Cinit <args> *:Cinit*
303 Shortcut for 'cargo init`.
304
305:Crun <args> *:Crun*
306 Shortcut for 'cargo run`.
307
308:Ctest <args> *:Ctest*
309 Shortcut for 'cargo test`.
310
311:Cupdate <args> *:Cupdate*
312 Shortcut for 'cargo update`.
313
314:Cbench <args> *:Cbench*
315 Shortcut for 'cargo bench`.
316
317:Csearch <args> *:Csearch*
318 Shortcut for 'cargo search`.
319
320:Cpublish <args> *:Cpublish*
321 Shortcut for 'cargo publish`.
322
323:Cinstall <args> *:Cinstall*
324 Shortcut for 'cargo install`.
325
326:Cruntarget <args> *:Cruntarget*
327 Shortcut for 'cargo run --bin' or 'cargo run --example',
328 depending on the currently open buffer.
329
330Formatting
331----------
332
333:RustFmt *:RustFmt*
334 Runs |g:rustfmt_command| on the current buffer. If
335 |g:rustfmt_options| is set then those will be passed to the
336 executable.
337
338 If |g:rustfmt_fail_silently| is 0 (the default) then it
339 will populate the |location-list| with the errors from
340 |g:rustfmt_command|. If |g:rustfmt_fail_silently| is set to 1
341 then it will not populate the |location-list|.
342
343:RustFmtRange *:RustFmtRange*
344 Runs |g:rustfmt_command| with selected range. See
345 |:RustFmt| for any other information.
346
347
348Playpen integration
349-------------------
350
351:RustPlay *:RustPlay*
352 This command will only work if you have web-api.vim installed
353 (available at https://github.com/mattn/webapi-vim). It sends the
354 current selection, or if nothing is selected, the entirety of the
355 current buffer to the Rust playpen, and emits a message with the
356 shortened URL to the playpen.
357
358 |g:rust_playpen_url| is the base URL to the playpen, by default
359 "https://play.rust-lang.org/".
360
361 |g:rust_shortener_url| is the base url for the shorterner, by
362 default "https://is.gd/"
363
364 |g:rust_clip_command| is the command to run to copy the
365 playpen url to the clipboard of your system.
366
367
368Evaluation of a single Rust file
369--------------------------------
370
371NOTE: These commands are useful only when working with standalone Rust files,
372which is usually not the case for common Rust development. If you wish to
373building Rust crates from with Vim can should use Vim's make, Syntastic, or
374functionality from other plugins.
375
376
377:RustRun [args] *:RustRun*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100378:RustRun! [rustc-args] [--] [args]
379 Compiles and runs the current file. If it has unsaved changes,
380 it will be saved first using |:update|. If the current file is
381 an unnamed buffer, it will be written to a temporary file
382 first. The compiled binary is always placed in a temporary
383 directory, but is run from the current directory.
384
385 The arguments given to |:RustRun| will be passed to the
386 compiled binary.
387
388 If ! is specified, the arguments are passed to rustc instead.
389 A "--" argument will separate the rustc arguments from the
390 arguments passed to the binary.
391
392 If |g:rustc_path| is defined, it is used as the path to rustc.
393 Otherwise it is assumed rustc can be found in $PATH.
394
Gregory Andersfc935942023-09-12 13:23:38 -0500395:RustExpand [args] *:RustExpand*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100396:RustExpand! [TYPE] [args]
397 Expands the current file using --pretty and displays the
398 results in a new split. If the current file has unsaved
399 changes, it will be saved first using |:update|. If the
400 current file is an unnamed buffer, it will be written to a
401 temporary file first.
402
403 The arguments given to |:RustExpand| will be passed to rustc.
404 This is largely intended for specifying various --cfg
405 configurations.
406
407 If ! is specified, the first argument is the expansion type to
408 pass to rustc --pretty. Otherwise it will default to
409 "expanded".
410
411 If |g:rustc_path| is defined, it is used as the path to rustc.
412 Otherwise it is assumed rustc can be found in $PATH.
413
Gregory Andersfc935942023-09-12 13:23:38 -0500414:RustEmitIr [args] *:RustEmitIr*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100415 Compiles the current file to LLVM IR and displays the results
416 in a new split. If the current file has unsaved changes, it
417 will be saved first using |:update|. If the current file is an
418 unnamed buffer, it will be written to a temporary file first.
419
420 The arguments given to |:RustEmitIr| will be passed to rustc.
421
422 If |g:rustc_path| is defined, it is used as the path to rustc.
423 Otherwise it is assumed rustc can be found in $PATH.
424
Gregory Andersfc935942023-09-12 13:23:38 -0500425:RustEmitAsm [args] *:RustEmitAsm*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100426 Compiles the current file to assembly and displays the results
427 in a new split. If the current file has unsaved changes, it
428 will be saved first using |:update|. If the current file is an
429 unnamed buffer, it will be written to a temporary file first.
430
431 The arguments given to |:RustEmitAsm| will be passed to rustc.
432
433 If |g:rustc_path| is defined, it is used as the path to rustc.
434 Otherwise it is assumed rustc can be found in $PATH.
435
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100436
Gregory Andersfc935942023-09-12 13:23:38 -0500437Running test(s)
438---------------
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100439
Gregory Andersfc935942023-09-12 13:23:38 -0500440:[N]RustTest[!] [options] *:RustTest*
441 Runs a test under the cursor when the current buffer is in a
442 cargo project with "cargo test" command. If the command did
443 not find any test function under the cursor, it stops with an
444 error message.
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100445
Gregory Andersfc935942023-09-12 13:23:38 -0500446 When N is given, adjust the size of the new window to N lines
447 or columns.
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100448
Gregory Andersfc935942023-09-12 13:23:38 -0500449 When ! is given, runs all tests regardless of current cursor
450 position.
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100451
Gregory Andersfc935942023-09-12 13:23:38 -0500452 When [options] is given, it is passed to "cargo" command
453 arguments.
454
455 When the current buffer is outside cargo project, the command
456 runs "rustc --test" command instead of "cargo test" as
457 fallback. All tests are run regardless of adding ! since there
458 is no way to run specific test function with rustc. [options]
459 is passed to "rustc" command arguments in the case.
460
461 Takes optional modifiers (see |<mods>|): >
462 :tab RustTest
463 :belowright 16RustTest
464 :leftabove vert 80RustTest
465<
466rust.vim Debugging
467------------------
468
469:RustInfo *:RustInfo*
470 Emits debugging info of the Vim Rust plugin.
471
472:RustInfoToClipboard *:RustInfoClipboard*
473 Saves debugging info of the Vim Rust plugin to the default
474 register.
475
476:RustInfoToFile [filename] *:RustInfoToFile*
Dominique Pellé960822a2023-09-24 23:07:39 +0200477 Saves debugging info of the Vim Rust plugin to the given file,
478 overwriting it.
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100479
480==============================================================================
Gregory Andersfc935942023-09-12 13:23:38 -0500481MAPPINGS *rust-mappings*
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100482
483This plugin defines mappings for |[[| and |]]| to support hanging indents.
484
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100485==============================================================================
Gregory Andersfc935942023-09-12 13:23:38 -0500486 vim:tw=78:sw=4:noet:ts=8:ft=help:norl: