From 9df78ec4a4e41ca94b25f292aba90e266f104f02 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 29 Mar 2021 21:23:45 +0200 Subject: Properly resolve intra doc links in hover and goto_definition --- crates/ide/src/hover.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 02a1a5b37..5a497e92d 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -115,10 +115,11 @@ pub(crate) fn hover( _ => ast::Comment::cast(token.clone()) .and_then(|comment| { + let def = doc_owner_to_def(&sema, &node)?; + let docs = def.docs(db)?; let (idl_range, link, ns) = - extract_positioned_link_from_comment(position.offset, &comment)?; + extract_positioned_link_from_comment(position.offset, &comment, docs)?; range = Some(idl_range); - let def = doc_owner_to_def(&sema, &node)?; resolve_doc_path_for_def(db, def, &link, ns) }) .map(Definition::ModuleDef), @@ -3812,23 +3813,33 @@ fn main() { fn hover_intra_doc_links() { check( r#" -/// This is the [`foo`](foo$0) function. -fn foo() {} + +pub mod theitem { + /// This is the item. Cool! + pub struct TheItem; +} + +/// Gives you a [`TheItem$0`]. +/// +/// [`TheItem`]: theitem::TheItem +pub fn gimme() -> theitem::TheItem { + theitem::TheItem +} "#, expect![[r#" - *[`foo`](foo)* + *[`TheItem`]* ```rust - test + test::theitem ``` ```rust - fn foo() + pub struct TheItem ``` --- - This is the [`foo`](https://docs.rs/test/*/test/fn.foo.html) function. + This is the item. Cool! "#]], ); } -- cgit v1.2.3 From bb56b7a75cfae8297535d55fbddbee9875cbc756 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 30 Mar 2021 18:27:16 +0200 Subject: Use new new docs string source mapping in goto_def and hover --- crates/ide/src/hover.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 5a497e92d..1e66219e4 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -1,6 +1,6 @@ use either::Either; use hir::{ - AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module, + AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, InFile, Module, ModuleDef, Semantics, }; use ide_db::{ @@ -16,8 +16,8 @@ use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, Toke use crate::{ display::{macro_label, TryToNav}, doc_links::{ - doc_owner_to_def, extract_positioned_link_from_comment, remove_links, - resolve_doc_path_for_def, rewrite_links, + doc_attributes, extract_definitions_from_markdown, remove_links, resolve_doc_path_for_def, + rewrite_links, }, markdown_remove::remove_markdown, markup::Markup, @@ -114,11 +114,18 @@ pub(crate) fn hover( ), _ => ast::Comment::cast(token.clone()) - .and_then(|comment| { - let def = doc_owner_to_def(&sema, &node)?; - let docs = def.docs(db)?; + .and_then(|_| { + let (attributes, def) = doc_attributes(&sema, &node)?; + let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?; 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); resolve_doc_path_for_def(db, def, &link, ns) }) -- cgit v1.2.3