mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Use new new docs string source mapping in goto_def and hover
This commit is contained in:
parent
9a327311e4
commit
bb56b7a75c
4 changed files with 72 additions and 118 deletions
|
@ -15,10 +15,7 @@ use ide_db::{
|
||||||
defs::{Definition, NameClass, NameRefClass},
|
defs::{Definition, NameClass, NameRefClass},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use syntax::{
|
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, SyntaxToken, TokenAtOffset, T};
|
||||||
ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TextSize,
|
|
||||||
TokenAtOffset, T,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{FilePosition, Semantics};
|
use crate::{FilePosition, Semantics};
|
||||||
|
|
||||||
|
@ -119,77 +116,22 @@ pub(crate) fn external_docs(
|
||||||
pub(crate) fn extract_definitions_from_markdown(
|
pub(crate) fn extract_definitions_from_markdown(
|
||||||
markdown: &str,
|
markdown: &str,
|
||||||
) -> Vec<(Range<usize>, String, Option<hir::Namespace>)> {
|
) -> Vec<(Range<usize>, String, Option<hir::Namespace>)> {
|
||||||
extract_definitions_from_markdown_(markdown, &mut broken_link_clone_cb).collect()
|
Parser::new_with_broken_link_callback(
|
||||||
}
|
markdown,
|
||||||
|
Options::empty(),
|
||||||
fn extract_definitions_from_markdown_<'a>(
|
Some(&mut broken_link_clone_cb),
|
||||||
markdown: &'a str,
|
)
|
||||||
cb: &'a mut dyn FnMut(BrokenLink<'_>) -> Option<(CowStr<'a>, CowStr<'a>)>,
|
.into_offset_iter()
|
||||||
) -> impl Iterator<Item = (Range<usize>, String, Option<hir::Namespace>)> + 'a {
|
.filter_map(|(event, range)| {
|
||||||
Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(cb))
|
if let Event::Start(Tag::Link(_, target, title)) = event {
|
||||||
.into_offset_iter()
|
let link = if target.is_empty() { title } else { target };
|
||||||
.filter_map(|(event, range)| {
|
let (link, ns) = parse_intra_doc_link(&link);
|
||||||
if let Event::Start(Tag::Link(_, target, title)) = event {
|
Some((range, link.to_string(), ns))
|
||||||
let link = if target.is_empty() { title } else { target };
|
} else {
|
||||||
let (link, ns) = parse_intra_doc_link(&link);
|
None
|
||||||
Some((range, link.to_string(), ns))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extracts a link from a comment at the given position returning the spanning range, link and
|
|
||||||
/// optionally it's namespace.
|
|
||||||
pub(crate) fn extract_positioned_link_from_comment(
|
|
||||||
position: TextSize,
|
|
||||||
comment: &ast::Comment,
|
|
||||||
docs: hir::Documentation,
|
|
||||||
) -> Option<(TextRange, String, Option<hir::Namespace>)> {
|
|
||||||
let doc_comment = comment.doc_comment()?.to_string() + "\n" + docs.as_str();
|
|
||||||
let comment_start =
|
|
||||||
comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32);
|
|
||||||
let len = comment.syntax().text_range().len().into();
|
|
||||||
let mut cb = broken_link_clone_cb;
|
|
||||||
// because pulldown_cmarks lifetimes are wrong we gotta dance around a few temporaries here
|
|
||||||
let res = extract_definitions_from_markdown_(&doc_comment, &mut cb)
|
|
||||||
.take_while(|&(Range { end, .. }, ..)| end < len)
|
|
||||||
.find_map(|(Range { start, end }, def_link, ns)| {
|
|
||||||
let range = TextRange::at(
|
|
||||||
comment_start + TextSize::from(start as u32),
|
|
||||||
TextSize::from((end - start) as u32),
|
|
||||||
);
|
|
||||||
range.contains(position).then(|| (range, def_link, ns))
|
|
||||||
});
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Turns a syntax node into it's [`Definition`] if it can hold docs.
|
|
||||||
pub(crate) fn doc_owner_to_def(
|
|
||||||
sema: &Semantics<RootDatabase>,
|
|
||||||
item: &SyntaxNode,
|
|
||||||
) -> Option<Definition> {
|
|
||||||
let res: hir::ModuleDef = match_ast! {
|
|
||||||
match item {
|
|
||||||
ast::SourceFile(_it) => sema.scope(item).module()?.into(),
|
|
||||||
ast::Fn(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Struct(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Enum(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Union(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Trait(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Const(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Static(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::TypeAlias(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Variant(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Trait(it) => sema.to_def(&it)?.into(),
|
|
||||||
ast::Impl(it) => return sema.to_def(&it).map(Definition::SelfType),
|
|
||||||
ast::Macro(it) => return sema.to_def(&it).map(Definition::Macro),
|
|
||||||
ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field),
|
|
||||||
ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field),
|
|
||||||
_ => return None,
|
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
Some(Definition::ModuleDef(res))
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_doc_path_for_def(
|
pub(crate) fn resolve_doc_path_for_def(
|
||||||
|
@ -219,6 +161,33 @@ pub(crate) fn resolve_doc_path_for_def(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn doc_attributes(
|
||||||
|
sema: &Semantics<RootDatabase>,
|
||||||
|
node: &SyntaxNode,
|
||||||
|
) -> Option<(hir::AttrsWithOwner, Definition)> {
|
||||||
|
match_ast! {
|
||||||
|
match node {
|
||||||
|
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
|
||||||
|
ast::Module(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
|
||||||
|
ast::Fn(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Function(def)))),
|
||||||
|
ast::Struct(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(def))))),
|
||||||
|
ast::Union(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Union(def))))),
|
||||||
|
ast::Enum(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(def))))),
|
||||||
|
ast::Variant(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Variant(def)))),
|
||||||
|
ast::Trait(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Trait(def)))),
|
||||||
|
ast::Static(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Static(def)))),
|
||||||
|
ast::Const(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Const(def)))),
|
||||||
|
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::TypeAlias(def)))),
|
||||||
|
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
|
||||||
|
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
|
||||||
|
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
|
||||||
|
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
|
||||||
|
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
|
||||||
|
_ => return None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn broken_link_clone_cb<'a, 'b>(link: BrokenLink<'a>) -> Option<(CowStr<'b>, CowStr<'b>)> {
|
fn broken_link_clone_cb<'a, 'b>(link: BrokenLink<'a>) -> Option<(CowStr<'b>, CowStr<'b>)> {
|
||||||
// These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
|
// These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
|
||||||
// this is fixed in the repo but not on the crates.io release yet
|
// this is fixed in the repo but not on the crates.io release yet
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::Semantics;
|
use hir::{InFile, Semantics};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
defs::{NameClass, NameRefClass},
|
defs::{NameClass, NameRefClass},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
|
@ -8,7 +8,7 @@ use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, Toke
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
display::TryToNav,
|
display::TryToNav,
|
||||||
doc_links::{doc_owner_to_def, extract_positioned_link_from_comment, resolve_doc_path_for_def},
|
doc_links::{doc_attributes, extract_definitions_from_markdown, resolve_doc_path_for_def},
|
||||||
FilePosition, NavigationTarget, RangeInfo,
|
FilePosition, NavigationTarget, RangeInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -30,11 +30,16 @@ pub(crate) fn goto_definition(
|
||||||
let original_token = pick_best(file.token_at_offset(position.offset))?;
|
let original_token = pick_best(file.token_at_offset(position.offset))?;
|
||||||
let token = sema.descend_into_macros(original_token.clone());
|
let token = sema.descend_into_macros(original_token.clone());
|
||||||
let parent = token.parent()?;
|
let parent = token.parent()?;
|
||||||
if let Some(comment) = ast::Comment::cast(token) {
|
if let Some(_) = ast::Comment::cast(token) {
|
||||||
let docs = doc_owner_to_def(&sema, &parent)?.docs(db)?;
|
let (attributes, def) = doc_attributes(&sema, &parent)?;
|
||||||
|
|
||||||
let (_, link, ns) = extract_positioned_link_from_comment(position.offset, &comment, docs)?;
|
let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
|
||||||
let def = doc_owner_to_def(&sema, &parent)?;
|
let (_, link, ns) =
|
||||||
|
extract_definitions_from_markdown(docs.as_str()).into_iter().find(|(range, ..)| {
|
||||||
|
doc_mapping.map(range.clone()).map_or(false, |InFile { file_id, value: range }| {
|
||||||
|
file_id == position.file_id.into() && range.contains(position.offset)
|
||||||
|
})
|
||||||
|
})?;
|
||||||
let nav = resolve_doc_path_for_def(db, def, &link, ns)?.try_to_nav(db)?;
|
let nav = resolve_doc_path_for_def(db, def, &link, ns)?.try_to_nav(db)?;
|
||||||
return Some(RangeInfo::new(original_token.text_range(), vec![nav]));
|
return Some(RangeInfo::new(original_token.text_range(), vec![nav]));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{
|
use hir::{
|
||||||
AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module,
|
AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, InFile, Module,
|
||||||
ModuleDef, Semantics,
|
ModuleDef, Semantics,
|
||||||
};
|
};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
|
@ -16,8 +16,8 @@ use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, Toke
|
||||||
use crate::{
|
use crate::{
|
||||||
display::{macro_label, TryToNav},
|
display::{macro_label, TryToNav},
|
||||||
doc_links::{
|
doc_links::{
|
||||||
doc_owner_to_def, extract_positioned_link_from_comment, remove_links,
|
doc_attributes, extract_definitions_from_markdown, remove_links, resolve_doc_path_for_def,
|
||||||
resolve_doc_path_for_def, rewrite_links,
|
rewrite_links,
|
||||||
},
|
},
|
||||||
markdown_remove::remove_markdown,
|
markdown_remove::remove_markdown,
|
||||||
markup::Markup,
|
markup::Markup,
|
||||||
|
@ -114,11 +114,18 @@ pub(crate) fn hover(
|
||||||
),
|
),
|
||||||
|
|
||||||
_ => ast::Comment::cast(token.clone())
|
_ => ast::Comment::cast(token.clone())
|
||||||
.and_then(|comment| {
|
.and_then(|_| {
|
||||||
let def = doc_owner_to_def(&sema, &node)?;
|
let (attributes, def) = doc_attributes(&sema, &node)?;
|
||||||
let docs = def.docs(db)?;
|
let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
|
||||||
let (idl_range, link, ns) =
|
let (idl_range, link, ns) =
|
||||||
extract_positioned_link_from_comment(position.offset, &comment, docs)?;
|
extract_definitions_from_markdown(docs.as_str()).into_iter().find_map(|(range, link, ns)| {
|
||||||
|
let InFile { file_id, value: range } = doc_mapping.map(range.clone())?;
|
||||||
|
if file_id == position.file_id.into() && range.contains(position.offset) {
|
||||||
|
Some((range, link, ns))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})?;
|
||||||
range = Some(idl_range);
|
range = Some(idl_range);
|
||||||
resolve_doc_path_for_def(db, def, &link, ns)
|
resolve_doc_path_for_def(db, def, &link, ns)
|
||||||
})
|
})
|
||||||
|
|
|
@ -3,15 +3,15 @@
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{HasAttrs, InFile, Semantics};
|
use hir::{InFile, Semantics};
|
||||||
use ide_db::{call_info::ActiveParameter, defs::Definition, SymbolKind};
|
use ide_db::{call_info::ActiveParameter, SymbolKind};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
match_ast, AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize,
|
AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
doc_links::{extract_definitions_from_markdown, resolve_doc_path_for_def},
|
doc_links::{doc_attributes, extract_definitions_from_markdown, resolve_doc_path_for_def},
|
||||||
Analysis, HlMod, HlRange, HlTag, RootDatabase,
|
Analysis, HlMod, HlRange, HlTag, RootDatabase,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -90,33 +90,6 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
|
||||||
"edition2021",
|
"edition2021",
|
||||||
];
|
];
|
||||||
|
|
||||||
fn doc_attributes<'node>(
|
|
||||||
sema: &Semantics<RootDatabase>,
|
|
||||||
node: &'node SyntaxNode,
|
|
||||||
) -> Option<(hir::AttrsWithOwner, Definition)> {
|
|
||||||
match_ast! {
|
|
||||||
match node {
|
|
||||||
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
|
|
||||||
ast::Module(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
|
|
||||||
ast::Fn(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Function(def)))),
|
|
||||||
ast::Struct(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(def))))),
|
|
||||||
ast::Union(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Union(def))))),
|
|
||||||
ast::Enum(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(def))))),
|
|
||||||
ast::Variant(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Variant(def)))),
|
|
||||||
ast::Trait(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Trait(def)))),
|
|
||||||
ast::Static(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Static(def)))),
|
|
||||||
ast::Const(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Const(def)))),
|
|
||||||
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::TypeAlias(def)))),
|
|
||||||
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
|
|
||||||
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
|
|
||||||
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
|
|
||||||
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
|
|
||||||
// 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.
|
/// Injection of syntax highlighting of doctests.
|
||||||
pub(super) fn doc_comment(
|
pub(super) fn doc_comment(
|
||||||
hl: &mut Highlights,
|
hl: &mut Highlights,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue