diff options
Diffstat (limited to 'docs/user/features.md')
-rw-r--r-- | docs/user/features.md | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/docs/user/features.md b/docs/user/features.md new file mode 100644 index 000000000..5df606aee --- /dev/null +++ b/docs/user/features.md | |||
@@ -0,0 +1,168 @@ | |||
1 | This documents is an index of features that rust-analyzer language server provides. | ||
2 | |||
3 | ### Go to symbol in workspace <kbd>ctrl+t</kbd> | ||
4 | |||
5 | It mostly works on top of the built-in LSP functionality, however `#` and `*` | ||
6 | symbols can be used to narrow down the search. Specifically, | ||
7 | |||
8 | - `#Foo` searches for `Foo` type in the current workspace | ||
9 | - `#foo#` searches for `foo` function in the current workspace | ||
10 | - `#Foo*` searches for `Foo` type among dependencies, excluding `stdlib` | ||
11 | - `#foo#*` searches for `foo` function among dependencies. | ||
12 | |||
13 | That is, `#` switches from "types" to all symbols, `*` switches from the current | ||
14 | workspace to dependencies. | ||
15 | |||
16 | ### Commands <kbd>ctrl+shift+p</kbd> | ||
17 | |||
18 | #### Show Rust Syntax Tree | ||
19 | |||
20 | Shows the parse tree of the current file. It exists mostly for debugging | ||
21 | rust-analyzer itself. | ||
22 | |||
23 | #### Extend Selection | ||
24 | |||
25 | Extends the current selection to the encompassing syntactic construct | ||
26 | (expression, statement, item, module, etc). It works with multiple cursors. Do | ||
27 | bind this command to a key, its super-useful! Expected to be upstreamed to LSP soonish: | ||
28 | https://github.com/Microsoft/language-server-protocol/issues/613 | ||
29 | |||
30 | #### Matching Brace | ||
31 | |||
32 | If the cursor is on any brace (`<>(){}[]`) which is a part of a brace-pair, | ||
33 | moves cursor to the matching brace. It uses the actual parser to determine | ||
34 | braces, so it won't confuse generics with comparisons. | ||
35 | |||
36 | #### Parent Module | ||
37 | |||
38 | Navigates to the parent module of the current module. | ||
39 | |||
40 | #### Join Lines | ||
41 | |||
42 | Join selected lines into one, smartly fixing up whitespace and trailing commas. | ||
43 | |||
44 | #### Run | ||
45 | |||
46 | Shows popup suggesting to run a test/benchmark/binary **at the current cursor | ||
47 | location**. Super useful for repeatedly running just a single test. Do bind this | ||
48 | to a shortcut! | ||
49 | |||
50 | |||
51 | ### On Typing Assists | ||
52 | |||
53 | Some features trigger on typing certain characters: | ||
54 | |||
55 | - typing `let =` tries to smartly add `;` if `=` is followed by an existing expression. | ||
56 | - Enter inside comments automatically inserts `///` | ||
57 | - typing `.` in a chain method call auto-indents | ||
58 | |||
59 | |||
60 | |||
61 | |||
62 | |||
63 | ### Code Actions (Assists) | ||
64 | |||
65 | These are triggered in a particular context via light bulb. We use custom code on | ||
66 | the VS Code side to be able to position cursor. | ||
67 | |||
68 | |||
69 | - Flip `,` | ||
70 | |||
71 | ```rust | ||
72 | // before: | ||
73 | fn foo(x: usize,<|> dim: (usize, usize)) | ||
74 | // after: | ||
75 | fn foo(dim: (usize, usize), x: usize) | ||
76 | ``` | ||
77 | |||
78 | - Add `#[derive]` | ||
79 | |||
80 | ```rust | ||
81 | // before: | ||
82 | struct Foo { | ||
83 | <|>x: i32 | ||
84 | } | ||
85 | // after: | ||
86 | #[derive(<|>)] | ||
87 | struct Foo { | ||
88 | x: i32 | ||
89 | } | ||
90 | ``` | ||
91 | |||
92 | - Add `impl` | ||
93 | |||
94 | ```rust | ||
95 | // before: | ||
96 | struct Foo<'a, T: Debug> { | ||
97 | <|>t: T | ||
98 | } | ||
99 | // after: | ||
100 | struct Foo<'a, T: Debug> { | ||
101 | t: T | ||
102 | } | ||
103 | |||
104 | impl<'a, T: Debug> Foo<'a, T> { | ||
105 | <|> | ||
106 | } | ||
107 | ``` | ||
108 | |||
109 | - Change visibility | ||
110 | |||
111 | ```rust | ||
112 | // before: | ||
113 | fn<|> foo() {} | ||
114 | |||
115 | // after | ||
116 | pub(crate) fn foo() {} | ||
117 | ``` | ||
118 | |||
119 | - Introduce variable: | ||
120 | |||
121 | ```rust | ||
122 | // before: | ||
123 | fn foo() { | ||
124 | foo(<|>1 + 1<|>); | ||
125 | } | ||
126 | |||
127 | // after: | ||
128 | fn foo() { | ||
129 | let var_name = 1 + 1; | ||
130 | foo(var_name); | ||
131 | } | ||
132 | ``` | ||
133 | |||
134 | - Replace if-let with match: | ||
135 | |||
136 | ```rust | ||
137 | // before: | ||
138 | impl VariantData { | ||
139 | pub fn is_struct(&self) -> bool { | ||
140 | if <|>let VariantData::Struct(..) = *self { | ||
141 | true | ||
142 | } else { | ||
143 | false | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | |||
148 | // after: | ||
149 | impl VariantData { | ||
150 | pub fn is_struct(&self) -> bool { | ||
151 | <|>match *self { | ||
152 | VariantData::Struct(..) => true, | ||
153 | _ => false, | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | ``` | ||
158 | |||
159 | - Split import | ||
160 | |||
161 | ```rust | ||
162 | // before: | ||
163 | use algo:<|>:visitor::{Visitor, visit}; | ||
164 | //after: | ||
165 | use algo::{<|>visitor::{Visitor, visit}}; | ||
166 | ``` | ||
167 | |||
168 | |||