From 11e9bc60a2d9c22dbf51b7e1aa3d6e30a7006a35 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 16 Mar 2021 18:57:47 +0100 Subject: Move doc-comment highlight injection from AST to HIR --- crates/ide/src/syntax_highlighting.rs | 2 +- crates/ide/src/syntax_highlighting/inject.rs | 65 ++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 15 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 870146d24..ba3447b3a 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -150,7 +150,7 @@ fn traverse( WalkEvent::Enter(it) => it, WalkEvent::Leave(it) => { if let Some(node) = it.as_node() { - inject::doc_comment(hl, node); + inject::doc_comment(hl, sema, node); } continue; } diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index 8cdc3688f..5b065c09f 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs @@ -1,8 +1,12 @@ //! "Recursive" Syntax highlighting for code in doctests and fixtures. -use hir::Semantics; +use either::Either; +use hir::{HasAttrs, Semantics}; use ide_db::call_info::ActiveParameter; -use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; +use syntax::{ + ast::{self, AstNode, AttrsOwner}, + match_ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize, +}; use crate::{Analysis, HlMod, HlRange, HlTag, RootDatabase}; @@ -81,16 +85,46 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[ "edition2021", ]; +fn doc_attributes<'node>( + sema: &Semantics, + node: &'node SyntaxNode, +) -> Option<(Box, hir::Attrs)> { + match_ast! { + match node { + ast::SourceFile(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Fn(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Struct(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Union(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::RecordField(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::TupleField(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Enum(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Variant(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Trait(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Module(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Static(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Const(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::TypeAlias(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::Impl(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::MacroRules(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::MacroRules(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + // ast::MacroDef(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + // ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + _ => return None + } + } +} + /// Injection of syntax highlighting of doctests. -pub(super) fn doc_comment(hl: &mut Highlights, node: &SyntaxNode) { - let doc_comments = node - .children_with_tokens() - .filter_map(|it| it.into_token().and_then(ast::Comment::cast)) - .filter(|it| it.kind().doc.is_some()); +pub(super) fn doc_comment(hl: &mut Highlights, sema: &Semantics, node: &SyntaxNode) { + let (owner, attributes) = match doc_attributes(sema, node) { + Some(it) => it, + None => return, + }; - if !doc_comments.clone().any(|it| it.text().contains(RUSTDOC_FENCE)) { + if attributes.docs().map_or(true, |docs| !String::from(docs).contains(RUSTDOC_FENCE)) { return; } + let doc_comments = attributes.by_key("doc").attrs().map(|attr| attr.to_src(&*owner)); let mut inj = Injector::default(); inj.add_unmapped("fn doctest() {\n"); @@ -102,11 +136,17 @@ pub(super) fn doc_comment(hl: &mut Highlights, node: &SyntaxNode) { // spanning comment ranges. let mut new_comments = Vec::new(); for comment in doc_comments { - match comment.text().find(RUSTDOC_FENCE) { + let (line, range, prefix) = match &comment { + Either::Left(_) => continue, // FIXME + Either::Right(comment) => { + (comment.text(), comment.syntax().text_range(), comment.prefix()) + } + }; + match line.find(RUSTDOC_FENCE) { Some(idx) => { is_codeblock = !is_codeblock; // Check whether code is rust by inspecting fence guards - let guards = &comment.text()[idx + RUSTDOC_FENCE.len()..]; + let guards = &line[idx + RUSTDOC_FENCE.len()..]; let is_rust = guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim())); is_doctest = is_codeblock && is_rust; @@ -116,10 +156,7 @@ pub(super) fn doc_comment(hl: &mut Highlights, node: &SyntaxNode) { None => (), } - let line: &str = comment.text(); - let range = comment.syntax().text_range(); - - let mut pos = TextSize::of(comment.prefix()); + let mut pos = TextSize::of(prefix); // whitespace after comment is ignored if let Some(ws) = line[pos.into()..].chars().next().filter(|c| c.is_whitespace()) { pos += TextSize::of(ws); -- cgit v1.2.3 From acc6458390eb7ed5947fe8b6a0628b4a9018fad1 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 16 Mar 2021 19:06:58 +0100 Subject: Replace trait object boxing with extra AttrsOwnerNode --- crates/ide/src/syntax_highlighting/inject.rs | 67 ++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 19 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index 5b065c09f..086db40e5 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs @@ -4,7 +4,7 @@ use either::Either; use hir::{HasAttrs, Semantics}; use ide_db::call_info::ActiveParameter; use syntax::{ - ast::{self, AstNode, AttrsOwner}, + ast::{self, AstNode, AttrsOwner, DocCommentsOwner}, match_ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize, }; @@ -85,28 +85,57 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[ "edition2021", ]; +// Basically an owned dyn AttrsOwner without extra Boxing +struct AttrsOwnerNode { + node: SyntaxNode, +} + +impl AttrsOwnerNode { + fn new(node: N) -> Self { + AttrsOwnerNode { node: node.syntax().clone() } + } +} + +impl AttrsOwner for AttrsOwnerNode {} +impl AstNode for AttrsOwnerNode { + fn can_cast(_: syntax::SyntaxKind) -> bool + where + Self: Sized, + { + false + } + fn cast(_: SyntaxNode) -> Option + where + Self: Sized, + { + None + } + fn syntax(&self) -> &SyntaxNode { + &self.node + } +} + fn doc_attributes<'node>( sema: &Semantics, node: &'node SyntaxNode, -) -> Option<(Box, hir::Attrs)> { +) -> Option<(AttrsOwnerNode, hir::Attrs)> { match_ast! { match node { - ast::SourceFile(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Fn(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Struct(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Union(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::RecordField(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::TupleField(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Enum(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Variant(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Trait(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Module(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Static(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Const(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::TypeAlias(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::Impl(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::MacroRules(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), - ast::MacroRules(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), + ast::SourceFile(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Fn(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Struct(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Union(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::RecordField(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::TupleField(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Enum(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Variant(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Trait(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Module(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Static(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Const(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::TypeAlias(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::Impl(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), + ast::MacroRules(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))), // ast::MacroDef(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), // ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), _ => return None @@ -124,7 +153,7 @@ pub(super) fn doc_comment(hl: &mut Highlights, sema: &Semantics, n if attributes.docs().map_or(true, |docs| !String::from(docs).contains(RUSTDOC_FENCE)) { return; } - let doc_comments = attributes.by_key("doc").attrs().map(|attr| attr.to_src(&*owner)); + let doc_comments = attributes.by_key("doc").attrs().map(|attr| attr.to_src(&owner)); let mut inj = Injector::default(); inj.add_unmapped("fn doctest() {\n"); -- cgit v1.2.3 From c766492d2625dba65c3bd933841c71938f6dc747 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 16 Mar 2021 21:05:07 +0100 Subject: Properly handle doc attributes in doc-comment highlight injection --- crates/ide/src/syntax_highlighting/inject.rs | 48 +++++++++++++++++++--- .../test_data/highlight_doctest.html | 18 +++++++- crates/ide/src/syntax_highlighting/tests.rs | 16 ++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index 086db40e5..0f1de4fb8 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs @@ -5,7 +5,7 @@ use hir::{HasAttrs, Semantics}; use ide_db::call_info::ActiveParameter; use syntax::{ ast::{self, AstNode, AttrsOwner, DocCommentsOwner}, - match_ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize, + match_ast, AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize, }; use crate::{Analysis, HlMod, HlRange, HlTag, RootDatabase}; @@ -153,7 +153,6 @@ pub(super) fn doc_comment(hl: &mut Highlights, sema: &Semantics, n if attributes.docs().map_or(true, |docs| !String::from(docs).contains(RUSTDOC_FENCE)) { return; } - let doc_comments = attributes.by_key("doc").attrs().map(|attr| attr.to_src(&owner)); let mut inj = Injector::default(); inj.add_unmapped("fn doctest() {\n"); @@ -164,13 +163,28 @@ pub(super) fn doc_comment(hl: &mut Highlights, sema: &Semantics, n // Replace the original, line-spanning comment ranges by new, only comment-prefix // spanning comment ranges. let mut new_comments = Vec::new(); - for comment in doc_comments { - let (line, range, prefix) = match &comment { - Either::Left(_) => continue, // FIXME + let mut string; + for attr in attributes.by_key("doc").attrs() { + let src = attr.to_src(&owner); + let (line, range, prefix) = match &src { + Either::Left(it) => { + string = match find_doc_string_in_attr(attr, it) { + Some(it) => it, + None => continue, + }; + let text_range = string.syntax().text_range(); + let text_range = TextRange::new( + text_range.start() + TextSize::from(1), + text_range.end() - TextSize::from(1), + ); + let text = string.text(); + (&text[1..text.len() - 1], text_range, "") + } Either::Right(comment) => { (comment.text(), comment.syntax().text_range(), comment.prefix()) } }; + match line.find(RUSTDOC_FENCE) { Some(idx) => { is_codeblock = !is_codeblock; @@ -222,3 +236,27 @@ pub(super) fn doc_comment(hl: &mut Highlights, sema: &Semantics, n }); } } + +fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option { + match it.literal() { + // #[doc = lit] + Some(lit) => match lit.kind() { + ast::LiteralKind::String(it) => Some(it), + _ => None, + }, + // #[cfg_attr(..., doc = "", ...)] + None => { + // We gotta hunt the string token manually here + let text = attr.string_value()?; + // FIXME: We just pick the first string literal that has the same text as the doc attribute + // This means technically we might highlight the wrong one + it.syntax() + .descendants_with_tokens() + .filter_map(NodeOrToken::into_token) + .filter_map(ast::String::cast) + .find(|string| { + string.text().get(1..string.text().len() - 1).map_or(false, |it| it == text) + }) + } + } +} 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 5e877df88..45817faf9 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -105,4 +105,20 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd ($expr:expr) => { $expr } -} \ No newline at end of file +} + +/// ```rust +/// let _ = example(&[1, 2, 3]); +/// ``` +/// +/// ``` +/// loop {} +#[cfg_attr(not(feature = "false"), doc = "loop {}")] +#[doc = "loop {}"] +/// ``` +/// +#[cfg_attr(feature = "alloc", doc = "```rust")] +#[cfg_attr(not(feature = "alloc"), doc = "```ignore")] +/// let _ = example(&alloc::vec![1, 2, 3]); +/// ``` +pub fn mix_and_match() {} \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 9d0cd1af5..a5ef2d29b 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -541,6 +541,22 @@ macro_rules! noop { $expr } } + +/// ```rust +/// let _ = example(&[1, 2, 3]); +/// ``` +/// +/// ``` +/// loop {} +#[cfg_attr(not(feature = "false"), doc = "loop {}")] +#[doc = "loop {}"] +/// ``` +/// +#[cfg_attr(feature = "alloc", doc = "```rust")] +#[cfg_attr(not(feature = "alloc"), doc = "```ignore")] +/// let _ = example(&alloc::vec![1, 2, 3]); +/// ``` +pub fn mix_and_match() {} "# .trim(), expect_file!["./test_data/highlight_doctest.html"], -- cgit v1.2.3 From cdfb5c353f09138540ae66a2eb80a6a81802bbd6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 17 Mar 2021 11:22:40 +0100 Subject: Remove quadratic attr source lookup --- crates/ide/src/syntax_highlighting/inject.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ide') diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index 0f1de4fb8..d4c367f66 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs @@ -153,6 +153,7 @@ pub(super) fn doc_comment(hl: &mut Highlights, sema: &Semantics, n if attributes.docs().map_or(true, |docs| !String::from(docs).contains(RUSTDOC_FENCE)) { return; } + let attrs_source_map = attributes.source_map(&owner); let mut inj = Injector::default(); inj.add_unmapped("fn doctest() {\n"); @@ -165,7 +166,7 @@ pub(super) fn doc_comment(hl: &mut Highlights, sema: &Semantics, n let mut new_comments = Vec::new(); let mut string; for attr in attributes.by_key("doc").attrs() { - let src = attr.to_src(&owner); + let src = attrs_source_map.source_of(&attr); let (line, range, prefix) = match &src { Either::Left(it) => { string = match find_doc_string_in_attr(attr, it) { -- cgit v1.2.3