Skip to content

Commit 7d09dba

Browse files
author
Timothée Delabrouille
committed
added methods src_root and location to External crate, remove extern_location function
1 parent 855c2d1 commit 7d09dba

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

src/librustdoc/clean/types.rs

+52
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt;
44
use std::hash::{Hash, Hasher};
55
use std::iter::FromIterator;
66
use std::lazy::SyncOnceCell as OnceCell;
7+
use std::path::PathBuf;
78
use std::rc::Rc;
89
use std::sync::Arc;
910
use std::{slice, vec};
@@ -90,6 +91,57 @@ impl ExternalCrate {
9091
tcx.crate_name(self.crate_num)
9192
}
9293

94+
crate fn src_root(&self, tcx: TyCtxt<'_>) -> PathBuf {
95+
match self.src(tcx) {
96+
FileName::Real(ref p) => match p.local_path().parent() {
97+
Some(p) => p.to_path_buf(),
98+
None => PathBuf::new(),
99+
},
100+
_ => PathBuf::new(),
101+
}
102+
}
103+
104+
/// Attempts to find where an external crate is located, given that we're
105+
/// rendering in to the specified source destination.
106+
crate fn location(
107+
&self,
108+
extern_url: Option<&str>,
109+
ast_attrs: &[ast::Attribute],
110+
dst: &std::path::Path,
111+
tcx: TyCtxt<'_>,
112+
) -> ExternalLocation {
113+
use ExternalLocation::*;
114+
// See if there's documentation generated into the local directory
115+
let local_location = dst.join(&*self.name(tcx).as_str());
116+
if local_location.is_dir() {
117+
return Local;
118+
}
119+
120+
if let Some(url) = extern_url {
121+
let mut url = url.to_string();
122+
if !url.ends_with('/') {
123+
url.push('/');
124+
}
125+
return Remote(url);
126+
}
127+
128+
// Failing that, see if there's an attribute specifying where to find this
129+
// external crate
130+
ast_attrs
131+
.lists(sym::doc)
132+
.filter(|a| a.has_name(sym::html_root_url))
133+
.filter_map(|a| a.value_str())
134+
.map(|url| {
135+
let mut url = url.to_string();
136+
if !url.ends_with('/') {
137+
url.push('/')
138+
}
139+
Remote(url)
140+
})
141+
.next()
142+
.unwrap_or(Unknown) // Well, at least we tried.
143+
}
144+
93145
crate fn keywords(&self, tcx: TyCtxt<'_>) -> ThinVec<(DefId, Symbol)> {
94146
let root = self.def_id();
95147

src/librustdoc/formats/cache.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
77
use rustc_middle::middle::privacy::AccessLevels;
88
use rustc_middle::ty::TyCtxt;
9-
use rustc_span::source_map::FileName;
109
use rustc_span::symbol::sym;
1110
use rustc_span::Symbol;
1211

@@ -15,7 +14,7 @@ use crate::fold::DocFolder;
1514
use crate::formats::item_type::ItemType;
1615
use crate::formats::Impl;
1716
use crate::html::markdown::short_markdown_summary;
18-
use crate::html::render::cache::{extern_location, get_index_search_type, ExternalLocation};
17+
use crate::html::render::cache::{get_index_search_type, ExternalLocation};
1918
use crate::html::render::IndexItem;
2019

2120
/// This cache is used to store information about the [`clean::Crate`] being
@@ -155,21 +154,13 @@ impl Cache {
155154
// Cache where all our extern crates are located
156155
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
157156
for &(n, ref e) in &krate.externs {
158-
let src_root = match e.src(tcx) {
159-
FileName::Real(ref p) => match p.local_path().parent() {
160-
Some(p) => p.to_path_buf(),
161-
None => PathBuf::new(),
162-
},
163-
_ => PathBuf::new(),
164-
};
165157
let name = e.name(tcx);
166158
let extern_url = extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
167159
let did = DefId { krate: n, index: CRATE_DEF_INDEX };
168160
self.extern_locations.insert(
169161
n,
170-
(name, src_root, extern_location(e, extern_url, tcx.get_attrs(did), &dst, tcx)),
162+
(name, e.src_root(tcx), e.location(extern_url, tcx.get_attrs(did), &dst, tcx)),
171163
);
172-
173164
self.external_paths.insert(did, (vec![name.to_string()], ItemType::Module));
174165
}
175166

src/librustdoc/html/render/cache.rs

+2-45
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
use std::collections::BTreeMap;
2-
use std::path::Path;
32

4-
use rustc_ast::ast;
53
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
64
use rustc_middle::ty::TyCtxt;
7-
use rustc_span::symbol::{sym, Symbol};
5+
use rustc_span::symbol::Symbol;
86
use serde::ser::{Serialize, SerializeStruct, Serializer};
97

108
use crate::clean;
119
use crate::clean::types::{
12-
AttributesExt, FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate,
10+
FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate,
1311
};
1412
use crate::formats::cache::Cache;
1513
use crate::formats::item_type::ItemType;
@@ -26,47 +24,6 @@ crate enum ExternalLocation {
2624
Unknown,
2725
}
2826

29-
/// Attempts to find where an external crate is located, given that we're
30-
/// rendering in to the specified source destination.
31-
crate fn extern_location(
32-
e: &clean::ExternalCrate,
33-
extern_url: Option<&str>,
34-
ast_attrs: &[ast::Attribute],
35-
dst: &Path,
36-
tcx: TyCtxt<'_>,
37-
) -> ExternalLocation {
38-
use ExternalLocation::*;
39-
// See if there's documentation generated into the local directory
40-
let local_location = dst.join(&*e.name(tcx).as_str());
41-
if local_location.is_dir() {
42-
return Local;
43-
}
44-
45-
if let Some(url) = extern_url {
46-
let mut url = url.to_string();
47-
if !url.ends_with('/') {
48-
url.push('/');
49-
}
50-
return Remote(url);
51-
}
52-
53-
// Failing that, see if there's an attribute specifying where to find this
54-
// external crate
55-
ast_attrs
56-
.lists(sym::doc)
57-
.filter(|a| a.has_name(sym::html_root_url))
58-
.filter_map(|a| a.value_str())
59-
.map(|url| {
60-
let mut url = url.to_string();
61-
if !url.ends_with('/') {
62-
url.push('/')
63-
}
64-
Remote(url)
65-
})
66-
.next()
67-
.unwrap_or(Unknown) // Well, at least we tried.
68-
}
69-
7027
/// Builds the search index from the collected metadata
7128
crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<'tcx>) -> String {
7229
let mut defid_to_pathid = FxHashMap::default();

0 commit comments

Comments
 (0)