mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Make hover work for intra doc links in macro invocations
This commit is contained in:
parent
42eb4efb5b
commit
d99adc5738
3 changed files with 139 additions and 24 deletions
|
@ -17,7 +17,7 @@ use la_arena::ArenaMap;
|
||||||
use mbe::{syntax_node_to_token_tree, DelimiterKind};
|
use mbe::{syntax_node_to_token_tree, DelimiterKind};
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode, AttrsOwner},
|
ast::{self, AstNode, AttrsOwner, IsString},
|
||||||
match_ast, AstPtr, AstToken, SmolStr, SyntaxNode, TextRange, TextSize,
|
match_ast, AstPtr, AstToken, SmolStr, SyntaxNode, TextRange, TextSize,
|
||||||
};
|
};
|
||||||
use tt::Subtree;
|
use tt::Subtree;
|
||||||
|
@ -610,6 +610,7 @@ pub struct DocsRangeMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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>> {
|
pub fn map(&self, range: TextRange) -> Option<InFile<TextRange>> {
|
||||||
let found = self.mapping.binary_search_by(|(probe, ..)| probe.ordering(range)).ok()?;
|
let found = self.mapping.binary_search_by(|(probe, ..)| probe.ordering(range)).ok()?;
|
||||||
let (line_docs_range, idx, original_line_src_range) = self.mapping[found];
|
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);
|
let InFile { file_id, value: source } = self.source_map.source_of_id(idx);
|
||||||
match source {
|
match source {
|
||||||
Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here
|
Either::Left(attr) => {
|
||||||
// as well as for whats done in syntax highlight doc injection
|
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) => {
|
Either::Right(comment) => {
|
||||||
let text_range = comment.syntax().text_range();
|
let text_range = comment.syntax().text_range();
|
||||||
let range = TextRange::at(
|
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub(crate) struct AttrId {
|
pub(crate) struct AttrId {
|
||||||
is_doc_comment: bool,
|
is_doc_comment: bool,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::iter;
|
use std::{convert::TryFrom, iter};
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
|
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
|
||||||
|
@ -14,8 +14,12 @@ use ide_db::{
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use stdx::format_to;
|
use stdx::format_to;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo, ast, display::fn_as_proc_macro_label, match_ast, AstNode, Direction, SyntaxKind::*,
|
algo,
|
||||||
SyntaxNode, SyntaxToken, T,
|
ast::{self, IsString},
|
||||||
|
display::fn_as_proc_macro_label,
|
||||||
|
match_ast, AstNode, AstToken, Direction,
|
||||||
|
SyntaxKind::*,
|
||||||
|
SyntaxNode, SyntaxToken, TextSize, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -115,36 +119,53 @@ pub(crate) fn hover(
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let descended = sema.descend_into_macros_many(original_token.clone());
|
let descended = sema.descend_into_macros_many(original_token.clone());
|
||||||
|
// magic intra doc link handling
|
||||||
// FIXME handle doc attributes? TokenMap currently doesn't work with comments
|
// FIXME: Lift this out to some other place, goto def wants this as well
|
||||||
if original_token.kind() == COMMENT {
|
let comment_prefix_len = match_ast! {
|
||||||
let relative_comment_offset = offset - original_token.text_range().start();
|
match original_token {
|
||||||
// intra-doc links
|
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);
|
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| {
|
return descended.iter().find_map(|t| {
|
||||||
match t.kind() {
|
let (node, descended_prefix_len) = match_ast! {
|
||||||
COMMENT => (),
|
match t {
|
||||||
TOKEN_TREE => {}
|
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,
|
_ => return None,
|
||||||
}
|
}
|
||||||
let node = t.parent()?;
|
};
|
||||||
let absolute_comment_offset = t.text_range().start() + relative_comment_offset;
|
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 (attributes, def) = doc_attributes(sema, &node)?;
|
||||||
let (docs, doc_mapping) = attributes.docs_with_rangemap(sema.db)?;
|
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)| {
|
|(range, link, ns)| {
|
||||||
let mapped = doc_mapping.map(range)?;
|
let mapped = doc_mapping.map(range)?;
|
||||||
(mapped.file_id == file_id.into()
|
(mapped.value.contains(abs_in_expansion_offset))
|
||||||
&& mapped.value.contains(absolute_comment_offset))
|
|
||||||
.then(|| (mapped.value, link, ns))
|
.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)? {
|
let def = match resolve_doc_path_for_def(sema.db, def, &link, ns)? {
|
||||||
Either::Left(it) => Definition::ModuleDef(it),
|
Either::Left(it) => Definition::ModuleDef(it),
|
||||||
Either::Right(it) => Definition::Macro(it),
|
Either::Right(it) => Definition::Macro(it),
|
||||||
};
|
};
|
||||||
let res = hover_for_definition(sema, file_id, def, &node, config)?;
|
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)
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,7 +149,18 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
|
||||||
let k: SyntaxKind = token.kind();
|
let k: SyntaxKind = token.kind();
|
||||||
if k == COMMENT {
|
if k == COMMENT {
|
||||||
if let Some(tokens) = conv.convert_doc_comment(&token) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue