aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/syntax_highlighting
diff options
context:
space:
mode:
authorLeander Tentrup <[email protected]>2020-06-14 13:43:43 +0100
committerLeander Tentrup <[email protected]>2020-06-15 21:13:53 +0100
commitc4b3db0c2f307d1d782af88ded260e4c6593cae0 (patch)
treedd32c19bf36913db87d0dde84f70e6fd1b4c2e5a /crates/ra_ide/src/syntax_highlighting
parentf4f51171ca6d99b693df2ef6fb71f0347999aa9f (diff)
Syntactic highlighting of NAME_REF for injections
This commit adds a function that tries to determine the syntax highlighting class of NAME_REFs based on the usage. It is used for highlighting injections (such as highlighting of doctests) as the semantic logic will most of the time result in unresolved references. It also adds a color to unresolved references in HTML encoding.
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting')
-rw-r--r--crates/ra_ide/src/syntax_highlighting/html.rs3
-rw-r--r--crates/ra_ide/src/syntax_highlighting/injection.rs2
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs14
3 files changed, 15 insertions, 4 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs
index 5bada6252..853b4a20f 100644
--- a/crates/ra_ide/src/syntax_highlighting/html.rs
+++ b/crates/ra_ide/src/syntax_highlighting/html.rs
@@ -19,7 +19,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
19 ) 19 )
20 } 20 }
21 21
22 let ranges = highlight(db, file_id, None); 22 let ranges = highlight(db, file_id, None, false);
23 let text = parse.tree().syntax().to_string(); 23 let text = parse.tree().syntax().to_string();
24 let mut prev_pos = TextSize::from(0); 24 let mut prev_pos = TextSize::from(0);
25 let mut buf = String::new(); 25 let mut buf = String::new();
@@ -84,6 +84,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
84.variable { color: #DCDCCC; } 84.variable { color: #DCDCCC; }
85.format_specifier { color: #CC696B; } 85.format_specifier { color: #CC696B; }
86.mutable { text-decoration: underline; } 86.mutable { text-decoration: underline; }
87.unresolved_reference { color: #FC5555; }
87 88
88.keyword { color: #F0DFAF; font-weight: bold; } 89.keyword { color: #F0DFAF; font-weight: bold; }
89.keyword.unsafe { color: #BC8383; font-weight: bold; } 90.keyword.unsafe { color: #BC8383; font-weight: bold; }
diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs
index 3575a0fc6..a02ffe59e 100644
--- a/crates/ra_ide/src/syntax_highlighting/injection.rs
+++ b/crates/ra_ide/src/syntax_highlighting/injection.rs
@@ -137,7 +137,7 @@ pub(super) fn highlight_doc_comment(
137 let (analysis, tmp_file_id) = Analysis::from_single_file(text); 137 let (analysis, tmp_file_id) = Analysis::from_single_file(text);
138 138
139 stack.push(); 139 stack.push();
140 for mut h in analysis.highlight(tmp_file_id).unwrap() { 140 for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() {
141 // Determine start offset and end offset in case of multi-line ranges 141 // Determine start offset and end offset in case of multi-line ranges
142 let mut start_offset = None; 142 let mut start_offset = None;
143 let mut end_offset = None; 143 let mut end_offset = None;
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs
index 070b24f45..062b3ff4a 100644
--- a/crates/ra_ide/src/syntax_highlighting/tests.rs
+++ b/crates/ra_ide/src/syntax_highlighting/tests.rs
@@ -287,7 +287,13 @@ fn main() {
287fn test_highlight_doctest() { 287fn test_highlight_doctest() {
288 check_highlighting( 288 check_highlighting(
289 r#" 289 r#"
290struct Foo {
291 bar: bool,
292}
293
290impl Foo { 294impl Foo {
295 pub const bar: bool = true;
296
291 /// Constructs a new `Foo`. 297 /// Constructs a new `Foo`.
292 /// 298 ///
293 /// # Examples 299 /// # Examples
@@ -297,7 +303,7 @@ impl Foo {
297 /// let mut foo: Foo = Foo::new(); 303 /// let mut foo: Foo = Foo::new();
298 /// ``` 304 /// ```
299 pub const fn new() -> Foo { 305 pub const fn new() -> Foo {
300 Foo { } 306 Foo { bar: true }
301 } 307 }
302 308
303 /// `bar` method on `Foo`. 309 /// `bar` method on `Foo`.
@@ -305,11 +311,15 @@ impl Foo {
305 /// # Examples 311 /// # Examples
306 /// 312 ///
307 /// ``` 313 /// ```
314 /// use x::y;
315 ///
308 /// let foo = Foo::new(); 316 /// let foo = Foo::new();
309 /// 317 ///
310 /// // calls bar on foo 318 /// // calls bar on foo
311 /// assert!(foo.bar()); 319 /// assert!(foo.bar());
312 /// 320 ///
321 /// let bar = foo.bar || Foo::bar;
322 ///
313 /// /* multi-line 323 /// /* multi-line
314 /// comment */ 324 /// comment */
315 /// 325 ///
@@ -330,7 +340,7 @@ impl Foo {
330 .trim(), 340 .trim(),
331 "crates/ra_ide/src/snapshots/highlight_doctest.html", 341 "crates/ra_ide/src/snapshots/highlight_doctest.html",
332 false, 342 false,
333 ) 343 );
334} 344}
335 345
336/// Highlights the code given by the `ra_fixture` argument, renders the 346/// Highlights the code given by the `ra_fixture` argument, renders the