Skip to content

Commit 4f5f608

Browse files
committed
Reorganise code
1 parent b34ad4b commit 4f5f608

File tree

9 files changed

+347
-260
lines changed

9 files changed

+347
-260
lines changed

Cargo.lock

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_hir/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ arrayvec = "0.5.1"
1616

1717
itertools = "0.9.0"
1818

19+
url = "2.1.1"
20+
1921
stdx = { path = "../stdx" }
2022
ra_syntax = { path = "../ra_syntax" }
2123
ra_db = { path = "../ra_db" }
2224
ra_prof = { path = "../ra_prof" }
2325
hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
2426
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
2527
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
28+
ra_tt = { path = "../ra_tt" }

crates/ra_hir/src/code_model.rs

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use hir_def::{
1616
type_ref::{Mutability, TypeRef},
1717
AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
1818
ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId,
19-
TraitId, TypeAliasId, TypeParamId, UnionId,
19+
TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
2020
};
2121
use hir_expand::{
2222
diagnostics::DiagnosticSink,
@@ -35,12 +35,14 @@ use ra_syntax::{
3535
ast::{self, AttrsOwner, NameOwner},
3636
AstNode,
3737
};
38+
use ra_tt::{Ident, Leaf, Literal, TokenTree};
3839
use rustc_hash::FxHashSet;
3940
use stdx::impl_from;
4041

4142
use crate::{
4243
db::{DefDatabase, HirDatabase},
4344
has_source::HasSource,
45+
link_rewrite::Resolvable,
4446
HirDisplay, InFile, Name,
4547
};
4648

@@ -120,6 +122,33 @@ impl Crate {
120122
pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {
121123
db.crate_graph().iter().map(|id| Crate { id }).collect()
122124
}
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+
}
123152
}
124153

125154
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1709,3 +1738,76 @@ pub trait HasVisibility {
17091738
vis.is_visible_from(db.upcast(), module.id)
17101739
}
17111740
}
1741+
1742+
impl 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+
1765+
impl 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+
1775+
impl 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+
1785+
impl 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+
1795+
impl 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+
1805+
impl 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+
}

crates/ra_hir/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub mod diagnostics;
2727

2828
mod from_id;
2929
mod code_model;
30+
mod link_rewrite;
3031

3132
mod has_source;
3233

@@ -38,6 +39,7 @@ pub use crate::{
3839
Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
3940
},
4041
has_source::HasSource,
42+
link_rewrite::resolve_doc_link,
4143
semantics::{original_range, PathResolution, Semantics, SemanticsScope},
4244
};
4345

0 commit comments

Comments
 (0)