5794: Simplify building a symbol hierarchy
 r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-08-18 12:32:32 +00:00 committed by GitHub
commit a3947129c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 18 deletions
crates/rust-analyzer/src
docs/dev

@ -271,24 +271,24 @@ pub(crate) fn handle_document_symbol(
};
parents.push((doc_symbol, symbol.parent));
}
let mut document_symbols = Vec::new();
// Constructs `document_symbols` from `parents`, in order from the end.
while let Some((node, parent)) = parents.pop() {
match parent {
None => document_symbols.push(node),
Some(i) => {
parents[i].0.children.get_or_insert_with(Vec::new).push(node);
}
}
}
fn reverse(symbols: &mut Vec<DocumentSymbol>) {
for sym in symbols.iter_mut() {
sym.children.as_mut().map(|c| reverse(c));
// Builds hierarchy from a flat list, in reverse order (so that indices
// makes sense)
let document_symbols = {
let mut acc = Vec::new();
while let Some((mut node, parent_idx)) = parents.pop() {
if let Some(children) = &mut node.children {
children.reverse();
}
let parent = match parent_idx {
None => &mut acc,
Some(i) => parents[i].0.children.get_or_insert_with(Vec::new),
};
parent.push(node);
}
symbols.reverse();
}
reverse(&mut document_symbols);
acc.reverse();
acc
};
let res = if snap.config.client_caps.hierarchical_symbols {
document_symbols.into()

@ -148,8 +148,13 @@ struct Foo {
Use boring and long names for local variables ([yay code completion](https://github.com/rust-analyzer/rust-analyzer/pull/4162#discussion_r417130973)).
The default name is a lowercased name of the type: `global_state: GlobalState`.
Avoid ad-hoc acronyms and contractions, but use the ones that exist consistently (`db`, `ctx`, `acc`).
The default name for "result of the function" local variable is `res`.
The default name for "I don't really care about the name" variable is `it`.
Default names:
* `res` -- "result of the function" local variable
* `it` -- I don't really care about the name
* `n_foo` -- number of foos
* `foo_idx` -- index of `foo`
# Collection types