aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/code_model.rs104
-rw-r--r--crates/ra_hir/src/lib.rs2
-rw-r--r--crates/ra_hir/src/link_rewrite.rs226
3 files changed, 331 insertions, 1 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 8284ab757..8154e9bf6 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -16,7 +16,7 @@ use hir_def::{
16 type_ref::{Mutability, TypeRef}, 16 type_ref::{Mutability, TypeRef},
17 AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, 17 AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
18 ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, 18 ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId,
19 TraitId, TypeAliasId, TypeParamId, UnionId, 19 TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
20}; 20};
21use hir_expand::{ 21use hir_expand::{
22 diagnostics::DiagnosticSink, 22 diagnostics::DiagnosticSink,
@@ -35,12 +35,14 @@ use ra_syntax::{
35 ast::{self, AttrsOwner, NameOwner}, 35 ast::{self, AttrsOwner, NameOwner},
36 AstNode, 36 AstNode,
37}; 37};
38use ra_tt::{Ident, Leaf, Literal, TokenTree};
38use rustc_hash::FxHashSet; 39use rustc_hash::FxHashSet;
39use stdx::impl_from; 40use stdx::impl_from;
40 41
41use crate::{ 42use crate::{
42 db::{DefDatabase, HirDatabase}, 43 db::{DefDatabase, HirDatabase},
43 has_source::HasSource, 44 has_source::HasSource,
45 link_rewrite::Resolvable,
44 HirDisplay, InFile, Name, 46 HirDisplay, InFile, Name,
45}; 47};
46 48
@@ -120,6 +122,33 @@ impl Crate {
120 pub fn all(db: &dyn HirDatabase) -> Vec<Crate> { 122 pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {
121 db.crate_graph().iter().map(|id| Crate { id }).collect() 123 db.crate_graph().iter().map(|id| Crate { id }).collect()
122 } 124 }
125
126 /// Try to get the root URL of the documentation of a crate.
127 pub fn get_doc_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> {
128 // Look for #![doc(html_root_url = "...")]
129 let attrs = db.attrs(AttrDef::from(self.root_module(db)?).into());
130 let doc_attr_q = attrs.by_key("doc");
131
132 let doc_url = if doc_attr_q.exists() {
133 doc_attr_q.tt_values().map(|tt| {
134 let name = tt.token_trees.iter()
135 .skip_while(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Ident(Ident{text: ref ident, ..})) if ident == "html_root_url"))
136 .skip(2)
137 .next();
138
139 match name {
140 Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text),
141 _ => None
142 }
143 }).flat_map(|t| t).next().map(|s| s.to_string())
144 } else {
145 None
146 };
147
148 doc_url
149 .map(|s| s.trim_matches('"').trim_end_matches("/").to_owned() + "/")
150 .map(|s| s.to_string())
151 }
123} 152}
124 153
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 154#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1709,3 +1738,76 @@ pub trait HasVisibility {
1709 vis.is_visible_from(db.upcast(), module.id) 1738 vis.is_visible_from(db.upcast(), module.id)
1710 } 1739 }
1711} 1740}
1741
1742impl Resolvable for ModuleDef {
1743 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1744 Some(match self {
1745 ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db),
1746 ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db),
1747 ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db),
1748 ModuleDef::EnumVariant(ev) => {
1749 GenericDefId::from(GenericDef::from(ev.clone())).resolver(db)
1750 }
1751 ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db),
1752 ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db),
1753 ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db),
1754 ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db),
1755 // FIXME: This should be a resolver relative to `std/core`
1756 ModuleDef::BuiltinType(_t) => None?,
1757 })
1758 }
1759
1760 fn try_into_module_def(self) -> Option<ModuleDef> {
1761 Some(self)
1762 }
1763}
1764
1765impl Resolvable for TypeParam {
1766 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1767 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1768 }
1769
1770 fn try_into_module_def(self) -> Option<ModuleDef> {
1771 None
1772 }
1773}
1774
1775impl Resolvable for MacroDef {
1776 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1777 Some(Into::<ModuleId>::into(self.module(db)?).resolver(db))
1778 }
1779
1780 fn try_into_module_def(self) -> Option<ModuleDef> {
1781 None
1782 }
1783}
1784
1785impl Resolvable for Field {
1786 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1787 Some(Into::<VariantId>::into(Into::<VariantDef>::into(self.parent_def(db))).resolver(db))
1788 }
1789
1790 fn try_into_module_def(self) -> Option<ModuleDef> {
1791 None
1792 }
1793}
1794
1795impl Resolvable for ImplDef {
1796 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1797 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1798 }
1799
1800 fn try_into_module_def(self) -> Option<ModuleDef> {
1801 None
1802 }
1803}
1804
1805impl Resolvable for Local {
1806 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
1807 Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
1808 }
1809
1810 fn try_into_module_def(self) -> Option<ModuleDef> {
1811 None
1812 }
1813}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index e2d13dbfd..b33293a18 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_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 Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, 39 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
diff --git a/crates/ra_hir/src/link_rewrite.rs b/crates/ra_hir/src/link_rewrite.rs
new file mode 100644
index 000000000..a66f1e6fc
--- /dev/null
+++ b/crates/ra_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 ra_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}