From 8c0f454d115a5ce5fa4a9a0aa7116eac99a292f9 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 18 Mar 2021 15:22:27 +0100 Subject: Use a highlight modifier for intra doc links --- crates/ide/src/syntax_highlighting/html.rs | 2 +- crates/ide/src/syntax_highlighting/inject.rs | 44 ++++++++++++++++------ crates/ide/src/syntax_highlighting/tags.rs | 14 ++++--- .../test_data/highlight_assoc_functions.html | 2 +- .../test_data/highlight_doctest.html | 6 +-- .../test_data/highlight_extern_crate.html | 2 +- .../test_data/highlight_injection.html | 2 +- .../test_data/highlight_strings.html | 2 +- .../test_data/highlight_unsafe.html | 2 +- .../test_data/highlighting.html | 2 +- .../syntax_highlighting/test_data/injection.html | 2 +- .../test_data/rainbow_highlighting.html | 2 +- crates/rust-analyzer/src/semantic_tokens.rs | 2 +- crates/rust-analyzer/src/to_proto.rs | 2 +- 14 files changed, 55 insertions(+), 31 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/syntax_highlighting/html.rs b/crates/ide/src/syntax_highlighting/html.rs index 1d34731ab..5327af845 100644 --- a/crates/ide/src/syntax_highlighting/html.rs +++ b/crates/ide/src/syntax_highlighting/html.rs @@ -59,7 +59,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; } diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index e6dbd307e..f359eacf2 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs @@ -4,7 +4,7 @@ use std::{mem, ops::Range}; 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, -) -> bool { +) -> Option { 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) } diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index ce46e5127..93db79b89 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -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 @@ impl HlTag { 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", } } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html index 60c7518af..4635ea927 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html @@ -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; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 5d802a647..045162eb8 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -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 } } -/// [`Foo`](Foo) is a struct -/// [`all_the_links`](all_the_links) is this function +/// [`Foo`](Foo) is a struct +/// [`all_the_links`](all_the_links) is this function /// [`noop`](noop) is a macro below pub fn all_the_links() {} diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html b/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html index 4e312765c..ca9bb1e7d 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html @@ -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; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html index 57dfe7509..9215ddd9e 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html @@ -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; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index 75dbd0f14..e860d713e 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -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; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html index 423256a20..6a6555208 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html @@ -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; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index fffe8c0f5..8b2dd3b70 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -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; } diff --git a/crates/ide/src/syntax_highlighting/test_data/injection.html b/crates/ide/src/syntax_highlighting/test_data/injection.html index 34d8deb68..9ab46d05c 100644 --- a/crates/ide/src/syntax_highlighting/test_data/injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/injection.html @@ -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; } diff --git a/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html b/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html index d9ca3a4c4..666b0b228 100644 --- a/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html @@ -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; } diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index 0cb7d12a7..a3c5e9ccf 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -52,7 +52,6 @@ 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 @@ define_semantic_token_modifiers![ (UNSAFE, "unsafe"), (ATTRIBUTE_MODIFIER, "attribute"), (CALLABLE, "callable"), + (INTRA_DOC_LINK, "intraDocLink"), ]; #[derive(Default)] diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 70501618e..1ddea9278 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -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; -- cgit v1.2.3