diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 55 | ||||
-rw-r--r-- | crates/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 7fc94d3cd..eb1c54639 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}; | |||
24 | pub(crate) use html::highlight_as_html; | 24 | pub(crate) use html::highlight_as_html; |
25 | pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}; | 25 | pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}; |
26 | 26 | ||
27 | #[derive(Debug)] | 27 | #[derive(Debug, Clone)] |
28 | pub struct HighlightedRange { | 28 | pub struct HighlightedRange { |
29 | pub range: TextRange, | 29 | pub range: TextRange, |
30 | pub highlight: Highlight, | 30 | pub highlight: Highlight, |
@@ -55,13 +55,55 @@ pub(crate) fn highlight( | |||
55 | }; | 55 | }; |
56 | 56 | ||
57 | let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); | 57 | let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); |
58 | let mut res = Vec::new(); | 58 | // We use a stack for the DFS traversal below. |
59 | // When we leave a node, the we use it to flatten the highlighted ranges. | ||
60 | let mut res: Vec<Vec<HighlightedRange>> = vec![Vec::new()]; | ||
59 | 61 | ||
60 | let mut current_macro_call: Option<ast::MacroCall> = None; | 62 | let mut current_macro_call: Option<ast::MacroCall> = None; |
61 | 63 | ||
62 | // Walk all nodes, keeping track of whether we are inside a macro or not. | 64 | // Walk all nodes, keeping track of whether we are inside a macro or not. |
63 | // If in macro, expand it first and highlight the expanded code. | 65 | // If in macro, expand it first and highlight the expanded code. |
64 | for event in root.preorder_with_tokens() { | 66 | for event in root.preorder_with_tokens() { |
67 | match &event { | ||
68 | WalkEvent::Enter(_) => res.push(Vec::new()), | ||
69 | WalkEvent::Leave(_) => { | ||
70 | /* Flattens the highlighted ranges. | ||
71 | * | ||
72 | * For example `#[cfg(feature = "foo")]` contains the nested ranges: | ||
73 | * 1) parent-range: Attribute [0, 23) | ||
74 | * 2) child-range: String [16, 21) | ||
75 | * | ||
76 | * The following code implements the flattening, for our example this results to: | ||
77 | * `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]` | ||
78 | */ | ||
79 | let children = res.pop().unwrap(); | ||
80 | let prev = res.last_mut().unwrap(); | ||
81 | let needs_flattening = !children.is_empty() | ||
82 | && !prev.is_empty() | ||
83 | && children.first().unwrap().range.is_subrange(&prev.last().unwrap().range); | ||
84 | if !needs_flattening { | ||
85 | prev.extend(children); | ||
86 | } else { | ||
87 | let mut parent = prev.pop().unwrap(); | ||
88 | for ele in children { | ||
89 | assert!(ele.range.is_subrange(&parent.range)); | ||
90 | let mut cloned = parent.clone(); | ||
91 | parent.range = TextRange::from_to(parent.range.start(), ele.range.start()); | ||
92 | cloned.range = TextRange::from_to(ele.range.end(), cloned.range.end()); | ||
93 | if !parent.range.is_empty() { | ||
94 | prev.push(parent); | ||
95 | } | ||
96 | prev.push(ele); | ||
97 | parent = cloned; | ||
98 | } | ||
99 | if !parent.range.is_empty() { | ||
100 | prev.push(parent); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | }; | ||
105 | let current = res.last_mut().expect("during DFS traversal, the stack must not be empty"); | ||
106 | |||
65 | let event_range = match &event { | 107 | let event_range = match &event { |
66 | WalkEvent::Enter(it) => it.text_range(), | 108 | WalkEvent::Enter(it) => it.text_range(), |
67 | WalkEvent::Leave(it) => it.text_range(), | 109 | WalkEvent::Leave(it) => it.text_range(), |
@@ -77,7 +119,7 @@ pub(crate) fn highlight( | |||
77 | WalkEvent::Enter(Some(mc)) => { | 119 | WalkEvent::Enter(Some(mc)) => { |
78 | current_macro_call = Some(mc.clone()); | 120 | current_macro_call = Some(mc.clone()); |
79 | if let Some(range) = macro_call_range(&mc) { | 121 | if let Some(range) = macro_call_range(&mc) { |
80 | res.push(HighlightedRange { | 122 | current.push(HighlightedRange { |
81 | range, | 123 | range, |
82 | highlight: HighlightTag::Macro.into(), | 124 | highlight: HighlightTag::Macro.into(), |
83 | binding_hash: None, | 125 | binding_hash: None, |
@@ -119,7 +161,7 @@ pub(crate) fn highlight( | |||
119 | 161 | ||
120 | if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) { | 162 | if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) { |
121 | let expanded = element_to_highlight.as_token().unwrap().clone(); | 163 | let expanded = element_to_highlight.as_token().unwrap().clone(); |
122 | if highlight_injection(&mut res, &sema, token, expanded).is_some() { | 164 | if highlight_injection(current, &sema, token, expanded).is_some() { |
123 | continue; | 165 | continue; |
124 | } | 166 | } |
125 | } | 167 | } |
@@ -127,11 +169,12 @@ pub(crate) fn highlight( | |||
127 | if let Some((highlight, binding_hash)) = | 169 | if let Some((highlight, binding_hash)) = |
128 | highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight) | 170 | highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight) |
129 | { | 171 | { |
130 | res.push(HighlightedRange { range, highlight, binding_hash }); | 172 | current.push(HighlightedRange { range, highlight, binding_hash }); |
131 | } | 173 | } |
132 | } | 174 | } |
133 | 175 | ||
134 | res | 176 | assert_eq!(res.len(), 1, "after DFS traversal, the stack should only contain a single element"); |
177 | res.pop().unwrap() | ||
135 | } | 178 | } |
136 | 179 | ||
137 | fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { | 180 | fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { |
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index 98c030791..7f442bd0f 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() { | |||
131 | 131 | ||
132 | assert_eq!(&highlights[0].highlight.to_string(), "field.declaration"); | 132 | assert_eq!(&highlights[0].highlight.to_string(), "field.declaration"); |
133 | } | 133 | } |
134 | |||
135 | #[test] | ||
136 | fn test_flattening() { | ||
137 | let (analysis, file_id) = single_file(r#"#[cfg(feature = "foo")]"#); | ||
138 | |||
139 | let highlights = analysis.highlight(file_id).unwrap(); | ||
140 | |||
141 | // The source code snippet contains 2 nested highlights: | ||
142 | // 1) Attribute spanning the whole string | ||
143 | // 2) The string "foo" | ||
144 | // The resulting flattening splits the attribute range: | ||
145 | assert_eq!(highlights.len(), 3); | ||
146 | assert_eq!(&highlights[0].highlight.to_string(), "attribute"); | ||
147 | assert_eq!(&highlights[1].highlight.to_string(), "string_literal"); | ||
148 | assert_eq!(&highlights[2].highlight.to_string(), "attribute"); | ||
149 | } | ||