From c4b3db0c2f307d1d782af88ded260e4c6593cae0 Mon Sep 17 00:00:00 2001 From: Leander Tentrup Date: Sun, 14 Jun 2020 14:43:43 +0200 Subject: 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. --- crates/ra_ide/src/syntax_highlighting/html.rs | 3 ++- crates/ra_ide/src/syntax_highlighting/injection.rs | 2 +- crates/ra_ide/src/syntax_highlighting/tests.rs | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting') 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 ) } - let ranges = highlight(db, file_id, None); + let ranges = highlight(db, file_id, None, false); let text = parse.tree().syntax().to_string(); let mut prev_pos = TextSize::from(0); let mut buf = String::new(); @@ -84,6 +84,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .variable { color: #DCDCCC; } .format_specifier { color: #CC696B; } .mutable { text-decoration: underline; } +.unresolved_reference { color: #FC5555; } .keyword { color: #F0DFAF; font-weight: bold; } .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( let (analysis, tmp_file_id) = Analysis::from_single_file(text); stack.push(); - for mut h in analysis.highlight(tmp_file_id).unwrap() { + for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() { // Determine start offset and end offset in case of multi-line ranges let mut start_offset = None; 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() { fn test_highlight_doctest() { check_highlighting( r#" +struct Foo { + bar: bool, +} + impl Foo { + pub const bar: bool = true; + /// Constructs a new `Foo`. /// /// # Examples @@ -297,7 +303,7 @@ impl Foo { /// let mut foo: Foo = Foo::new(); /// ``` pub const fn new() -> Foo { - Foo { } + Foo { bar: true } } /// `bar` method on `Foo`. @@ -305,11 +311,15 @@ impl Foo { /// # Examples /// /// ``` + /// use x::y; + /// /// let foo = Foo::new(); /// /// // calls bar on foo /// assert!(foo.bar()); /// + /// let bar = foo.bar || Foo::bar; + /// /// /* multi-line /// comment */ /// @@ -330,7 +340,7 @@ impl Foo { .trim(), "crates/ra_ide/src/snapshots/highlight_doctest.html", false, - ) + ); } /// Highlights the code given by the `ra_fixture` argument, renders the -- cgit v1.2.3