From bb45aca9093ffe61cf444d538b3de737117c9a64 Mon Sep 17 00:00:00 2001 From: Leander Tentrup Date: Thu, 2 Apr 2020 14:34:51 +0200 Subject: [PATCH] Flatten nested highlight ranges during DFS traversal --- crates/ra_ide/src/syntax_highlighting.rs | 55 +++++++++++++++++-- .../ra_ide/src/syntax_highlighting/tests.rs | 16 ++++++ 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 7fc94d3cdb..eb1c54639d 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -24,7 +24,7 @@ use crate::{call_info::call_info_for_token, Analysis, FileId}; pub(crate) use html::highlight_as_html; pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct HighlightedRange { pub range: TextRange, pub highlight: Highlight, @@ -55,13 +55,55 @@ pub(crate) fn highlight( }; let mut bindings_shadow_count: FxHashMap = FxHashMap::default(); - let mut res = Vec::new(); + // 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 current_macro_call: Option = None; // Walk all nodes, keeping track of whether we are inside a macro or not. // 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); + } + } + } + }; + 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(), WalkEvent::Leave(it) => it.text_range(), @@ -77,7 +119,7 @@ pub(crate) fn highlight( WalkEvent::Enter(Some(mc)) => { current_macro_call = Some(mc.clone()); if let Some(range) = macro_call_range(&mc) { - res.push(HighlightedRange { + current.push(HighlightedRange { range, highlight: HighlightTag::Macro.into(), binding_hash: None, @@ -119,7 +161,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(&mut res, &sema, token, expanded).is_some() { + if highlight_injection(current, &sema, token, expanded).is_some() { continue; } } @@ -127,11 +169,12 @@ pub(crate) fn highlight( if let Some((highlight, binding_hash)) = highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight) { - res.push(HighlightedRange { range, highlight, binding_hash }); + current.push(HighlightedRange { range, highlight, binding_hash }); } } - res + assert_eq!(res.len(), 1, "after DFS traversal, the stack should only contain a single element"); + res.pop().unwrap() } fn macro_call_range(macro_call: &ast::MacroCall) -> Option { diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index 98c0307917..7f442bd0fe 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs @@ -131,3 +131,19 @@ fn test_ranges() { assert_eq!(&highlights[0].highlight.to_string(), "field.declaration"); } + +#[test] +fn test_flattening() { + let (analysis, file_id) = single_file(r#"#[cfg(feature = "foo")]"#); + + let highlights = analysis.highlight(file_id).unwrap(); + + // The source code snippet contains 2 nested highlights: + // 1) Attribute spanning the whole string + // 2) The string "foo" + // The resulting flattening splits the attribute range: + assert_eq!(highlights.len(), 3); + assert_eq!(&highlights[0].highlight.to_string(), "attribute"); + assert_eq!(&highlights[1].highlight.to_string(), "string_literal"); + assert_eq!(&highlights[2].highlight.to_string(), "attribute"); +}