diff options
Diffstat (limited to 'crates/ra_analysis/src/syntax_highlighting.rs')
-rw-r--r-- | crates/ra_analysis/src/syntax_highlighting.rs | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index 38219da71..35e153ca0 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs | |||
@@ -9,19 +9,19 @@ use crate::{ | |||
9 | 9 | ||
10 | pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { | 10 | pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { |
11 | let source_file = db.source_file(file_id); | 11 | let source_file = db.source_file(file_id); |
12 | let mut res = ra_editor::highlight(&source_file); | 12 | let mut res = ra_editor::highlight(source_file.syntax()); |
13 | for macro_call in source_file | 13 | for macro_call in source_file |
14 | .syntax() | 14 | .syntax() |
15 | .descendants() | 15 | .descendants() |
16 | .filter_map(ast::MacroCall::cast) | 16 | .filter_map(ast::MacroCall::cast) |
17 | { | 17 | { |
18 | if let Some(exp) = crate::macros::expand(db, file_id, macro_call) { | 18 | if let Some((off, exp)) = hir::MacroDef::ast_expand(macro_call) { |
19 | let mapped_ranges = ra_editor::highlight(exp.source_file()) | 19 | let mapped_ranges = ra_editor::highlight(exp.syntax().borrowed()) |
20 | .into_iter() | 20 | .into_iter() |
21 | .filter_map(|r| { | 21 | .filter_map(|r| { |
22 | let mapped_range = exp.map_range_back(r.range)?; | 22 | let mapped_range = exp.map_range_back(r.range)?; |
23 | let res = HighlightedRange { | 23 | let res = HighlightedRange { |
24 | range: mapped_range, | 24 | range: mapped_range + off, |
25 | tag: r.tag, | 25 | tag: r.tag, |
26 | }; | 26 | }; |
27 | Some(res) | 27 | Some(res) |
@@ -43,8 +43,9 @@ mod tests { | |||
43 | " | 43 | " |
44 | fn main() { | 44 | fn main() { |
45 | ctry!({ let x = 92; x}); | 45 | ctry!({ let x = 92; x}); |
46 | vec![{ let x = 92; x}]; | ||
46 | } | 47 | } |
47 | ", | 48 | ", |
48 | ); | 49 | ); |
49 | let highlights = analysis.highlight(file_id).unwrap(); | 50 | let highlights = analysis.highlight(file_id).unwrap(); |
50 | assert_eq_dbg( | 51 | assert_eq_dbg( |
@@ -53,10 +54,39 @@ mod tests { | |||
53 | HighlightedRange { range: [41; 46), tag: "macro" }, | 54 | HighlightedRange { range: [41; 46), tag: "macro" }, |
54 | HighlightedRange { range: [49; 52), tag: "keyword" }, | 55 | HighlightedRange { range: [49; 52), tag: "keyword" }, |
55 | HighlightedRange { range: [57; 59), tag: "literal" }, | 56 | HighlightedRange { range: [57; 59), tag: "literal" }, |
57 | HighlightedRange { range: [82; 86), tag: "macro" }, | ||
58 | HighlightedRange { range: [89; 92), tag: "keyword" }, | ||
59 | HighlightedRange { range: [97; 99), tag: "literal" }, | ||
56 | HighlightedRange { range: [49; 52), tag: "keyword" }, | 60 | HighlightedRange { range: [49; 52), tag: "keyword" }, |
57 | HighlightedRange { range: [53; 54), tag: "function" }, | 61 | HighlightedRange { range: [53; 54), tag: "function" }, |
58 | HighlightedRange { range: [57; 59), tag: "literal" }, | 62 | HighlightedRange { range: [57; 59), tag: "literal" }, |
59 | HighlightedRange { range: [61; 62), tag: "text" }]"#, | 63 | HighlightedRange { range: [61; 62), tag: "text" }, |
64 | HighlightedRange { range: [89; 92), tag: "keyword" }, | ||
65 | HighlightedRange { range: [93; 94), tag: "function" }, | ||
66 | HighlightedRange { range: [97; 99), tag: "literal" }, | ||
67 | HighlightedRange { range: [101; 102), tag: "text" }]"#, | ||
68 | &highlights, | ||
69 | ) | ||
70 | } | ||
71 | |||
72 | // FIXME: this test is not really necessary: artifact of the inital hacky | ||
73 | // macros implementation. | ||
74 | #[test] | ||
75 | fn highlight_query_group_macro() { | ||
76 | let (analysis, file_id) = single_file( | ||
77 | " | ||
78 | salsa::query_group! { | ||
79 | pub trait HirDatabase: SyntaxDatabase {} | ||
80 | } | ||
81 | ", | ||
82 | ); | ||
83 | let highlights = analysis.highlight(file_id).unwrap(); | ||
84 | assert_eq_dbg( | ||
85 | r#"[HighlightedRange { range: [20; 32), tag: "macro" }, | ||
86 | HighlightedRange { range: [13; 18), tag: "text" }, | ||
87 | HighlightedRange { range: [51; 54), tag: "keyword" }, | ||
88 | HighlightedRange { range: [55; 60), tag: "keyword" }, | ||
89 | HighlightedRange { range: [61; 72), tag: "function" }]"#, | ||
60 | &highlights, | 90 | &highlights, |
61 | ) | 91 | ) |
62 | } | 92 | } |