aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/syntax_highlighting.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting.rs')
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs59
1 files changed, 54 insertions, 5 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index d833a816b..83d161f45 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};
24pub(crate) use html::highlight_as_html; 24pub(crate) use html::highlight_as_html;
25pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}; 25pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag};
26 26
27#[derive(Debug)] 27#[derive(Debug, Clone)]
28pub struct HighlightedRange { 28pub 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,10 +169,17 @@ 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
176 assert_eq!(res.len(), 1, "after DFS traversal, the stack should only contain a single element");
177 let res = res.pop().unwrap();
178 // Check that ranges are sorted and disjoint
179 assert!(res
180 .iter()
181 .zip(res.iter().skip(1))
182 .all(|(left, right)| left.range.end() <= right.range.start()));
134 res 183 res
135} 184}
136 185