Make hover work for intra doc links in macro invocations

This commit is contained in:
Lukas Wirth 2021-09-23 17:32:39 +02:00
parent 42eb4efb5b
commit d99adc5738
3 changed files with 139 additions and 24 deletions

View File

@ -17,7 +17,7 @@ use la_arena::ArenaMap;
use mbe::{syntax_node_to_token_tree, DelimiterKind};
use smallvec::{smallvec, SmallVec};
use syntax::{
ast::{self, AstNode, AttrsOwner},
ast::{self, AstNode, AttrsOwner, IsString},
match_ast, AstPtr, AstToken, SmolStr, SyntaxNode, TextRange, TextSize,
};
use tt::Subtree;
@ -610,6 +610,7 @@ pub struct DocsRangeMap {
}
impl DocsRangeMap {
/// Maps a [`TextRange`] relative to the documentation string back to its AST range
pub fn map(&self, range: TextRange) -> Option<InFile<TextRange>> {
let found = self.mapping.binary_search_by(|(probe, ..)| probe.ordering(range)).ok()?;
let (line_docs_range, idx, original_line_src_range) = self.mapping[found];
@ -621,8 +622,15 @@ impl DocsRangeMap {
let InFile { file_id, value: source } = self.source_map.source_of_id(idx);
match source {
Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here
// as well as for whats done in syntax highlight doc injection
Either::Left(attr) => {
let string = get_doc_string_in_attr(&attr)?;
let text_range = string.open_quote_text_range()?;
let range = TextRange::at(
text_range.end() + original_line_src_range.start() + relative_range.start(),
string.syntax().text_range().len().min(range.len()),
);
Some(InFile { file_id, value: range })
}
Either::Right(comment) => {
let text_range = comment.syntax().text_range();
let range = TextRange::at(
@ -638,6 +646,22 @@ impl DocsRangeMap {
}
}
fn get_doc_string_in_attr(it: &ast::Attr) -> Option<ast::String> {
match it.expr() {
// #[doc = lit]
Some(ast::Expr::Literal(lit)) => match lit.kind() {
ast::LiteralKind::String(it) => Some(it),
_ => None,
},
// #[cfg_attr(..., doc = "", ...)]
None => {
// FIXME: See highlight injection for what to do here
None
}
_ => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct AttrId {
is_doc_comment: bool,

View File

@ -1,4 +1,4 @@
use std::iter;
use std::{convert::TryFrom, iter};
use either::Either;
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
@ -14,8 +14,12 @@ use ide_db::{
use itertools::Itertools;
use stdx::format_to;
use syntax::{
algo, ast, display::fn_as_proc_macro_label, match_ast, AstNode, Direction, SyntaxKind::*,
SyntaxNode, SyntaxToken, T,
algo,
ast::{self, IsString},
display::fn_as_proc_macro_label,
match_ast, AstNode, AstToken, Direction,
SyntaxKind::*,
SyntaxNode, SyntaxToken, TextSize, T,
};
use crate::{
@ -115,36 +119,53 @@ pub(crate) fn hover(
})?;
let descended = sema.descend_into_macros_many(original_token.clone());
// FIXME handle doc attributes? TokenMap currently doesn't work with comments
if original_token.kind() == COMMENT {
let relative_comment_offset = offset - original_token.text_range().start();
// intra-doc links
// magic intra doc link handling
// FIXME: Lift this out to some other place, goto def wants this as well
let comment_prefix_len = match_ast! {
match original_token {
ast::Comment(comment) => TextSize::try_from(comment.prefix().len()).ok(),
ast::String(string) => original_token.ancestors().find_map(ast::Attr::cast)
.filter(|attr| attr.simple_name().as_deref() == Some("doc")).and_then(|_| string.open_quote_text_range().map(|it| it.len())),
_ => None,
}
};
if let Some(prefix_len) = comment_prefix_len {
cov_mark::hit!(no_highlight_on_comment_hover);
// offset relative to the comments contents
let original_start = original_token.text_range().start();
let relative_comment_offset = offset - original_start - prefix_len;
return descended.iter().find_map(|t| {
match t.kind() {
COMMENT => (),
TOKEN_TREE => {}
_ => return None,
}
let node = t.parent()?;
let absolute_comment_offset = t.text_range().start() + relative_comment_offset;
let (node, descended_prefix_len) = match_ast! {
match t {
ast::Comment(comment) => (t.parent()?, TextSize::try_from(comment.prefix().len()).ok()?),
ast::String(string) => (t.ancestors().skip_while(|n| n.kind() != ATTR).nth(1)?, string.open_quote_text_range()?.len()),
_ => return None,
}
};
let token_start = t.text_range().start();
let abs_in_expansion_offset = token_start + relative_comment_offset + descended_prefix_len;
let (attributes, def) = doc_attributes(sema, &node)?;
let (docs, doc_mapping) = attributes.docs_with_rangemap(sema.db)?;
let (idl_range, link, ns) = extract_definitions_from_docs(&docs).into_iter().find_map(
let (in_expansion_range, link, ns) = extract_definitions_from_docs(&docs).into_iter().find_map(
|(range, link, ns)| {
let mapped = doc_mapping.map(range)?;
(mapped.file_id == file_id.into()
&& mapped.value.contains(absolute_comment_offset))
.then(|| (mapped.value, link, ns))
(mapped.value.contains(abs_in_expansion_offset))
.then(|| (mapped.value, link, ns))
},
)?;
// get the relative range to the doc/attribute in the expansion
let in_expansion_relative_range = in_expansion_range - descended_prefix_len - token_start;
// Apply relative range to the original input comment
let absolute_range = in_expansion_relative_range + original_start + prefix_len;
let def = match resolve_doc_path_for_def(sema.db, def, &link, ns)? {
Either::Left(it) => Definition::ModuleDef(it),
Either::Right(it) => Definition::Macro(it),
};
let res = hover_for_definition(sema, file_id, def, &node, config)?;
Some(RangeInfo::new(idl_range, res))
Some(RangeInfo::new(absolute_range, res))
});
}
@ -4941,4 +4962,63 @@ fn foo() {
"#]],
);
}
#[test]
fn hover_intra_in_macro() {
check(
r#"
macro_rules! foo_macro {
($(#[$attr:meta])* $name:ident) => {
$(#[$attr])*
pub struct $name;
}
}
foo_macro!(
/// Doc comment for [`Foo$0`]
Foo
);
"#,
expect![[r#"
*[`Foo`]*
```rust
test
```
```rust
pub struct Foo
```
---
Doc comment for [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
"#]],
);
}
#[test]
fn hover_intra_in_attr() {
check(
r#"
#[doc = "Doc comment for [`Foo$0`]"]
pub struct Foo;
"#,
expect![[r#"
*[`Foo`]*
```rust
test
```
```rust
pub struct Foo
```
---
Doc comment for [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
"#]],
);
}
}

View File

@ -149,7 +149,18 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
let k: SyntaxKind = token.kind();
if k == COMMENT {
if let Some(tokens) = conv.convert_doc_comment(&token) {
result.extend(tokens);
// FIXME: There has to be a better way to do this
// Add the comments token id to the converted doc string
let id = conv.id_alloc().alloc(range);
result.extend(tokens.into_iter().map(|mut tt| {
if let tt::TokenTree::Subtree(sub) = &mut tt {
if let tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) = &mut sub.token_trees[2]
{
lit.id = id
}
}
tt
}));
}
continue;
}