Simplify building a symbol hierarchy

This commit is contained in:
Aleksey Kladov 2020-08-18 14:27:17 +02:00
parent 2252a65f23
commit e01cfe2b45

View File

@ -271,24 +271,24 @@ pub(crate) fn handle_document_symbol(
}; };
parents.push((doc_symbol, symbol.parent)); 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>) { // Builds hierarchy from a flat list, in reverse order (so that indices
for sym in symbols.iter_mut() { // makes sense)
sym.children.as_mut().map(|c| reverse(c)); 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(); acc.reverse();
} acc
reverse(&mut document_symbols); };
let res = if snap.config.client_caps.hierarchical_symbols { let res = if snap.config.client_caps.hierarchical_symbols {
document_symbols.into() document_symbols.into()