diff options
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting.rs')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 59 |
1 files changed, 55 insertions, 4 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 796f0e545..3a5cbee9b 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs | |||
@@ -12,11 +12,12 @@ use ra_ide_db::{ | |||
12 | }; | 12 | }; |
13 | use ra_prof::profile; | 13 | use ra_prof::profile; |
14 | use ra_syntax::{ | 14 | use ra_syntax::{ |
15 | ast, AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxKind::*, TextRange, WalkEvent, T, | 15 | ast, AstNode, AstToken, Direction, NodeOrToken, SyntaxElement, SyntaxKind::*, SyntaxToken, |
16 | TextRange, WalkEvent, T, | ||
16 | }; | 17 | }; |
17 | use rustc_hash::FxHashMap; | 18 | use rustc_hash::FxHashMap; |
18 | 19 | ||
19 | use crate::{references::classify_name_ref, FileId}; | 20 | use crate::{call_info::call_info_for_token, references::classify_name_ref, Analysis, FileId}; |
20 | 21 | ||
21 | pub(crate) use html::highlight_as_html; | 22 | pub(crate) use html::highlight_as_html; |
22 | pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}; | 23 | pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}; |
@@ -94,11 +95,12 @@ pub(crate) fn highlight( | |||
94 | WalkEvent::Enter(it) => it, | 95 | WalkEvent::Enter(it) => it, |
95 | WalkEvent::Leave(_) => continue, | 96 | WalkEvent::Leave(_) => continue, |
96 | }; | 97 | }; |
98 | |||
97 | let range = element.text_range(); | 99 | let range = element.text_range(); |
98 | 100 | ||
99 | let element_to_highlight = if current_macro_call.is_some() { | 101 | let element_to_highlight = if current_macro_call.is_some() { |
100 | // Inside a macro -- expand it first | 102 | // Inside a macro -- expand it first |
101 | let token = match element.into_token() { | 103 | let token = match element.clone().into_token() { |
102 | Some(it) if it.parent().kind() == TOKEN_TREE => it, | 104 | Some(it) if it.parent().kind() == TOKEN_TREE => it, |
103 | _ => continue, | 105 | _ => continue, |
104 | }; | 106 | }; |
@@ -110,9 +112,17 @@ pub(crate) fn highlight( | |||
110 | _ => token.into(), | 112 | _ => token.into(), |
111 | } | 113 | } |
112 | } else { | 114 | } else { |
113 | element | 115 | element.clone() |
114 | }; | 116 | }; |
115 | 117 | ||
118 | if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) { | ||
119 | let expanded = element_to_highlight.as_token().unwrap().clone(); | ||
120 | if highlight_injection(&mut res, &sema, token, expanded).is_some() { | ||
121 | eprintln!("res = {:?}", res); | ||
122 | continue; | ||
123 | } | ||
124 | } | ||
125 | |||
116 | if let Some((highlight, binding_hash)) = | 126 | if let Some((highlight, binding_hash)) = |
117 | highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight) | 127 | highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight) |
118 | { | 128 | { |
@@ -281,3 +291,44 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight { | |||
281 | _ => default, | 291 | _ => default, |
282 | } | 292 | } |
283 | } | 293 | } |
294 | |||
295 | fn highlight_injection( | ||
296 | acc: &mut Vec<HighlightedRange>, | ||
297 | sema: &Semantics<RootDatabase>, | ||
298 | literal: ast::RawString, | ||
299 | expanded: SyntaxToken, | ||
300 | ) -> Option<()> { | ||
301 | let call_info = call_info_for_token(&sema, expanded)?; | ||
302 | let idx = call_info.active_parameter?; | ||
303 | let name = call_info.signature.parameter_names.get(idx)?; | ||
304 | if name != "ra_fixture" { | ||
305 | return None; | ||
306 | } | ||
307 | let value = literal.value()?; | ||
308 | let (analysis, tmp_file_id) = Analysis::from_single_file(value); | ||
309 | |||
310 | if let Some(range) = literal.open_quote_text_range() { | ||
311 | acc.push(HighlightedRange { | ||
312 | range, | ||
313 | highlight: HighlightTag::LiteralString.into(), | ||
314 | binding_hash: None, | ||
315 | }) | ||
316 | } | ||
317 | |||
318 | for mut h in analysis.highlight(tmp_file_id).unwrap() { | ||
319 | if let Some(r) = literal.map_range_up(h.range) { | ||
320 | h.range = r; | ||
321 | acc.push(h) | ||
322 | } | ||
323 | } | ||
324 | |||
325 | if let Some(range) = literal.close_quote_text_range() { | ||
326 | acc.push(HighlightedRange { | ||
327 | range, | ||
328 | highlight: HighlightTag::LiteralString.into(), | ||
329 | binding_hash: None, | ||
330 | }) | ||
331 | } | ||
332 | |||
333 | Some(()) | ||
334 | } | ||