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(-) (limited to 'crates') 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