Split loop into orthogonal phases

This commit is contained in:
Aleksey Kladov 2020-02-27 11:56:42 +01:00
parent 9bb1718639
commit a2dbdbba00

View file

@ -69,7 +69,7 @@ pub(crate) fn highlight(
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
let mut res = Vec::new(); let mut res = Vec::new();
let mut in_macro_call = None; let mut current_macro_call: Option<ast::MacroCall> = None;
for event in root.preorder_with_tokens() { for event in root.preorder_with_tokens() {
let event_range = match &event { let event_range = match &event {
@ -81,58 +81,57 @@ pub(crate) fn highlight(
continue; continue;
} }
match event { match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) {
WalkEvent::Enter(node) => match node.kind() { WalkEvent::Enter(Some(mc)) => {
MACRO_CALL => { current_macro_call = Some(mc.clone());
in_macro_call = Some(node.clone()); if let Some(range) = highlight_macro(&mc) {
if let Some(range) = highlight_macro(node) { res.push(HighlightedRange {
res.push(HighlightedRange { range,
range, highlight: HighlightTag::Macro.into(),
highlight: HighlightTag::Macro.into(), binding_hash: None,
binding_hash: None, });
});
}
} }
_ if in_macro_call.is_some() => { continue;
if let Some(token) = node.as_token() { }
if let Some((highlight, binding_hash)) = WalkEvent::Leave(Some(mc)) => {
highlight_token_tree(&sema, &mut bindings_shadow_count, token.clone()) assert!(current_macro_call == Some(mc));
{ current_macro_call = None;
res.push(HighlightedRange { continue;
range: node.text_range(), }
highlight, _ => (),
binding_hash, }
});
} let node = match event {
} WalkEvent::Enter(it) => it,
} WalkEvent::Leave(_) => continue,
_ => { };
if let Some((highlight, binding_hash)) =
highlight_node(&sema, &mut bindings_shadow_count, node.clone()) if current_macro_call.is_some() {
{ if let Some(token) = node.into_token() {
res.push(HighlightedRange { if let Some((highlight, binding_hash)) =
range: node.text_range(), highlight_token_tree(&sema, &mut bindings_shadow_count, token.clone())
highlight, {
binding_hash, res.push(HighlightedRange {
}); range: token.text_range(),
} highlight,
} binding_hash,
}, });
WalkEvent::Leave(node) => {
if let Some(m) = in_macro_call.as_ref() {
if *m == node {
in_macro_call = None;
}
} }
} }
continue;
}
if let Some((highlight, binding_hash)) =
highlight_node(&sema, &mut bindings_shadow_count, node.clone())
{
res.push(HighlightedRange { range: node.text_range(), highlight, binding_hash });
} }
} }
res res
} }
fn highlight_macro(node: SyntaxElement) -> Option<TextRange> { fn highlight_macro(macro_call: &ast::MacroCall) -> Option<TextRange> {
let macro_call = ast::MacroCall::cast(node.as_node()?.clone())?;
let path = macro_call.path()?; let path = macro_call.path()?;
let name_ref = path.segment()?.name_ref()?; let name_ref = path.segment()?.name_ref()?;