aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-26 19:26:07 +0100
committerGitHub <[email protected]>2020-08-26 19:26:07 +0100
commite7da5816d67fadc056ee43310f9d04164c2617d9 (patch)
tree854e94856b4d120d156b67e790d7f2ba74e14610 /crates/hir/src
parent7e012ae8b54705bc254154cdff3a503cce3299a7 (diff)
parentf8a59adf5e9633aa5d10efcdbf70b408d280ef01 (diff)
Merge #5895
5895: Tease apart orthogonal concerns in markdown link rewriting r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/hir/src')
-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
4 files changed, 65 insertions, 320 deletions
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};