diff options
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 32 | ||||
-rw-r--r-- | docs/dev/style.md | 9 |
2 files changed, 23 insertions, 18 deletions
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 69ab1b3b1..67ca9f055 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -271,24 +271,24 @@ pub(crate) fn handle_document_symbol( | |||
271 | }; | 271 | }; |
272 | parents.push((doc_symbol, symbol.parent)); | 272 | parents.push((doc_symbol, symbol.parent)); |
273 | } | 273 | } |
274 | let mut document_symbols = Vec::new(); | ||
275 | // Constructs `document_symbols` from `parents`, in order from the end. | ||
276 | while let Some((node, parent)) = parents.pop() { | ||
277 | match parent { | ||
278 | None => document_symbols.push(node), | ||
279 | Some(i) => { | ||
280 | parents[i].0.children.get_or_insert_with(Vec::new).push(node); | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | 274 | ||
285 | fn reverse(symbols: &mut Vec<DocumentSymbol>) { | 275 | // Builds hierarchy from a flat list, in reverse order (so that indices |
286 | for sym in symbols.iter_mut() { | 276 | // makes sense) |
287 | sym.children.as_mut().map(|c| reverse(c)); | 277 | let document_symbols = { |
278 | let mut acc = Vec::new(); | ||
279 | while let Some((mut node, parent_idx)) = parents.pop() { | ||
280 | if let Some(children) = &mut node.children { | ||
281 | children.reverse(); | ||
282 | } | ||
283 | let parent = match parent_idx { | ||
284 | None => &mut acc, | ||
285 | Some(i) => parents[i].0.children.get_or_insert_with(Vec::new), | ||
286 | }; | ||
287 | parent.push(node); | ||
288 | } | 288 | } |
289 | symbols.reverse(); | 289 | acc.reverse(); |
290 | } | 290 | acc |
291 | reverse(&mut document_symbols); | 291 | }; |
292 | 292 | ||
293 | let res = if snap.config.client_caps.hierarchical_symbols { | 293 | let res = if snap.config.client_caps.hierarchical_symbols { |
294 | document_symbols.into() | 294 | document_symbols.into() |
diff --git a/docs/dev/style.md b/docs/dev/style.md index 8effddcda..2454087e8 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md | |||
@@ -148,8 +148,13 @@ struct Foo { | |||
148 | Use boring and long names for local variables ([yay code completion](https://github.com/rust-analyzer/rust-analyzer/pull/4162#discussion_r417130973)). | 148 | Use boring and long names for local variables ([yay code completion](https://github.com/rust-analyzer/rust-analyzer/pull/4162#discussion_r417130973)). |
149 | The default name is a lowercased name of the type: `global_state: GlobalState`. | 149 | The default name is a lowercased name of the type: `global_state: GlobalState`. |
150 | Avoid ad-hoc acronyms and contractions, but use the ones that exist consistently (`db`, `ctx`, `acc`). | 150 | Avoid ad-hoc acronyms and contractions, but use the ones that exist consistently (`db`, `ctx`, `acc`). |
151 | The default name for "result of the function" local variable is `res`. | 151 | |
152 | The default name for "I don't really care about the name" variable is `it`. | 152 | Default names: |
153 | |||
154 | * `res` -- "result of the function" local variable | ||
155 | * `it` -- I don't really care about the name | ||
156 | * `n_foo` -- number of foos | ||
157 | * `foo_idx` -- index of `foo` | ||
153 | 158 | ||
154 | # Collection types | 159 | # Collection types |
155 | 160 | ||