diff options
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/html.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/injection.rs | 168 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tags.rs | 8 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tests.rs | 123 |
4 files changed, 275 insertions, 26 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index edfe61f39..5bada6252 100644 --- a/crates/ra_ide/src/syntax_highlighting/html.rs +++ b/crates/ra_ide/src/syntax_highlighting/html.rs | |||
@@ -69,6 +69,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
69 | .string_literal { color: #CC9393; } | 69 | .string_literal { color: #CC9393; } |
70 | .field { color: #94BFF3; } | 70 | .field { color: #94BFF3; } |
71 | .function { color: #93E0E3; } | 71 | .function { color: #93E0E3; } |
72 | .function.unsafe { color: #BC8383; } | ||
73 | .operator.unsafe { color: #BC8383; } | ||
72 | .parameter { color: #94BFF3; } | 74 | .parameter { color: #94BFF3; } |
73 | .text { color: #DCDCCC; } | 75 | .text { color: #DCDCCC; } |
74 | .type { color: #7CB8BB; } | 76 | .type { color: #7CB8BB; } |
diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs new file mode 100644 index 000000000..3575a0fc6 --- /dev/null +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs | |||
@@ -0,0 +1,168 @@ | |||
1 | //! Syntax highlighting injections such as highlighting of documentation tests. | ||
2 | |||
3 | use std::{collections::BTreeMap, convert::TryFrom}; | ||
4 | |||
5 | use ast::{HasQuotes, HasStringValue}; | ||
6 | use hir::Semantics; | ||
7 | use ra_syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; | ||
8 | use stdx::SepBy; | ||
9 | |||
10 | use crate::{call_info::ActiveParameter, Analysis, HighlightTag, HighlightedRange, RootDatabase}; | ||
11 | |||
12 | use super::HighlightedRangeStack; | ||
13 | |||
14 | pub(super) fn highlight_injection( | ||
15 | acc: &mut HighlightedRangeStack, | ||
16 | sema: &Semantics<RootDatabase>, | ||
17 | literal: ast::RawString, | ||
18 | expanded: SyntaxToken, | ||
19 | ) -> Option<()> { | ||
20 | let active_parameter = ActiveParameter::at_token(&sema, expanded)?; | ||
21 | if !active_parameter.name.starts_with("ra_fixture") { | ||
22 | return None; | ||
23 | } | ||
24 | let value = literal.value()?; | ||
25 | let (analysis, tmp_file_id) = Analysis::from_single_file(value); | ||
26 | |||
27 | if let Some(range) = literal.open_quote_text_range() { | ||
28 | acc.add(HighlightedRange { | ||
29 | range, | ||
30 | highlight: HighlightTag::StringLiteral.into(), | ||
31 | binding_hash: None, | ||
32 | }) | ||
33 | } | ||
34 | |||
35 | for mut h in analysis.highlight(tmp_file_id).unwrap() { | ||
36 | if let Some(r) = literal.map_range_up(h.range) { | ||
37 | h.range = r; | ||
38 | acc.add(h) | ||
39 | } | ||
40 | } | ||
41 | |||
42 | if let Some(range) = literal.close_quote_text_range() { | ||
43 | acc.add(HighlightedRange { | ||
44 | range, | ||
45 | highlight: HighlightTag::StringLiteral.into(), | ||
46 | binding_hash: None, | ||
47 | }) | ||
48 | } | ||
49 | |||
50 | Some(()) | ||
51 | } | ||
52 | |||
53 | /// Mapping from extracted documentation code to original code | ||
54 | type RangesMap = BTreeMap<TextSize, TextSize>; | ||
55 | |||
56 | /// Extracts Rust code from documentation comments as well as a mapping from | ||
57 | /// the extracted source code back to the original source ranges. | ||
58 | /// Lastly, a vector of new comment highlight ranges (spanning only the | ||
59 | /// comment prefix) is returned which is used in the syntax highlighting | ||
60 | /// injection to replace the previous (line-spanning) comment ranges. | ||
61 | pub(super) fn extract_doc_comments( | ||
62 | node: &SyntaxNode, | ||
63 | ) -> Option<(String, RangesMap, Vec<HighlightedRange>)> { | ||
64 | // wrap the doctest into function body to get correct syntax highlighting | ||
65 | let prefix = "fn doctest() {\n"; | ||
66 | let suffix = "}\n"; | ||
67 | // Mapping from extracted documentation code to original code | ||
68 | let mut range_mapping: RangesMap = BTreeMap::new(); | ||
69 | let mut line_start = TextSize::try_from(prefix.len()).unwrap(); | ||
70 | let mut is_doctest = false; | ||
71 | // Replace the original, line-spanning comment ranges by new, only comment-prefix | ||
72 | // spanning comment ranges. | ||
73 | let mut new_comments = Vec::new(); | ||
74 | let doctest = node | ||
75 | .children_with_tokens() | ||
76 | .filter_map(|el| el.into_token().and_then(ast::Comment::cast)) | ||
77 | .filter(|comment| comment.kind().doc.is_some()) | ||
78 | .filter(|comment| { | ||
79 | if comment.text().contains("```") { | ||
80 | is_doctest = !is_doctest; | ||
81 | false | ||
82 | } else { | ||
83 | is_doctest | ||
84 | } | ||
85 | }) | ||
86 | .map(|comment| { | ||
87 | let prefix_len = comment.prefix().len(); | ||
88 | let line: &str = comment.text().as_str(); | ||
89 | let range = comment.syntax().text_range(); | ||
90 | |||
91 | // whitespace after comment is ignored | ||
92 | let pos = if let Some(ws) = line.chars().nth(prefix_len).filter(|c| c.is_whitespace()) { | ||
93 | prefix_len + ws.len_utf8() | ||
94 | } else { | ||
95 | prefix_len | ||
96 | }; | ||
97 | |||
98 | // lines marked with `#` should be ignored in output, we skip the `#` char | ||
99 | let pos = if let Some(ws) = line.chars().nth(pos).filter(|&c| c == '#') { | ||
100 | pos + ws.len_utf8() | ||
101 | } else { | ||
102 | pos | ||
103 | }; | ||
104 | |||
105 | range_mapping.insert(line_start, range.start() + TextSize::try_from(pos).unwrap()); | ||
106 | new_comments.push(HighlightedRange { | ||
107 | range: TextRange::new( | ||
108 | range.start(), | ||
109 | range.start() + TextSize::try_from(pos).unwrap(), | ||
110 | ), | ||
111 | highlight: HighlightTag::Comment.into(), | ||
112 | binding_hash: None, | ||
113 | }); | ||
114 | line_start += range.len() - TextSize::try_from(pos).unwrap(); | ||
115 | line_start += TextSize::try_from('\n'.len_utf8()).unwrap(); | ||
116 | |||
117 | line[pos..].to_owned() | ||
118 | }) | ||
119 | .sep_by("\n") | ||
120 | .to_string(); | ||
121 | |||
122 | if doctest.is_empty() { | ||
123 | return None; | ||
124 | } | ||
125 | |||
126 | let doctest = format!("{}{}{}", prefix, doctest, suffix); | ||
127 | Some((doctest, range_mapping, new_comments)) | ||
128 | } | ||
129 | |||
130 | /// Injection of syntax highlighting of doctests. | ||
131 | pub(super) fn highlight_doc_comment( | ||
132 | text: String, | ||
133 | range_mapping: RangesMap, | ||
134 | new_comments: Vec<HighlightedRange>, | ||
135 | stack: &mut HighlightedRangeStack, | ||
136 | ) { | ||
137 | let (analysis, tmp_file_id) = Analysis::from_single_file(text); | ||
138 | |||
139 | stack.push(); | ||
140 | for mut h in analysis.highlight(tmp_file_id).unwrap() { | ||
141 | // Determine start offset and end offset in case of multi-line ranges | ||
142 | let mut start_offset = None; | ||
143 | let mut end_offset = None; | ||
144 | for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { | ||
145 | if line_start <= &h.range.start() { | ||
146 | start_offset.get_or_insert(orig_line_start - line_start); | ||
147 | break; | ||
148 | } else { | ||
149 | end_offset.get_or_insert(orig_line_start - line_start); | ||
150 | } | ||
151 | } | ||
152 | if let Some(start_offset) = start_offset { | ||
153 | h.range = TextRange::new( | ||
154 | h.range.start() + start_offset, | ||
155 | h.range.end() + end_offset.unwrap_or(start_offset), | ||
156 | ); | ||
157 | stack.add(h); | ||
158 | } | ||
159 | } | ||
160 | |||
161 | // Inject the comment prefix highlight ranges | ||
162 | stack.push(); | ||
163 | for comment in new_comments { | ||
164 | stack.add(comment); | ||
165 | } | ||
166 | stack.pop_and_inject(false); | ||
167 | stack.pop_and_inject(true); | ||
168 | } | ||
diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs index 1514531de..94f466966 100644 --- a/crates/ra_ide/src/syntax_highlighting/tags.rs +++ b/crates/ra_ide/src/syntax_highlighting/tags.rs | |||
@@ -24,12 +24,14 @@ pub enum HighlightTag { | |||
24 | Enum, | 24 | Enum, |
25 | EnumVariant, | 25 | EnumVariant, |
26 | Field, | 26 | Field, |
27 | FormatSpecifier, | ||
27 | Function, | 28 | Function, |
28 | Keyword, | 29 | Keyword, |
29 | Lifetime, | 30 | Lifetime, |
30 | Macro, | 31 | Macro, |
31 | Module, | 32 | Module, |
32 | NumericLiteral, | 33 | NumericLiteral, |
34 | Operator, | ||
33 | SelfKeyword, | 35 | SelfKeyword, |
34 | SelfType, | 36 | SelfType, |
35 | Static, | 37 | Static, |
@@ -41,8 +43,6 @@ pub enum HighlightTag { | |||
41 | Union, | 43 | Union, |
42 | Local, | 44 | Local, |
43 | UnresolvedReference, | 45 | UnresolvedReference, |
44 | FormatSpecifier, | ||
45 | Operator, | ||
46 | } | 46 | } |
47 | 47 | ||
48 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | 48 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] |
@@ -72,12 +72,14 @@ impl HighlightTag { | |||
72 | HighlightTag::Enum => "enum", | 72 | HighlightTag::Enum => "enum", |
73 | HighlightTag::EnumVariant => "enum_variant", | 73 | HighlightTag::EnumVariant => "enum_variant", |
74 | HighlightTag::Field => "field", | 74 | HighlightTag::Field => "field", |
75 | HighlightTag::FormatSpecifier => "format_specifier", | ||
75 | HighlightTag::Function => "function", | 76 | HighlightTag::Function => "function", |
76 | HighlightTag::Keyword => "keyword", | 77 | HighlightTag::Keyword => "keyword", |
77 | HighlightTag::Lifetime => "lifetime", | 78 | HighlightTag::Lifetime => "lifetime", |
78 | HighlightTag::Macro => "macro", | 79 | HighlightTag::Macro => "macro", |
79 | HighlightTag::Module => "module", | 80 | HighlightTag::Module => "module", |
80 | HighlightTag::NumericLiteral => "numeric_literal", | 81 | HighlightTag::NumericLiteral => "numeric_literal", |
82 | HighlightTag::Operator => "operator", | ||
81 | HighlightTag::SelfKeyword => "self_keyword", | 83 | HighlightTag::SelfKeyword => "self_keyword", |
82 | HighlightTag::SelfType => "self_type", | 84 | HighlightTag::SelfType => "self_type", |
83 | HighlightTag::Static => "static", | 85 | HighlightTag::Static => "static", |
@@ -89,8 +91,6 @@ impl HighlightTag { | |||
89 | HighlightTag::Union => "union", | 91 | HighlightTag::Union => "union", |
90 | HighlightTag::Local => "variable", | 92 | HighlightTag::Local => "variable", |
91 | HighlightTag::UnresolvedReference => "unresolved_reference", | 93 | HighlightTag::UnresolvedReference => "unresolved_reference", |
92 | HighlightTag::FormatSpecifier => "format_specifier", | ||
93 | HighlightTag::Operator => "operator", | ||
94 | } | 94 | } |
95 | } | 95 | } |
96 | } | 96 | } |
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index eb43a23da..949bf59a0 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs | |||
@@ -9,7 +9,7 @@ use crate::{ | |||
9 | 9 | ||
10 | #[test] | 10 | #[test] |
11 | fn test_highlighting() { | 11 | fn test_highlighting() { |
12 | let (analysis, file_id) = single_file( | 12 | check_highlighting( |
13 | r#" | 13 | r#" |
14 | #[derive(Clone, Debug)] | 14 | #[derive(Clone, Debug)] |
15 | struct Foo { | 15 | struct Foo { |
@@ -65,6 +65,8 @@ fn main() { | |||
65 | let y = &mut x; | 65 | let y = &mut x; |
66 | let z = &y; | 66 | let z = &y; |
67 | 67 | ||
68 | let Foo { x: z, y } = Foo { x: z, y }; | ||
69 | |||
68 | y; | 70 | y; |
69 | } | 71 | } |
70 | 72 | ||
@@ -84,17 +86,14 @@ impl<T> Option<T> { | |||
84 | } | 86 | } |
85 | "# | 87 | "# |
86 | .trim(), | 88 | .trim(), |
89 | "crates/ra_ide/src/snapshots/highlighting.html", | ||
90 | false, | ||
87 | ); | 91 | ); |
88 | let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html"); | ||
89 | let actual_html = &analysis.highlight_as_html(file_id, false).unwrap(); | ||
90 | let expected_html = &read_text(&dst_file); | ||
91 | fs::write(dst_file, &actual_html).unwrap(); | ||
92 | assert_eq_text!(expected_html, actual_html); | ||
93 | } | 92 | } |
94 | 93 | ||
95 | #[test] | 94 | #[test] |
96 | fn test_rainbow_highlighting() { | 95 | fn test_rainbow_highlighting() { |
97 | let (analysis, file_id) = single_file( | 96 | check_highlighting( |
98 | r#" | 97 | r#" |
99 | fn main() { | 98 | fn main() { |
100 | let hello = "hello"; | 99 | let hello = "hello"; |
@@ -110,12 +109,9 @@ fn bar() { | |||
110 | } | 109 | } |
111 | "# | 110 | "# |
112 | .trim(), | 111 | .trim(), |
112 | "crates/ra_ide/src/snapshots/rainbow_highlighting.html", | ||
113 | true, | ||
113 | ); | 114 | ); |
114 | let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html"); | ||
115 | let actual_html = &analysis.highlight_as_html(file_id, true).unwrap(); | ||
116 | let expected_html = &read_text(&dst_file); | ||
117 | fs::write(dst_file, &actual_html).unwrap(); | ||
118 | assert_eq_text!(expected_html, actual_html); | ||
119 | } | 115 | } |
120 | 116 | ||
121 | #[test] | 117 | #[test] |
@@ -153,7 +149,7 @@ fn test_ranges() { | |||
153 | 149 | ||
154 | #[test] | 150 | #[test] |
155 | fn test_flattening() { | 151 | fn test_flattening() { |
156 | let (analysis, file_id) = single_file( | 152 | check_highlighting( |
157 | r##" | 153 | r##" |
158 | fn fixture(ra_fixture: &str) {} | 154 | fn fixture(ra_fixture: &str) {} |
159 | 155 | ||
@@ -167,13 +163,9 @@ fn main() { | |||
167 | ); | 163 | ); |
168 | }"## | 164 | }"## |
169 | .trim(), | 165 | .trim(), |
166 | "crates/ra_ide/src/snapshots/highlight_injection.html", | ||
167 | false, | ||
170 | ); | 168 | ); |
171 | |||
172 | let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_injection.html"); | ||
173 | let actual_html = &analysis.highlight_as_html(file_id, false).unwrap(); | ||
174 | let expected_html = &read_text(&dst_file); | ||
175 | fs::write(dst_file, &actual_html).unwrap(); | ||
176 | assert_eq_text!(expected_html, actual_html); | ||
177 | } | 169 | } |
178 | 170 | ||
179 | #[test] | 171 | #[test] |
@@ -192,7 +184,7 @@ macro_rules! test {} | |||
192 | fn test_string_highlighting() { | 184 | fn test_string_highlighting() { |
193 | // The format string detection is based on macro-expansion, | 185 | // The format string detection is based on macro-expansion, |
194 | // thus, we have to copy the macro definition from `std` | 186 | // thus, we have to copy the macro definition from `std` |
195 | let (analysis, file_id) = single_file( | 187 | check_highlighting( |
196 | r#" | 188 | r#" |
197 | macro_rules! println { | 189 | macro_rules! println { |
198 | ($($arg:tt)*) => ({ | 190 | ($($arg:tt)*) => ({ |
@@ -218,6 +210,7 @@ fn main() { | |||
218 | println!("{argument}", argument = "test"); // => "test" | 210 | println!("{argument}", argument = "test"); // => "test" |
219 | println!("{name} {}", 1, name = 2); // => "2 1" | 211 | println!("{name} {}", 1, name = 2); // => "2 1" |
220 | println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" | 212 | println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" |
213 | println!("{{{}}}", 2); // => "{2}" | ||
221 | println!("Hello {:5}!", "x"); | 214 | println!("Hello {:5}!", "x"); |
222 | println!("Hello {:1$}!", "x", 5); | 215 | println!("Hello {:1$}!", "x", 5); |
223 | println!("Hello {1:0$}!", 5, "x"); | 216 | println!("Hello {1:0$}!", 5, "x"); |
@@ -249,10 +242,96 @@ fn main() { | |||
249 | println!("{ничоси}", ничоси = 92); | 242 | println!("{ничоси}", ничоси = 92); |
250 | }"# | 243 | }"# |
251 | .trim(), | 244 | .trim(), |
245 | "crates/ra_ide/src/snapshots/highlight_strings.html", | ||
246 | false, | ||
252 | ); | 247 | ); |
248 | } | ||
249 | |||
250 | #[test] | ||
251 | fn test_unsafe_highlighting() { | ||
252 | check_highlighting( | ||
253 | r#" | ||
254 | unsafe fn unsafe_fn() {} | ||
255 | |||
256 | struct HasUnsafeFn; | ||
257 | |||
258 | impl HasUnsafeFn { | ||
259 | unsafe fn unsafe_method(&self) {} | ||
260 | } | ||
261 | |||
262 | fn main() { | ||
263 | let x = &5 as *const usize; | ||
264 | unsafe { | ||
265 | unsafe_fn(); | ||
266 | HasUnsafeFn.unsafe_method(); | ||
267 | let y = *(x); | ||
268 | let z = -x; | ||
269 | } | ||
270 | } | ||
271 | "# | ||
272 | .trim(), | ||
273 | "crates/ra_ide/src/snapshots/highlight_unsafe.html", | ||
274 | false, | ||
275 | ); | ||
276 | } | ||
277 | |||
278 | #[test] | ||
279 | fn test_highlight_doctest() { | ||
280 | check_highlighting( | ||
281 | r#" | ||
282 | impl Foo { | ||
283 | /// Constructs a new `Foo`. | ||
284 | /// | ||
285 | /// # Examples | ||
286 | /// | ||
287 | /// ``` | ||
288 | /// # #![allow(unused_mut)] | ||
289 | /// let mut foo: Foo = Foo::new(); | ||
290 | /// ``` | ||
291 | pub const fn new() -> Foo { | ||
292 | Foo { } | ||
293 | } | ||
294 | |||
295 | /// `bar` method on `Foo`. | ||
296 | /// | ||
297 | /// # Examples | ||
298 | /// | ||
299 | /// ``` | ||
300 | /// let foo = Foo::new(); | ||
301 | /// | ||
302 | /// // calls bar on foo | ||
303 | /// assert!(foo.bar()); | ||
304 | /// | ||
305 | /// /* multi-line | ||
306 | /// comment */ | ||
307 | /// | ||
308 | /// let multi_line_string = "Foo | ||
309 | /// bar | ||
310 | /// "; | ||
311 | /// | ||
312 | /// ``` | ||
313 | /// | ||
314 | /// ``` | ||
315 | /// let foobar = Foo::new().bar(); | ||
316 | /// ``` | ||
317 | pub fn foo(&self) -> bool { | ||
318 | true | ||
319 | } | ||
320 | } | ||
321 | "# | ||
322 | .trim(), | ||
323 | "crates/ra_ide/src/snapshots/highlight_doctest.html", | ||
324 | false, | ||
325 | ) | ||
326 | } | ||
253 | 327 | ||
254 | let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_strings.html"); | 328 | /// Highlights the code given by the `ra_fixture` argument, renders the |
255 | let actual_html = &analysis.highlight_as_html(file_id, false).unwrap(); | 329 | /// result as HTML, and compares it with the HTML file given as `snapshot`. |
330 | /// Note that the `snapshot` file is overwritten by the rendered HTML. | ||
331 | fn check_highlighting(ra_fixture: &str, snapshot: &str, rainbow: bool) { | ||
332 | let (analysis, file_id) = single_file(ra_fixture); | ||
333 | let dst_file = project_dir().join(snapshot); | ||
334 | let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap(); | ||
256 | let expected_html = &read_text(&dst_file); | 335 | let expected_html = &read_text(&dst_file); |
257 | fs::write(dst_file, &actual_html).unwrap(); | 336 | fs::write(dst_file, &actual_html).unwrap(); |
258 | assert_eq_text!(expected_html, actual_html); | 337 | assert_eq_text!(expected_html, actual_html); |