]> git.lizzy.rs Git - rust.git/blob - docs/user/features.md
Add document in features.md
[rust.git] / docs / user / features.md
1 This documents is an index of features that rust-analyzer language server
2 provides. Shortcuts are for the default VS Code layout. If there's no shortcut,
3 you can use <kbd>Ctrl+Shift+P</kbd> to search for the corresponding action.
4
5 ### Workspace Symbol <kbd>ctrl+t</kbd>
6
7 Uses fuzzy-search to find types, modules and function by name across your
8 project and dependencies. This is **the** most useful feature, which improves code
9 navigation tremendously. It mostly works on top of the built-in LSP
10 functionality, however `#` and `*` symbols can be used to narrow down the
11 search. Specifically,
12
13 - `Foo` searches for `Foo` type in the current workspace
14 - `foo#` searches for `foo` function in the current workspace
15 - `Foo*` searches for `Foo` type among dependencies, including `stdlib`
16 - `foo#*` searches for `foo` function among dependencies.
17
18 That is, `#` switches from "types" to all symbols, `*` switches from the current
19 workspace to dependencies.
20
21 ### Document Symbol <kbd>ctrl+shift+o</kbd>
22
23 Provides a tree of the symbols defined in the file. Can be used to
24
25 * fuzzy search symbol in a file (super useful)
26 * draw breadcrumbs to describe the context around the cursor
27 * draw outline of the file
28
29 ### On Typing Assists
30
31 Some features trigger on typing certain characters:
32
33 - typing `let =` tries to smartly add `;` if `=` is followed by an existing expression.
34 - Enter inside comments automatically inserts `///`
35 - typing `.` in a chain method call auto-indents
36
37 ### Extend Selection
38
39 Extends the current selection to the encompassing syntactic construct
40 (expression, statement, item, module, etc). It works with multiple cursors. This
41 is a relatively new feature of LSP:
42 https://github.com/Microsoft/language-server-protocol/issues/613, check your
43 editor's LSP library to see if this feature is supported.
44
45 ### Go to Definition
46
47 Navigates to the definition of an identifier.
48
49 ### Go to Implementation
50
51 Navigates to the impl block of structs, enums or traits. Also implemented as a code lens.
52
53 ### Go to Type Defintion
54
55 Navigates to the type of an identifier.
56
57 ### Commands <kbd>ctrl+shift+p</kbd>
58
59 #### Run
60
61 Shows popup suggesting to run a test/benchmark/binary **at the current cursor
62 location**. Super useful for repeatedly running just a single test. Do bind this
63 to a shortcut!
64
65 #### Parent Module
66
67 Navigates to the parent module of the current module.
68
69 #### Matching Brace
70
71 If the cursor is on any brace (`<>(){}[]`) which is a part of a brace-pair,
72 moves cursor to the matching brace. It uses the actual parser to determine
73 braces, so it won't confuse generics with comparisons.
74
75 #### Join Lines
76
77 Join selected lines into one, smartly fixing up whitespace and trailing commas.
78
79 #### Show Syntax Tree
80
81 Shows the parse tree of the current file. It exists mostly for debugging
82 rust-analyzer itself.
83
84 #### Expand Macro Recursively
85
86 Shows the full macro expansion of the macro at current cursor.
87
88 #### Status
89
90 Shows internal statistic about memory usage of rust-analyzer
91
92 #### Run garbage collection
93
94 Manually triggers GC
95
96 #### Start Cargo Watch
97
98 Start `cargo watch` for live error highlighting. Will prompt to install if it's not already installed.
99
100 #### Stop Cargo Watch
101
102 Stop `cargo watch`
103
104 ### Assists (Code Actions)
105
106 Assists, or code actions, are small local refactorings, available in a particular context.
107 They are usually triggered by a shortcut or by clicking a light bulb icon in the editor.
108
109 See [assists.md](./assists.md) for the list of available assists.
110
111 ### Magic Completions
112
113 In addition to usual reference completion, rust-analyzer provides some ✨magic✨
114 completions as well:
115
116 Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor
117 is placed at the appropriate position. Even though `if` is easy to type, you
118 still want to complete it, to get ` { }` for free! `return` is inserted with a
119 space or `;` depending on the return type of the function.
120
121 When completing a function call, `()` are automatically inserted. If function
122 takes arguments, cursor is positioned inside the parenthesis.
123
124 There are postifx completions, which can be triggerd by typing something like
125 `foo().if`. The word after `.` determines postifx completion, possible variants are:
126
127 - `expr.if` -> `if expr {}`
128 - `expr.match` -> `match expr {}`
129 - `expr.while` -> `while expr {}`
130 - `expr.ref` -> `&expr`
131 - `expr.refm` -> `&mut expr`
132 - `expr.not` -> `!expr`
133 - `expr.dbg` -> `dbg!(expr)`
134
135 There also snippet completions:
136
137 #### Inside Expressions
138
139 - `pd` -> `println!("{:?}")`
140 - `ppd` -> `println!("{:#?}")`
141
142 #### Inside Modules
143
144 - `tfn` -> `#[test] fn f(){}`
145
146 ### Code highlighting
147
148 Experimental feature to let rust-analyzer highlight Rust code instead of using the
149 default highlighter.
150
151 #### Rainbow highlighting
152
153 Experimental feature that, given code highlighting using rust-analyzer is
154 active, will pick unique colors for identifiers.