From aae26bc5b864971ef54f4d95d5ed89a6436334e2 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Mon, 15 Jun 2020 20:17:26 -0400 Subject: [PATCH] Add highlighting support for doc comments --- .../src/snapshots/highlight_doctest.html | 76 +++++++++---------- crates/ra_ide/src/syntax_highlighting.rs | 9 ++- .../src/syntax_highlighting/injection.rs | 8 +- crates/ra_ide/src/syntax_highlighting/tags.rs | 3 + crates/rust-analyzer/src/to_proto.rs | 1 + 5 files changed, 56 insertions(+), 41 deletions(-) diff --git a/crates/ra_ide/src/snapshots/highlight_doctest.html b/crates/ra_ide/src/snapshots/highlight_doctest.html index 13a5d1b12a..f1e007b09b 100644 --- a/crates/ra_ide/src/snapshots/highlight_doctest.html +++ b/crates/ra_ide/src/snapshots/highlight_doctest.html @@ -38,48 +38,48 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd impl Foo { pub const bar: bool = true; - /// Constructs a new `Foo`. - /// - /// # Examples - /// - /// ``` - /// # #![allow(unused_mut)] - /// let mut foo: Foo = Foo::new(); - /// ``` + /// Constructs a new `Foo`. + /// + /// # Examples + /// + /// ``` + /// # #![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 - /// bar - /// "; - /// - /// ``` - /// - /// ```rust,no_run - /// let foobar = Foo::new().bar(); - /// ``` - /// - /// ```sh - /// echo 1 - /// ``` + /// `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 + /// bar + /// "; + /// + /// ``` + /// + /// ```rust,no_run + /// let foobar = Foo::new().bar(); + /// ``` + /// + /// ```sh + /// echo 1 + /// ``` pub fn foo(&self) -> bool { true } diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 5a4de450c6..68dff45b7f 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -475,7 +475,14 @@ fn highlight_element( } // Simple token-based highlighting - COMMENT => HighlightTag::Comment.into(), + COMMENT => { + let comment = element.into_token().and_then(ast::Comment::cast)?; + if comment.kind().doc.is_some() { + Highlight::from(HighlightTag::Comment) | HighlightModifier::Documentation + } else { + HighlightTag::Comment.into() + } + } STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HighlightTag::StringLiteral.into(), ATTR => HighlightTag::Attribute.into(), INT_NUMBER | FLOAT_NUMBER => HighlightTag::NumericLiteral.into(), diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 929a5cc5c0..40436c5a22 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs @@ -7,7 +7,10 @@ use hir::Semantics; use ra_syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; use stdx::SepBy; -use crate::{call_info::ActiveParameter, Analysis, HighlightTag, HighlightedRange, RootDatabase}; +use crate::{ + call_info::ActiveParameter, Analysis, Highlight, HighlightModifier, HighlightTag, + HighlightedRange, RootDatabase, +}; use super::HighlightedRangeStack; @@ -118,7 +121,8 @@ pub(super) fn extract_doc_comments( range.start(), range.start() + TextSize::try_from(pos).unwrap(), ), - highlight: HighlightTag::Comment.into(), + highlight: Highlight::from(HighlightTag::Comment) + | HighlightModifier::Documentation, binding_hash: None, }); line_start += range.len() - TextSize::try_from(pos).unwrap(); diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs index 94f466966a..f593ecad81 100644 --- a/crates/ra_ide/src/syntax_highlighting/tags.rs +++ b/crates/ra_ide/src/syntax_highlighting/tags.rs @@ -55,6 +55,7 @@ pub enum HighlightModifier { /// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is /// not. Definition, + Documentation, Mutable, Unsafe, } @@ -106,6 +107,7 @@ impl HighlightModifier { HighlightModifier::Attribute, HighlightModifier::ControlFlow, HighlightModifier::Definition, + HighlightModifier::Documentation, HighlightModifier::Mutable, HighlightModifier::Unsafe, ]; @@ -115,6 +117,7 @@ impl HighlightModifier { HighlightModifier::Attribute => "attribute", HighlightModifier::ControlFlow => "control", HighlightModifier::Definition => "declaration", + HighlightModifier::Documentation => "documentation", HighlightModifier::Mutable => "mutable", HighlightModifier::Unsafe => "unsafe", } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 2851b4d313..84d6f5155e 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -330,6 +330,7 @@ fn semantic_token_type_and_modifiers( let modifier = match modifier { HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER, HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION, + HighlightModifier::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION, HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW, HighlightModifier::Mutable => semantic_tokens::MUTABLE, HighlightModifier::Unsafe => semantic_tokens::UNSAFE,