Skip to content

Commit 452afae

Browse files
committed
Changes from review
1 parent 5452368 commit 452afae

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

crates/hir/src/code_model.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,16 @@ impl Crate {
126126
}
127127

128128
/// 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> {
129+
pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> {
130130
// Look for #![doc(html_root_url = "...")]
131131
let attrs = db.attrs(AttrDef::from(self.root_module(db)).into());
132132
let doc_attr_q = attrs.by_key("doc");
133133

134-
let doc_url = if doc_attr_q.exists() {
135-
doc_attr_q.tt_values().map(|tt| {
134+
if !doc_attr_q.exists() {
135+
return None;
136+
}
137+
138+
let doc_url = doc_attr_q.tt_values().map(|tt| {
136139
let name = tt.token_trees.iter()
137140
.skip_while(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Ident(Ident{text: ref ident, ..})) if ident == "html_root_url"))
138141
.skip(2)
@@ -142,14 +145,9 @@ impl Crate {
142145
Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text),
143146
_ => None
144147
}
145-
}).flat_map(|t| t).next().map(|s| s.to_string())
146-
} else {
147-
None
148-
};
148+
}).flat_map(|t| t).next();
149149

150-
doc_url
151-
.map(|s| s.trim_matches('"').trim_end_matches("/").to_owned() + "/")
152-
.map(|s| s.to_string())
150+
doc_url.map(|s| s.trim_matches('"').trim_end_matches("/").to_owned() + "/")
153151
}
154152
}
155153

crates/hir/src/doc_links.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,17 @@ fn try_resolve_intra<T: Resolvable, D: DefDatabase + HirDatabase>(
3838
let link_target =
3939
if link_target.is_empty() { link_text.trim_matches('`') } else { link_target };
4040

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);
41+
let doclink = IntraDocLink::from(link_target);
4642

4743
// Parse link as a module path
48-
let path = Path::parse(link_target).ok()?;
44+
let path = Path::parse(doclink.path).ok()?;
4945
let modpath = ModPath::from_src(path, &Hygiene::new_unhygienic()).unwrap();
5046

5147
// Resolve it relative to symbol's location (according to the RFC this should consider small scopes)
5248
let resolver = definition.resolver(db)?;
5349

5450
let resolved = resolver.resolve_module_path_in_items(db, &modpath);
55-
let (defid, namespace) = match namespace {
51+
let (defid, namespace) = match doclink.namespace {
5652
// FIXME: .or(resolved.macros)
5753
None => resolved
5854
.types
@@ -133,7 +129,7 @@ fn strip_prefixes_suffixes(mut s: &str) -> &str {
133129

134130
fn get_doc_url(db: &dyn HirDatabase, krate: &Crate) -> Option<Url> {
135131
krate
136-
.get_doc_url(db)
132+
.get_html_root_url(db)
137133
.or_else(||
138134
// Fallback to docs.rs
139135
// FIXME: Specify an exact version here. This may be difficult, as multiple versions of the same crate could exist.
@@ -164,6 +160,17 @@ fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<S
164160
})
165161
}
166162

163+
struct IntraDocLink<'s> {
164+
path: &'s str,
165+
namespace: Option<Namespace>,
166+
}
167+
168+
impl<'s> From<&'s str> for IntraDocLink<'s> {
169+
fn from(s: &'s str) -> Self {
170+
Self { path: strip_prefixes_suffixes(s), namespace: Namespace::from_intra_spec(s) }
171+
}
172+
}
173+
167174
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
168175
enum Namespace {
169176
Types,

0 commit comments

Comments
 (0)