aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorZac Pullar-Strecker <[email protected]>2020-08-24 09:26:45 +0100
committerZac Pullar-Strecker <[email protected]>2020-08-24 09:26:45 +0100
commit4f5f608b499ad50aa66b2213e3b92dd2fe61ed68 (patch)
tree6ca8e205c82c5b50c4c4d77dcfe0bea09dfe31d7 /crates
parentb34ad4bc8ad368a096c14d3710239a5a2eb9ccb5 (diff)
Reorganise code
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/Cargo.toml3
-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
-rw-r--r--crates/ra_ide/Cargo.toml6
-rw-r--r--crates/ra_ide/src/link_rewrite.rs241
-rw-r--r--crates/ra_ide_db/Cargo.toml1
-rw-r--r--crates/ra_ide_db/src/defs.rs17
8 files changed, 345 insertions, 255 deletions
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml
index c260bb193..a8c4875d4 100644
--- a/crates/ra_hir/Cargo.toml
+++ b/crates/ra_hir/Cargo.toml
@@ -16,6 +16,8 @@ arrayvec = "0.5.1"
16 16
17itertools = "0.9.0" 17itertools = "0.9.0"
18 18
19url = "2.1.1"
20
19stdx = { path = "../stdx" } 21stdx = { path = "../stdx" }
20ra_syntax = { path = "../ra_syntax" } 22ra_syntax = { path = "../ra_syntax" }
21ra_db = { path = "../ra_db" } 23ra_db = { path = "../ra_db" }
@@ -23,3 +25,4 @@ ra_prof = { path = "../ra_prof" }
23hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" } 25hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
24hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } 26hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
25hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" } 27hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
28ra_tt = { path = "../ra_tt" }
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}
diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml
index 485aa9081..de606f57e 100644
--- a/crates/ra_ide/Cargo.toml
+++ b/crates/ra_ide/Cargo.toml
@@ -17,10 +17,9 @@ indexmap = "1.3.2"
17itertools = "0.9.0" 17itertools = "0.9.0"
18log = "0.4.8" 18log = "0.4.8"
19rustc-hash = "1.1.0" 19rustc-hash = "1.1.0"
20url = "2.1.1" 20oorandom = "11.1.2"
21pulldown-cmark-to-cmark = "5.0.0" 21pulldown-cmark-to-cmark = "5.0.0"
22pulldown-cmark = {version = "0.7.2", default-features = false} 22pulldown-cmark = {version = "0.7.2", default-features = false}
23oorandom = "11.1.2"
24 23
25stdx = { path = "../stdx" } 24stdx = { path = "../stdx" }
26 25
@@ -34,9 +33,6 @@ ra_prof = { path = "../ra_prof" }
34test_utils = { path = "../test_utils" } 33test_utils = { path = "../test_utils" }
35ra_assists = { path = "../ra_assists" } 34ra_assists = { path = "../ra_assists" }
36ra_ssr = { path = "../ra_ssr" } 35ra_ssr = { path = "../ra_ssr" }
37ra_hir_def = { path = "../ra_hir_def" }
38ra_tt = { path = "../ra_tt" }
39ra_parser = { path = "../ra_parser" }
40 36
41# ra_ide should depend only on the top-level `hir` package. if you need 37# ra_ide should depend only on the top-level `hir` package. if you need
42# something from some `hir_xxx` subpackage, reexport the API via `hir`. 38# something from some `hir_xxx` subpackage, reexport the API via `hir`.
diff --git a/crates/ra_ide/src/link_rewrite.rs b/crates/ra_ide/src/link_rewrite.rs
index 37d695bb8..94d2c31c2 100644
--- a/crates/ra_ide/src/link_rewrite.rs
+++ b/crates/ra_ide/src/link_rewrite.rs
@@ -1,17 +1,10 @@
1//! Resolves and rewrites links in markdown documentation for hovers/completion windows. 1//! This is a wrapper around [`hir::link_rewrite`] connecting it to the markdown parser.
2 2
3use std::iter::once;
4
5use itertools::Itertools;
6use pulldown_cmark::{CowStr, Event, Options, Parser, Tag}; 3use pulldown_cmark::{CowStr, Event, Options, Parser, Tag};
7use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; 4use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions};
8use url::Url;
9 5
10use hir::{Adt, AsName, AttrDef, Crate, Hygiene, ItemInNs, ModPath, ModuleDef}; 6use hir::resolve_doc_link;
11use ra_hir_def::db::DefDatabase;
12use ra_ide_db::{defs::Definition, RootDatabase}; 7use ra_ide_db::{defs::Definition, RootDatabase};
13use ra_syntax::ast::Path;
14use ra_tt::{Ident, Leaf, Literal, TokenTree};
15 8
16/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) 9/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
17pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { 10pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String {
@@ -31,9 +24,14 @@ pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition)
31 // Two posibilities: 24 // Two posibilities:
32 // * path-based links: `../../module/struct.MyStruct.html` 25 // * path-based links: `../../module/struct.MyStruct.html`
33 // * module-based links (AKA intra-doc links): `super::super::module::MyStruct` 26 // * module-based links (AKA intra-doc links): `super::super::module::MyStruct`
34 let resolved = try_resolve_intra(db, definition, title, &target).or_else(|| { 27 let resolved = match definition {
35 try_resolve_path(db, definition, &target).map(|target| (target, title.to_string())) 28 Definition::ModuleDef(t) => resolve_doc_link(db, t, title, target),
36 }); 29 Definition::Macro(t) => resolve_doc_link(db, t, title, target),
30 Definition::Field(t) => resolve_doc_link(db, t, title, target),
31 Definition::SelfType(t) => resolve_doc_link(db, t, title, target),
32 Definition::Local(t) => resolve_doc_link(db, t, title, target),
33 Definition::TypeParam(t) => resolve_doc_link(db, t, title, target),
34 };
37 35
38 match resolved { 36 match resolved {
39 Some((target, title)) => (target, title), 37 Some((target, title)) => (target, title),
@@ -79,222 +77,3 @@ fn map_links<'e>(
79 _ => evt, 77 _ => evt,
80 }) 78 })
81} 79}
82
83#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
84enum Namespace {
85 Types,
86 Values,
87 Macros,
88}
89
90static TYPES: ([&str; 7], [&str; 0]) =
91 (["type", "struct", "enum", "mod", "trait", "union", "module"], []);
92static VALUES: ([&str; 8], [&str; 1]) =
93 (["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
94static MACROS: ([&str; 1], [&str; 1]) = (["macro"], ["!"]);
95
96impl Namespace {
97 /// Extract the specified namespace from an intra-doc-link if one exists.
98 ///
99 /// # Examples
100 ///
101 /// * `struct MyStruct` -> `Namespace::Types`
102 /// * `panic!` -> `Namespace::Macros`
103 /// * `fn@from_intra_spec` -> `Namespace::Values`
104 fn from_intra_spec(s: &str) -> Option<Self> {
105 [
106 (Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
107 (Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
108 (Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
109 ]
110 .iter()
111 .filter(|(_ns, (prefixes, suffixes))| {
112 prefixes
113 .clone()
114 .map(|prefix| {
115 s.starts_with(*prefix)
116 && s.chars()
117 .nth(prefix.len() + 1)
118 .map(|c| c == '@' || c == ' ')
119 .unwrap_or(false)
120 })
121 .any(|cond| cond)
122 || suffixes
123 .clone()
124 .map(|suffix| {
125 s.starts_with(*suffix)
126 && s.chars()
127 .nth(suffix.len() + 1)
128 .map(|c| c == '@' || c == ' ')
129 .unwrap_or(false)
130 })
131 .any(|cond| cond)
132 })
133 .map(|(ns, (_, _))| *ns)
134 .next()
135 }
136}
137
138// Strip prefixes, suffixes, and inline code marks from the given string.
139fn strip_prefixes_suffixes(mut s: &str) -> &str {
140 s = s.trim_matches('`');
141
142 [
143 (TYPES.0.iter(), TYPES.1.iter()),
144 (VALUES.0.iter(), VALUES.1.iter()),
145 (MACROS.0.iter(), MACROS.1.iter()),
146 ]
147 .iter()
148 .for_each(|(prefixes, suffixes)| {
149 prefixes.clone().for_each(|prefix| s = s.trim_start_matches(*prefix));
150 suffixes.clone().for_each(|suffix| s = s.trim_end_matches(*suffix));
151 });
152 s.trim_start_matches("@").trim()
153}
154
155/// Try to resolve path to local documentation via intra-doc-links (i.e. `super::gateway::Shard`).
156///
157/// See [RFC1946](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md).
158fn try_resolve_intra(
159 db: &RootDatabase,
160 definition: &Definition,
161 link_text: &str,
162 link_target: &str,
163) -> Option<(String, String)> {
164 // Set link_target for implied shortlinks
165 let link_target =
166 if link_target.is_empty() { link_text.trim_matches('`') } else { link_target };
167
168 // Namespace disambiguation
169 let namespace = Namespace::from_intra_spec(link_target);
170
171 // Strip prefixes/suffixes
172 let link_target = strip_prefixes_suffixes(link_target);
173
174 // Parse link as a module path
175 let path = Path::parse(link_target).ok()?;
176 let modpath = ModPath::from_src(path, &Hygiene::new_unhygienic()).unwrap();
177
178 // Resolve it relative to symbol's location (according to the RFC this should consider small scopes)
179 let resolver = definition.resolver(db)?;
180
181 let resolved = resolver.resolve_module_path_in_items(db, &modpath);
182 let (defid, namespace) = match namespace {
183 // FIXME: .or(resolved.macros)
184 None => resolved
185 .types
186 .map(|t| (t.0, Namespace::Types))
187 .or(resolved.values.map(|t| (t.0, Namespace::Values)))?,
188 Some(ns @ Namespace::Types) => (resolved.types?.0, ns),
189 Some(ns @ Namespace::Values) => (resolved.values?.0, ns),
190 // FIXME:
191 Some(Namespace::Macros) => None?,
192 };
193
194 // Get the filepath of the final symbol
195 let def: ModuleDef = defid.into();
196 let module = def.module(db)?;
197 let krate = module.krate();
198 let ns = match namespace {
199 Namespace::Types => ItemInNs::Types(defid),
200 Namespace::Values => ItemInNs::Values(defid),
201 // FIXME:
202 Namespace::Macros => None?,
203 };
204 let import_map = db.import_map(krate.into());
205 let path = import_map.path_of(ns)?;
206
207 Some((
208 get_doc_url(db, &krate)?
209 .join(&format!("{}/", krate.display_name(db)?))
210 .ok()?
211 .join(&path.segments.iter().map(|name| name.to_string()).join("/"))
212 .ok()?
213 .join(&get_symbol_filename(db, &Definition::ModuleDef(def))?)
214 .ok()?
215 .into_string(),
216 strip_prefixes_suffixes(link_text).to_string(),
217 ))
218}
219
220/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
221fn try_resolve_path(db: &RootDatabase, definition: &Definition, link: &str) -> Option<String> {
222 if !link.contains("#") && !link.contains(".html") {
223 return None;
224 }
225 let ns = if let Definition::ModuleDef(moddef) = definition {
226 ItemInNs::Types(moddef.clone().into())
227 } else {
228 return None;
229 };
230 let module = definition.module(db)?;
231 let krate = module.krate();
232 let import_map = db.import_map(krate.into());
233 let base = once(format!("{}", krate.display_name(db)?))
234 .chain(import_map.path_of(ns)?.segments.iter().map(|name| format!("{}", name)))
235 .join("/");
236
237 get_doc_url(db, &krate)
238 .and_then(|url| url.join(&base).ok())
239 .and_then(|url| {
240 get_symbol_filename(db, definition).as_deref().map(|f| url.join(f).ok()).flatten()
241 })
242 .and_then(|url| url.join(link).ok())
243 .map(|url| url.into_string())
244}
245
246/// Try to get the root URL of the documentation of a crate.
247fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
248 // Look for #![doc(html_root_url = "...")]
249 let attrs = db.attrs(AttrDef::from(krate.root_module(db)?).into());
250 let doc_attr_q = attrs.by_key("doc");
251
252 let doc_url = if doc_attr_q.exists() {
253 doc_attr_q.tt_values().map(|tt| {
254 let name = tt.token_trees.iter()
255 .skip_while(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Ident(Ident{text: ref ident, ..})) if ident == "html_root_url"))
256 .skip(2)
257 .next();
258
259 match name {
260 Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text),
261 _ => None
262 }
263 }).flat_map(|t| t).next().map(|s| s.to_string())
264 } else {
265 // Fallback to docs.rs
266 // FIXME: Specify an exact version here (from Cargo.lock)
267 Some(format!("https://docs.rs/{}/*", krate.display_name(db)?))
268 };
269
270 doc_url
271 .map(|s| s.trim_matches('"').trim_end_matches("/").to_owned() + "/")
272 .and_then(|s| Url::parse(&s).ok())
273}
274
275/// Get the filename and extension generated for a symbol by rustdoc.
276///
277/// Example: `struct.Shard.html`
278fn get_symbol_filename(db: &RootDatabase, definition: &Definition) -> Option<String> {
279 Some(match definition {
280 Definition::ModuleDef(def) => match def {
281 ModuleDef::Adt(adt) => match adt {
282 Adt::Struct(s) => format!("struct.{}.html", s.name(db)),
283 Adt::Enum(e) => format!("enum.{}.html", e.name(db)),
284 Adt::Union(u) => format!("union.{}.html", u.name(db)),
285 },
286 ModuleDef::Module(_) => "index.html".to_string(),
287 ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
288 ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
289 ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.as_name()),
290 ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)),
291 ModuleDef::EnumVariant(ev) => {
292 format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db))
293 }
294 ModuleDef::Const(c) => format!("const.{}.html", c.name(db)?),
295 ModuleDef::Static(s) => format!("static.{}.html", s.name(db)?),
296 },
297 Definition::Macro(m) => format!("macro.{}.html", m.name(db)?),
298 _ => None?,
299 })
300}
diff --git a/crates/ra_ide_db/Cargo.toml b/crates/ra_ide_db/Cargo.toml
index f345f1de8..2716a38cc 100644
--- a/crates/ra_ide_db/Cargo.toml
+++ b/crates/ra_ide_db/Cargo.toml
@@ -26,7 +26,6 @@ ra_text_edit = { path = "../ra_text_edit" }
26ra_db = { path = "../ra_db" } 26ra_db = { path = "../ra_db" }
27ra_prof = { path = "../ra_prof" } 27ra_prof = { path = "../ra_prof" }
28test_utils = { path = "../test_utils" } 28test_utils = { path = "../test_utils" }
29ra_hir_def = { path = "../ra_hir_def" }
30 29
31# ra_ide should depend only on the top-level `hir` package. if you need 30# ra_ide should depend only on the top-level `hir` package. if you need
32# something from some `hir_xxx` subpackage, reexport the API via `hir`. 31# something from some `hir_xxx` subpackage, reexport the API via `hir`.
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index 80c99935d..df56f2d9e 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -6,7 +6,6 @@
6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). 6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
7 7
8use hir::{ 8use hir::{
9 db::{DefDatabase, HirDatabase},
10 Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, 9 Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution,
11 Semantics, TypeParam, Visibility, 10 Semantics, TypeParam, Visibility,
12}; 11};
@@ -17,7 +16,6 @@ use ra_syntax::{
17}; 16};
18 17
19use crate::RootDatabase; 18use crate::RootDatabase;
20use ra_hir_def::resolver::{HasResolver, Resolver};
21 19
22// FIXME: a more precise name would probably be `Symbol`? 20// FIXME: a more precise name would probably be `Symbol`?
23#[derive(Debug, PartialEq, Eq, Copy, Clone)] 21#[derive(Debug, PartialEq, Eq, Copy, Clone)]
@@ -78,21 +76,6 @@ impl Definition {
78 }; 76 };
79 Some(name) 77 Some(name)
80 } 78 }
81
82 pub fn resolver<D: HirDatabase + DefDatabase>(&self, db: &D) -> Option<Resolver> {
83 use hir::VariantDef;
84 use ra_hir_def::*;
85 Some(match self {
86 Definition::ModuleDef(def) => def.resolver(db)?,
87 Definition::Field(field) => {
88 Into::<VariantId>::into(Into::<VariantDef>::into(field.parent_def(db))).resolver(db)
89 }
90 Definition::Macro(m) => Into::<ModuleId>::into(m.module(db)?).resolver(db),
91 Definition::SelfType(imp) => Into::<ImplId>::into(imp.clone()).resolver(db),
92 Definition::Local(local) => Into::<DefWithBodyId>::into(local.parent(db)).resolver(db),
93 Definition::TypeParam(tp) => Into::<ModuleId>::into(tp.module(db)).resolver(db),
94 })
95 }
96} 79}
97 80
98#[derive(Debug)] 81#[derive(Debug)]