From 29a846464b63259f5152d61a5520bffcc2cb8703 Mon Sep 17 00:00:00 2001 From: Leander Tentrup Date: Thu, 16 Apr 2020 14:29:58 +0200 Subject: Refactor flattening logic for highlighted syntax ranges --- crates/ra_ide/src/syntax_highlighting.rs | 139 +++++++++++++++++++------------ 1 file changed, 85 insertions(+), 54 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 7b15b82bd..e7d9bf696 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -31,6 +31,80 @@ pub struct HighlightedRange { pub binding_hash: Option, } +#[derive(Debug)] +struct HighlightedRangeStack { + stack: Vec>, +} + +/// We use a stack to implement the flattening logic for the highlighted +/// syntax ranges. +impl HighlightedRangeStack { + fn new() -> Self { + Self { stack: vec![Vec::new()] } + } + + fn push(&mut self) { + self.stack.push(Vec::new()); + } + + /// Flattens the highlighted ranges. + /// + /// For example `#[cfg(feature = "foo")]` contains the nested ranges: + /// 1) parent-range: Attribute [0, 23) + /// 2) child-range: String [16, 21) + /// + /// The following code implements the flattening, for our example this results to: + /// `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]` + fn pop(&mut self) { + let children = self.stack.pop().unwrap(); + let prev = self.stack.last_mut().unwrap(); + let needs_flattening = !children.is_empty() + && !prev.is_empty() + && children.first().unwrap().range.is_subrange(&prev.last().unwrap().range); + if !needs_flattening { + prev.extend(children); + } else { + let mut parent = prev.pop().unwrap(); + for ele in children { + assert!(ele.range.is_subrange(&parent.range)); + let mut cloned = parent.clone(); + parent.range = TextRange::from_to(parent.range.start(), ele.range.start()); + cloned.range = TextRange::from_to(ele.range.end(), cloned.range.end()); + if !parent.range.is_empty() { + prev.push(parent); + } + prev.push(ele); + parent = cloned; + } + if !parent.range.is_empty() { + prev.push(parent); + } + } + } + + fn add(&mut self, range: HighlightedRange) { + self.stack + .last_mut() + .expect("during DFS traversal, the stack must not be empty") + .push(range) + } + + fn flattened(mut self) -> Vec { + assert_eq!( + self.stack.len(), + 1, + "after DFS traversal, the stack should only contain a single element" + ); + let res = self.stack.pop().unwrap(); + // Check that ranges are sorted and disjoint + assert!(res + .iter() + .zip(res.iter().skip(1)) + .all(|(left, right)| left.range.end() <= right.range.start())); + res + } +} + pub(crate) fn highlight( db: &RootDatabase, file_id: FileId, @@ -57,7 +131,7 @@ pub(crate) fn highlight( let mut bindings_shadow_count: FxHashMap = FxHashMap::default(); // We use a stack for the DFS traversal below. // When we leave a node, the we use it to flatten the highlighted ranges. - let mut res: Vec> = vec![Vec::new()]; + let mut stack = HighlightedRangeStack::new(); let mut current_macro_call: Option = None; @@ -65,44 +139,9 @@ pub(crate) fn highlight( // If in macro, expand it first and highlight the expanded code. for event in root.preorder_with_tokens() { match &event { - WalkEvent::Enter(_) => res.push(Vec::new()), - WalkEvent::Leave(_) => { - /* Flattens the highlighted ranges. - * - * For example `#[cfg(feature = "foo")]` contains the nested ranges: - * 1) parent-range: Attribute [0, 23) - * 2) child-range: String [16, 21) - * - * The following code implements the flattening, for our example this results to: - * `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]` - */ - let children = res.pop().unwrap(); - let prev = res.last_mut().unwrap(); - let needs_flattening = !children.is_empty() - && !prev.is_empty() - && children.first().unwrap().range.is_subrange(&prev.last().unwrap().range); - if !needs_flattening { - prev.extend(children); - } else { - let mut parent = prev.pop().unwrap(); - for ele in children { - assert!(ele.range.is_subrange(&parent.range)); - let mut cloned = parent.clone(); - parent.range = TextRange::from_to(parent.range.start(), ele.range.start()); - cloned.range = TextRange::from_to(ele.range.end(), cloned.range.end()); - if !parent.range.is_empty() { - prev.push(parent); - } - prev.push(ele); - parent = cloned; - } - if !parent.range.is_empty() { - prev.push(parent); - } - } - } + WalkEvent::Enter(_) => stack.push(), + WalkEvent::Leave(_) => stack.pop(), }; - let current = res.last_mut().expect("during DFS traversal, the stack must not be empty"); let event_range = match &event { WalkEvent::Enter(it) => it.text_range(), @@ -119,7 +158,7 @@ pub(crate) fn highlight( WalkEvent::Enter(Some(mc)) => { current_macro_call = Some(mc.clone()); if let Some(range) = macro_call_range(&mc) { - current.push(HighlightedRange { + stack.add(HighlightedRange { range, highlight: HighlightTag::Macro.into(), binding_hash: None, @@ -161,7 +200,7 @@ pub(crate) fn highlight( if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) { let expanded = element_to_highlight.as_token().unwrap().clone(); - if highlight_injection(current, &sema, token, expanded).is_some() { + if highlight_injection(&mut stack, &sema, token, expanded).is_some() { continue; } } @@ -169,19 +208,11 @@ pub(crate) fn highlight( if let Some((highlight, binding_hash)) = highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight) { - current.push(HighlightedRange { range, highlight, binding_hash }); + stack.add(HighlightedRange { range, highlight, binding_hash }); } } - assert_eq!(res.len(), 1, "after DFS traversal, the stack should only contain a single element"); - let mut res = res.pop().unwrap(); - res.sort_by_key(|range| range.range.start()); - // Check that ranges are sorted and disjoint - assert!(res - .iter() - .zip(res.iter().skip(1)) - .all(|(left, right)| left.range.end() <= right.range.start())); - res + stack.flattened() } fn macro_call_range(macro_call: &ast::MacroCall) -> Option { @@ -358,7 +389,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight { } fn highlight_injection( - acc: &mut Vec, + acc: &mut HighlightedRangeStack, sema: &Semantics, literal: ast::RawString, expanded: SyntaxToken, @@ -373,7 +404,7 @@ fn highlight_injection( let (analysis, tmp_file_id) = Analysis::from_single_file(value); if let Some(range) = literal.open_quote_text_range() { - acc.push(HighlightedRange { + acc.add(HighlightedRange { range, highlight: HighlightTag::StringLiteral.into(), binding_hash: None, @@ -383,12 +414,12 @@ fn highlight_injection( for mut h in analysis.highlight(tmp_file_id).unwrap() { if let Some(r) = literal.map_range_up(h.range) { h.range = r; - acc.push(h) + acc.add(h) } } if let Some(range) = literal.close_quote_text_range() { - acc.push(HighlightedRange { + acc.add(HighlightedRange { range, highlight: HighlightTag::StringLiteral.into(), binding_hash: None, -- cgit v1.2.3