]> git.lizzy.rs Git - rust.git/commitdiff
Use a highlight modifier for intra doc links
authorLukas Wirth <lukastw97@gmail.com>
Thu, 18 Mar 2021 14:22:27 +0000 (15:22 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Thu, 18 Mar 2021 21:32:07 +0000 (22:32 +0100)
14 files changed:
crates/ide/src/syntax_highlighting/html.rs
crates/ide/src/syntax_highlighting/inject.rs
crates/ide/src/syntax_highlighting/tags.rs
crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html
crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html
crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html
crates/ide/src/syntax_highlighting/test_data/highlight_injection.html
crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html
crates/ide/src/syntax_highlighting/test_data/highlighting.html
crates/ide/src/syntax_highlighting/test_data/injection.html
crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html
crates/rust-analyzer/src/semantic_tokens.rs
crates/rust-analyzer/src/to_proto.rs

index 1d34731ab99bcd10fe060ef0d1d7d73125fe56e4..5327af845c2a9a96a3e7a87315dd983bec38867b 100644 (file)
@@ -59,7 +59,7 @@ fn html_escape(text: &str) -> String {
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index e6dbd307ec866ba142d12e3f5110ec54b0c0f6b2..f359eacf2a0904ee3e597b17a4f44d90bff1d281 100644 (file)
@@ -4,7 +4,7 @@
 
 use either::Either;
 use hir::{HasAttrs, InFile, Semantics};
-use ide_db::{call_info::ActiveParameter, defs::Definition};
+use ide_db::{call_info::ActiveParameter, defs::Definition, SymbolKind};
 use syntax::{
     ast::{self, AstNode, AttrsOwner, DocCommentsOwner},
     match_ast, AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize,
@@ -225,13 +225,16 @@ pub(super) fn doc_comment(
                     intra_doc_links.extend(
                         extract_definitions_from_markdown(line)
                             .into_iter()
-                            .filter(|(link, ns, _)| {
-                                validate_intra_doc_link(sema.db, &def, link, *ns)
+                            .filter_map(|(link, ns, range)| {
+                                validate_intra_doc_link(sema.db, &def, &link, ns).zip(Some(range))
                             })
-                            .map(|(.., Range { start, end })| {
-                                TextRange::at(
-                                    prev_range_start + TextSize::from(start as u32),
-                                    TextSize::from((end - start) as u32),
+                            .map(|(def, Range { start, end })| {
+                                (
+                                    def,
+                                    TextRange::at(
+                                        prev_range_start + TextSize::from(start as u32),
+                                        TextSize::from((end - start) as u32),
+                                    ),
                                 )
                             }),
                     );
@@ -255,10 +258,13 @@ pub(super) fn doc_comment(
         }
     }
 
-    for range in intra_doc_links {
+    for (def, range) in intra_doc_links {
         hl.add(HlRange {
             range,
-            highlight: HlTag::IntraDocLink | HlMod::Documentation,
+            highlight: module_def_to_hl_tag(def)
+                | HlMod::Documentation
+                | HlMod::Injected
+                | HlMod::IntraDocLink,
             binding_hash: None,
         });
     }
@@ -317,7 +323,7 @@ fn validate_intra_doc_link(
     def: &Definition,
     link: &str,
     ns: Option<hir::Namespace>,
-) -> bool {
+) -> Option<hir::ModuleDef> {
     match def {
         Definition::ModuleDef(def) => match def {
             hir::ModuleDef::Module(it) => it.resolve_doc_path(db, &link, ns),
@@ -337,5 +343,21 @@ fn validate_intra_doc_link(
         | Definition::GenericParam(_)
         | Definition::Label(_) => None,
     }
-    .is_some()
+}
+
+fn module_def_to_hl_tag(def: hir::ModuleDef) -> HlTag {
+    let symbol = match def {
+        hir::ModuleDef::Module(_) => SymbolKind::Module,
+        hir::ModuleDef::Function(_) => SymbolKind::Function,
+        hir::ModuleDef::Adt(hir::Adt::Struct(_)) => SymbolKind::Struct,
+        hir::ModuleDef::Adt(hir::Adt::Enum(_)) => SymbolKind::Enum,
+        hir::ModuleDef::Adt(hir::Adt::Union(_)) => SymbolKind::Union,
+        hir::ModuleDef::Variant(_) => SymbolKind::Variant,
+        hir::ModuleDef::Const(_) => SymbolKind::Const,
+        hir::ModuleDef::Static(_) => SymbolKind::Static,
+        hir::ModuleDef::Trait(_) => SymbolKind::Trait,
+        hir::ModuleDef::TypeAlias(_) => SymbolKind::TypeAlias,
+        hir::ModuleDef::BuiltinType(_) => return HlTag::BuiltinType,
+    };
+    HlTag::Symbol(symbol)
 }
index ce46e5127a181619c72a2f45f3a0ac449b07024d..93db79b895dc2fa99ae15c34c6b4a40cf3594d80 100644 (file)
@@ -26,7 +26,6 @@ pub enum HlTag {
     Comment,
     EscapeSequence,
     FormatSpecifier,
-    IntraDocLink,
     Keyword,
     NumericLiteral,
     Operator,
@@ -57,6 +56,8 @@ pub enum HlMod {
     Static,
     /// Used for items in impls&traits.
     Associated,
+    /// Used for intra doc links in doc injection.
+    IntraDocLink,
 
     /// Keep this last!
     Unsafe,
@@ -117,7 +118,6 @@ fn as_str(self) -> &'static str {
             HlTag::Comment => "comment",
             HlTag::EscapeSequence => "escape_sequence",
             HlTag::FormatSpecifier => "format_specifier",
-            HlTag::IntraDocLink => "intra_doc_link",
             HlTag::Keyword => "keyword",
             HlTag::Punctuation(punct) => match punct {
                 HlPunct::Bracket => "bracket",
@@ -151,6 +151,7 @@ impl HlMod {
         HlMod::ControlFlow,
         HlMod::Definition,
         HlMod::Documentation,
+        HlMod::IntraDocLink,
         HlMod::Injected,
         HlMod::Mutable,
         HlMod::Consuming,
@@ -162,17 +163,18 @@ impl HlMod {
 
     fn as_str(self) -> &'static str {
         match self {
+            HlMod::Associated => "associated",
             HlMod::Attribute => "attribute",
+            HlMod::Callable => "callable",
+            HlMod::Consuming => "consuming",
             HlMod::ControlFlow => "control",
             HlMod::Definition => "declaration",
             HlMod::Documentation => "documentation",
             HlMod::Injected => "injected",
+            HlMod::IntraDocLink => "intra_doc_link",
             HlMod::Mutable => "mutable",
-            HlMod::Consuming => "consuming",
-            HlMod::Unsafe => "unsafe",
-            HlMod::Callable => "callable",
             HlMod::Static => "static",
-            HlMod::Associated => "associated",
+            HlMod::Unsafe => "unsafe",
         }
     }
 
index 60c7518af8fb826173916af89d11ddf4c1578881..4635ea927606f527f388c3c68de83f4e182ec761 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index 5d802a64700a41bb44f6d72d3925dcb7f759ed27..045162eb83bcc9f36193f91226c70f011a0f553b 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
@@ -99,8 +99,8 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     <span class="brace">}</span>
 <span class="brace">}</span>
 
-<span class="comment documentation">/// </span><span class="intra_doc_link documentation">[`Foo`](Foo)</span><span class="comment documentation"> is a struct</span>
-<span class="comment documentation">/// </span><span class="intra_doc_link documentation">[`all_the_links`](all_the_links)</span><span class="comment documentation"> is this function</span>
+<span class="comment documentation">/// </span><span class="struct documentation intra_doc_link injected">[`Foo`](Foo)</span><span class="comment documentation"> is a struct</span>
+<span class="comment documentation">/// </span><span class="function documentation intra_doc_link injected">[`all_the_links`](all_the_links)</span><span class="comment documentation"> is this function</span>
 <span class="comment documentation">/// [`noop`](noop) is a macro below</span>
 <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration">all_the_links</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
 
index 4e312765c0f79a0b5d5c797ede1cc36cef3d77c8..ca9bb1e7d2ceb2acceeeddca3cb236ffb4c336f4 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index 57dfe7509690b1917ab2207e3267d905bda2fc75..9215ddd9e394af96f0ade891349adf7db8afb026 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index 75dbd0f1422fc2570d7b0035ed736a618beb946a..e860d713e014bcdc401085323aae5ea0c6fdd47a 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index 423256a20200e573928eee3b5733767f9209abf5..6a6555208cb5afd863e73cd6e5b63430cbc22374 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index fffe8c0f577ec33e5350d9cb37e24355ff63aba9..8b2dd3b70ed5bc34d64b167f400d662bf5297a10 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index 34d8deb6846b2632a676cb495059eaf87ae7374a..9ab46d05c032c817975d3f45c0ec97043a5cfd98 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index d9ca3a4c46f73f494cac246870994256d633d04d..666b0b228dfa3648c0a46732f8bf899ea01a30f0 100644 (file)
@@ -7,7 +7,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .label              { color: #DFAF8F; font-style: italic; }
 .comment            { color: #7F9F7F; }
 .documentation      { color: #629755; }
-.intra_doc_link     { color: #A9C577; }
+.intra_doc_link     { font-style: italic; }
 .injected           { opacity: 0.65 ; }
 .struct, .enum      { color: #7CB8BB; }
 .enum_variant       { color: #BDE0F3; }
index 0cb7d12a76b223a24e6f85cdb5ff1de7ef56b8bb..a3c5e9ccf447f3797f1cc475f94d4b492827c6da 100644 (file)
@@ -52,7 +52,6 @@ macro_rules! define_semantic_token_types {
     (ESCAPE_SEQUENCE, "escapeSequence"),
     (FORMAT_SPECIFIER, "formatSpecifier"),
     (GENERIC, "generic"),
-    (INTRA_DOC_LINK, "intraDocLink"),
     (LABEL, "label"),
     (LIFETIME, "lifetime"),
     (PARENTHESIS, "parenthesis"),
@@ -90,6 +89,7 @@ macro_rules! define_semantic_token_modifiers {
     (UNSAFE, "unsafe"),
     (ATTRIBUTE_MODIFIER, "attribute"),
     (CALLABLE, "callable"),
+    (INTRA_DOC_LINK, "intraDocLink"),
 ];
 
 #[derive(Default)]
index 70501618e88bd9ef5c1cdfda97f30533959ba97c..1ddea927880dc58d293e91356a73c0410884a0b0 100644 (file)
@@ -443,7 +443,6 @@ fn semantic_token_type_and_modifiers(
         HlTag::Comment => lsp_types::SemanticTokenType::COMMENT,
         HlTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE,
         HlTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,
-        HlTag::IntraDocLink => semantic_tokens::INTRA_DOC_LINK,
         HlTag::Keyword => lsp_types::SemanticTokenType::KEYWORD,
         HlTag::None => semantic_tokens::GENERIC,
         HlTag::Operator => lsp_types::SemanticTokenType::OPERATOR,
@@ -474,6 +473,7 @@ fn semantic_token_type_and_modifiers(
             HlMod::Unsafe => semantic_tokens::UNSAFE,
             HlMod::Callable => semantic_tokens::CALLABLE,
             HlMod::Static => lsp_types::SemanticTokenModifier::STATIC,
+            HlMod::IntraDocLink => semantic_tokens::INTRA_DOC_LINK,
             HlMod::Associated => continue,
         };
         mods |= modifier;