aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir/src')
-rw-r--r--crates/hir/src/code_model.rs122
-rw-r--r--crates/hir/src/lib.rs7
-rw-r--r--crates/hir/src/link_rewrite.rs226
3 files changed, 351 insertions, 4 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index 3d92d0c0d..9395efe4f 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -20,7 +20,7 @@ use hir_def::{
20 type_ref::{Mutability, TypeRef}, 20 type_ref::{Mutability, TypeRef},
21 AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, 21 AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
22 ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, 22 ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId,
23 TraitId, TypeAliasId, TypeParamId, UnionId, 23 TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
24}; 24};
25use hir_expand::{ 25use hir_expand::{
26 diagnostics::DiagnosticSink, 26 diagnostics::DiagnosticSink,
@@ -39,10 +39,12 @@ use syntax::{
39 ast::{self, AttrsOwner, NameOwner}, 39 ast::{self, AttrsOwner, NameOwner},
40 AstNode, SmolStr, 40 AstNode, SmolStr,
41}; 41};
42use tt::{Ident, Leaf, Literal, TokenTree};
42 43
43use crate::{ 44use crate::{
44 db::{DefDatabase, HirDatabase}, 45 db::{DefDatabase, HirDatabase},
45 has_source::HasSource, 46 has_source::HasSource,
47 link_rewrite::Resolvable,
46 HirDisplay, InFile, Name, 48 HirDisplay, InFile, Name,
47}; 49};
48 50
@@ -122,6 +124,33 @@ impl Crate {
122 pub fn all(db: &dyn HirDatabase) -> Vec<Crate> { 124 pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {
123 db.crate_graph().iter().map(|id| Crate { id }).collect() 125 db.crate_graph().iter().map(|id| Crate { id }).collect()
124 } 126 }
127
128 /// Try to get the root URL of the documentation of a crate.
129 pub fn get_doc_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> {
130 // Look for #![doc(html_root_url = "...")]
131 let attrs = db.attrs(AttrDef::from(self.root_module(db)).into());
132 let doc_attr_q = attrs.by_key("doc");
133
134 let doc_url = if doc_attr_q.exists() {
135 doc_attr_q.tt_values().map(|tt| {
136 let name = tt.token_trees.iter()
137 .skip_while(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Ident(Ident{text: ref ident, ..})) if ident == "html_root_url"))
138 .skip(2)
139 .next();
140
141 match name {
142 Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text),
143 _ => None
144 }
145 }).flat_map(|t| t).next().map(|s| s.to_string())
146 } else {
147 None
148 };
149
150 doc_url
151 .map(|s| s.trim_matches('"').trim_end_matches("/").to_owned() + "/")
152 .map(|s| s.to_string())
153 }
125} 154}
126 155
127#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -198,7 +227,6 @@ impl ModuleDef {
198 ModuleDef::Function(it) => Some(it.name(db)), 227 ModuleDef::Function(it) => Some(it.name(db)),
199 ModuleDef::EnumVariant(it) => Some(it.name(db)), 228 ModuleDef::EnumVariant(it) => Some(it.name(db)),
200 ModuleDef::TypeAlias(it) => Some(it.name(db)), 229 ModuleDef::TypeAlias(it) => Some(it.name(db)),
201
202 ModuleDef::Module(it) => it.name(db), 230 ModuleDef::Module(it) => it.name(db),
203 ModuleDef::Const(it) => it.name(db), 231 ModuleDef::Const(it) => it.name(db),
204 ModuleDef::Static(it) => it.name(db), 232 ModuleDef::Static(it) => it.name(db),
@@ -206,6 +234,23 @@ impl ModuleDef {
206 ModuleDef::BuiltinType(it) => Some(it.as_name()), 234 ModuleDef::BuiltinType(it) => Some(it.as_name()),
207 } 235 }
208 } 236 }
237
238 pub fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
239 Some(match self {
240 ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db),
241 ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db),
242 ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db),
243 ModuleDef::EnumVariant(ev) => {
244 GenericDefId::from(GenericDef::from(ev.clone())).resolver(db)
245 }
246 ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db),
247 ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db),
248 ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db),
249 ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db),
250 // FIXME: This should be a resolver relative to `std/core`
251 ModuleDef::BuiltinType(_t) => None?,
252 })
253 }
209} 254}
210 255
211pub use hir_def::{ 256pub use hir_def::{
@@ -1771,3 +1816,76 @@ pub trait HasVisibility {
1771 vis.is_visible_from(db.upcast(), module.id) 1816 vis.is_visible_from(db.upcast(), module.id)
1772 } 1817 }
1773} 1818}
1819
1820impl Resolvable for ModuleDef {
1821 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1822 Some(match self {
1823 ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db),
1824 ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db),
1825 ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db),
1826 ModuleDef::EnumVariant(ev) => {
1827 GenericDefId::from(GenericDef::from(ev.clone())).resolver(db)
1828 }
1829 ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db),
1830 ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db),
1831 ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db),
1832 ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db),
1833 // FIXME: This should be a resolver relative to `std/core`
1834 ModuleDef::BuiltinType(_t) => None?,
1835 })
1836 }
1837
1838 fn try_into_module_def(self) -> Option<ModuleDef> {
1839 Some(self)
1840 }
1841}
1842
1843impl Resolvable for TypeParam {
1844 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1845 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1846 }
1847
1848 fn try_into_module_def(self) -> Option<ModuleDef> {
1849 None
1850 }
1851}
1852
1853impl Resolvable for MacroDef {
1854 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1855 Some(Into::<ModuleId>::into(self.module(db)?).resolver(db))
1856 }
1857
1858 fn try_into_module_def(self) -> Option<ModuleDef> {
1859 None
1860 }
1861}
1862
1863impl Resolvable for Field {
1864 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1865 Some(Into::<VariantId>::into(Into::<VariantDef>::into(self.parent_def(db))).resolver(db))
1866 }
1867
1868 fn try_into_module_def(self) -> Option<ModuleDef> {
1869 None
1870 }
1871}
1872
1873impl Resolvable for ImplDef {
1874 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1875 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1876 }
1877
1878 fn try_into_module_def(self) -> Option<ModuleDef> {
1879 None
1880 }
1881}
1882
1883impl Resolvable for Local {
1884 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1885 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1886 }
1887
1888 fn try_into_module_def(self) -> Option<ModuleDef> {
1889 None
1890 }
1891}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 78d8651cb..ae2f1fd4d 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -27,6 +27,7 @@ pub mod diagnostics;
27 27
28mod from_id; 28mod from_id;
29mod code_model; 29mod code_model;
30mod link_rewrite;
30 31
31mod has_source; 32mod has_source;
32 33
@@ -38,6 +39,7 @@ pub use crate::{
38 ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, 39 ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
39 }, 40 },
40 has_source::HasSource, 41 has_source::HasSource,
42 link_rewrite::resolve_doc_link,
41 semantics::{original_range, PathResolution, Semantics, SemanticsScope}, 43 semantics::{original_range, PathResolution, Semantics, SemanticsScope},
42}; 44};
43 45
@@ -47,13 +49,14 @@ pub use hir_def::{
47 body::scope::ExprScopes, 49 body::scope::ExprScopes,
48 builtin_type::BuiltinType, 50 builtin_type::BuiltinType,
49 docs::Documentation, 51 docs::Documentation,
52 item_scope::ItemInNs,
50 nameres::ModuleSource, 53 nameres::ModuleSource,
51 path::ModPath, 54 path::ModPath,
52 type_ref::{Mutability, TypeRef}, 55 type_ref::{Mutability, TypeRef},
53}; 56};
54pub use hir_expand::{ 57pub use hir_expand::{
55 name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, /* FIXME */ MacroDefId, 58 name::AsName, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc,
56 MacroFile, Origin, 59 /* FIXME */ MacroDefId, MacroFile, Origin,
57}; 60};
58pub use hir_ty::display::HirDisplay; 61pub use hir_ty::display::HirDisplay;
59 62
diff --git a/crates/hir/src/link_rewrite.rs b/crates/hir/src/link_rewrite.rs
new file mode 100644
index 000000000..dad3a39cf
--- /dev/null
+++ b/crates/hir/src/link_rewrite.rs
@@ -0,0 +1,226 @@
1//! Resolves and rewrites links in markdown documentation for hovers/completion windows.
2
3use std::iter::once;
4
5use itertools::Itertools;
6use url::Url;
7
8use crate::{db::HirDatabase, Adt, AsName, Crate, Hygiene, ItemInNs, ModPath, ModuleDef};
9use hir_def::{db::DefDatabase, resolver::Resolver};
10use syntax::ast::Path;
11
12pub fn resolve_doc_link<T: Resolvable + Clone, D: DefDatabase + HirDatabase>(
13 db: &D,
14 definition: &T,
15 link_text: &str,
16 link_target: &str,
17) -> Option<(String, String)> {
18 try_resolve_intra(db, definition, link_text, &link_target).or_else(|| {
19 if let Some(definition) = definition.clone().try_into_module_def() {
20 try_resolve_path(db, &definition, &link_target)
21 .map(|target| (target, link_text.to_string()))
22 } else {
23 None
24 }
25 })
26}
27
28/// Try to resolve path to local documentation via intra-doc-links (i.e. `super::gateway::Shard`).
29///
30/// See [RFC1946](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md).
31fn try_resolve_intra<T: Resolvable, D: DefDatabase + HirDatabase>(
32 db: &D,
33 definition: &T,
34 link_text: &str,
35 link_target: &str,
36) -> Option<(String, String)> {
37 // Set link_target for implied shortlinks
38 let link_target =
39 if link_target.is_empty() { link_text.trim_matches('`') } else { link_target };
40
41 // Namespace disambiguation
42 let namespace = Namespace::from_intra_spec(link_target);
43
44 // Strip prefixes/suffixes
45 let link_target = strip_prefixes_suffixes(link_target);
46
47 // Parse link as a module path
48 let path = Path::parse(link_target).ok()?;
49 let modpath = ModPath::from_src(path, &Hygiene::new_unhygienic()).unwrap();
50
51 // Resolve it relative to symbol's location (according to the RFC this should consider small scopes)
52 let resolver = definition.resolver(db)?;
53
54 let resolved = resolver.resolve_module_path_in_items(db, &modpath);
55 let (defid, namespace) = match namespace {
56 // FIXME: .or(resolved.macros)
57 None => resolved
58 .types
59 .map(|t| (t.0, Namespace::Types))
60 .or(resolved.values.map(|t| (t.0, Namespace::Values)))?,
61 Some(ns @ Namespace::Types) => (resolved.types?.0, ns),
62 Some(ns @ Namespace::Values) => (resolved.values?.0, ns),
63 // FIXME:
64 Some(Namespace::Macros) => None?,
65 };
66
67 // Get the filepath of the final symbol
68 let def: ModuleDef = defid.into();
69 let module = def.module(db)?;
70 let krate = module.krate();
71 let ns = match namespace {
72 Namespace::Types => ItemInNs::Types(defid),
73 Namespace::Values => ItemInNs::Values(defid),
74 // FIXME:
75 Namespace::Macros => None?,
76 };
77 let import_map = db.import_map(krate.into());
78 let path = import_map.path_of(ns)?;
79
80 Some((
81 get_doc_url(db, &krate)?
82 .join(&format!("{}/", krate.display_name(db)?))
83 .ok()?
84 .join(&path.segments.iter().map(|name| name.to_string()).join("/"))
85 .ok()?
86 .join(&get_symbol_filename(db, &def)?)
87 .ok()?
88 .into_string(),
89 strip_prefixes_suffixes(link_text).to_string(),
90 ))
91}
92
93/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
94fn try_resolve_path(db: &dyn HirDatabase, moddef: &ModuleDef, link_target: &str) -> Option<String> {
95 if !link_target.contains("#") && !link_target.contains(".html") {
96 return None;
97 }
98 let ns = ItemInNs::Types(moddef.clone().into());
99
100 let module = moddef.module(db)?;
101 let krate = module.krate();
102 let import_map = db.import_map(krate.into());
103 let base = once(format!("{}", krate.display_name(db)?))
104 .chain(import_map.path_of(ns)?.segments.iter().map(|name| format!("{}", name)))
105 .join("/");
106
107 get_doc_url(db, &krate)
108 .and_then(|url| url.join(&base).ok())
109 .and_then(|url| {
110 get_symbol_filename(db, moddef).as_deref().map(|f| url.join(f).ok()).flatten()
111 })
112 .and_then(|url| url.join(link_target).ok())
113 .map(|url| url.into_string())
114}
115
116// Strip prefixes, suffixes, and inline code marks from the given string.
117fn strip_prefixes_suffixes(mut s: &str) -> &str {
118 s = s.trim_matches('`');
119
120 [
121 (TYPES.0.iter(), TYPES.1.iter()),
122 (VALUES.0.iter(), VALUES.1.iter()),
123 (MACROS.0.iter(), MACROS.1.iter()),
124 ]
125 .iter()
126 .for_each(|(prefixes, suffixes)| {
127 prefixes.clone().for_each(|prefix| s = s.trim_start_matches(*prefix));
128 suffixes.clone().for_each(|suffix| s = s.trim_end_matches(*suffix));
129 });
130 let s = s.trim_start_matches("@").trim();
131 s
132}
133
134fn get_doc_url(db: &dyn HirDatabase, krate: &Crate) -> Option<Url> {
135 krate
136 .get_doc_url(db)
137 .or_else(||
138 // Fallback to docs.rs
139 // FIXME: Specify an exact version here. This may be difficult, as multiple versions of the same crate could exist.
140 Some(format!("https://docs.rs/{}/*/", krate.display_name(db)?)))
141 .and_then(|s| Url::parse(&s).ok())
142}
143
144/// Get the filename and extension generated for a symbol by rustdoc.
145///
146/// Example: `struct.Shard.html`
147fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<String> {
148 Some(match definition {
149 ModuleDef::Adt(adt) => match adt {
150 Adt::Struct(s) => format!("struct.{}.html", s.name(db)),
151 Adt::Enum(e) => format!("enum.{}.html", e.name(db)),
152 Adt::Union(u) => format!("union.{}.html", u.name(db)),
153 },
154 ModuleDef::Module(_) => "index.html".to_string(),
155 ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
156 ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
157 ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.as_name()),
158 ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)),
159 ModuleDef::EnumVariant(ev) => {
160 format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db))
161 }
162 ModuleDef::Const(c) => format!("const.{}.html", c.name(db)?),
163 ModuleDef::Static(s) => format!("static.{}.html", s.name(db)?),
164 })
165}
166
167#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
168enum Namespace {
169 Types,
170 Values,
171 Macros,
172}
173
174static TYPES: ([&str; 7], [&str; 0]) =
175 (["type", "struct", "enum", "mod", "trait", "union", "module"], []);
176static VALUES: ([&str; 8], [&str; 1]) =
177 (["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
178static MACROS: ([&str; 1], [&str; 1]) = (["macro"], ["!"]);
179
180impl Namespace {
181 /// Extract the specified namespace from an intra-doc-link if one exists.
182 ///
183 /// # Examples
184 ///
185 /// * `struct MyStruct` -> `Namespace::Types`
186 /// * `panic!` -> `Namespace::Macros`
187 /// * `fn@from_intra_spec` -> `Namespace::Values`
188 fn from_intra_spec(s: &str) -> Option<Self> {
189 [
190 (Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
191 (Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
192 (Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
193 ]
194 .iter()
195 .filter(|(_ns, (prefixes, suffixes))| {
196 prefixes
197 .clone()
198 .map(|prefix| {
199 s.starts_with(*prefix)
200 && s.chars()
201 .nth(prefix.len() + 1)
202 .map(|c| c == '@' || c == ' ')
203 .unwrap_or(false)
204 })
205 .any(|cond| cond)
206 || suffixes
207 .clone()
208 .map(|suffix| {
209 s.starts_with(*suffix)
210 && s.chars()
211 .nth(suffix.len() + 1)
212 .map(|c| c == '@' || c == ' ')
213 .unwrap_or(false)
214 })
215 .any(|cond| cond)
216 })
217 .map(|(ns, (_, _))| *ns)
218 .next()
219 }
220}
221
222/// Sealed trait used solely for the generic bound on [`resolve_doc_link`].
223pub trait Resolvable {
224 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver>;
225 fn try_into_module_def(self) -> Option<ModuleDef>;
226}