From 351bba9bee136d856f987037b6ecffd0642d606f Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 18 Jun 2020 09:37:22 -0400 Subject: [PATCH 1/7] Add support for marking doctest items as distinct from normal code, add default tag to all doctest elements --- crates/ra_ide/src/lib.rs | 4 +- crates/ra_ide/src/prime_caches.rs | 2 +- .../src/snapshots/highlight_doctest.html | 44 +++++++++---------- crates/ra_ide/src/syntax_highlighting.rs | 18 +++++--- crates/ra_ide/src/syntax_highlighting/html.rs | 2 +- .../src/syntax_highlighting/injection.rs | 6 ++- crates/ra_ide/src/syntax_highlighting/tags.rs | 3 ++ crates/rust-analyzer/src/semantic_tokens.rs | 1 + crates/rust-analyzer/src/to_proto.rs | 1 + 9 files changed, 47 insertions(+), 34 deletions(-) diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index ecac5134e6..22203b4a3c 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -443,13 +443,13 @@ impl Analysis { /// Computes syntax highlighting for the given file pub fn highlight(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false)) + self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false, None)) } /// Computes syntax highlighting for the given file range. pub fn highlight_range(&self, frange: FileRange) -> Cancelable> { self.with_db(|db| { - syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false) + syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false, None) }) } diff --git a/crates/ra_ide/src/prime_caches.rs b/crates/ra_ide/src/prime_caches.rs index c5ab5a1d87..dbed8a506a 100644 --- a/crates/ra_ide/src/prime_caches.rs +++ b/crates/ra_ide/src/prime_caches.rs @@ -7,6 +7,6 @@ use crate::{FileId, RootDatabase}; pub(crate) fn prime_caches(db: &RootDatabase, files: Vec) { for file in files { - let _ = crate::syntax_highlighting::highlight(db, file, None, false); + let _ = crate::syntax_highlighting::highlight(db, file, None, false, None); } } diff --git a/crates/ra_ide/src/snapshots/highlight_doctest.html b/crates/ra_ide/src/snapshots/highlight_doctest.html index ac546806eb..6b3932aff2 100644 --- a/crates/ra_ide/src/snapshots/highlight_doctest.html +++ b/crates/ra_ide/src/snapshots/highlight_doctest.html @@ -47,9 +47,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// # Examples /// /// ``` - /// # #![allow(unused_mut)] - /// let mut foo: Foo = Foo::new(); - /// ``` + /// # #![allow(unused_mut)] + /// let mut foo: Foo = Foo::new(); + /// ``` pub const fn new() -> Foo { Foo { bar: true } } @@ -59,27 +59,27 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// # Examples /// /// ``` - /// use x::y; - /// - /// let foo = Foo::new(); - /// - /// // calls bar on foo - /// assert!(foo.bar()); - /// - /// let bar = foo.bar || Foo::bar; - /// - /// /* multi-line - /// comment */ - /// - /// let multi_line_string = "Foo - /// bar - /// "; - /// - /// ``` + /// use x::y; + /// + /// let foo = Foo::new(); + /// + /// // calls bar on foo + /// assert!(foo.bar()); + /// + /// let bar = foo.bar || Foo::bar; + /// + /// /* multi-line + /// comment */ + /// + /// let multi_line_string = "Foo + /// bar + /// "; + /// + /// ``` /// /// ```rust,no_run - /// let foobar = Foo::new().bar(); - /// ``` + /// let foobar = Foo::new().bar(); + /// ``` /// /// ```sh /// echo 1 diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 448645bdc3..b4dcdba393 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -45,6 +45,7 @@ pub(crate) fn highlight( file_id: FileId, range_to_highlight: Option, syntactic_name_ref_highlighting: bool, + default_tag: Option, ) -> Vec { let _p = profile("highlight"); let sema = Semantics::new(db); @@ -107,6 +108,7 @@ pub(crate) fn highlight( &mut bindings_shadow_count, syntactic_name_ref_highlighting, name.syntax().clone().into(), + default_tag, ) { stack.add(HighlightedRange { range: name.syntax().text_range(), @@ -206,6 +208,7 @@ pub(crate) fn highlight( &mut bindings_shadow_count, syntactic_name_ref_highlighting, element_to_highlight.clone(), + default_tag, ) { stack.add(HighlightedRange { range, highlight, binding_hash }); if let Some(string) = @@ -430,13 +433,14 @@ fn highlight_element( bindings_shadow_count: &mut FxHashMap, syntactic_name_ref_highlighting: bool, element: SyntaxElement, + default_tag: Option, ) -> Option<(Highlight, Option)> { let db = sema.db; let mut binding_hash = None; let highlight: Highlight = match element.kind() { FN_DEF => { bindings_shadow_count.clear(); - return None; + default_tag?.into() } // Highlight definitions depending on the "type" of the definition. @@ -515,12 +519,12 @@ fn highlight_element( let expr = prefix_expr.expr()?; let ty = sema.type_of_expr(&expr)?; if !ty.is_raw_ptr() { - return None; + default_tag?.into() + } else { + let mut h = Highlight::new(HighlightTag::Operator); + h |= HighlightModifier::Unsafe; + h } - - let mut h = Highlight::new(HighlightTag::Operator); - h |= HighlightModifier::Unsafe; - h } T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { Highlight::new(HighlightTag::Macro) @@ -546,7 +550,7 @@ fn highlight_element( } } - _ => return None, + _ => default_tag?.into(), }; return Some((highlight, binding_hash)); diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 99b6b25ab9..9043024dfd 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, false); + let ranges = highlight(db, file_id, None, false, None); let text = parse.tree().syntax().to_string(); let mut prev_pos = TextSize::from(0); let mut buf = String::new(); diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 9d82b40095..bd38cdb6f3 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs @@ -150,7 +150,10 @@ pub(super) fn highlight_doc_comment( let (analysis, tmp_file_id) = Analysis::from_single_file(text); stack.push(); - for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() { + for mut h in analysis + .with_db(|db| super::highlight(db, tmp_file_id, None, true, Some(HighlightTag::Operator))) + .unwrap() + { // Determine start offset and end offset in case of multi-line ranges let mut start_offset = None; let mut end_offset = None; @@ -172,6 +175,7 @@ pub(super) fn highlight_doc_comment( h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(), ); + h.highlight |= HighlightModifier::Injected; stack.add(h); } } diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs index 93bbb4b4dc..f5ab738652 100644 --- a/crates/ra_ide/src/syntax_highlighting/tags.rs +++ b/crates/ra_ide/src/syntax_highlighting/tags.rs @@ -57,6 +57,7 @@ pub enum HighlightModifier { /// not. Definition, Documentation, + Injected, Mutable, Unsafe, } @@ -110,6 +111,7 @@ impl HighlightModifier { HighlightModifier::ControlFlow, HighlightModifier::Definition, HighlightModifier::Documentation, + HighlightModifier::Injected, HighlightModifier::Mutable, HighlightModifier::Unsafe, ]; @@ -120,6 +122,7 @@ impl HighlightModifier { HighlightModifier::ControlFlow => "control", HighlightModifier::Definition => "declaration", HighlightModifier::Documentation => "documentation", + HighlightModifier::Injected => "injected", HighlightModifier::Mutable => "mutable", HighlightModifier::Unsafe => "unsafe", } diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index 2ea63d33b1..6f43667a34 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -68,6 +68,7 @@ macro_rules! define_semantic_token_modifiers { define_semantic_token_modifiers![ (CONSTANT, "constant"), (CONTROL_FLOW, "controlFlow"), + (INJECTED, "injected"), (MUTABLE, "mutable"), (UNSAFE, "unsafe"), (ATTRIBUTE_MODIFIER, "attribute"), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 7b45b169d6..dee5d7859e 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -331,6 +331,7 @@ fn semantic_token_type_and_modifiers( HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER, HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION, HighlightModifier::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION, + HighlightModifier::Injected => semantic_tokens::INJECTED, HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW, HighlightModifier::Mutable => semantic_tokens::MUTABLE, HighlightModifier::Unsafe => semantic_tokens::UNSAFE, From e137d9accb0ad73e1794e55a76c18ed4c8ca85e9 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 18 Jun 2020 10:30:40 -0400 Subject: [PATCH 2/7] Add default color and opacity for documentation and injected, respectively, in the html generator --- crates/ra_ide/src/snapshots/highlight_doctest.html | 2 ++ crates/ra_ide/src/snapshots/highlight_injection.html | 2 ++ crates/ra_ide/src/snapshots/highlight_strings.html | 2 ++ crates/ra_ide/src/snapshots/highlight_unsafe.html | 2 ++ crates/ra_ide/src/snapshots/highlighting.html | 2 ++ crates/ra_ide/src/snapshots/rainbow_highlighting.html | 2 ++ crates/ra_ide/src/syntax_highlighting/html.rs | 2 ++ 7 files changed, 14 insertions(+) diff --git a/crates/ra_ide/src/snapshots/highlight_doctest.html b/crates/ra_ide/src/snapshots/highlight_doctest.html index 6b3932aff2..f9a11710e9 100644 --- a/crates/ra_ide/src/snapshots/highlight_doctest.html +++ b/crates/ra_ide/src/snapshots/highlight_doctest.html @@ -5,6 +5,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } +.documentation { color: #00CC00; } +.injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } .string_literal { color: #CC9393; } diff --git a/crates/ra_ide/src/snapshots/highlight_injection.html b/crates/ra_ide/src/snapshots/highlight_injection.html index 47dbd7bc8a..4c3fa2a7bc 100644 --- a/crates/ra_ide/src/snapshots/highlight_injection.html +++ b/crates/ra_ide/src/snapshots/highlight_injection.html @@ -5,6 +5,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } +.documentation { color: #00CC00; } +.injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } .string_literal { color: #CC9393; } diff --git a/crates/ra_ide/src/snapshots/highlight_strings.html b/crates/ra_ide/src/snapshots/highlight_strings.html index b46fa44c6a..245c425f5e 100644 --- a/crates/ra_ide/src/snapshots/highlight_strings.html +++ b/crates/ra_ide/src/snapshots/highlight_strings.html @@ -5,6 +5,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } +.documentation { color: #00CC00; } +.injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } .string_literal { color: #CC9393; } diff --git a/crates/ra_ide/src/snapshots/highlight_unsafe.html b/crates/ra_ide/src/snapshots/highlight_unsafe.html index 73438fbb4c..fe986b98c9 100644 --- a/crates/ra_ide/src/snapshots/highlight_unsafe.html +++ b/crates/ra_ide/src/snapshots/highlight_unsafe.html @@ -5,6 +5,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } +.documentation { color: #00CC00; } +.injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } .string_literal { color: #CC9393; } diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index 0c4f0a0187..c101af8ea7 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -5,6 +5,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } +.documentation { color: #00CC00; } +.injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } .string_literal { color: #CC9393; } diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index a74a70069d..97b296d72a 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html @@ -5,6 +5,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } +.documentation { color: #00CC00; } +.injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } .string_literal { color: #CC9393; } diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 9043024dfd..191c586a33 100644 --- a/crates/ra_ide/src/syntax_highlighting/html.rs +++ b/crates/ra_ide/src/syntax_highlighting/html.rs @@ -64,6 +64,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } +.documentation { color: #00CC00; } +.injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } .string_literal { color: #CC9393; } From d8230acd84dc931f72b9dd32b8fbc2aa887d8b4b Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Mon, 22 Jun 2020 10:28:07 -0400 Subject: [PATCH 3/7] Add punctuation highlighting for highlighting punctuation in doctests, fix highlighting in doctests --- crates/ra_ide/src/lib.rs | 4 +- crates/ra_ide/src/prime_caches.rs | 2 +- .../src/snapshots/highlight_doctest.html | 84 ++++++------ .../src/snapshots/highlight_injection.html | 22 +-- .../src/snapshots/highlight_strings.html | 106 +++++++-------- .../src/snapshots/highlight_unsafe.html | 30 ++--- crates/ra_ide/src/snapshots/highlighting.html | 126 +++++++++--------- .../src/snapshots/rainbow_highlighting.html | 22 +-- crates/ra_ide/src/syntax_highlighting.rs | 25 ++-- crates/ra_ide/src/syntax_highlighting/html.rs | 4 +- .../src/syntax_highlighting/injection.rs | 4 +- crates/ra_ide/src/syntax_highlighting/tags.rs | 2 + crates/rust-analyzer/src/semantic_tokens.rs | 5 +- crates/rust-analyzer/src/to_proto.rs | 1 + 14 files changed, 220 insertions(+), 217 deletions(-) diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 22203b4a3c..31bd6cf034 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -443,13 +443,13 @@ impl Analysis { /// Computes syntax highlighting for the given file pub fn highlight(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false, None)) + self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false, false)) } /// Computes syntax highlighting for the given file range. pub fn highlight_range(&self, frange: FileRange) -> Cancelable> { self.with_db(|db| { - syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false, None) + syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false, false) }) } diff --git a/crates/ra_ide/src/prime_caches.rs b/crates/ra_ide/src/prime_caches.rs index dbed8a506a..59386c5e63 100644 --- a/crates/ra_ide/src/prime_caches.rs +++ b/crates/ra_ide/src/prime_caches.rs @@ -7,6 +7,6 @@ use crate::{FileId, RootDatabase}; pub(crate) fn prime_caches(db: &RootDatabase, files: Vec) { for file in files { - let _ = crate::syntax_highlighting::highlight(db, file, None, false, None); + let _ = crate::syntax_highlighting::highlight(db, file, None, false, false); } } diff --git a/crates/ra_ide/src/snapshots/highlight_doctest.html b/crates/ra_ide/src/snapshots/highlight_doctest.html index f9a11710e9..95f3d2d1eb 100644 --- a/crates/ra_ide/src/snapshots/highlight_doctest.html +++ b/crates/ra_ide/src/snapshots/highlight_doctest.html @@ -5,7 +5,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } -.documentation { color: #00CC00; } +.documentation { color: #629755; } .injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } @@ -35,67 +35,67 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .control { font-style: italic; }
/// ```
-/// let _ = "early doctests should not go boom";
-/// ```
-struct Foo {
-    bar: bool,
-}
+/// let _ = "early doctests should not go boom";
+/// ```
+struct Foo {
+    bar: bool,
+}
 
-impl Foo {
-    pub const bar: bool = true;
+impl Foo {
+    pub const bar: bool = true;
 
     /// Constructs a new `Foo`.
     ///
     /// # Examples
     ///
     /// ```
-    /// # #![allow(unused_mut)]
-    /// let mut foo: Foo = Foo::new();
-    /// ```
-    pub const fn new() -> Foo {
-        Foo { bar: true }
-    }
+    /// # #![allow(unused_mut)]
+    /// let mut foo: Foo = Foo::new();
+    /// ```
+    pub const fn new() -> Foo {
+        Foo { bar: true }
+    }
 
     /// `bar` method on `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 */
-    ///
-    /// let multi_line_string = "Foo
+    /// use x::y;
+    ///
+    /// let foo = Foo::new();
+    ///
+    /// // calls bar on foo
+    /// assert!(foo.bar());
+    ///
+    /// let bar = foo.bar || Foo::bar;
+    ///
+    /// /* multi-line
+    ///        comment */
+    ///
+    /// let multi_line_string = "Foo
     ///   bar
-    ///          ";
-    ///
-    /// ```
+    ///          ";
+    ///
+    /// ```
     ///
     /// ```rust,no_run
-    /// let foobar = Foo::new().bar();
-    /// ```
+    /// let foobar = Foo::new().bar();
+    /// ```
     ///
     /// ```sh
     /// echo 1
     /// ```
-    pub fn foo(&self) -> bool {
+    pub fn foo(&self) -> bool {
         true
-    }
-}
+    }
+}
 
 /// ```
-/// noop!(1);
-/// ```
-macro_rules! noop {
-    ($expr:expr) => {
-        $expr
-    }
-}
\ No newline at end of file +/// noop!(1); +/// ``` +macro_rules! noop { + ($expr:expr) => { + $expr + } +} \ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/highlight_injection.html b/crates/ra_ide/src/snapshots/highlight_injection.html index 4c3fa2a7bc..bc19313d82 100644 --- a/crates/ra_ide/src/snapshots/highlight_injection.html +++ b/crates/ra_ide/src/snapshots/highlight_injection.html @@ -5,7 +5,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } -.documentation { color: #00CC00; } +.documentation { color: #629755; } .injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } @@ -34,14 +34,14 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
fn fixture(ra_fixture: &str) {}
+
fn fixture(ra_fixture: &str) {}
 
-fn main() {
-    fixture(r#"
-        trait Foo {
-            fn foo() {
-                println!("2 + 2 = {}", 4);
-            }
-        }"#
-    );
-}
\ No newline at end of file +fn main() { + fixture(r#" + trait Foo { + fn foo() { + println!("2 + 2 = {}", 4); + } + }"# + ); +}
\ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/highlight_strings.html b/crates/ra_ide/src/snapshots/highlight_strings.html index 245c425f5e..910b2978d2 100644 --- a/crates/ra_ide/src/snapshots/highlight_strings.html +++ b/crates/ra_ide/src/snapshots/highlight_strings.html @@ -5,7 +5,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } -.documentation { color: #00CC00; } +.documentation { color: #629755; } .injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } @@ -34,62 +34,62 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
macro_rules! println {
-    ($($arg:tt)*) => ({
-        $crate::io::_print($crate::format_args_nl!($($arg)*));
-    })
-}
+
macro_rules! println {
+    ($($arg:tt)*) => ({
+        $crate::io::_print($crate::format_args_nl!($($arg)*));
+    })
+}
 #[rustc_builtin_macro]
-macro_rules! format_args_nl {
-    ($fmt:expr) => {{ /* compiler built-in */ }};
-    ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
-}
+macro_rules! format_args_nl {
+    ($fmt:expr) => {{ /* compiler built-in */ }};
+    ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
+}
 
-fn main() {
+fn main() {
     // from https://doc.rust-lang.org/std/fmt/index.html
-    println!("Hello");                 // => "Hello"
-    println!("Hello, {}!", "world");   // => "Hello, world!"
-    println!("The number is {}", 1);   // => "The number is 1"
-    println!("{:?}", (3, 4));          // => "(3, 4)"
-    println!("{value}", value=4);      // => "4"
-    println!("{} {}", 1, 2);           // => "1 2"
-    println!("{:04}", 42);             // => "0042" with leading zerosV
-    println!("{1} {} {0} {}", 1, 2);   // => "2 1 1 2"
-    println!("{argument}", argument = "test");   // => "test"
-    println!("{name} {}", 1, name = 2);          // => "2 1"
-    println!("{a} {c} {b}", a="a", b='b', c=3);  // => "a 3 b"
-    println!("{{{}}}", 2);                       // => "{2}"
-    println!("Hello {:5}!", "x");
-    println!("Hello {:1$}!", "x", 5);
-    println!("Hello {1:0$}!", 5, "x");
-    println!("Hello {:width$}!", "x", width = 5);
-    println!("Hello {:<5}!", "x");
-    println!("Hello {:-<5}!", "x");
-    println!("Hello {:^5}!", "x");
-    println!("Hello {:>5}!", "x");
-    println!("Hello {:+}!", 5);
-    println!("{:#x}!", 27);
-    println!("Hello {:05}!", 5);
-    println!("Hello {:05}!", -5);
-    println!("{:#010x}!", 27);
-    println!("Hello {0} is {1:.5}", "x", 0.01);
-    println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
-    println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
-    println!("Hello {} is {:.*}",    "x", 5, 0.01);
-    println!("Hello {} is {2:.*}",   "x", 5, 0.01);
-    println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
-    println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
-    println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
-    println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
-    println!("Hello {{}}");
-    println!("{{ Hello");
+    println!("Hello");                 // => "Hello"
+    println!("Hello, {}!", "world");   // => "Hello, world!"
+    println!("The number is {}", 1);   // => "The number is 1"
+    println!("{:?}", (3, 4));          // => "(3, 4)"
+    println!("{value}", value=4);      // => "4"
+    println!("{} {}", 1, 2);           // => "1 2"
+    println!("{:04}", 42);             // => "0042" with leading zerosV
+    println!("{1} {} {0} {}", 1, 2);   // => "2 1 1 2"
+    println!("{argument}", argument = "test");   // => "test"
+    println!("{name} {}", 1, name = 2);          // => "2 1"
+    println!("{a} {c} {b}", a="a", b='b', c=3);  // => "a 3 b"
+    println!("{{{}}}", 2);                       // => "{2}"
+    println!("Hello {:5}!", "x");
+    println!("Hello {:1$}!", "x", 5);
+    println!("Hello {1:0$}!", 5, "x");
+    println!("Hello {:width$}!", "x", width = 5);
+    println!("Hello {:<5}!", "x");
+    println!("Hello {:-<5}!", "x");
+    println!("Hello {:^5}!", "x");
+    println!("Hello {:>5}!", "x");
+    println!("Hello {:+}!", 5);
+    println!("{:#x}!", 27);
+    println!("Hello {:05}!", 5);
+    println!("Hello {:05}!", -5);
+    println!("{:#010x}!", 27);
+    println!("Hello {0} is {1:.5}", "x", 0.01);
+    println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
+    println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
+    println!("Hello {} is {:.*}",    "x", 5, 0.01);
+    println!("Hello {} is {2:.*}",   "x", 5, 0.01);
+    println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
+    println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
+    println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
+    println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
+    println!("Hello {{}}");
+    println!("{{ Hello");
 
-    println!(r"Hello, {}!", "world");
+    println!(r"Hello, {}!", "world");
 
     // escape sequences
-    println!("Hello\nWorld");
-    println!("\u{48}\x65\x6C\x6C\x6F World");
+    println!("Hello\nWorld");
+    println!("\u{48}\x65\x6C\x6C\x6F World");
 
-    println!("{\x41}", A = 92);
-    println!("{ничоси}", ничоси = 92);
-}
\ No newline at end of file + println!("{\x41}", A = 92); + println!("{ничоси}", ничоси = 92); +}
\ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/highlight_unsafe.html b/crates/ra_ide/src/snapshots/highlight_unsafe.html index fe986b98c9..fa986a3bc6 100644 --- a/crates/ra_ide/src/snapshots/highlight_unsafe.html +++ b/crates/ra_ide/src/snapshots/highlight_unsafe.html @@ -5,7 +5,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } -.documentation { color: #00CC00; } +.documentation { color: #629755; } .injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } @@ -34,20 +34,20 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
unsafe fn unsafe_fn() {}
+
unsafe fn unsafe_fn() {}
 
-struct HasUnsafeFn;
+struct HasUnsafeFn;
 
-impl HasUnsafeFn {
-    unsafe fn unsafe_method(&self) {}
-}
+impl HasUnsafeFn {
+    unsafe fn unsafe_method(&self) {}
+}
 
-fn main() {
-    let x = &5 as *const usize;
-    unsafe {
-        unsafe_fn();
-        HasUnsafeFn.unsafe_method();
-        let y = *(x);
-        let z = -x;
-    }
-}
\ No newline at end of file +fn main() { + let x = &5 as *const usize; + unsafe { + unsafe_fn(); + HasUnsafeFn.unsafe_method(); + let y = *(x); + let z = -x; + } +}
\ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index c101af8ea7..e1852c4092 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -5,7 +5,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } -.documentation { color: #00CC00; } +.documentation { color: #629755; } .injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } @@ -34,84 +34,84 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
#[derive(Clone, Debug)]
-struct Foo {
-    pub x: i32,
-    pub y: i32,
-}
+
#[derive(Clone, Debug)]
+struct Foo {
+    pub x: i32,
+    pub y: i32,
+}
 
-trait Bar {
-    fn bar(&self) -> i32;
-}
+trait Bar {
+    fn bar(&self) -> i32;
+}
 
-impl Bar for Foo {
-    fn bar(&self) -> i32 {
-        self.x
-    }
-}
+impl Bar for Foo {
+    fn bar(&self) -> i32 {
+        self.x
+    }
+}
 
-static mut STATIC_MUT: i32 = 0;
+static mut STATIC_MUT: i32 = 0;
 
-fn foo<'a, T>() -> T {
-    foo::<'a, i32>()
-}
+fn foo<'a, T>() -> T {
+    foo::<'a, i32>()
+}
 
-macro_rules! def_fn {
-    ($($tt:tt)*) => {$($tt)*}
-}
+macro_rules! def_fn {
+    ($($tt:tt)*) => {$($tt)*}
+}
 
-def_fn! {
-    fn bar() -> u32 {
+def_fn! {
+    fn bar() -> u32 {
         100
-    }
-}
+    }
+}
 
-macro_rules! noop {
-    ($expr:expr) => {
-        $expr
-    }
-}
+macro_rules! noop {
+    ($expr:expr) => {
+        $expr
+    }
+}
 
 // comment
-fn main() {
-    println!("Hello, {}!", 92);
+fn main() {
+    println!("Hello, {}!", 92);
 
-    let mut vec = Vec::new();
-    if true {
-        let x = 92;
-        vec.push(Foo { x, y: 1 });
-    }
-    unsafe {
-        vec.set_len(0);
-        STATIC_MUT = 1;
-    }
+    let mut vec = Vec::new();
+    if true {
+        let x = 92;
+        vec.push(Foo { x, y: 1 });
+    }
+    unsafe {
+        vec.set_len(0);
+        STATIC_MUT = 1;
+    }
 
-    for e in vec {
+    for e in vec {
         // Do nothing
-    }
+    }
 
-    noop!(noop!(1));
+    noop!(noop!(1));
 
-    let mut x = 42;
-    let y = &mut x;
-    let z = &y;
+    let mut x = 42;
+    let y = &mut x;
+    let z = &y;
 
-    let Foo { x: z, y } = Foo { x: z, y };
+    let Foo { x: z, y } = Foo { x: z, y };
 
-    y;
-}
+    y;
+}
 
-enum Option<T> {
-    Some(T),
-    None,
-}
-use Option::*;
+enum Option<T> {
+    Some(T),
+    None,
+}
+use Option::*;
 
-impl<T> Option<T> {
-    fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
-        match other {
-            None => unimplemented!(),
-            Nope => Nope,
-        }
-    }
-}
\ No newline at end of file +impl<T> Option<T> { + fn and<U>(self, other: Option<U>) -> Option<(T, U)> { + match other { + None => unimplemented!(), + Nope => Nope, + } + } +}
\ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index 97b296d72a..d05a56cf2d 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html @@ -5,7 +5,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } -.documentation { color: #00CC00; } +.documentation { color: #629755; } .injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } @@ -34,15 +34,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
fn main() {
-    let hello = "hello";
-    let x = hello.to_string();
-    let y = hello.to_string();
+
fn main() {
+    let hello = "hello";
+    let x = hello.to_string();
+    let y = hello.to_string();
 
-    let x = "other color please!";
-    let y = x.to_string();
-}
+    let x = "other color please!";
+    let y = x.to_string();
+}
 
-fn bar() {
-    let mut hello = "hello";
-}
\ No newline at end of file +fn bar() { + let mut hello = "hello"; +}
\ No newline at end of file diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index b4dcdba393..8e714a9998 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -45,7 +45,7 @@ pub(crate) fn highlight( file_id: FileId, range_to_highlight: Option, syntactic_name_ref_highlighting: bool, - default_tag: Option, + should_highlight_punctuation: bool, ) -> Vec { let _p = profile("highlight"); let sema = Semantics::new(db); @@ -108,7 +108,7 @@ pub(crate) fn highlight( &mut bindings_shadow_count, syntactic_name_ref_highlighting, name.syntax().clone().into(), - default_tag, + should_highlight_punctuation, ) { stack.add(HighlightedRange { range: name.syntax().text_range(), @@ -208,7 +208,7 @@ pub(crate) fn highlight( &mut bindings_shadow_count, syntactic_name_ref_highlighting, element_to_highlight.clone(), - default_tag, + true, ) { stack.add(HighlightedRange { range, highlight, binding_hash }); if let Some(string) = @@ -331,12 +331,12 @@ impl HighlightedRangeStack { /// can only modify the last range currently on the stack. /// Can be used to do injections that span multiple ranges, like the /// doctest injection below. - /// If `delete` is set to true, the parent range is deleted instead of + /// If `inject` is set to true, the parent range is deleted instead of /// intersected. /// /// Note that `pop` can be simulated by `pop_and_inject(false)` but the /// latter is computationally more expensive. - fn pop_and_inject(&mut self, delete: bool) { + fn pop_and_inject(&mut self, inject: bool) { let mut children = self.stack.pop().unwrap(); let prev = self.stack.last_mut().unwrap(); children.sort_by_key(|range| range.range.start()); @@ -347,14 +347,14 @@ impl HighlightedRangeStack { prev.iter().position(|parent| parent.range.contains_range(child.range)) { let cloned = Self::intersect(&mut prev[idx], &child); - let insert_idx = if delete || prev[idx].range.is_empty() { + let insert_idx = if inject || prev[idx].range.is_empty() { prev.remove(idx); idx } else { idx + 1 }; prev.insert(insert_idx, child); - if !delete && !cloned.range.is_empty() { + if !inject && !cloned.range.is_empty() { prev.insert(insert_idx + 1, cloned); } } else if let Some(_idx) = @@ -433,14 +433,14 @@ fn highlight_element( bindings_shadow_count: &mut FxHashMap, syntactic_name_ref_highlighting: bool, element: SyntaxElement, - default_tag: Option, + should_highlight_punctuation: bool, ) -> Option<(Highlight, Option)> { let db = sema.db; let mut binding_hash = None; let highlight: Highlight = match element.kind() { FN_DEF => { bindings_shadow_count.clear(); - default_tag?.into() + return None; } // Highlight definitions depending on the "type" of the definition. @@ -518,10 +518,10 @@ fn highlight_element( let expr = prefix_expr.expr()?; let ty = sema.type_of_expr(&expr)?; + let mut h = HighlightTag::Operator.into(); if !ty.is_raw_ptr() { - default_tag?.into() + h } else { - let mut h = Highlight::new(HighlightTag::Operator); h |= HighlightModifier::Unsafe; h } @@ -550,7 +550,8 @@ fn highlight_element( } } - _ => default_tag?.into(), + p if should_highlight_punctuation && p.is_punct() => HighlightTag::Punctuation.into(), + _ => return None, }; return Some((highlight, binding_hash)); diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 191c586a33..2a27c81dd1 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, false, None); + let ranges = highlight(db, file_id, None, false, false); let text = parse.tree().syntax().to_string(); let mut prev_pos = TextSize::from(0); let mut buf = String::new(); @@ -64,7 +64,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .lifetime { color: #DFAF8F; font-style: italic; } .comment { color: #7F9F7F; } -.documentation { color: #00CC00; } +.documentation { color: #629755; } .injected { opacity: 0.65 ; } .struct, .enum { color: #7CB8BB; } .enum_variant { color: #BDE0F3; } diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index bd38cdb6f3..8c724af5b5 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs @@ -150,9 +150,7 @@ pub(super) fn highlight_doc_comment( let (analysis, tmp_file_id) = Analysis::from_single_file(text); stack.push(); - for mut h in analysis - .with_db(|db| super::highlight(db, tmp_file_id, None, true, Some(HighlightTag::Operator))) - .unwrap() + for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true, true)).unwrap() { // Determine start offset and end offset in case of multi-line ranges let mut start_offset = None; diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs index f5ab738652..7f8e91e8d4 100644 --- a/crates/ra_ide/src/syntax_highlighting/tags.rs +++ b/crates/ra_ide/src/syntax_highlighting/tags.rs @@ -33,6 +33,7 @@ pub enum HighlightTag { Module, NumericLiteral, Operator, + Punctuation, SelfKeyword, SelfType, Static, @@ -84,6 +85,7 @@ impl HighlightTag { HighlightTag::Module => "module", HighlightTag::NumericLiteral => "numeric_literal", HighlightTag::Operator => "operator", + HighlightTag::Punctuation => "punctuation", HighlightTag::SelfKeyword => "self_keyword", HighlightTag::SelfType => "self_type", HighlightTag::Static => "static", diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index 6f43667a34..30f25250f6 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -39,13 +39,14 @@ define_semantic_token_types![ (BOOLEAN, "boolean"), (BUILTIN_TYPE, "builtinType"), (ENUM_MEMBER, "enumMember"), + (ESCAPE_SEQUENCE, "escapeSequence"), + (FORMAT_SPECIFIER, "formatSpecifier"), (LIFETIME, "lifetime"), + (PUNCTUATION, "punctuation"), (SELF_KEYWORD, "selfKeyword"), (TYPE_ALIAS, "typeAlias"), (UNION, "union"), (UNRESOLVED_REFERENCE, "unresolvedReference"), - (FORMAT_SPECIFIER, "formatSpecifier"), - (ESCAPE_SEQUENCE, "escapeSequence"), ]; macro_rules! define_semantic_token_modifiers { diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index dee5d7859e..efc4523477 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -323,6 +323,7 @@ fn semantic_token_type_and_modifiers( HighlightTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE, HighlightTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER, HighlightTag::Operator => lsp_types::SemanticTokenType::OPERATOR, + HighlightTag::Punctuation => semantic_tokens::PUNCTUATION, HighlightTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE, }; From 2a56323537442958008d0fddd78e33df425b11a9 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Tue, 23 Jun 2020 09:17:53 -0400 Subject: [PATCH 4/7] Update injection mechanism and stop injecting through highlight element, switch to more general new highlight tag, generic --- crates/ra_ide/src/lib.rs | 4 +- crates/ra_ide/src/prime_caches.rs | 2 +- .../src/snapshots/highlight_doctest.html | 62 ++++----- .../src/snapshots/highlight_injection.html | 20 +-- .../src/snapshots/highlight_strings.html | 104 +++++++-------- .../src/snapshots/highlight_unsafe.html | 28 ++-- crates/ra_ide/src/snapshots/highlighting.html | 124 +++++++++--------- .../src/snapshots/rainbow_highlighting.html | 20 +-- crates/ra_ide/src/syntax_highlighting.rs | 55 +++++--- crates/ra_ide/src/syntax_highlighting/html.rs | 2 +- .../src/syntax_highlighting/injection.rs | 12 +- crates/ra_ide/src/syntax_highlighting/tags.rs | 4 +- crates/rust-analyzer/src/semantic_tokens.rs | 2 +- crates/rust-analyzer/src/to_proto.rs | 2 +- 14 files changed, 231 insertions(+), 210 deletions(-) diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 31bd6cf034..ecac5134e6 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -443,13 +443,13 @@ impl Analysis { /// Computes syntax highlighting for the given file pub fn highlight(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false, false)) + self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false)) } /// Computes syntax highlighting for the given file range. pub fn highlight_range(&self, frange: FileRange) -> Cancelable> { self.with_db(|db| { - syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false, false) + syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false) }) } diff --git a/crates/ra_ide/src/prime_caches.rs b/crates/ra_ide/src/prime_caches.rs index 59386c5e63..c5ab5a1d87 100644 --- a/crates/ra_ide/src/prime_caches.rs +++ b/crates/ra_ide/src/prime_caches.rs @@ -7,6 +7,6 @@ use crate::{FileId, RootDatabase}; pub(crate) fn prime_caches(db: &RootDatabase, files: Vec) { for file in files { - let _ = crate::syntax_highlighting::highlight(db, file, None, false, false); + let _ = crate::syntax_highlighting::highlight(db, file, None, false); } } diff --git a/crates/ra_ide/src/snapshots/highlight_doctest.html b/crates/ra_ide/src/snapshots/highlight_doctest.html index 95f3d2d1eb..e8155def75 100644 --- a/crates/ra_ide/src/snapshots/highlight_doctest.html +++ b/crates/ra_ide/src/snapshots/highlight_doctest.html @@ -35,67 +35,67 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .control { font-style: italic; }
/// ```
-/// let _ = "early doctests should not go boom";
-/// ```
-struct Foo {
-    bar: bool,
-}
+/// let _ = "early doctests should not go boom";
+/// ```
+struct Foo {
+    bar: bool,
+}
 
-impl Foo {
-    pub const bar: bool = true;
+impl Foo {
+    pub const bar: bool = true;
 
     /// Constructs a new `Foo`.
     ///
     /// # Examples
     ///
     /// ```
-    /// # #![allow(unused_mut)]
-    /// let mut foo: Foo = Foo::new();
-    /// ```
-    pub const fn new() -> Foo {
-        Foo { bar: true }
-    }
+    /// # #![allow(unused_mut)]
+    /// let mut foo: Foo = Foo::new();
+    /// ```
+    pub const fn new() -> Foo {
+        Foo { bar: true }
+    }
 
     /// `bar` method on `Foo`.
     ///
     /// # Examples
     ///
     /// ```
-    /// use x::y;
+    /// use x::y;
     ///
-    /// let foo = Foo::new();
+    /// let foo = Foo::new();
     ///
     /// // calls bar on foo
-    /// assert!(foo.bar());
+    /// assert!(foo.bar());
     ///
-    /// let bar = foo.bar || Foo::bar;
+    /// let bar = foo.bar || Foo::bar;
     ///
     /// /* multi-line
     ///        comment */
     ///
-    /// let multi_line_string = "Foo
+    /// let multi_line_string = "Foo
     ///   bar
-    ///          ";
+    ///          ";
     ///
     /// ```
     ///
     /// ```rust,no_run
-    /// let foobar = Foo::new().bar();
-    /// ```
+    /// let foobar = Foo::new().bar();
+    /// ```
     ///
     /// ```sh
     /// echo 1
     /// ```
-    pub fn foo(&self) -> bool {
+    pub fn foo(&self) -> bool {
         true
-    }
-}
+    }
+}
 
 /// ```
-/// noop!(1);
-/// ```
-macro_rules! noop {
-    ($expr:expr) => {
-        $expr
-    }
-}
\ No newline at end of file +/// noop!(1); +/// ``` +macro_rules! noop { + ($expr:expr) => { + $expr + } +} \ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/highlight_injection.html b/crates/ra_ide/src/snapshots/highlight_injection.html index bc19313d82..1b0349bae2 100644 --- a/crates/ra_ide/src/snapshots/highlight_injection.html +++ b/crates/ra_ide/src/snapshots/highlight_injection.html @@ -34,14 +34,14 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
fn fixture(ra_fixture: &str) {}
+
fn fixture(ra_fixture: &str) {}
 
-fn main() {
-    fixture(r#"
-        trait Foo {
-            fn foo() {
-                println!("2 + 2 = {}", 4);
-            }
-        }"#
-    );
-}
\ No newline at end of file +fn main() { + fixture(r#" + trait Foo { + fn foo() { + println!("2 + 2 = {}", 4); + } + }"# + ); +}
\ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/highlight_strings.html b/crates/ra_ide/src/snapshots/highlight_strings.html index 910b2978d2..d184b56910 100644 --- a/crates/ra_ide/src/snapshots/highlight_strings.html +++ b/crates/ra_ide/src/snapshots/highlight_strings.html @@ -34,62 +34,62 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
macro_rules! println {
-    ($($arg:tt)*) => ({
-        $crate::io::_print($crate::format_args_nl!($($arg)*));
-    })
-}
+
macro_rules! println {
+    ($($arg:tt)*) => ({
+        $crate::io::_print($crate::format_args_nl!($($arg)*));
+    })
+}
 #[rustc_builtin_macro]
-macro_rules! format_args_nl {
-    ($fmt:expr) => {{ /* compiler built-in */ }};
-    ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
-}
+macro_rules! format_args_nl {
+    ($fmt:expr) => {{ /* compiler built-in */ }};
+    ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
+}
 
-fn main() {
+fn main() {
     // from https://doc.rust-lang.org/std/fmt/index.html
-    println!("Hello");                 // => "Hello"
-    println!("Hello, {}!", "world");   // => "Hello, world!"
-    println!("The number is {}", 1);   // => "The number is 1"
-    println!("{:?}", (3, 4));          // => "(3, 4)"
-    println!("{value}", value=4);      // => "4"
-    println!("{} {}", 1, 2);           // => "1 2"
-    println!("{:04}", 42);             // => "0042" with leading zerosV
-    println!("{1} {} {0} {}", 1, 2);   // => "2 1 1 2"
-    println!("{argument}", argument = "test");   // => "test"
-    println!("{name} {}", 1, name = 2);          // => "2 1"
-    println!("{a} {c} {b}", a="a", b='b', c=3);  // => "a 3 b"
-    println!("{{{}}}", 2);                       // => "{2}"
-    println!("Hello {:5}!", "x");
-    println!("Hello {:1$}!", "x", 5);
-    println!("Hello {1:0$}!", 5, "x");
-    println!("Hello {:width$}!", "x", width = 5);
-    println!("Hello {:<5}!", "x");
-    println!("Hello {:-<5}!", "x");
-    println!("Hello {:^5}!", "x");
-    println!("Hello {:>5}!", "x");
-    println!("Hello {:+}!", 5);
-    println!("{:#x}!", 27);
-    println!("Hello {:05}!", 5);
-    println!("Hello {:05}!", -5);
-    println!("{:#010x}!", 27);
-    println!("Hello {0} is {1:.5}", "x", 0.01);
-    println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
-    println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
-    println!("Hello {} is {:.*}",    "x", 5, 0.01);
-    println!("Hello {} is {2:.*}",   "x", 5, 0.01);
-    println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
-    println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
-    println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
-    println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
-    println!("Hello {{}}");
-    println!("{{ Hello");
+    println!("Hello");                 // => "Hello"
+    println!("Hello, {}!", "world");   // => "Hello, world!"
+    println!("The number is {}", 1);   // => "The number is 1"
+    println!("{:?}", (3, 4));          // => "(3, 4)"
+    println!("{value}", value=4);      // => "4"
+    println!("{} {}", 1, 2);           // => "1 2"
+    println!("{:04}", 42);             // => "0042" with leading zerosV
+    println!("{1} {} {0} {}", 1, 2);   // => "2 1 1 2"
+    println!("{argument}", argument = "test");   // => "test"
+    println!("{name} {}", 1, name = 2);          // => "2 1"
+    println!("{a} {c} {b}", a="a", b='b', c=3);  // => "a 3 b"
+    println!("{{{}}}", 2);                       // => "{2}"
+    println!("Hello {:5}!", "x");
+    println!("Hello {:1$}!", "x", 5);
+    println!("Hello {1:0$}!", 5, "x");
+    println!("Hello {:width$}!", "x", width = 5);
+    println!("Hello {:<5}!", "x");
+    println!("Hello {:-<5}!", "x");
+    println!("Hello {:^5}!", "x");
+    println!("Hello {:>5}!", "x");
+    println!("Hello {:+}!", 5);
+    println!("{:#x}!", 27);
+    println!("Hello {:05}!", 5);
+    println!("Hello {:05}!", -5);
+    println!("{:#010x}!", 27);
+    println!("Hello {0} is {1:.5}", "x", 0.01);
+    println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
+    println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
+    println!("Hello {} is {:.*}",    "x", 5, 0.01);
+    println!("Hello {} is {2:.*}",   "x", 5, 0.01);
+    println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
+    println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
+    println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
+    println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
+    println!("Hello {{}}");
+    println!("{{ Hello");
 
-    println!(r"Hello, {}!", "world");
+    println!(r"Hello, {}!", "world");
 
     // escape sequences
-    println!("Hello\nWorld");
-    println!("\u{48}\x65\x6C\x6C\x6F World");
+    println!("Hello\nWorld");
+    println!("\u{48}\x65\x6C\x6C\x6F World");
 
-    println!("{\x41}", A = 92);
-    println!("{ничоси}", ничоси = 92);
-}
\ No newline at end of file + println!("{\x41}", A = 92); + println!("{ничоси}", ничоси = 92); +}
\ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/highlight_unsafe.html b/crates/ra_ide/src/snapshots/highlight_unsafe.html index fa986a3bc6..6936e949fe 100644 --- a/crates/ra_ide/src/snapshots/highlight_unsafe.html +++ b/crates/ra_ide/src/snapshots/highlight_unsafe.html @@ -34,20 +34,20 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
unsafe fn unsafe_fn() {}
+
unsafe fn unsafe_fn() {}
 
-struct HasUnsafeFn;
+struct HasUnsafeFn;
 
-impl HasUnsafeFn {
-    unsafe fn unsafe_method(&self) {}
-}
+impl HasUnsafeFn {
+    unsafe fn unsafe_method(&self) {}
+}
 
-fn main() {
-    let x = &5 as *const usize;
-    unsafe {
-        unsafe_fn();
-        HasUnsafeFn.unsafe_method();
-        let y = *(x);
-        let z = -x;
-    }
-}
\ No newline at end of file +fn main() { + let x = &5 as *const usize; + unsafe { + unsafe_fn(); + HasUnsafeFn.unsafe_method(); + let y = *(x); + let z = -x; + } +}
\ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index e1852c4092..8d0b38f958 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -34,84 +34,84 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
#[derive(Clone, Debug)]
-struct Foo {
-    pub x: i32,
-    pub y: i32,
-}
+
#[derive(Clone, Debug)]
+struct Foo {
+    pub x: i32,
+    pub y: i32,
+}
 
-trait Bar {
-    fn bar(&self) -> i32;
-}
+trait Bar {
+    fn bar(&self) -> i32;
+}
 
-impl Bar for Foo {
-    fn bar(&self) -> i32 {
-        self.x
-    }
-}
+impl Bar for Foo {
+    fn bar(&self) -> i32 {
+        self.x
+    }
+}
 
-static mut STATIC_MUT: i32 = 0;
+static mut STATIC_MUT: i32 = 0;
 
-fn foo<'a, T>() -> T {
-    foo::<'a, i32>()
-}
+fn foo<'a, T>() -> T {
+    foo::<'a, i32>()
+}
 
-macro_rules! def_fn {
-    ($($tt:tt)*) => {$($tt)*}
-}
+macro_rules! def_fn {
+    ($($tt:tt)*) => {$($tt)*}
+}
 
-def_fn! {
-    fn bar() -> u32 {
+def_fn! {
+    fn bar() -> u32 {
         100
-    }
-}
+    }
+}
 
-macro_rules! noop {
-    ($expr:expr) => {
-        $expr
-    }
-}
+macro_rules! noop {
+    ($expr:expr) => {
+        $expr
+    }
+}
 
 // comment
-fn main() {
-    println!("Hello, {}!", 92);
+fn main() {
+    println!("Hello, {}!", 92);
 
-    let mut vec = Vec::new();
-    if true {
-        let x = 92;
-        vec.push(Foo { x, y: 1 });
-    }
-    unsafe {
-        vec.set_len(0);
-        STATIC_MUT = 1;
-    }
+    let mut vec = Vec::new();
+    if true {
+        let x = 92;
+        vec.push(Foo { x, y: 1 });
+    }
+    unsafe {
+        vec.set_len(0);
+        STATIC_MUT = 1;
+    }
 
-    for e in vec {
+    for e in vec {
         // Do nothing
-    }
+    }
 
-    noop!(noop!(1));
+    noop!(noop!(1));
 
-    let mut x = 42;
-    let y = &mut x;
-    let z = &y;
+    let mut x = 42;
+    let y = &mut x;
+    let z = &y;
 
-    let Foo { x: z, y } = Foo { x: z, y };
+    let Foo { x: z, y } = Foo { x: z, y };
 
-    y;
-}
+    y;
+}
 
-enum Option<T> {
-    Some(T),
-    None,
-}
-use Option::*;
+enum Option<T> {
+    Some(T),
+    None,
+}
+use Option::*;
 
-impl<T> Option<T> {
-    fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
-        match other {
-            None => unimplemented!(),
-            Nope => Nope,
-        }
-    }
-}
\ No newline at end of file +impl<T> Option<T> { + fn and<U>(self, other: Option<U>) -> Option<(T, U)> { + match other { + None => unimplemented!(), + Nope => Nope, + } + } +}
\ No newline at end of file diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index d05a56cf2d..9516c74410 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html @@ -34,15 +34,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } -
fn main() {
-    let hello = "hello";
-    let x = hello.to_string();
-    let y = hello.to_string();
+
fn main() {
+    let hello = "hello";
+    let x = hello.to_string();
+    let y = hello.to_string();
 
-    let x = "other color please!";
-    let y = x.to_string();
-}
+    let x = "other color please!";
+    let y = x.to_string();
+}
 
-fn bar() {
-    let mut hello = "hello";
-}
\ No newline at end of file +fn bar() { + let mut hello = "hello"; +}
\ No newline at end of file diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 8e714a9998..3dffddce57 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -45,7 +45,6 @@ pub(crate) fn highlight( file_id: FileId, range_to_highlight: Option, syntactic_name_ref_highlighting: bool, - should_highlight_punctuation: bool, ) -> Vec { let _p = profile("highlight"); let sema = Semantics::new(db); @@ -108,7 +107,6 @@ pub(crate) fn highlight( &mut bindings_shadow_count, syntactic_name_ref_highlighting, name.syntax().clone().into(), - should_highlight_punctuation, ) { stack.add(HighlightedRange { range: name.syntax().text_range(), @@ -208,7 +206,6 @@ pub(crate) fn highlight( &mut bindings_shadow_count, syntactic_name_ref_highlighting, element_to_highlight.clone(), - true, ) { stack.add(HighlightedRange { range, highlight, binding_hash }); if let Some(string) = @@ -239,7 +236,7 @@ pub(crate) fn highlight( }); } } - stack.pop_and_inject(false); + stack.pop_and_inject(None); } } else if let Some(string) = element_to_highlight.as_token().cloned().and_then(ast::RawString::cast) @@ -327,6 +324,17 @@ impl HighlightedRangeStack { cloned } + /// Remove the `HighlightRange` of `parent` that's currently covered by `child`. + fn intersect_partial(parent: &mut HighlightedRange, child: &HighlightedRange) { + assert!( + parent.range.start() <= child.range.start() + && parent.range.end() >= child.range.start() + && child.range.end() > parent.range.end() + ); + + parent.range = TextRange::new(parent.range.start(), child.range.start()); + } + /// Similar to `pop`, but can modify arbitrary prior ranges (where `pop`) /// can only modify the last range currently on the stack. /// Can be used to do injections that span multiple ranges, like the @@ -336,7 +344,7 @@ impl HighlightedRangeStack { /// /// Note that `pop` can be simulated by `pop_and_inject(false)` but the /// latter is computationally more expensive. - fn pop_and_inject(&mut self, inject: bool) { + fn pop_and_inject(&mut self, overwrite_parent: Option) { let mut children = self.stack.pop().unwrap(); let prev = self.stack.last_mut().unwrap(); children.sort_by_key(|range| range.range.start()); @@ -346,26 +354,41 @@ impl HighlightedRangeStack { if let Some(idx) = prev.iter().position(|parent| parent.range.contains_range(child.range)) { + if let Some(tag) = overwrite_parent { + prev[idx].highlight = tag; + } + let cloned = Self::intersect(&mut prev[idx], &child); - let insert_idx = if inject || prev[idx].range.is_empty() { + let insert_idx = if prev[idx].range.is_empty() { prev.remove(idx); idx } else { idx + 1 }; prev.insert(insert_idx, child); - if !inject && !cloned.range.is_empty() { + if !cloned.range.is_empty() { prev.insert(insert_idx + 1, cloned); } - } else if let Some(_idx) = - prev.iter().position(|parent| parent.range.contains(child.range.start())) - { - unreachable!("child range should be completely contained in parent range"); } else { - let idx = prev - .binary_search_by_key(&child.range.start(), |range| range.range.start()) - .unwrap_or_else(|x| x); - prev.insert(idx, child); + let maybe_idx = + prev.iter().position(|parent| parent.range.contains(child.range.start())); + if let (Some(_), Some(idx)) = (overwrite_parent, maybe_idx) { + Self::intersect_partial(&mut prev[idx], &child); + let insert_idx = if prev[idx].range.is_empty() { + prev.remove(idx); + idx + } else { + idx + 1 + }; + prev.insert(insert_idx, child); + } else if let None = maybe_idx { + let idx = prev + .binary_search_by_key(&child.range.start(), |range| range.range.start()) + .unwrap_or_else(|x| x); + prev.insert(idx, child); + } else { + unreachable!("child range should be completely contained in parent range"); + } } } } @@ -433,7 +456,6 @@ fn highlight_element( bindings_shadow_count: &mut FxHashMap, syntactic_name_ref_highlighting: bool, element: SyntaxElement, - should_highlight_punctuation: bool, ) -> Option<(Highlight, Option)> { let db = sema.db; let mut binding_hash = None; @@ -550,7 +572,6 @@ fn highlight_element( } } - p if should_highlight_punctuation && p.is_punct() => HighlightTag::Punctuation.into(), _ => return None, }; diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 2a27c81dd1..0c74f73701 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, false, false); + 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(); diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 8c724af5b5..181c21256e 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs @@ -8,8 +8,8 @@ use ra_syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; use stdx::SepBy; use crate::{ - call_info::ActiveParameter, Analysis, HighlightModifier, HighlightTag, HighlightedRange, - RootDatabase, + call_info::ActiveParameter, Analysis, Highlight, HighlightModifier, HighlightTag, + HighlightedRange, RootDatabase, }; use super::HighlightedRangeStack; @@ -150,8 +150,7 @@ pub(super) fn highlight_doc_comment( let (analysis, tmp_file_id) = Analysis::from_single_file(text); stack.push(); - for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true, true)).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; @@ -183,6 +182,7 @@ pub(super) fn highlight_doc_comment( for comment in new_comments { stack.add(comment); } - stack.pop_and_inject(false); - stack.pop_and_inject(true); + stack.pop_and_inject(None); + stack + .pop_and_inject(Some(Highlight::from(HighlightTag::Generic) | HighlightModifier::Injected)); } diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs index 7f8e91e8d4..e8e251cfc0 100644 --- a/crates/ra_ide/src/syntax_highlighting/tags.rs +++ b/crates/ra_ide/src/syntax_highlighting/tags.rs @@ -27,13 +27,13 @@ pub enum HighlightTag { Field, FormatSpecifier, Function, + Generic, Keyword, Lifetime, Macro, Module, NumericLiteral, Operator, - Punctuation, SelfKeyword, SelfType, Static, @@ -79,13 +79,13 @@ impl HighlightTag { HighlightTag::Field => "field", HighlightTag::FormatSpecifier => "format_specifier", HighlightTag::Function => "function", + HighlightTag::Generic => "generic", HighlightTag::Keyword => "keyword", HighlightTag::Lifetime => "lifetime", HighlightTag::Macro => "macro", HighlightTag::Module => "module", HighlightTag::NumericLiteral => "numeric_literal", HighlightTag::Operator => "operator", - HighlightTag::Punctuation => "punctuation", HighlightTag::SelfKeyword => "self_keyword", HighlightTag::SelfType => "self_type", HighlightTag::Static => "static", diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index 30f25250f6..10fe40cb50 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -41,8 +41,8 @@ define_semantic_token_types![ (ENUM_MEMBER, "enumMember"), (ESCAPE_SEQUENCE, "escapeSequence"), (FORMAT_SPECIFIER, "formatSpecifier"), + (GENERIC, "generic"), (LIFETIME, "lifetime"), - (PUNCTUATION, "punctuation"), (SELF_KEYWORD, "selfKeyword"), (TYPE_ALIAS, "typeAlias"), (UNION, "union"), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index efc4523477..da9887a9a3 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -295,6 +295,7 @@ fn semantic_token_type_and_modifiers( HighlightTag::SelfType => lsp_types::SemanticTokenType::TYPE, HighlightTag::Field => lsp_types::SemanticTokenType::PROPERTY, HighlightTag::Function => lsp_types::SemanticTokenType::FUNCTION, + HighlightTag::Generic => semantic_tokens::GENERIC, HighlightTag::Module => lsp_types::SemanticTokenType::NAMESPACE, HighlightTag::Constant => { mods |= semantic_tokens::CONSTANT; @@ -323,7 +324,6 @@ fn semantic_token_type_and_modifiers( HighlightTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE, HighlightTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER, HighlightTag::Operator => lsp_types::SemanticTokenType::OPERATOR, - HighlightTag::Punctuation => semantic_tokens::PUNCTUATION, HighlightTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE, }; From 1e9095ae8c98813ba2e89318f35f0ec15cc833b3 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Tue, 23 Jun 2020 12:15:45 -0400 Subject: [PATCH 5/7] Update comment for pop_and_inject Co-authored-by: Leander Tentrup --- crates/ra_ide/src/syntax_highlighting.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 3dffddce57..4061a335e6 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -339,8 +339,8 @@ impl HighlightedRangeStack { /// can only modify the last range currently on the stack. /// Can be used to do injections that span multiple ranges, like the /// doctest injection below. - /// If `inject` is set to true, the parent range is deleted instead of - /// intersected. + /// If `overwrite_parent` is non-optional, the highlighting of the parent range + /// is overwritten with the argument. /// /// Note that `pop` can be simulated by `pop_and_inject(false)` but the /// latter is computationally more expensive. From a8a606cdc4abc3e7f5938ffaf0128b0935b3ab8a Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Tue, 23 Jun 2020 12:40:55 -0400 Subject: [PATCH 6/7] Remove unrelated change --- crates/ra_ide/src/syntax_highlighting.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 4061a335e6..91f27aa2a9 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -540,12 +540,10 @@ fn highlight_element( let expr = prefix_expr.expr()?; let ty = sema.type_of_expr(&expr)?; - let mut h = HighlightTag::Operator.into(); if !ty.is_raw_ptr() { - h + return None; } else { - h |= HighlightModifier::Unsafe; - h + HighlightTag::Operator | HighlightModifier::Unsafe } } T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { From 0d87eee3a9d3950cd02a80bf3974b4b165c5a76c Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Tue, 23 Jun 2020 19:35:09 -0400 Subject: [PATCH 7/7] Improve readability be replacing hard-to-read if-else branches with a match --- crates/ra_ide/src/syntax_highlighting.rs | 36 +++++++++++++----------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 91f27aa2a9..028b559022 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -372,22 +372,26 @@ impl HighlightedRangeStack { } else { let maybe_idx = prev.iter().position(|parent| parent.range.contains(child.range.start())); - if let (Some(_), Some(idx)) = (overwrite_parent, maybe_idx) { - Self::intersect_partial(&mut prev[idx], &child); - let insert_idx = if prev[idx].range.is_empty() { - prev.remove(idx); - idx - } else { - idx + 1 - }; - prev.insert(insert_idx, child); - } else if let None = maybe_idx { - let idx = prev - .binary_search_by_key(&child.range.start(), |range| range.range.start()) - .unwrap_or_else(|x| x); - prev.insert(idx, child); - } else { - unreachable!("child range should be completely contained in parent range"); + match (overwrite_parent, maybe_idx) { + (Some(_), Some(idx)) => { + Self::intersect_partial(&mut prev[idx], &child); + let insert_idx = if prev[idx].range.is_empty() { + prev.remove(idx); + idx + } else { + idx + 1 + }; + prev.insert(insert_idx, child); + } + (_, None) => { + let idx = prev + .binary_search_by_key(&child.range.start(), |range| range.range.start()) + .unwrap_or_else(|x| x); + prev.insert(idx, child); + } + _ => { + unreachable!("child range should be completely contained in parent range"); + } } } }