mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 20:35:09 +00:00
Merge #9637
9637: Overhaul doc_links testing infra r=Veykril a=Veykril and fix several issues with current implementation. Fixes #9617 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
4705df44c7
9 changed files with 633 additions and 778 deletions
|
@ -114,16 +114,15 @@ fn resolve_doc_path(
|
||||||
let path = ast::Path::parse(link).ok()?;
|
let path = ast::Path::parse(link).ok()?;
|
||||||
let modpath = ModPath::from_src(db.upcast(), path, &Hygiene::new_unhygienic()).unwrap();
|
let modpath = ModPath::from_src(db.upcast(), path, &Hygiene::new_unhygienic()).unwrap();
|
||||||
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
|
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
|
||||||
if resolved == PerNs::none() {
|
let resolved = if resolved == PerNs::none() {
|
||||||
if let Some(trait_id) = resolver.resolve_module_path_in_trait_items(db.upcast(), &modpath) {
|
resolver.resolve_module_path_in_trait_assoc_items(db.upcast(), &modpath)?
|
||||||
return Some(ModuleDefId::TraitId(trait_id));
|
} else {
|
||||||
};
|
resolved
|
||||||
}
|
|
||||||
let def = match ns {
|
|
||||||
Some(Namespace::Types) => resolved.take_types()?,
|
|
||||||
Some(Namespace::Values) => resolved.take_values()?,
|
|
||||||
Some(Namespace::Macros) => return None,
|
|
||||||
None => resolved.iter_items().find_map(|it| it.as_module_def_id())?,
|
|
||||||
};
|
};
|
||||||
Some(def)
|
match ns {
|
||||||
|
Some(Namespace::Types) => resolved.take_types(),
|
||||||
|
Some(Namespace::Values) => resolved.take_values(),
|
||||||
|
Some(Namespace::Macros) => None,
|
||||||
|
None => resolved.iter_items().find_map(|it| it.as_module_def_id()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -295,12 +295,19 @@ impl ModuleDef {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> {
|
pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> {
|
||||||
let mut segments = vec![self.name(db)?.to_string()];
|
let mut segments = vec![self.name(db)?];
|
||||||
for m in self.module(db)?.path_to_root(db) {
|
for m in self.module(db)?.path_to_root(db) {
|
||||||
segments.extend(m.name(db).map(|it| it.to_string()))
|
segments.extend(m.name(db))
|
||||||
}
|
}
|
||||||
segments.reverse();
|
segments.reverse();
|
||||||
Some(segments.join("::"))
|
Some(segments.into_iter().join("::"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn canonical_module_path(
|
||||||
|
&self,
|
||||||
|
db: &dyn HirDatabase,
|
||||||
|
) -> Option<impl Iterator<Item = Module>> {
|
||||||
|
self.module(db).map(|it| it.path_to_root(db).into_iter().rev())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
|
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
|
||||||
|
|
|
@ -20,10 +20,10 @@ use crate::{
|
||||||
path::{ModPath, PathKind},
|
path::{ModPath, PathKind},
|
||||||
per_ns::PerNs,
|
per_ns::PerNs,
|
||||||
visibility::{RawVisibility, Visibility},
|
visibility::{RawVisibility, Visibility},
|
||||||
AdtId, AssocContainerId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId,
|
AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, EnumId,
|
||||||
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, LifetimeParamId, LocalModuleId,
|
EnumVariantId, FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, LifetimeParamId,
|
||||||
Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
|
LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
|
||||||
VariantId,
|
TypeParamId, VariantId,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
|
@ -144,15 +144,28 @@ impl Resolver {
|
||||||
self.resolve_module_path(db, path, BuiltinShadowMode::Module)
|
self.resolve_module_path(db, path, BuiltinShadowMode::Module)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_module_path_in_trait_items(
|
pub fn resolve_module_path_in_trait_assoc_items(
|
||||||
&self,
|
&self,
|
||||||
db: &dyn DefDatabase,
|
db: &dyn DefDatabase,
|
||||||
path: &ModPath,
|
path: &ModPath,
|
||||||
) -> Option<TraitId> {
|
) -> Option<PerNs> {
|
||||||
let (item_map, module) = self.module_scope()?;
|
let (item_map, module) = self.module_scope()?;
|
||||||
let (module_res, ..) = item_map.resolve_path(db, module, path, BuiltinShadowMode::Module);
|
let (module_res, idx) = item_map.resolve_path(db, module, path, BuiltinShadowMode::Module);
|
||||||
match module_res.take_types()? {
|
match module_res.take_types()? {
|
||||||
ModuleDefId::TraitId(it) => Some(it),
|
ModuleDefId::TraitId(it) => {
|
||||||
|
let idx = idx?;
|
||||||
|
let unresolved = &path.segments()[idx..];
|
||||||
|
let assoc = match unresolved {
|
||||||
|
[it] => it,
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
let &(_, assoc) = db.trait_data(it).items.iter().find(|(n, _)| n == assoc)?;
|
||||||
|
Some(match assoc {
|
||||||
|
AssocItemId::FunctionId(it) => PerNs::values(it.into(), Visibility::Public),
|
||||||
|
AssocItemId::ConstId(it) => PerNs::values(it.into(), Visibility::Public),
|
||||||
|
AssocItemId::TypeAliasId(it) => PerNs::types(it.into(), Visibility::Public),
|
||||||
|
})
|
||||||
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,16 @@
|
||||||
//! Extracts, resolves and rewrites links and intra-doc links in markdown documentation.
|
//! Extracts, resolves and rewrites links and intra-doc links in markdown documentation.
|
||||||
|
|
||||||
use std::{
|
mod intra_doc_links;
|
||||||
convert::{TryFrom, TryInto},
|
|
||||||
iter::once,
|
use std::convert::{TryFrom, TryInto};
|
||||||
};
|
|
||||||
|
|
||||||
use itertools::Itertools;
|
|
||||||
use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
|
use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
|
||||||
use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions};
|
use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions};
|
||||||
|
use stdx::format_to;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use hir::{
|
use hir::{
|
||||||
db::{DefDatabase, HirDatabase},
|
db::HirDatabase, Adt, AsAssocItem, AssocItem, AssocItemContainer, Crate, HasAttrs, ModuleDef,
|
||||||
Adt, AsAssocItem, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs, ModuleDef,
|
|
||||||
};
|
};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
defs::{Definition, NameClass, NameRefClass},
|
defs::{Definition, NameClass, NameRefClass},
|
||||||
|
@ -21,17 +19,20 @@ use ide_db::{
|
||||||
};
|
};
|
||||||
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, TextRange, T};
|
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, TextRange, T};
|
||||||
|
|
||||||
use crate::{FilePosition, Semantics};
|
use crate::{
|
||||||
|
doc_links::intra_doc_links::{parse_intra_doc_link, strip_prefixes_suffixes},
|
||||||
|
FilePosition, Semantics,
|
||||||
|
};
|
||||||
|
|
||||||
pub(crate) type DocumentationLink = String;
|
pub(crate) type DocumentationLink = String;
|
||||||
|
|
||||||
/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
|
/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
|
||||||
pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String {
|
pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: Definition) -> String {
|
||||||
let mut cb = broken_link_clone_cb;
|
let mut cb = broken_link_clone_cb;
|
||||||
let doc =
|
let doc =
|
||||||
Parser::new_with_broken_link_callback(markdown, Options::ENABLE_TASKLISTS, Some(&mut cb));
|
Parser::new_with_broken_link_callback(markdown, Options::ENABLE_TASKLISTS, Some(&mut cb));
|
||||||
|
|
||||||
let doc = map_links(doc, |target, title: &str| {
|
let doc = map_links(doc, |target, title| {
|
||||||
// This check is imperfect, there's some overlap between valid intra-doc links
|
// This check is imperfect, there's some overlap between valid intra-doc links
|
||||||
// and valid URLs so we choose to be too eager to try to resolve what might be
|
// and valid URLs so we choose to be too eager to try to resolve what might be
|
||||||
// a URL.
|
// a URL.
|
||||||
|
@ -41,10 +42,10 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Defi
|
||||||
// Two possibilities:
|
// Two possibilities:
|
||||||
// * path-based links: `../../module/struct.MyStruct.html`
|
// * path-based links: `../../module/struct.MyStruct.html`
|
||||||
// * module-based links (AKA intra-doc links): `super::super::module::MyStruct`
|
// * module-based links (AKA intra-doc links): `super::super::module::MyStruct`
|
||||||
if let Some(rewritten) = rewrite_intra_doc_link(db, *definition, target, title) {
|
if let Some(rewritten) = rewrite_intra_doc_link(db, definition, target, title) {
|
||||||
return rewritten;
|
return rewritten;
|
||||||
}
|
}
|
||||||
if let Definition::ModuleDef(def) = *definition {
|
if let Definition::ModuleDef(def) = definition {
|
||||||
if let Some(target) = rewrite_url_link(db, def, target) {
|
if let Some(target) = rewrite_url_link(db, def, target) {
|
||||||
return (target, title.to_string());
|
return (target, title.to_string());
|
||||||
}
|
}
|
||||||
|
@ -54,9 +55,13 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Defi
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
let mut options = CmarkOptions::default();
|
cmark_with_options(
|
||||||
options.code_block_backticks = 3;
|
doc,
|
||||||
cmark_with_options(doc, &mut out, None, options).ok();
|
&mut out,
|
||||||
|
None,
|
||||||
|
CmarkOptions { code_block_backticks: 3, ..Default::default() },
|
||||||
|
)
|
||||||
|
.ok();
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,9 +77,9 @@ pub(crate) fn remove_links(markdown: &str) -> String {
|
||||||
};
|
};
|
||||||
let doc = Parser::new_with_broken_link_callback(markdown, opts, Some(&mut cb));
|
let doc = Parser::new_with_broken_link_callback(markdown, opts, Some(&mut cb));
|
||||||
let doc = doc.filter_map(move |evt| match evt {
|
let doc = doc.filter_map(move |evt| match evt {
|
||||||
Event::Start(Tag::Link(link_type, ref target, ref title)) => {
|
Event::Start(Tag::Link(link_type, target, title)) => {
|
||||||
if link_type == LinkType::Inline && target.contains("://") {
|
if link_type == LinkType::Inline && target.contains("://") {
|
||||||
Some(Event::Start(Tag::Link(link_type, target.clone(), title.clone())))
|
Some(Event::Start(Tag::Link(link_type, target, title)))
|
||||||
} else {
|
} else {
|
||||||
drop_link = true;
|
drop_link = true;
|
||||||
None
|
None
|
||||||
|
@ -88,9 +93,13 @@ pub(crate) fn remove_links(markdown: &str) -> String {
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
let mut options = CmarkOptions::default();
|
cmark_with_options(
|
||||||
options.code_block_backticks = 3;
|
doc,
|
||||||
cmark_with_options(doc, &mut out, None, options).ok();
|
&mut out,
|
||||||
|
None,
|
||||||
|
CmarkOptions { code_block_backticks: 3, ..Default::default() },
|
||||||
|
)
|
||||||
|
.ok();
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,10 +108,10 @@ pub(crate) fn external_docs(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
position: &FilePosition,
|
position: &FilePosition,
|
||||||
) -> Option<DocumentationLink> {
|
) -> Option<DocumentationLink> {
|
||||||
let sema = Semantics::new(db);
|
let sema = &Semantics::new(db);
|
||||||
let file = sema.parse(position.file_id).syntax().clone();
|
let file = sema.parse(position.file_id).syntax().clone();
|
||||||
let token = pick_best_token(file.token_at_offset(position.offset), |kind| match kind {
|
let token = pick_best_token(file.token_at_offset(position.offset), |kind| match kind {
|
||||||
IDENT | INT_NUMBER => 3,
|
IDENT | INT_NUMBER | T![self] => 3,
|
||||||
T!['('] | T![')'] => 2,
|
T!['('] | T![')'] => 2,
|
||||||
kind if kind.is_trivia() => 0,
|
kind if kind.is_trivia() => 0,
|
||||||
_ => 1,
|
_ => 1,
|
||||||
|
@ -112,13 +121,13 @@ pub(crate) fn external_docs(
|
||||||
let node = token.parent()?;
|
let node = token.parent()?;
|
||||||
let definition = match_ast! {
|
let definition = match_ast! {
|
||||||
match node {
|
match node {
|
||||||
ast::NameRef(name_ref) => match NameRefClass::classify(&sema, &name_ref)? {
|
ast::NameRef(name_ref) => match NameRefClass::classify(sema, &name_ref)? {
|
||||||
NameRefClass::Definition(def) => def,
|
NameRefClass::Definition(def) => def,
|
||||||
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
|
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
|
||||||
Definition::Field(field_ref)
|
Definition::Field(field_ref)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ast::Name(name) => match NameClass::classify(&sema, &name)? {
|
ast::Name(name) => match NameClass::classify(sema, &name)? {
|
||||||
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
|
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
|
||||||
NameClass::PatFieldShorthand { local_def: _, field_ref } => Definition::Field(field_ref),
|
NameClass::PatFieldShorthand { local_def: _, field_ref } => Definition::Field(field_ref),
|
||||||
},
|
},
|
||||||
|
@ -129,28 +138,27 @@ pub(crate) fn external_docs(
|
||||||
get_doc_link(db, definition)
|
get_doc_link(db, definition)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts all links from a given markdown text.
|
/// Extracts all links from a given markdown text returning the definition text range, link-text
|
||||||
pub(crate) fn extract_definitions_from_markdown(
|
/// and the namespace if known.
|
||||||
markdown: &str,
|
pub(crate) fn extract_definitions_from_docs(
|
||||||
|
docs: &hir::Documentation,
|
||||||
) -> Vec<(TextRange, String, Option<hir::Namespace>)> {
|
) -> Vec<(TextRange, String, Option<hir::Namespace>)> {
|
||||||
Parser::new_with_broken_link_callback(
|
Parser::new_with_broken_link_callback(
|
||||||
markdown,
|
docs.as_str(),
|
||||||
Options::ENABLE_TASKLISTS,
|
Options::ENABLE_TASKLISTS,
|
||||||
Some(&mut broken_link_clone_cb),
|
Some(&mut broken_link_clone_cb),
|
||||||
)
|
)
|
||||||
.into_offset_iter()
|
.into_offset_iter()
|
||||||
.filter_map(|(event, range)| {
|
.filter_map(|(event, range)| match event {
|
||||||
if let Event::Start(Tag::Link(_, target, title)) = event {
|
Event::Start(Tag::Link(_, target, _)) => {
|
||||||
let link = if target.is_empty() { title } else { target };
|
let (link, ns) = parse_intra_doc_link(&target);
|
||||||
let (link, ns) = parse_intra_doc_link(&link);
|
|
||||||
Some((
|
Some((
|
||||||
TextRange::new(range.start.try_into().ok()?, range.end.try_into().ok()?),
|
TextRange::new(range.start.try_into().ok()?, range.end.try_into().ok()?),
|
||||||
link.to_string(),
|
link.to_string(),
|
||||||
ns,
|
ns,
|
||||||
))
|
))
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
_ => None,
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
@ -226,83 +234,45 @@ fn broken_link_clone_cb<'a, 'b>(link: BrokenLink<'a>) -> Option<(CowStr<'b>, Cow
|
||||||
// This should cease to be a problem if RFC2988 (Stable Rustdoc URLs) is implemented
|
// This should cease to be a problem if RFC2988 (Stable Rustdoc URLs) is implemented
|
||||||
// https://github.com/rust-lang/rfcs/pull/2988
|
// https://github.com/rust-lang/rfcs/pull/2988
|
||||||
fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
|
fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
|
||||||
// Get the outermost definition for the module def. This is used to resolve the public path to the type,
|
let (target, frag) = match definition {
|
||||||
// then we can join the method, field, etc onto it if required.
|
Definition::ModuleDef(def) => {
|
||||||
let target_def: ModuleDef = match definition {
|
if let Some(assoc_item) = def.as_assoc_item(db) {
|
||||||
Definition::ModuleDef(def) => match def {
|
let def = match assoc_item.container(db) {
|
||||||
ModuleDef::Function(f) => f
|
AssocItemContainer::Trait(t) => t.into(),
|
||||||
.as_assoc_item(db)
|
AssocItemContainer::Impl(i) => i.self_ty(db).as_adt()?.into(),
|
||||||
.and_then(|assoc| match assoc.container(db) {
|
};
|
||||||
AssocItemContainer::Trait(t) => Some(t.into()),
|
let frag = get_assoc_item_fragment(db, assoc_item)?;
|
||||||
AssocItemContainer::Impl(impl_) => {
|
(def, Some(frag))
|
||||||
impl_.self_ty(db).as_adt().map(|adt| adt.into())
|
} else {
|
||||||
}
|
(def, None)
|
||||||
})
|
|
||||||
.unwrap_or_else(|| def),
|
|
||||||
def => def,
|
|
||||||
},
|
|
||||||
Definition::Field(f) => f.parent_def(db).into(),
|
|
||||||
// FIXME: Handle macros
|
|
||||||
_ => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let ns = ItemInNs::from(target_def);
|
|
||||||
|
|
||||||
let krate = match definition {
|
|
||||||
// Definition::module gives back the parent module, we don't want that as it fails for root modules
|
|
||||||
Definition::ModuleDef(ModuleDef::Module(module)) => module.krate(),
|
|
||||||
_ => definition.module(db)?.krate(),
|
|
||||||
};
|
|
||||||
// FIXME: using import map doesn't make sense here. What we want here is
|
|
||||||
// canonical path. What import map returns is the shortest path suitable for
|
|
||||||
// import. See this test:
|
|
||||||
cov_mark::hit!(test_reexport_order);
|
|
||||||
let import_map = db.import_map(krate.into());
|
|
||||||
|
|
||||||
let mut base = krate.display_name(db)?.to_string();
|
|
||||||
let is_root_module = matches!(
|
|
||||||
definition,
|
|
||||||
Definition::ModuleDef(ModuleDef::Module(module)) if krate.root_module(db) == module
|
|
||||||
);
|
|
||||||
if !is_root_module {
|
|
||||||
base = once(base)
|
|
||||||
.chain(import_map.path_of(ns)?.segments.iter().map(|name| name.to_string()))
|
|
||||||
.join("/");
|
|
||||||
}
|
|
||||||
base += "/";
|
|
||||||
|
|
||||||
let filename = get_symbol_filename(db, &target_def);
|
|
||||||
let fragment = match definition {
|
|
||||||
Definition::ModuleDef(def) => match def {
|
|
||||||
ModuleDef::Function(f) => {
|
|
||||||
get_symbol_fragment(db, &FieldOrAssocItem::AssocItem(AssocItem::Function(f)))
|
|
||||||
}
|
}
|
||||||
ModuleDef::Const(c) => {
|
}
|
||||||
get_symbol_fragment(db, &FieldOrAssocItem::AssocItem(AssocItem::Const(c)))
|
Definition::Field(field) => {
|
||||||
}
|
let def = match field.parent_def(db) {
|
||||||
ModuleDef::TypeAlias(ty) => {
|
hir::VariantDef::Struct(it) => it.into(),
|
||||||
get_symbol_fragment(db, &FieldOrAssocItem::AssocItem(AssocItem::TypeAlias(ty)))
|
hir::VariantDef::Union(it) => it.into(),
|
||||||
}
|
hir::VariantDef::Variant(it) => it.into(),
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
Definition::Field(field) => get_symbol_fragment(db, &FieldOrAssocItem::Field(field)),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
get_doc_url(db, &krate)?
|
|
||||||
.join(&base)
|
|
||||||
.ok()
|
|
||||||
.and_then(|mut url| {
|
|
||||||
if !matches!(definition, Definition::ModuleDef(ModuleDef::Module(..))) {
|
|
||||||
url.path_segments_mut().ok()?.pop();
|
|
||||||
};
|
};
|
||||||
Some(url)
|
(def, Some(format!("structfield.{}", field.name(db))))
|
||||||
})
|
}
|
||||||
.and_then(|url| url.join(filename.as_deref()?).ok())
|
// FIXME macros
|
||||||
.and_then(
|
Definition::Macro(_) => return None,
|
||||||
|url| if let Some(fragment) = fragment { url.join(&fragment).ok() } else { Some(url) },
|
// FIXME impls
|
||||||
)
|
Definition::SelfType(_) => return None,
|
||||||
.map(|url| url.into())
|
Definition::Local(_) | Definition::GenericParam(_) | Definition::Label(_) => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let krate = crate_of_def(db, target)?;
|
||||||
|
let mut url = get_doc_base_url(db, &krate)?;
|
||||||
|
|
||||||
|
if let Some(path) = mod_path_of_def(db, target) {
|
||||||
|
url = url.join(&path).ok()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
url = url.join(&get_symbol_filename(db, &target)?).ok()?;
|
||||||
|
url.set_fragment(frag.as_deref());
|
||||||
|
|
||||||
|
Some(url.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_intra_doc_link(
|
fn rewrite_intra_doc_link(
|
||||||
|
@ -311,37 +281,30 @@ fn rewrite_intra_doc_link(
|
||||||
target: &str,
|
target: &str,
|
||||||
title: &str,
|
title: &str,
|
||||||
) -> Option<(String, String)> {
|
) -> Option<(String, String)> {
|
||||||
let link = if target.is_empty() { title } else { target };
|
let (link, ns) = parse_intra_doc_link(target);
|
||||||
let (link, ns) = parse_intra_doc_link(link);
|
|
||||||
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
|
|
||||||
let krate = resolved.module(db)?.krate();
|
|
||||||
let canonical_path = resolved.canonical_path(db)?;
|
|
||||||
let mut new_url = get_doc_url(db, &krate)?
|
|
||||||
.join(&format!("{}/", krate.display_name(db)?))
|
|
||||||
.ok()?
|
|
||||||
.join(&canonical_path.replace("::", "/"))
|
|
||||||
.ok()?
|
|
||||||
.join(&get_symbol_filename(db, &resolved)?)
|
|
||||||
.ok()?;
|
|
||||||
|
|
||||||
if let ModuleDef::Trait(t) = resolved {
|
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
|
||||||
if let Some(assoc_item) = t.items(db).into_iter().find_map(|assoc_item| {
|
let krate = crate_of_def(db, resolved)?;
|
||||||
if let Some(name) = assoc_item.name(db) {
|
let mut url = get_doc_base_url(db, &krate)?;
|
||||||
if *link == format!("{}::{}", canonical_path, name) {
|
|
||||||
return Some(assoc_item);
|
if let Some(path) = mod_path_of_def(db, resolved) {
|
||||||
}
|
url = url.join(&path).ok()?;
|
||||||
}
|
|
||||||
None
|
|
||||||
}) {
|
|
||||||
if let Some(fragment) =
|
|
||||||
get_symbol_fragment(db, &FieldOrAssocItem::AssocItem(assoc_item))
|
|
||||||
{
|
|
||||||
new_url = new_url.join(&fragment).ok()?;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Some((new_url.into(), strip_prefixes_suffixes(title).to_string()))
|
let (resolved, frag) = if let Some(assoc_item) = resolved.as_assoc_item(db) {
|
||||||
|
let resolved = match assoc_item.container(db) {
|
||||||
|
AssocItemContainer::Trait(t) => t.into(),
|
||||||
|
AssocItemContainer::Impl(i) => i.self_ty(db).as_adt()?.into(),
|
||||||
|
};
|
||||||
|
let frag = get_assoc_item_fragment(db, assoc_item)?;
|
||||||
|
(resolved, Some(frag))
|
||||||
|
} else {
|
||||||
|
(resolved, None)
|
||||||
|
};
|
||||||
|
url = url.join(&get_symbol_filename(db, &resolved)?).ok()?;
|
||||||
|
url.set_fragment(frag.as_deref());
|
||||||
|
|
||||||
|
Some((url.into(), strip_prefixes_suffixes(title).to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
|
/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
|
||||||
|
@ -350,18 +313,32 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option<S
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let module = def.module(db)?;
|
let krate = crate_of_def(db, def)?;
|
||||||
let krate = module.krate();
|
let mut url = get_doc_base_url(db, &krate)?;
|
||||||
let canonical_path = def.canonical_path(db)?;
|
|
||||||
let base = format!("{}/{}", krate.display_name(db)?, canonical_path.replace("::", "/"));
|
|
||||||
|
|
||||||
get_doc_url(db, &krate)
|
if let Some(path) = mod_path_of_def(db, def) {
|
||||||
.and_then(|url| url.join(&base).ok())
|
url = url.join(&path).ok()?;
|
||||||
.and_then(|url| {
|
}
|
||||||
get_symbol_filename(db, &def).as_deref().map(|f| url.join(f).ok()).flatten()
|
|
||||||
})
|
url = url.join(&get_symbol_filename(db, &def)?).ok()?;
|
||||||
.and_then(|url| url.join(target).ok())
|
url.join(target).ok().map(Into::into)
|
||||||
.map(|url| url.into())
|
}
|
||||||
|
|
||||||
|
fn crate_of_def(db: &RootDatabase, def: ModuleDef) -> Option<Crate> {
|
||||||
|
let krate = match def {
|
||||||
|
// Definition::module gives back the parent module, we don't want that as it fails for root modules
|
||||||
|
ModuleDef::Module(module) => module.krate(),
|
||||||
|
_ => def.module(db)?.krate(),
|
||||||
|
};
|
||||||
|
Some(krate)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mod_path_of_def(db: &RootDatabase, def: ModuleDef) -> Option<String> {
|
||||||
|
def.canonical_module_path(db).map(|it| {
|
||||||
|
let mut path = String::new();
|
||||||
|
it.flat_map(|it| it.name(db)).for_each(|name| format_to!(path, "{}/", name));
|
||||||
|
path
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rewrites a markdown document, applying 'callback' to each link.
|
/// Rewrites a markdown document, applying 'callback' to each link.
|
||||||
|
@ -373,14 +350,18 @@ fn map_links<'e>(
|
||||||
let mut link_target: Option<CowStr> = None;
|
let mut link_target: Option<CowStr> = None;
|
||||||
|
|
||||||
events.map(move |evt| match evt {
|
events.map(move |evt| match evt {
|
||||||
Event::Start(Tag::Link(_link_type, ref target, _)) => {
|
Event::Start(Tag::Link(_, ref target, _)) => {
|
||||||
in_link = true;
|
in_link = true;
|
||||||
link_target = Some(target.clone());
|
link_target = Some(target.clone());
|
||||||
evt
|
evt
|
||||||
}
|
}
|
||||||
Event::End(Tag::Link(link_type, _target, _)) => {
|
Event::End(Tag::Link(link_type, target, _)) => {
|
||||||
in_link = false;
|
in_link = false;
|
||||||
Event::End(Tag::Link(link_type, link_target.take().unwrap(), CowStr::Borrowed("")))
|
Event::End(Tag::Link(
|
||||||
|
link_type,
|
||||||
|
link_target.take().unwrap_or(target),
|
||||||
|
CowStr::Borrowed(""),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
Event::Text(s) if in_link => {
|
Event::Text(s) if in_link => {
|
||||||
let (link_target_s, link_name) = callback(&link_target.take().unwrap(), &s);
|
let (link_target_s, link_name) = callback(&link_target.take().unwrap(), &s);
|
||||||
|
@ -396,70 +377,14 @@ fn map_links<'e>(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const TYPES: ([&str; 9], [&str; 0]) =
|
|
||||||
(["type", "struct", "enum", "mod", "trait", "union", "module", "prim", "primitive"], []);
|
|
||||||
const VALUES: ([&str; 8], [&str; 1]) =
|
|
||||||
(["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
|
|
||||||
const MACROS: ([&str; 2], [&str; 1]) = (["macro", "derive"], ["!"]);
|
|
||||||
|
|
||||||
/// Extract the specified namespace from an intra-doc-link if one exists.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// * `struct MyStruct` -> ("MyStruct", `Namespace::Types`)
|
|
||||||
/// * `panic!` -> ("panic", `Namespace::Macros`)
|
|
||||||
/// * `fn@from_intra_spec` -> ("from_intra_spec", `Namespace::Values`)
|
|
||||||
fn parse_intra_doc_link(s: &str) -> (&str, Option<hir::Namespace>) {
|
|
||||||
let s = s.trim_matches('`');
|
|
||||||
|
|
||||||
[
|
|
||||||
(hir::Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
|
|
||||||
(hir::Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
|
|
||||||
(hir::Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
|
|
||||||
]
|
|
||||||
.iter()
|
|
||||||
.cloned()
|
|
||||||
.find_map(|(ns, (mut prefixes, mut suffixes))| {
|
|
||||||
if let Some(prefix) = prefixes.find(|&&prefix| {
|
|
||||||
s.starts_with(prefix)
|
|
||||||
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
|
|
||||||
}) {
|
|
||||||
Some((&s[prefix.len() + 1..], ns))
|
|
||||||
} else {
|
|
||||||
suffixes.find_map(|&suffix| s.strip_suffix(suffix).zip(Some(ns)))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.map_or((s, None), |(s, ns)| (s, Some(ns)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn strip_prefixes_suffixes(s: &str) -> &str {
|
|
||||||
[
|
|
||||||
(TYPES.0.iter(), TYPES.1.iter()),
|
|
||||||
(VALUES.0.iter(), VALUES.1.iter()),
|
|
||||||
(MACROS.0.iter(), MACROS.1.iter()),
|
|
||||||
]
|
|
||||||
.iter()
|
|
||||||
.cloned()
|
|
||||||
.find_map(|(mut prefixes, mut suffixes)| {
|
|
||||||
if let Some(prefix) = prefixes.find(|&&prefix| {
|
|
||||||
s.starts_with(prefix)
|
|
||||||
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
|
|
||||||
}) {
|
|
||||||
Some(&s[prefix.len() + 1..])
|
|
||||||
} else {
|
|
||||||
suffixes.find_map(|&suffix| s.strip_suffix(suffix))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap_or(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the root URL for the documentation of a crate.
|
/// Get the root URL for the documentation of a crate.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```ignore
|
||||||
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
||||||
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
/// ```
|
/// ```
|
||||||
fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
|
fn get_doc_base_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
|
||||||
|
let display_name = krate.display_name(db)?;
|
||||||
krate
|
krate
|
||||||
.get_html_root_url(db)
|
.get_html_root_url(db)
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
|
@ -468,14 +393,14 @@ fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
|
||||||
//
|
//
|
||||||
// FIXME: clicking on the link should just open the file in the editor,
|
// FIXME: clicking on the link should just open the file in the editor,
|
||||||
// instead of falling back to external urls.
|
// instead of falling back to external urls.
|
||||||
Some(format!("https://docs.rs/{}/*/", krate.display_name(db)?))
|
Some(format!("https://docs.rs/{krate}/*/", krate = display_name))
|
||||||
})
|
})
|
||||||
.and_then(|s| Url::parse(&s).ok())
|
.and_then(|s| Url::parse(&s).ok()?.join(&format!("{}/", display_name)).ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the filename and extension generated for a symbol by rustdoc.
|
/// Get the filename and extension generated for a symbol by rustdoc.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```ignore
|
||||||
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
||||||
/// ^^^^^^^^^^^^^^^^^^^
|
/// ^^^^^^^^^^^^^^^^^^^
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -486,7 +411,10 @@ fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<S
|
||||||
Adt::Enum(e) => format!("enum.{}.html", e.name(db)),
|
Adt::Enum(e) => format!("enum.{}.html", e.name(db)),
|
||||||
Adt::Union(u) => format!("union.{}.html", u.name(db)),
|
Adt::Union(u) => format!("union.{}.html", u.name(db)),
|
||||||
},
|
},
|
||||||
ModuleDef::Module(_) => "index.html".to_string(),
|
ModuleDef::Module(m) => match m.name(db) {
|
||||||
|
Some(name) => format!("{}/index.html", name),
|
||||||
|
None => String::from("index.html"),
|
||||||
|
},
|
||||||
ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
|
ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
|
||||||
ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
|
ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
|
||||||
ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.name()),
|
ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.name()),
|
||||||
|
@ -499,57 +427,44 @@ fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<S
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FieldOrAssocItem {
|
|
||||||
Field(Field),
|
|
||||||
AssocItem(AssocItem),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the fragment required to link to a specific field, method, associated type, or associated constant.
|
/// Get the fragment required to link to a specific field, method, associated type, or associated constant.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```ignore
|
||||||
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
||||||
/// ^^^^^^^^^^^^^^
|
/// ^^^^^^^^^^^^^^
|
||||||
/// ```
|
/// ```
|
||||||
fn get_symbol_fragment(db: &dyn HirDatabase, field_or_assoc: &FieldOrAssocItem) -> Option<String> {
|
fn get_assoc_item_fragment(db: &dyn HirDatabase, assoc_item: hir::AssocItem) -> Option<String> {
|
||||||
Some(match field_or_assoc {
|
Some(match assoc_item {
|
||||||
FieldOrAssocItem::Field(field) => format!("#structfield.{}", field.name(db)),
|
AssocItem::Function(function) => {
|
||||||
FieldOrAssocItem::AssocItem(assoc) => match assoc {
|
let is_trait_method =
|
||||||
AssocItem::Function(function) => {
|
function.as_assoc_item(db).and_then(|assoc| assoc.containing_trait(db)).is_some();
|
||||||
let is_trait_method = function
|
// This distinction may get more complicated when specialization is available.
|
||||||
.as_assoc_item(db)
|
// Rustdoc makes this decision based on whether a method 'has defaultness'.
|
||||||
.and_then(|assoc| assoc.containing_trait(db))
|
// Currently this is only the case for provided trait methods.
|
||||||
.is_some();
|
if is_trait_method && !function.has_body(db) {
|
||||||
// This distinction may get more complicated when specialization is available.
|
format!("tymethod.{}", function.name(db))
|
||||||
// Rustdoc makes this decision based on whether a method 'has defaultness'.
|
} else {
|
||||||
// Currently this is only the case for provided trait methods.
|
format!("method.{}", function.name(db))
|
||||||
if is_trait_method && !function.has_body(db) {
|
|
||||||
format!("#tymethod.{}", function.name(db))
|
|
||||||
} else {
|
|
||||||
format!("#method.{}", function.name(db))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
AssocItem::Const(constant) => format!("#associatedconstant.{}", constant.name(db)?),
|
}
|
||||||
AssocItem::TypeAlias(ty) => format!("#associatedtype.{}", ty.name(db)),
|
AssocItem::Const(constant) => format!("associatedconstant.{}", constant.name(db)?),
|
||||||
},
|
AssocItem::TypeAlias(ty) => format!("associatedtype.{}", ty.name(db)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
use ide_db::base_db::FileRange;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
use crate::fixture;
|
use crate::{display::TryToNav, fixture};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
use super::*;
|
||||||
let (analysis, position) = fixture::position(ra_fixture);
|
|
||||||
let url = analysis.external_docs(position).unwrap().expect("could not find url for symbol");
|
|
||||||
|
|
||||||
expect.assert_eq(&url)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_doc_url_crate() {
|
fn external_docs_doc_url_crate() {
|
||||||
check(
|
check_external_docs(
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs crate:main deps:test
|
//- /main.rs crate:main deps:test
|
||||||
use test$0::Foo;
|
use test$0::Foo;
|
||||||
|
@ -561,8 +476,8 @@ pub struct Foo;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_doc_url_struct() {
|
fn external_docs_doc_url_struct() {
|
||||||
check(
|
check_external_docs(
|
||||||
r#"
|
r#"
|
||||||
pub struct Fo$0o;
|
pub struct Fo$0o;
|
||||||
"#,
|
"#,
|
||||||
|
@ -571,91 +486,142 @@ pub struct Fo$0o;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_doc_url_fn() {
|
fn external_docs_doc_url_struct_field() {
|
||||||
check(
|
check_external_docs(
|
||||||
r#"
|
|
||||||
pub fn fo$0o() {}
|
|
||||||
"#,
|
|
||||||
expect![[r##"https://docs.rs/test/*/test/fn.foo.html#method.foo"##]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_doc_url_inherent_method() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
|
|
||||||
impl Foo {
|
|
||||||
pub fn met$0hod() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
"#,
|
|
||||||
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#method.method"##]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_doc_url_trait_provided_method() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub trait Bar {
|
|
||||||
fn met$0hod() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
"#,
|
|
||||||
expect![[r##"https://docs.rs/test/*/test/trait.Bar.html#method.method"##]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_doc_url_trait_required_method() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub trait Foo {
|
|
||||||
fn met$0hod();
|
|
||||||
}
|
|
||||||
|
|
||||||
"#,
|
|
||||||
expect![[r##"https://docs.rs/test/*/test/trait.Foo.html#tymethod.method"##]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_doc_url_field() {
|
|
||||||
check(
|
|
||||||
r#"
|
r#"
|
||||||
pub struct Foo {
|
pub struct Foo {
|
||||||
pub fie$0ld: ()
|
field$0: ()
|
||||||
}
|
}
|
||||||
|
|
||||||
"#,
|
"#,
|
||||||
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#structfield.field"##]],
|
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#structfield.field"##]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_module() {
|
fn external_docs_doc_url_fn() {
|
||||||
check(
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub fn fo$0o() {}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/fn.foo.html"##]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn external_docs_doc_url_impl_assoc() {
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub struct Foo;
|
||||||
|
impl Foo {
|
||||||
|
pub fn method$0() {}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#method.method"##]],
|
||||||
|
);
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub struct Foo;
|
||||||
|
impl Foo {
|
||||||
|
const CONST$0: () = ();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#associatedconstant.CONST"##]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn external_docs_doc_url_impl_trait_assoc() {
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub struct Foo;
|
||||||
|
pub trait Trait {
|
||||||
|
fn method() {}
|
||||||
|
}
|
||||||
|
impl Trait for Foo {
|
||||||
|
pub fn method$0() {}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#method.method"##]],
|
||||||
|
);
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub struct Foo;
|
||||||
|
pub trait Trait {
|
||||||
|
const CONST: () = ();
|
||||||
|
}
|
||||||
|
impl Trait for Foo {
|
||||||
|
const CONST$0: () = ();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#associatedconstant.CONST"##]],
|
||||||
|
);
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub struct Foo;
|
||||||
|
pub trait Trait {
|
||||||
|
type Type;
|
||||||
|
}
|
||||||
|
impl Trait for Foo {
|
||||||
|
type Type$0 = ();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#associatedtype.Type"##]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn external_docs_doc_url_trait_assoc() {
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub trait Foo {
|
||||||
|
fn method$0();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/trait.Foo.html#tymethod.method"##]],
|
||||||
|
);
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub trait Foo {
|
||||||
|
const CONST$0: ();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/trait.Foo.html#associatedconstant.CONST"##]],
|
||||||
|
);
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
pub trait Foo {
|
||||||
|
type Type$0;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"https://docs.rs/test/*/test/trait.Foo.html#associatedtype.Type"##]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn external_docs_trait() {
|
||||||
|
check_external_docs(
|
||||||
|
r#"
|
||||||
|
trait Trait$0 {}
|
||||||
|
"#,
|
||||||
|
expect![[r#"https://docs.rs/test/*/test/trait.Trait.html"#]],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn external_docs_module() {
|
||||||
|
check_external_docs(
|
||||||
r#"
|
r#"
|
||||||
pub mod foo {
|
pub mod foo {
|
||||||
pub mod ba$0r {}
|
pub mod ba$0r {}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"https://docs.rs/test/*/test/foo/bar/index.html"#]],
|
expect![[r#"https://docs.rs/test/*/test/foo/bar/index.html"#]],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_reexport_order() {
|
fn external_docs_reexport_order() {
|
||||||
cov_mark::check!(test_reexport_order);
|
check_external_docs(
|
||||||
// FIXME: This should return
|
|
||||||
//
|
|
||||||
// https://docs.rs/test/*/test/wrapper/modulestruct.Item.html
|
|
||||||
//
|
|
||||||
// That is, we should point inside the module, rather than at the
|
|
||||||
// re-export.
|
|
||||||
check(
|
|
||||||
r#"
|
r#"
|
||||||
pub mod wrapper {
|
pub mod wrapper {
|
||||||
pub use module::Item;
|
pub use module::Item;
|
||||||
|
@ -669,7 +635,185 @@ fn foo() {
|
||||||
let bar: wrapper::It$0em;
|
let bar: wrapper::It$0em;
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"https://docs.rs/test/*/test/wrapper/struct.Item.html"#]],
|
expect![[r#"https://docs.rs/test/*/test/wrapper/module/struct.Item.html"#]],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_trait_items() {
|
||||||
|
check_doc_links(
|
||||||
|
r#"
|
||||||
|
/// [`Trait`]
|
||||||
|
/// [`Trait::Type`]
|
||||||
|
/// [`Trait::CONST`]
|
||||||
|
/// [`Trait::func`]
|
||||||
|
trait Trait$0 {
|
||||||
|
// ^^^^^ Trait
|
||||||
|
type Type;
|
||||||
|
// ^^^^ Trait::Type
|
||||||
|
const CONST: usize;
|
||||||
|
// ^^^^^ Trait::CONST
|
||||||
|
fn func();
|
||||||
|
// ^^^^ Trait::func
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rewrite_html_root_url() {
|
||||||
|
check_rewrite(
|
||||||
|
r#"
|
||||||
|
#![doc(arbitrary_attribute = "test", html_root_url = "https:/example.com", arbitrary_attribute2)]
|
||||||
|
|
||||||
|
pub mod foo {
|
||||||
|
pub struct Foo;
|
||||||
|
}
|
||||||
|
/// [Foo](foo::Foo)
|
||||||
|
pub struct B$0ar
|
||||||
|
"#,
|
||||||
|
expect![[r#"[Foo](https://example.com/test/foo/struct.Foo.html)"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rewrite_on_field() {
|
||||||
|
// FIXME: Should be
|
||||||
|
// [Foo](https://docs.rs/test/*/test/struct.Foo.html)
|
||||||
|
check_rewrite(
|
||||||
|
r#"
|
||||||
|
pub struct Foo {
|
||||||
|
/// [Foo](struct.Foo.html)
|
||||||
|
fie$0ld: ()
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"[Foo](struct.Foo.html)"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rewrite_struct() {
|
||||||
|
check_rewrite(
|
||||||
|
r#"
|
||||||
|
/// [Foo]
|
||||||
|
pub struct $0Foo;
|
||||||
|
"#,
|
||||||
|
expect![[r#"[Foo](https://docs.rs/test/*/test/struct.Foo.html)"#]],
|
||||||
|
);
|
||||||
|
check_rewrite(
|
||||||
|
r#"
|
||||||
|
/// [`Foo`]
|
||||||
|
pub struct $0Foo;
|
||||||
|
"#,
|
||||||
|
expect![[r#"[`Foo`](https://docs.rs/test/*/test/struct.Foo.html)"#]],
|
||||||
|
);
|
||||||
|
check_rewrite(
|
||||||
|
r#"
|
||||||
|
/// [Foo](struct.Foo.html)
|
||||||
|
pub struct $0Foo;
|
||||||
|
"#,
|
||||||
|
expect![[r#"[Foo](https://docs.rs/test/*/test/struct.Foo.html)"#]],
|
||||||
|
);
|
||||||
|
check_rewrite(
|
||||||
|
r#"
|
||||||
|
/// [struct Foo](struct.Foo.html)
|
||||||
|
pub struct $0Foo;
|
||||||
|
"#,
|
||||||
|
expect![[r#"[struct Foo](https://docs.rs/test/*/test/struct.Foo.html)"#]],
|
||||||
|
);
|
||||||
|
check_rewrite(
|
||||||
|
r#"
|
||||||
|
/// [my Foo][foo]
|
||||||
|
///
|
||||||
|
/// [foo]: Foo
|
||||||
|
pub struct $0Foo;
|
||||||
|
"#,
|
||||||
|
expect![[r#"[my Foo](https://docs.rs/test/*/test/struct.Foo.html)"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_external_docs(ra_fixture: &str, expect: Expect) {
|
||||||
|
let (analysis, position) = fixture::position(ra_fixture);
|
||||||
|
let url = analysis.external_docs(position).unwrap().expect("could not find url for symbol");
|
||||||
|
|
||||||
|
expect.assert_eq(&url)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_rewrite(ra_fixture: &str, expect: Expect) {
|
||||||
|
let (analysis, position) = fixture::position(ra_fixture);
|
||||||
|
let sema = &Semantics::new(&*analysis.db);
|
||||||
|
let (cursor_def, docs) = def_under_cursor(sema, &position);
|
||||||
|
let res = rewrite_links(sema.db, docs.as_str(), cursor_def);
|
||||||
|
expect.assert_eq(&res)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_doc_links(ra_fixture: &str) {
|
||||||
|
let key_fn = |&(FileRange { file_id, range }, _): &_| (file_id, range.start());
|
||||||
|
|
||||||
|
let (analysis, position, mut expected) = fixture::annotations(ra_fixture);
|
||||||
|
expected.sort_by_key(key_fn);
|
||||||
|
let sema = &Semantics::new(&*analysis.db);
|
||||||
|
let (cursor_def, docs) = def_under_cursor(sema, &position);
|
||||||
|
let defs = extract_definitions_from_docs(&docs);
|
||||||
|
let actual: Vec<_> = defs
|
||||||
|
.into_iter()
|
||||||
|
.map(|(_, link, ns)| {
|
||||||
|
let def = resolve_doc_path_for_def(sema.db, cursor_def, &link, ns)
|
||||||
|
.unwrap_or_else(|| panic!("Failed to resolve {}", link));
|
||||||
|
let nav_target = def.try_to_nav(sema.db).unwrap();
|
||||||
|
let range = FileRange {
|
||||||
|
file_id: nav_target.file_id,
|
||||||
|
range: nav_target.focus_or_full_range(),
|
||||||
|
};
|
||||||
|
(range, link)
|
||||||
|
})
|
||||||
|
.sorted_by_key(key_fn)
|
||||||
|
.collect();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn def_under_cursor(
|
||||||
|
sema: &Semantics<RootDatabase>,
|
||||||
|
position: &FilePosition,
|
||||||
|
) -> (Definition, hir::Documentation) {
|
||||||
|
let (docs, def) = sema
|
||||||
|
.parse(position.file_id)
|
||||||
|
.syntax()
|
||||||
|
.token_at_offset(position.offset)
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.ancestors()
|
||||||
|
.find_map(|it| node_to_def(sema, &it))
|
||||||
|
.expect("no def found")
|
||||||
|
.unwrap();
|
||||||
|
let docs = docs.expect("no docs found for cursor def");
|
||||||
|
(def, docs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn node_to_def(
|
||||||
|
sema: &Semantics<RootDatabase>,
|
||||||
|
node: &SyntaxNode,
|
||||||
|
) -> Option<Option<(Option<hir::Documentation>, Definition)>> {
|
||||||
|
Some(match_ast! {
|
||||||
|
match node {
|
||||||
|
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
|
||||||
|
ast::Module(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
|
||||||
|
ast::Fn(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Function(def)))),
|
||||||
|
ast::Struct(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(def))))),
|
||||||
|
ast::Union(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Union(def))))),
|
||||||
|
ast::Enum(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(def))))),
|
||||||
|
ast::Variant(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Variant(def)))),
|
||||||
|
ast::Trait(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Trait(def)))),
|
||||||
|
ast::Static(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Static(def)))),
|
||||||
|
ast::Const(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Const(def)))),
|
||||||
|
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::TypeAlias(def)))),
|
||||||
|
ast::Impl(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::SelfType(def))),
|
||||||
|
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::Field(def))),
|
||||||
|
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::Field(def))),
|
||||||
|
ast::Macro(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::Macro(def))),
|
||||||
|
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
|
||||||
|
_ => return None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
79
crates/ide/src/doc_links/intra_doc_links.rs
Normal file
79
crates/ide/src/doc_links/intra_doc_links.rs
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
//! Helper tools for intra doc links.
|
||||||
|
|
||||||
|
const TYPES: ([&str; 9], [&str; 0]) =
|
||||||
|
(["type", "struct", "enum", "mod", "trait", "union", "module", "prim", "primitive"], []);
|
||||||
|
const VALUES: ([&str; 8], [&str; 1]) =
|
||||||
|
(["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
|
||||||
|
const MACROS: ([&str; 2], [&str; 1]) = (["macro", "derive"], ["!"]);
|
||||||
|
|
||||||
|
/// Extract the specified namespace from an intra-doc-link if one exists.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// * `struct MyStruct` -> ("MyStruct", `Namespace::Types`)
|
||||||
|
/// * `panic!` -> ("panic", `Namespace::Macros`)
|
||||||
|
/// * `fn@from_intra_spec` -> ("from_intra_spec", `Namespace::Values`)
|
||||||
|
pub(super) fn parse_intra_doc_link(s: &str) -> (&str, Option<hir::Namespace>) {
|
||||||
|
let s = s.trim_matches('`');
|
||||||
|
|
||||||
|
[
|
||||||
|
(hir::Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
|
||||||
|
(hir::Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
|
||||||
|
(hir::Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.cloned()
|
||||||
|
.find_map(|(ns, (mut prefixes, mut suffixes))| {
|
||||||
|
if let Some(prefix) = prefixes.find(|&&prefix| {
|
||||||
|
s.starts_with(prefix)
|
||||||
|
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
|
||||||
|
}) {
|
||||||
|
Some((&s[prefix.len() + 1..], ns))
|
||||||
|
} else {
|
||||||
|
suffixes.find_map(|&suffix| s.strip_suffix(suffix).zip(Some(ns)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map_or((s, None), |(s, ns)| (s, Some(ns)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn strip_prefixes_suffixes(s: &str) -> &str {
|
||||||
|
[
|
||||||
|
(TYPES.0.iter(), TYPES.1.iter()),
|
||||||
|
(VALUES.0.iter(), VALUES.1.iter()),
|
||||||
|
(MACROS.0.iter(), MACROS.1.iter()),
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.cloned()
|
||||||
|
.find_map(|(mut prefixes, mut suffixes)| {
|
||||||
|
if let Some(prefix) = prefixes.find(|&&prefix| {
|
||||||
|
s.starts_with(prefix)
|
||||||
|
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
|
||||||
|
}) {
|
||||||
|
Some(&s[prefix.len() + 1..])
|
||||||
|
} else {
|
||||||
|
suffixes.find_map(|&suffix| s.strip_suffix(suffix))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn check(link: &str, expected: Expect) {
|
||||||
|
let (l, a) = parse_intra_doc_link(link);
|
||||||
|
let a = a.map_or_else(String::new, |a| format!(" ({:?})", a));
|
||||||
|
expected.assert_eq(&format!("{}{}", l, a));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_name() {
|
||||||
|
check("foo", expect![[r#"foo"#]]);
|
||||||
|
check("struct Struct", expect![[r#"Struct (Types)"#]]);
|
||||||
|
check("makro!", expect![[r#"makro (Macros)"#]]);
|
||||||
|
check("fn@function", expect![[r#"function (Values)"#]]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, Text
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
display::{ToNav, TryToNav},
|
display::{ToNav, TryToNav},
|
||||||
doc_links::{doc_attributes, extract_definitions_from_markdown, resolve_doc_path_for_def},
|
doc_links::{doc_attributes, extract_definitions_from_docs, resolve_doc_path_for_def},
|
||||||
FilePosition, NavigationTarget, RangeInfo,
|
FilePosition, NavigationTarget, RangeInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ pub(crate) fn goto_definition(
|
||||||
|
|
||||||
let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
|
let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
|
||||||
let (_, link, ns) =
|
let (_, link, ns) =
|
||||||
extract_definitions_from_markdown(docs.as_str()).into_iter().find(|(range, ..)| {
|
extract_definitions_from_docs(&docs).into_iter().find(|(range, ..)| {
|
||||||
doc_mapping.map(*range).map_or(false, |InFile { file_id, value: range }| {
|
doc_mapping.map(*range).map_or(false, |InFile { file_id, value: range }| {
|
||||||
file_id == position.file_id.into() && range.contains(position.offset)
|
file_id == position.file_id.into() && range.contains(position.offset)
|
||||||
})
|
})
|
||||||
|
|
|
@ -19,7 +19,7 @@ use syntax::{
|
||||||
use crate::{
|
use crate::{
|
||||||
display::{macro_label, TryToNav},
|
display::{macro_label, TryToNav},
|
||||||
doc_links::{
|
doc_links::{
|
||||||
doc_attributes, extract_definitions_from_markdown, remove_links, resolve_doc_path_for_def,
|
doc_attributes, extract_definitions_from_docs, remove_links, resolve_doc_path_for_def,
|
||||||
rewrite_links,
|
rewrite_links,
|
||||||
},
|
},
|
||||||
markdown_remove::remove_markdown,
|
markdown_remove::remove_markdown,
|
||||||
|
@ -119,7 +119,7 @@ pub(crate) fn hover(
|
||||||
let (attributes, def) = doc_attributes(&sema, &node)?;
|
let (attributes, def) = doc_attributes(&sema, &node)?;
|
||||||
let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
|
let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
|
||||||
let (idl_range, link, ns) =
|
let (idl_range, link, ns) =
|
||||||
extract_definitions_from_markdown(docs.as_str()).into_iter().find_map(|(range, link, ns)| {
|
extract_definitions_from_docs(&docs).into_iter().find_map(|(range, link, ns)| {
|
||||||
let hir::InFile { file_id, value: range } = doc_mapping.map(range)?;
|
let hir::InFile { file_id, value: range } = doc_mapping.map(range)?;
|
||||||
if file_id == position.file_id.into() && range.contains(position.offset) {
|
if file_id == position.file_id.into() && range.contains(position.offset) {
|
||||||
Some((range, link, ns))
|
Some((range, link, ns))
|
||||||
|
@ -369,7 +369,7 @@ fn process_markup(
|
||||||
let markup = if !config.markdown() {
|
let markup = if !config.markdown() {
|
||||||
remove_markdown(markup)
|
remove_markdown(markup)
|
||||||
} else if config.links_in_hover {
|
} else if config.links_in_hover {
|
||||||
rewrite_links(db, markup, &def)
|
rewrite_links(db, markup, def)
|
||||||
} else {
|
} else {
|
||||||
remove_links(markup)
|
remove_links(markup)
|
||||||
};
|
};
|
||||||
|
@ -1779,334 +1779,6 @@ fn foo() { let bar = Ba$0r; }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_path_link() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
/// [Foo](struct.Foo.html)
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[Foo](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_path_link_no_strip() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
/// [struct Foo](struct.Foo.html)
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[struct Foo](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_path_link_field() {
|
|
||||||
// FIXME: Should be
|
|
||||||
// [Foo](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
pub struct Bar {
|
|
||||||
/// [Foo](struct.Foo.html)
|
|
||||||
fie$0ld: ()
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*field*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test::Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
field: ()
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[Foo](struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub mod foo {
|
|
||||||
pub struct Foo;
|
|
||||||
}
|
|
||||||
/// [Foo](foo::Foo)
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[Foo](https://docs.rs/test/*/test/foo/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link_html_root_url() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
#![doc(arbitrary_attribute = "test", html_root_url = "https:/example.com", arbitrary_attribute2)]
|
|
||||||
|
|
||||||
pub mod foo {
|
|
||||||
pub struct Foo;
|
|
||||||
}
|
|
||||||
/// [Foo](foo::Foo)
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[Foo](https://example.com/test/foo/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link_shortlink() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
/// [Foo]
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[Foo](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link_shortlink_code() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
/// [`Foo`]
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link_namespaced() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
fn Foo() {}
|
|
||||||
/// [Foo()]
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[Foo](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link_shortlink_namspaced_code() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
/// [`struct Foo`]
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link_shortlink_namspaced_code_with_at() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
/// [`struct@Foo`]
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link_reference() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub struct Foo;
|
|
||||||
/// [my Foo][foo]
|
|
||||||
///
|
|
||||||
/// [foo]: Foo
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[my Foo](https://docs.rs/test/*/test/struct.Foo.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn test_hover_intra_link_reference_to_trait_method() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
pub trait Foo {
|
|
||||||
fn buzz() -> usize;
|
|
||||||
}
|
|
||||||
/// [Foo][buzz]
|
|
||||||
///
|
|
||||||
/// [buzz]: Foo::buzz
|
|
||||||
pub struct B$0ar
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*Bar*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub struct Bar
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[Foo](https://docs.rs/test/*/test/trait.Foo.html#tymethod.buzz)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hover_external_url() {
|
fn test_hover_external_url() {
|
||||||
check(
|
check(
|
||||||
|
@ -2160,60 +1832,6 @@ pub struct B$0ar
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_doc_links_enum_variant() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
enum E {
|
|
||||||
/// [E]
|
|
||||||
V$0 { field: i32 }
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*V*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test::E
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
V { field: i32 }
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[E](https://docs.rs/test/*/test/enum.E.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_doc_links_field() {
|
|
||||||
check(
|
|
||||||
r#"
|
|
||||||
struct S {
|
|
||||||
/// [`S`]
|
|
||||||
field$0: i32
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
expect![[r#"
|
|
||||||
*field*
|
|
||||||
|
|
||||||
```rust
|
|
||||||
test::S
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
field: i32
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
[`S`](https://docs.rs/test/*/test/struct.S.html)
|
|
||||||
"#]],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hover_no_links() {
|
fn test_hover_no_links() {
|
||||||
check_hover_no_links(
|
check_hover_no_links(
|
||||||
|
|
|
@ -157,7 +157,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
|
||||||
hir::ModuleDef::Function(it) => runnable_fn(&sema, it),
|
hir::ModuleDef::Function(it) => runnable_fn(&sema, it),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
add_opt(runnable.or_else(|| module_def_doctest(&sema, def)), Some(def));
|
add_opt(runnable.or_else(|| module_def_doctest(sema.db, def)), Some(def));
|
||||||
}
|
}
|
||||||
Either::Right(impl_) => {
|
Either::Right(impl_) => {
|
||||||
add_opt(runnable_impl(&sema, &impl_), None);
|
add_opt(runnable_impl(&sema, &impl_), None);
|
||||||
|
@ -168,9 +168,9 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
|
||||||
(
|
(
|
||||||
match assoc {
|
match assoc {
|
||||||
hir::AssocItem::Function(it) => runnable_fn(&sema, it)
|
hir::AssocItem::Function(it) => runnable_fn(&sema, it)
|
||||||
.or_else(|| module_def_doctest(&sema, it.into())),
|
.or_else(|| module_def_doctest(sema.db, it.into())),
|
||||||
hir::AssocItem::Const(it) => module_def_doctest(&sema, it.into()),
|
hir::AssocItem::Const(it) => module_def_doctest(sema.db, it.into()),
|
||||||
hir::AssocItem::TypeAlias(it) => module_def_doctest(&sema, it.into()),
|
hir::AssocItem::TypeAlias(it) => module_def_doctest(sema.db, it.into()),
|
||||||
},
|
},
|
||||||
assoc,
|
assoc,
|
||||||
)
|
)
|
||||||
|
@ -382,61 +382,56 @@ fn runnable_mod_outline_definition(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Option<Runnable> {
|
fn module_def_doctest(db: &RootDatabase, def: hir::ModuleDef) -> Option<Runnable> {
|
||||||
let attrs = match def {
|
let attrs = match def {
|
||||||
hir::ModuleDef::Module(it) => it.attrs(sema.db),
|
hir::ModuleDef::Module(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Function(it) => it.attrs(sema.db),
|
hir::ModuleDef::Function(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Adt(it) => it.attrs(sema.db),
|
hir::ModuleDef::Adt(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Variant(it) => it.attrs(sema.db),
|
hir::ModuleDef::Variant(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Const(it) => it.attrs(sema.db),
|
hir::ModuleDef::Const(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Static(it) => it.attrs(sema.db),
|
hir::ModuleDef::Static(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Trait(it) => it.attrs(sema.db),
|
hir::ModuleDef::Trait(it) => it.attrs(db),
|
||||||
hir::ModuleDef::TypeAlias(it) => it.attrs(sema.db),
|
hir::ModuleDef::TypeAlias(it) => it.attrs(db),
|
||||||
hir::ModuleDef::BuiltinType(_) => return None,
|
hir::ModuleDef::BuiltinType(_) => return None,
|
||||||
};
|
};
|
||||||
if !has_runnable_doc_test(&attrs) {
|
if !has_runnable_doc_test(&attrs) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let def_name = def.name(sema.db).map(|it| it.to_string());
|
let def_name = def.name(db)?;
|
||||||
let test_id = def
|
let path = (|| {
|
||||||
.canonical_path(sema.db)
|
let mut path = String::new();
|
||||||
// This probably belongs to canonical path?
|
def.canonical_module_path(db)?
|
||||||
.map(|path| {
|
.flat_map(|it| it.name(db))
|
||||||
let assoc_def = match def {
|
.for_each(|name| format_to!(path, "{}::", name));
|
||||||
hir::ModuleDef::Function(it) => it.as_assoc_item(sema.db),
|
// This probably belongs to canonical_path?
|
||||||
hir::ModuleDef::Const(it) => it.as_assoc_item(sema.db),
|
if let Some(assoc_item) = def.as_assoc_item(db) {
|
||||||
hir::ModuleDef::TypeAlias(it) => it.as_assoc_item(sema.db),
|
if let hir::AssocItemContainer::Impl(imp) = assoc_item.container(db) {
|
||||||
_ => None,
|
let ty = imp.self_ty(db);
|
||||||
};
|
if let Some(adt) = ty.as_adt() {
|
||||||
// FIXME: this also looks very wrong
|
let name = adt.name(db);
|
||||||
if let Some(assoc_def) = assoc_def {
|
let mut ty_args = ty.type_arguments().peekable();
|
||||||
if let hir::AssocItemContainer::Impl(imp) = assoc_def.container(sema.db) {
|
format_to!(path, "{}", name);
|
||||||
let ty = imp.self_ty(sema.db);
|
if ty_args.peek().is_some() {
|
||||||
if let Some(adt) = ty.as_adt() {
|
format_to!(
|
||||||
let name = adt.name(sema.db);
|
path,
|
||||||
let idx = path.rfind(':').map_or(0, |idx| idx + 1);
|
"<{}>",
|
||||||
let (prefix, suffix) = path.split_at(idx);
|
ty_args.format_with(", ", |ty, cb| cb(&ty.display(db)))
|
||||||
let mut ty_args = ty.type_arguments().peekable();
|
);
|
||||||
let params = if ty_args.peek().is_some() {
|
|
||||||
format!(
|
|
||||||
"<{}>",
|
|
||||||
ty_args.format_with(", ", |ty, cb| cb(&ty.display(sema.db)))
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
String::new()
|
|
||||||
};
|
|
||||||
return format!("{}{}{}::{}", prefix, name, params, suffix);
|
|
||||||
}
|
}
|
||||||
|
format_to!(path, "::{}", def_name);
|
||||||
|
return Some(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
path
|
}
|
||||||
})
|
format_to!(path, "{}", def_name);
|
||||||
.map(TestId::Path)
|
Some(path)
|
||||||
.or_else(|| def_name.clone().map(TestId::Name))?;
|
})();
|
||||||
|
|
||||||
|
let test_id = path.map_or_else(|| TestId::Name(def_name.to_string()), TestId::Path);
|
||||||
|
|
||||||
let mut nav = match def {
|
let mut nav = match def {
|
||||||
hir::ModuleDef::Module(def) => NavigationTarget::from_module_to_decl(sema.db, def),
|
hir::ModuleDef::Module(def) => NavigationTarget::from_module_to_decl(db, def),
|
||||||
def => def.try_to_nav(sema.db)?,
|
def => def.try_to_nav(db)?,
|
||||||
};
|
};
|
||||||
nav.focus_range = None;
|
nav.focus_range = None;
|
||||||
nav.description = None;
|
nav.description = None;
|
||||||
|
|
|
@ -11,7 +11,7 @@ use syntax::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
doc_links::{doc_attributes, extract_definitions_from_markdown, resolve_doc_path_for_def},
|
doc_links::{doc_attributes, extract_definitions_from_docs, resolve_doc_path_for_def},
|
||||||
Analysis, HlMod, HlRange, HlTag, RootDatabase,
|
Analysis, HlMod, HlRange, HlTag, RootDatabase,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ pub(super) fn doc_comment(
|
||||||
let mut string;
|
let mut string;
|
||||||
|
|
||||||
if let Some((docs, doc_mapping)) = attributes.docs_with_rangemap(sema.db) {
|
if let Some((docs, doc_mapping)) = attributes.docs_with_rangemap(sema.db) {
|
||||||
extract_definitions_from_markdown(docs.as_str())
|
extract_definitions_from_docs(&docs)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|(range, link, ns)| {
|
.filter_map(|(range, link, ns)| {
|
||||||
let def = resolve_doc_path_for_def(sema.db, def, &link, ns)?;
|
let def = resolve_doc_path_for_def(sema.db, def, &link, ns)?;
|
||||||
|
|
Loading…
Reference in a new issue