aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-26 17:56:41 +0100
committerAleksey Kladov <[email protected]>2020-08-26 19:24:00 +0100
commitf8a59adf5e9633aa5d10efcdbf70b408d280ef01 (patch)
treed088d683f75bbd2bed837f1ca0aca61fdba5c11b
parent3d6c4c143b4b4c74810318eca1b5493e43535fff (diff)
Tease apart orthogonal concerns in markdown link rewriting
`hir` should know nothing about URLs, markdown and html. It should only be able to: * resolve stringy path from documentation * generate canonical stringy path for a def In contrast, link rewriting should not care about semantics of paths and names resolution, and should be concern only with text mangling bits.
-rw-r--r--Cargo.lock2
-rw-r--r--crates/hir/Cargo.toml1
-rw-r--r--crates/hir/src/attrs.rs133
-rw-r--r--crates/hir/src/code_model.rs10
-rw-r--r--crates/hir/src/doc_links.rs238
-rw-r--r--crates/hir/src/lib.rs4
-rw-r--r--crates/ide/Cargo.toml1
-rw-r--r--crates/ide/src/hover.rs54
-rw-r--r--crates/ide/src/link_rewrite.rs191
9 files changed, 297 insertions, 337 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 015f84ba8..f1a17fa90 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -498,7 +498,6 @@ dependencies = [
498 "stdx", 498 "stdx",
499 "syntax", 499 "syntax",
500 "tt", 500 "tt",
501 "url",
502] 501]
503 502
504[[package]] 503[[package]]
@@ -606,6 +605,7 @@ dependencies = [
606 "syntax", 605 "syntax",
607 "test_utils", 606 "test_utils",
608 "text_edit", 607 "text_edit",
608 "url",
609] 609]
610 610
611[[package]] 611[[package]]
diff --git a/crates/hir/Cargo.toml b/crates/hir/Cargo.toml
index 72f941c46..6dc5ad63b 100644
--- a/crates/hir/Cargo.toml
+++ b/crates/hir/Cargo.toml
@@ -15,7 +15,6 @@ rustc-hash = "1.1.0"
15either = "1.5.3" 15either = "1.5.3"
16arrayvec = "0.5.1" 16arrayvec = "0.5.1"
17itertools = "0.9.0" 17itertools = "0.9.0"
18url = "2.1.1"
19 18
20stdx = { path = "../stdx", version = "0.0.0" } 19stdx = { path = "../stdx", version = "0.0.0" }
21syntax = { path = "../syntax", version = "0.0.0" } 20syntax = { path = "../syntax", version = "0.0.0" }
diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs
index e5a539cb8..c3e820d89 100644
--- a/crates/hir/src/attrs.rs
+++ b/crates/hir/src/attrs.rs
@@ -1,20 +1,32 @@
1//! Attributes & documentation for hir types. 1//! Attributes & documentation for hir types.
2use hir_def::{ 2use hir_def::{
3 attr::Attrs, 3 attr::Attrs, docs::Documentation, path::ModPath, resolver::HasResolver, AttrDefId, ModuleDefId,
4 docs::Documentation,
5 resolver::{HasResolver, Resolver},
6 AdtId, AttrDefId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId,
7}; 4};
5use hir_expand::hygiene::Hygiene;
8use hir_ty::db::HirDatabase; 6use hir_ty::db::HirDatabase;
7use syntax::ast;
9 8
10use crate::{ 9use crate::{
11 doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef, 10 Adt, Const, Enum, EnumVariant, Field, Function, MacroDef, Module, ModuleDef, Static, Struct,
12 Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, 11 Trait, TypeAlias, Union,
13}; 12};
14 13
15pub trait HasAttrs { 14pub trait HasAttrs {
16 fn attrs(self, db: &dyn HirDatabase) -> Attrs; 15 fn attrs(self, db: &dyn HirDatabase) -> Attrs;
17 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>; 16 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>;
17 fn resolve_doc_path(
18 self,
19 db: &dyn HirDatabase,
20 link: &str,
21 ns: Option<Namespace>,
22 ) -> Option<ModuleDef>;
23}
24
25#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
26pub enum Namespace {
27 Types,
28 Values,
29 Macros,
18} 30}
19 31
20macro_rules! impl_has_attrs { 32macro_rules! impl_has_attrs {
@@ -28,6 +40,10 @@ macro_rules! impl_has_attrs {
28 let def = AttrDefId::$def_id(self.into()); 40 let def = AttrDefId::$def_id(self.into());
29 db.documentation(def) 41 db.documentation(def)
30 } 42 }
43 fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
44 let def = AttrDefId::$def_id(self.into());
45 resolve_doc_path(db, def, link, ns).map(ModuleDef::from)
46 }
31 } 47 }
32 )*}; 48 )*};
33} 49}
@@ -54,83 +70,42 @@ macro_rules! impl_has_attrs_adt {
54 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> { 70 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
55 Adt::$adt(self).docs(db) 71 Adt::$adt(self).docs(db)
56 } 72 }
73 fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
74 Adt::$adt(self).resolve_doc_path(db, link, ns)
75 }
57 } 76 }
58 )*}; 77 )*};
59} 78}
60 79
61impl_has_attrs_adt![Struct, Union, Enum]; 80impl_has_attrs_adt![Struct, Union, Enum];
62 81
63impl Resolvable for ModuleDef { 82fn resolve_doc_path(
64 fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> { 83 db: &dyn HirDatabase,
65 Some(match self { 84 def: AttrDefId,
66 ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db.upcast()), 85 link: &str,
67 ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db.upcast()), 86 ns: Option<Namespace>,
68 ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db.upcast()), 87) -> Option<ModuleDefId> {
69 ModuleDef::EnumVariant(ev) => { 88 let resolver = match def {
70 GenericDefId::from(GenericDef::from(ev.clone())).resolver(db.upcast()) 89 AttrDefId::ModuleId(it) => it.resolver(db.upcast()),
71 } 90 AttrDefId::FieldId(it) => it.parent.resolver(db.upcast()),
72 ModuleDef::Const(c) => { 91 AttrDefId::AdtId(it) => it.resolver(db.upcast()),
73 GenericDefId::from(GenericDef::from(c.clone())).resolver(db.upcast()) 92 AttrDefId::FunctionId(it) => it.resolver(db.upcast()),
74 } 93 AttrDefId::EnumVariantId(it) => it.parent.resolver(db.upcast()),
75 ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db.upcast()), 94 AttrDefId::StaticId(it) => it.resolver(db.upcast()),
76 ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db.upcast()), 95 AttrDefId::ConstId(it) => it.resolver(db.upcast()),
77 ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db.upcast()), 96 AttrDefId::TraitId(it) => it.resolver(db.upcast()),
78 // FIXME: This should be a resolver relative to `std/core` 97 AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()),
79 ModuleDef::BuiltinType(_t) => None?, 98 AttrDefId::ImplId(it) => it.resolver(db.upcast()),
80 }) 99 AttrDefId::MacroDefId(_) => return None,
81 } 100 };
82 101 let path = ast::Path::parse(link).ok()?;
83 fn try_into_module_def(self) -> Option<ModuleDef> { 102 let modpath = ModPath::from_src(path, &Hygiene::new_unhygienic()).unwrap();
84 Some(self) 103 let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
85 } 104 let def = match ns {
86} 105 Some(Namespace::Types) => resolved.take_types()?,
87 106 Some(Namespace::Values) => resolved.take_values()?,
88impl Resolvable for TypeParam { 107 Some(Namespace::Macros) => return None,
89 fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> { 108 None => resolved.iter_items().find_map(|it| it.as_module_def_id())?,
90 Some(ModuleId::from(self.module(db)).resolver(db.upcast())) 109 };
91 } 110 Some(def.into())
92
93 fn try_into_module_def(self) -> Option<ModuleDef> {
94 None
95 }
96}
97
98impl Resolvable for MacroDef {
99 fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
100 Some(ModuleId::from(self.module(db)?).resolver(db.upcast()))
101 }
102
103 fn try_into_module_def(self) -> Option<ModuleDef> {
104 None
105 }
106}
107
108impl Resolvable for Field {
109 fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
110 Some(VariantId::from(self.parent_def(db)).resolver(db.upcast()))
111 }
112
113 fn try_into_module_def(self) -> Option<ModuleDef> {
114 None
115 }
116}
117
118impl Resolvable for ImplDef {
119 fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
120 Some(ModuleId::from(self.module(db)).resolver(db.upcast()))
121 }
122
123 fn try_into_module_def(self) -> Option<ModuleDef> {
124 None
125 }
126}
127
128impl Resolvable for Local {
129 fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
130 Some(ModuleId::from(self.module(db)).resolver(db.upcast()))
131 }
132
133 fn try_into_module_def(self) -> Option<ModuleDef> {
134 None
135 }
136} 111}
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index c2ee20dbb..c2fc819e7 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -196,6 +196,16 @@ impl ModuleDef {
196 } 196 }
197 } 197 }
198 198
199 pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> {
200 let mut segments = Vec::new();
201 segments.push(self.name(db)?.to_string());
202 for m in self.module(db)?.path_to_root(db) {
203 segments.extend(m.name(db).map(|it| it.to_string()))
204 }
205 segments.reverse();
206 Some(segments.join("::"))
207 }
208
199 pub fn definition_visibility(&self, db: &dyn HirDatabase) -> Option<Visibility> { 209 pub fn definition_visibility(&self, db: &dyn HirDatabase) -> Option<Visibility> {
200 let module = match self { 210 let module = match self {
201 ModuleDef::Module(it) => it.parent(db)?, 211 ModuleDef::Module(it) => it.parent(db)?,
diff --git a/crates/hir/src/doc_links.rs b/crates/hir/src/doc_links.rs
deleted file mode 100644
index ddaffbec2..000000000
--- a/crates/hir/src/doc_links.rs
+++ /dev/null
@@ -1,238 +0,0 @@
1//! Resolves links in markdown documentation.
2
3use std::iter::once;
4
5use hir_def::resolver::Resolver;
6use itertools::Itertools;
7use syntax::ast::Path;
8use url::Url;
9
10use crate::{db::HirDatabase, Adt, AsName, Crate, Hygiene, ItemInNs, ModPath, ModuleDef};
11
12pub fn resolve_doc_link<T: Resolvable + Clone>(
13 db: &dyn HirDatabase,
14 definition: &T,
15 link_text: &str,
16 link_target: &str,
17) -> Option<(String, String)> {
18 let resolver = definition.resolver(db)?;
19 let module_def = definition.clone().try_into_module_def();
20 resolve_doc_link_impl(db, &resolver, module_def, link_text, link_target)
21}
22
23fn resolve_doc_link_impl(
24 db: &dyn HirDatabase,
25 resolver: &Resolver,
26 module_def: Option<ModuleDef>,
27 link_text: &str,
28 link_target: &str,
29) -> Option<(String, String)> {
30 try_resolve_intra(db, &resolver, link_text, &link_target).or_else(|| {
31 try_resolve_path(db, &module_def?, &link_target)
32 .map(|target| (target, link_text.to_string()))
33 })
34}
35
36/// Try to resolve path to local documentation via intra-doc-links (i.e. `super::gateway::Shard`).
37///
38/// See [RFC1946](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md).
39fn try_resolve_intra(
40 db: &dyn HirDatabase,
41 resolver: &Resolver,
42 link_text: &str,
43 link_target: &str,
44) -> Option<(String, String)> {
45 // Set link_target for implied shortlinks
46 let link_target =
47 if link_target.is_empty() { link_text.trim_matches('`') } else { link_target };
48
49 let doclink = IntraDocLink::from(link_target);
50
51 // Parse link as a module path
52 let path = Path::parse(doclink.path).ok()?;
53 let modpath = ModPath::from_src(path, &Hygiene::new_unhygienic()).unwrap();
54
55 let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
56 let (defid, namespace) = match doclink.namespace {
57 // FIXME: .or(resolved.macros)
58 None => resolved
59 .types
60 .map(|t| (t.0, Namespace::Types))
61 .or(resolved.values.map(|t| (t.0, Namespace::Values)))?,
62 Some(ns @ Namespace::Types) => (resolved.types?.0, ns),
63 Some(ns @ Namespace::Values) => (resolved.values?.0, ns),
64 // FIXME:
65 Some(Namespace::Macros) => return None,
66 };
67
68 // Get the filepath of the final symbol
69 let def: ModuleDef = defid.into();
70 let module = def.module(db)?;
71 let krate = module.krate();
72 let ns = match namespace {
73 Namespace::Types => ItemInNs::Types(defid),
74 Namespace::Values => ItemInNs::Values(defid),
75 // FIXME:
76 Namespace::Macros => None?,
77 };
78 let import_map = db.import_map(krate.into());
79 let path = import_map.path_of(ns)?;
80
81 Some((
82 get_doc_url(db, &krate)?
83 .join(&format!("{}/", krate.display_name(db)?))
84 .ok()?
85 .join(&path.segments.iter().map(|name| name.to_string()).join("/"))
86 .ok()?
87 .join(&get_symbol_filename(db, &def)?)
88 .ok()?
89 .into_string(),
90 strip_prefixes_suffixes(link_text).to_string(),
91 ))
92}
93
94/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
95fn try_resolve_path(db: &dyn HirDatabase, moddef: &ModuleDef, link_target: &str) -> Option<String> {
96 if !link_target.contains("#") && !link_target.contains(".html") {
97 return None;
98 }
99 let ns = ItemInNs::Types(moddef.clone().into());
100
101 let module = moddef.module(db)?;
102 let krate = module.krate();
103 let import_map = db.import_map(krate.into());
104 let base = once(format!("{}", krate.display_name(db)?))
105 .chain(import_map.path_of(ns)?.segments.iter().map(|name| format!("{}", name)))
106 .join("/");
107
108 get_doc_url(db, &krate)
109 .and_then(|url| url.join(&base).ok())
110 .and_then(|url| {
111 get_symbol_filename(db, moddef).as_deref().map(|f| url.join(f).ok()).flatten()
112 })
113 .and_then(|url| url.join(link_target).ok())
114 .map(|url| url.into_string())
115}
116
117/// Strip prefixes, suffixes, and inline code marks from the given string.
118fn strip_prefixes_suffixes(mut s: &str) -> &str {
119 s = s.trim_matches('`');
120
121 [
122 (TYPES.0.iter(), TYPES.1.iter()),
123 (VALUES.0.iter(), VALUES.1.iter()),
124 (MACROS.0.iter(), MACROS.1.iter()),
125 ]
126 .iter()
127 .for_each(|(prefixes, suffixes)| {
128 prefixes.clone().for_each(|prefix| s = s.trim_start_matches(*prefix));
129 suffixes.clone().for_each(|suffix| s = s.trim_end_matches(*suffix));
130 });
131 let s = s.trim_start_matches("@").trim();
132 s
133}
134
135fn get_doc_url(db: &dyn HirDatabase, krate: &Crate) -> Option<Url> {
136 krate
137 .get_html_root_url(db)
138 .or_else(||
139 // Fallback to docs.rs
140 // FIXME: Specify an exact version here. This may be difficult, as multiple versions of the same crate could exist.
141 Some(format!("https://docs.rs/{}/*/", krate.display_name(db)?)))
142 .and_then(|s| Url::parse(&s).ok())
143}
144
145/// Get the filename and extension generated for a symbol by rustdoc.
146///
147/// Example: `struct.Shard.html`
148fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<String> {
149 Some(match definition {
150 ModuleDef::Adt(adt) => match adt {
151 Adt::Struct(s) => format!("struct.{}.html", s.name(db)),
152 Adt::Enum(e) => format!("enum.{}.html", e.name(db)),
153 Adt::Union(u) => format!("union.{}.html", u.name(db)),
154 },
155 ModuleDef::Module(_) => "index.html".to_string(),
156 ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
157 ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
158 ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.as_name()),
159 ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)),
160 ModuleDef::EnumVariant(ev) => {
161 format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db))
162 }
163 ModuleDef::Const(c) => format!("const.{}.html", c.name(db)?),
164 ModuleDef::Static(s) => format!("static.{}.html", s.name(db)?),
165 })
166}
167
168struct IntraDocLink<'s> {
169 path: &'s str,
170 namespace: Option<Namespace>,
171}
172
173impl<'s> From<&'s str> for IntraDocLink<'s> {
174 fn from(s: &'s str) -> Self {
175 Self { path: strip_prefixes_suffixes(s), namespace: Namespace::from_intra_spec(s) }
176 }
177}
178
179#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
180enum Namespace {
181 Types,
182 Values,
183 Macros,
184}
185
186static TYPES: ([&str; 7], [&str; 0]) =
187 (["type", "struct", "enum", "mod", "trait", "union", "module"], []);
188static VALUES: ([&str; 8], [&str; 1]) =
189 (["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
190static MACROS: ([&str; 1], [&str; 1]) = (["macro"], ["!"]);
191
192impl Namespace {
193 /// Extract the specified namespace from an intra-doc-link if one exists.
194 ///
195 /// # Examples
196 ///
197 /// * `struct MyStruct` -> `Namespace::Types`
198 /// * `panic!` -> `Namespace::Macros`
199 /// * `fn@from_intra_spec` -> `Namespace::Values`
200 fn from_intra_spec(s: &str) -> Option<Self> {
201 [
202 (Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
203 (Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
204 (Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
205 ]
206 .iter()
207 .filter(|(_ns, (prefixes, suffixes))| {
208 prefixes
209 .clone()
210 .map(|prefix| {
211 s.starts_with(*prefix)
212 && s.chars()
213 .nth(prefix.len() + 1)
214 .map(|c| c == '@' || c == ' ')
215 .unwrap_or(false)
216 })
217 .any(|cond| cond)
218 || suffixes
219 .clone()
220 .map(|suffix| {
221 s.starts_with(*suffix)
222 && s.chars()
223 .nth(suffix.len() + 1)
224 .map(|c| c == '@' || c == ' ')
225 .unwrap_or(false)
226 })
227 .any(|cond| cond)
228 })
229 .map(|(ns, (_, _))| *ns)
230 .next()
231 }
232}
233
234/// Sealed trait used solely for the generic bound on [`resolve_doc_link`].
235pub trait Resolvable {
236 fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver>;
237 fn try_into_module_def(self) -> Option<ModuleDef>;
238}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 03915ea1b..b9d9c7e25 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -27,19 +27,17 @@ pub mod diagnostics;
27 27
28mod from_id; 28mod from_id;
29mod code_model; 29mod code_model;
30mod doc_links;
31mod attrs; 30mod attrs;
32mod has_source; 31mod has_source;
33 32
34pub use crate::{ 33pub use crate::{
35 attrs::HasAttrs, 34 attrs::{HasAttrs, Namespace},
36 code_model::{ 35 code_model::{
37 Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, 36 Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
38 Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function, 37 Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function,
39 GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, 38 GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static,
40 Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, 39 Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
41 }, 40 },
42 doc_links::resolve_doc_link,
43 has_source::HasSource, 41 has_source::HasSource,
44 semantics::{original_range, PathResolution, Semantics, SemanticsScope}, 42 semantics::{original_range, PathResolution, Semantics, SemanticsScope},
45}; 43};
diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml
index e61c276df..a15f704ca 100644
--- a/crates/ide/Cargo.toml
+++ b/crates/ide/Cargo.toml
@@ -18,6 +18,7 @@ rustc-hash = "1.1.0"
18oorandom = "11.1.2" 18oorandom = "11.1.2"
19pulldown-cmark-to-cmark = "5.0.0" 19pulldown-cmark-to-cmark = "5.0.0"
20pulldown-cmark = {version = "0.7.2", default-features = false} 20pulldown-cmark = {version = "0.7.2", default-features = false}
21url = "2.1.1"
21 22
22stdx = { path = "../stdx", version = "0.0.0" } 23stdx = { path = "../stdx", version = "0.0.0" }
23syntax = { path = "../syntax", version = "0.0.0" } 24syntax = { path = "../syntax", version = "0.0.0" }
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 536411704..efec0184e 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -1756,6 +1756,60 @@ pub struct B<|>ar
1756 } 1756 }
1757 1757
1758 #[test] 1758 #[test]
1759 fn test_doc_links_enum_variant() {
1760 check(
1761 r#"
1762enum E {
1763 /// [E]
1764 V<|> { field: i32 }
1765}
1766"#,
1767 expect![[r#"
1768 *V*
1769
1770 ```rust
1771 test::E
1772 ```
1773
1774 ```rust
1775 V
1776 ```
1777
1778 ---
1779
1780 [E](https://docs.rs/test/*/test/enum.E.html)
1781 "#]],
1782 );
1783 }
1784
1785 #[test]
1786 fn test_doc_links_field() {
1787 check(
1788 r#"
1789struct S {
1790 /// [`S`]
1791 field<|>: i32
1792}
1793"#,
1794 expect![[r#"
1795 *field*
1796
1797 ```rust
1798 test::S
1799 ```
1800
1801 ```rust
1802 field: i32
1803 ```
1804
1805 ---
1806
1807 [`S`](https://docs.rs/test/*/test/struct.S.html)
1808 "#]],
1809 );
1810 }
1811
1812 #[test]
1759 fn test_hover_macro_generated_struct_fn_doc_comment() { 1813 fn test_hover_macro_generated_struct_fn_doc_comment() {
1760 mark::check!(hover_macro_generated_struct_fn_doc_comment); 1814 mark::check!(hover_macro_generated_struct_fn_doc_comment);
1761 1815
diff --git a/crates/ide/src/link_rewrite.rs b/crates/ide/src/link_rewrite.rs
index ff3200eef..acedea71b 100644
--- a/crates/ide/src/link_rewrite.rs
+++ b/crates/ide/src/link_rewrite.rs
@@ -2,11 +2,11 @@
2//! 2//!
3//! Most of the implementation can be found in [`hir::doc_links`]. 3//! Most of the implementation can be found in [`hir::doc_links`].
4 4
5use hir::{Adt, Crate, HasAttrs, ModuleDef};
6use ide_db::{defs::Definition, RootDatabase};
5use pulldown_cmark::{CowStr, Event, Options, Parser, Tag}; 7use pulldown_cmark::{CowStr, Event, Options, Parser, Tag};
6use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; 8use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions};
7 9use url::Url;
8use hir::resolve_doc_link;
9use ide_db::{defs::Definition, RootDatabase};
10 10
11/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) 11/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
12pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { 12pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String {
@@ -26,19 +26,16 @@ pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition)
26 // Two posibilities: 26 // Two posibilities:
27 // * path-based links: `../../module/struct.MyStruct.html` 27 // * path-based links: `../../module/struct.MyStruct.html`
28 // * module-based links (AKA intra-doc links): `super::super::module::MyStruct` 28 // * module-based links (AKA intra-doc links): `super::super::module::MyStruct`
29 let resolved = match definition { 29 if let Some(rewritten) = rewrite_intra_doc_link(db, *definition, target, title) {
30 Definition::ModuleDef(t) => resolve_doc_link(db, t, title, target), 30 return rewritten;
31 Definition::Macro(t) => resolve_doc_link(db, t, title, target), 31 }
32 Definition::Field(t) => resolve_doc_link(db, t, title, target), 32 if let Definition::ModuleDef(def) = *definition {
33 Definition::SelfType(t) => resolve_doc_link(db, t, title, target), 33 if let Some(target) = rewrite_url_link(db, def, target) {
34 Definition::Local(t) => resolve_doc_link(db, t, title, target), 34 return (target, title.to_string());
35 Definition::TypeParam(t) => resolve_doc_link(db, t, title, target), 35 }
36 };
37
38 match resolved {
39 Some((target, title)) => (target, title),
40 None => (target.to_string(), title.to_string()),
41 } 36 }
37
38 (target.to_string(), title.to_string())
42 } 39 }
43 }); 40 });
44 let mut out = String::new(); 41 let mut out = String::new();
@@ -48,6 +45,64 @@ pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition)
48 out 45 out
49} 46}
50 47
48fn rewrite_intra_doc_link(
49 db: &RootDatabase,
50 def: Definition,
51 target: &str,
52 title: &str,
53) -> Option<(String, String)> {
54 let link = if target.is_empty() { title } else { target };
55 let (link, ns) = parse_link(link);
56 let resolved = match def {
57 Definition::ModuleDef(def) => match def {
58 ModuleDef::Module(it) => it.resolve_doc_path(db, link, ns),
59 ModuleDef::Function(it) => it.resolve_doc_path(db, link, ns),
60 ModuleDef::Adt(it) => it.resolve_doc_path(db, link, ns),
61 ModuleDef::EnumVariant(it) => it.resolve_doc_path(db, link, ns),
62 ModuleDef::Const(it) => it.resolve_doc_path(db, link, ns),
63 ModuleDef::Static(it) => it.resolve_doc_path(db, link, ns),
64 ModuleDef::Trait(it) => it.resolve_doc_path(db, link, ns),
65 ModuleDef::TypeAlias(it) => it.resolve_doc_path(db, link, ns),
66 ModuleDef::BuiltinType(_) => return None,
67 },
68 Definition::Macro(it) => it.resolve_doc_path(db, link, ns),
69 Definition::Field(it) => it.resolve_doc_path(db, link, ns),
70 Definition::SelfType(_) | Definition::Local(_) | Definition::TypeParam(_) => return None,
71 }?;
72 let krate = resolved.module(db)?.krate();
73 let canonical_path = resolved.canonical_path(db)?;
74 let new_target = get_doc_url(db, &krate)?
75 .join(&format!("{}/", krate.display_name(db)?))
76 .ok()?
77 .join(&canonical_path.replace("::", "/"))
78 .ok()?
79 .join(&get_symbol_filename(db, &resolved)?)
80 .ok()?
81 .into_string();
82 let new_title = strip_prefixes_suffixes(title);
83 Some((new_target, new_title.to_string()))
84}
85
86/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
87fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option<String> {
88 if !(target.contains("#") || target.contains(".html")) {
89 return None;
90 }
91
92 let module = def.module(db)?;
93 let krate = module.krate();
94 let canonical_path = def.canonical_path(db)?;
95 let base = format!("{}/{}", krate.display_name(db)?, canonical_path.replace("::", "/"));
96
97 get_doc_url(db, &krate)
98 .and_then(|url| url.join(&base).ok())
99 .and_then(|url| {
100 get_symbol_filename(db, &def).as_deref().map(|f| url.join(f).ok()).flatten()
101 })
102 .and_then(|url| url.join(target).ok())
103 .map(|url| url.into_string())
104}
105
51// Rewrites a markdown document, resolving links using `callback` and additionally striping prefixes/suffixes on link titles. 106// Rewrites a markdown document, resolving links using `callback` and additionally striping prefixes/suffixes on link titles.
52fn map_links<'e>( 107fn map_links<'e>(
53 events: impl Iterator<Item = Event<'e>>, 108 events: impl Iterator<Item = Event<'e>>,
@@ -79,3 +134,109 @@ fn map_links<'e>(
79 _ => evt, 134 _ => evt,
80 }) 135 })
81} 136}
137
138fn parse_link(s: &str) -> (&str, Option<hir::Namespace>) {
139 let path = strip_prefixes_suffixes(s);
140 let ns = ns_from_intra_spec(s);
141 (path, ns)
142}
143
144/// Strip prefixes, suffixes, and inline code marks from the given string.
145fn strip_prefixes_suffixes(mut s: &str) -> &str {
146 s = s.trim_matches('`');
147
148 [
149 (TYPES.0.iter(), TYPES.1.iter()),
150 (VALUES.0.iter(), VALUES.1.iter()),
151 (MACROS.0.iter(), MACROS.1.iter()),
152 ]
153 .iter()
154 .for_each(|(prefixes, suffixes)| {
155 prefixes.clone().for_each(|prefix| s = s.trim_start_matches(*prefix));
156 suffixes.clone().for_each(|suffix| s = s.trim_end_matches(*suffix));
157 });
158 s.trim_start_matches("@").trim()
159}
160
161static TYPES: ([&str; 7], [&str; 0]) =
162 (["type", "struct", "enum", "mod", "trait", "union", "module"], []);
163static VALUES: ([&str; 8], [&str; 1]) =
164 (["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
165static MACROS: ([&str; 1], [&str; 1]) = (["macro"], ["!"]);
166
167/// Extract the specified namespace from an intra-doc-link if one exists.
168///
169/// # Examples
170///
171/// * `struct MyStruct` -> `Namespace::Types`
172/// * `panic!` -> `Namespace::Macros`
173/// * `fn@from_intra_spec` -> `Namespace::Values`
174fn ns_from_intra_spec(s: &str) -> Option<hir::Namespace> {
175 [
176 (hir::Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
177 (hir::Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
178 (hir::Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
179 ]
180 .iter()
181 .filter(|(_ns, (prefixes, suffixes))| {
182 prefixes
183 .clone()
184 .map(|prefix| {
185 s.starts_with(*prefix)
186 && s.chars()
187 .nth(prefix.len() + 1)
188 .map(|c| c == '@' || c == ' ')
189 .unwrap_or(false)
190 })
191 .any(|cond| cond)
192 || suffixes
193 .clone()
194 .map(|suffix| {
195 s.starts_with(*suffix)
196 && s.chars()
197 .nth(suffix.len() + 1)
198 .map(|c| c == '@' || c == ' ')
199 .unwrap_or(false)
200 })
201 .any(|cond| cond)
202 })
203 .map(|(ns, (_, _))| *ns)
204 .next()
205}
206
207fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
208 krate
209 .get_html_root_url(db)
210 .or_else(|| {
211 // Fallback to docs.rs. This uses `display_name` and can never be
212 // correct, but that's what fallbacks are about.
213 //
214 // FIXME: clicking on the link should just open the file in the editor,
215 // instead of falling back to external urls.
216 Some(format!("https://docs.rs/{}/*/", krate.display_name(db)?))
217 })
218 .and_then(|s| Url::parse(&s).ok())
219}
220
221/// Get the filename and extension generated for a symbol by rustdoc.
222///
223/// Example: `struct.Shard.html`
224fn get_symbol_filename(db: &RootDatabase, definition: &ModuleDef) -> Option<String> {
225 Some(match definition {
226 ModuleDef::Adt(adt) => match adt {
227 Adt::Struct(s) => format!("struct.{}.html", s.name(db)),
228 Adt::Enum(e) => format!("enum.{}.html", e.name(db)),
229 Adt::Union(u) => format!("union.{}.html", u.name(db)),
230 },
231 ModuleDef::Module(_) => "index.html".to_string(),
232 ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
233 ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
234 ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t),
235 ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)),
236 ModuleDef::EnumVariant(ev) => {
237 format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db))
238 }
239 ModuleDef::Const(c) => format!("const.{}.html", c.name(db)?),
240 ModuleDef::Static(s) => format!("static.{}.html", s.name(db)?),
241 })
242}