From 819bbd08645a85cbf31025b31bd4daf69f821d20 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Feb 2020 11:37:21 +0100 Subject: Minor cleanup --- crates/ra_ide/src/syntax_highlighting.rs | 33 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 5f11b091c..22d0ed3e6 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -46,32 +46,35 @@ fn is_control_keyword(kind: SyntaxKind) -> bool { pub(crate) fn highlight( db: &RootDatabase, file_id: FileId, - range: Option, + range_to_highlight: Option, ) -> Vec { let _p = profile("highlight"); let sema = Semantics::new(db); - let root = sema.parse(file_id).syntax().clone(); + + // Determine the root based on the given range. + let (root, range_to_highlight) = { + let source_file = sema.parse(file_id); + match range_to_highlight { + Some(range) => { + let node = match source_file.syntax().covering_element(range) { + NodeOrToken::Node(it) => it, + NodeOrToken::Token(it) => it.parent(), + }; + (node, range) + } + None => (source_file.syntax().clone(), source_file.syntax().text_range()), + } + }; let mut bindings_shadow_count: FxHashMap = FxHashMap::default(); let mut res = Vec::new(); let mut in_macro_call = None; - // Determine the root based on the given range. - let (root, highlight_range) = if let Some(range) = range { - let root = match root.covering_element(range) { - NodeOrToken::Node(node) => node, - NodeOrToken::Token(token) => token.parent(), - }; - (root, range) - } else { - (root.clone(), root.text_range()) - }; - for event in root.preorder_with_tokens() { match event { WalkEvent::Enter(node) => { - if node.text_range().intersection(&highlight_range).is_none() { + if node.text_range().intersection(&range_to_highlight).is_none() { continue; } @@ -115,7 +118,7 @@ pub(crate) fn highlight( } } WalkEvent::Leave(node) => { - if node.text_range().intersection(&highlight_range).is_none() { + if node.text_range().intersection(&range_to_highlight).is_none() { continue; } -- cgit v1.2.3 From 9bb17186396b1bdad7b443135165575e81810be3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Feb 2020 11:39:54 +0100 Subject: Cleanup --- crates/ra_ide/src/syntax_highlighting.rs | 71 +++++++++++++++----------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 22d0ed3e6..1b6eb1174 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -72,41 +72,31 @@ pub(crate) fn highlight( let mut in_macro_call = None; for event in root.preorder_with_tokens() { - match event { - WalkEvent::Enter(node) => { - if node.text_range().intersection(&range_to_highlight).is_none() { - continue; - } + let event_range = match &event { + WalkEvent::Enter(it) => it.text_range(), + WalkEvent::Leave(it) => it.text_range(), + }; - match node.kind() { - MACRO_CALL => { - in_macro_call = Some(node.clone()); - if let Some(range) = highlight_macro(node) { - res.push(HighlightedRange { - range, - highlight: HighlightTag::Macro.into(), - binding_hash: None, - }); - } - } - _ if in_macro_call.is_some() => { - if let Some(token) = node.as_token() { - if let Some((highlight, binding_hash)) = highlight_token_tree( - &sema, - &mut bindings_shadow_count, - token.clone(), - ) { - res.push(HighlightedRange { - range: node.text_range(), - highlight, - binding_hash, - }); - } - } + if event_range.intersection(&range_to_highlight).is_none() { + continue; + } + + match event { + WalkEvent::Enter(node) => match node.kind() { + MACRO_CALL => { + in_macro_call = Some(node.clone()); + if let Some(range) = highlight_macro(node) { + res.push(HighlightedRange { + range, + highlight: HighlightTag::Macro.into(), + binding_hash: None, + }); } - _ => { + } + _ if in_macro_call.is_some() => { + if let Some(token) = node.as_token() { if let Some((highlight, binding_hash)) = - highlight_node(&sema, &mut bindings_shadow_count, node.clone()) + highlight_token_tree(&sema, &mut bindings_shadow_count, token.clone()) { res.push(HighlightedRange { range: node.text_range(), @@ -116,12 +106,19 @@ pub(crate) fn highlight( } } } - } - WalkEvent::Leave(node) => { - if node.text_range().intersection(&range_to_highlight).is_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, + }); + } } - + }, + WalkEvent::Leave(node) => { if let Some(m) = in_macro_call.as_ref() { if *m == node { in_macro_call = None; -- cgit v1.2.3 From a2dbdbba00376305a0cbcd98cd36cd53d5139d96 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Feb 2020 11:56:42 +0100 Subject: Split loop into orthogonal phases --- crates/ra_ide/src/syntax_highlighting.rs | 87 ++++++++++++++++---------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 1b6eb1174..e8ca7d652 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -69,7 +69,7 @@ pub(crate) fn highlight( let mut bindings_shadow_count: FxHashMap = FxHashMap::default(); let mut res = Vec::new(); - let mut in_macro_call = None; + let mut current_macro_call: Option = None; for event in root.preorder_with_tokens() { let event_range = match &event { @@ -81,58 +81,57 @@ pub(crate) fn highlight( continue; } - match event { - WalkEvent::Enter(node) => match node.kind() { - MACRO_CALL => { - in_macro_call = Some(node.clone()); - if let Some(range) = highlight_macro(node) { - res.push(HighlightedRange { - range, - highlight: HighlightTag::Macro.into(), - binding_hash: None, - }); - } + match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) { + WalkEvent::Enter(Some(mc)) => { + current_macro_call = Some(mc.clone()); + if let Some(range) = highlight_macro(&mc) { + res.push(HighlightedRange { + range, + highlight: HighlightTag::Macro.into(), + binding_hash: None, + }); } - _ if in_macro_call.is_some() => { - if let Some(token) = node.as_token() { - if let Some((highlight, binding_hash)) = - highlight_token_tree(&sema, &mut bindings_shadow_count, token.clone()) - { - res.push(HighlightedRange { - range: node.text_range(), - highlight, - binding_hash, - }); - } - } - } - _ => { - 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, - }); - } - } - }, - WalkEvent::Leave(node) => { - if let Some(m) = in_macro_call.as_ref() { - if *m == node { - in_macro_call = None; - } + continue; + } + WalkEvent::Leave(Some(mc)) => { + assert!(current_macro_call == Some(mc)); + current_macro_call = None; + continue; + } + _ => (), + } + + let node = match event { + WalkEvent::Enter(it) => it, + WalkEvent::Leave(_) => continue, + }; + + if current_macro_call.is_some() { + if let Some(token) = node.into_token() { + if let Some((highlight, binding_hash)) = + highlight_token_tree(&sema, &mut bindings_shadow_count, token.clone()) + { + res.push(HighlightedRange { + range: token.text_range(), + highlight, + binding_hash, + }); } } + 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 } -fn highlight_macro(node: SyntaxElement) -> Option { - let macro_call = ast::MacroCall::cast(node.as_node()?.clone())?; +fn highlight_macro(macro_call: &ast::MacroCall) -> Option { let path = macro_call.path()?; let name_ref = path.segment()?.name_ref()?; -- cgit v1.2.3