Skip to content
This repository was archived by the owner on Mar 1, 2019. It is now read-only.

Commit 50a1f80

Browse files
authored
Merge pull request #85 from jonasbb/wip-update-doc-urls
Update doc urls
2 parents 1a7c666 + 63ef23f commit 50a1f80

File tree

4 files changed

+144
-14
lines changed

4 files changed

+144
-14
lines changed

src/lib.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct AnalysisHost<L: AnalysisLoader = CargoAnalysisLoader> {
4545

4646
pub type AResult<T> = Result<T, AError>;
4747

48-
#[derive(Debug, Copy, Clone)]
48+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
4949
pub enum AError {
5050
MutexPoison,
5151
Unclassified,
@@ -469,25 +469,45 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
469469
}
470470

471471
match def.parent {
472-
Some(p) => analysis.with_defs(p, |parent| {
473-
let parent_qualpath = parent.qualname.replace("::", "/");
474-
let ns = name_space_for_def_kind(def.kind);
475-
format!(
476-
"{}/{}.t.html#{}.{}",
477-
analysis.doc_url_base,
478-
parent_qualpath,
479-
def.name,
480-
ns
481-
)
482-
}),
472+
Some(p) => {
473+
analysis.with_defs(p, |parent| match def.kind {
474+
DefKind::Field | DefKind::Method | DefKind::Tuple => {
475+
let ns = name_space_for_def_kind(def.kind);
476+
let mut res = AnalysisHost::<L>::mk_doc_url(&parent, analysis)
477+
.unwrap_or_else(|| "".into());
478+
res.push_str(&format!("#{}.{}", def.name, ns));
479+
res
480+
}
481+
DefKind::Mod => {
482+
let parent_qualpath = parent.qualname.replace("::", "/");
483+
format!(
484+
"{}/{}/{}/",
485+
analysis.doc_url_base,
486+
parent_qualpath.trim_right_matches('/'),
487+
def.name,
488+
)
489+
}
490+
_ => {
491+
let parent_qualpath = parent.qualname.replace("::", "/");
492+
let ns = name_space_for_def_kind(def.kind);
493+
format!(
494+
"{}/{}/{}.{}.html",
495+
analysis.doc_url_base,
496+
parent_qualpath,
497+
def.name,
498+
ns,
499+
)
500+
}
501+
})
502+
}
483503
None => {
484504
let qualpath = def.qualname.replace("::", "/");
485505
let ns = name_space_for_def_kind(def.kind);
486506
Some(format!(
487507
"{}/{}.{}.html",
488508
analysis.doc_url_base,
489509
qualpath,
490-
ns
510+
ns,
491511
))
492512
}
493513
}

src/raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ fn read_crate_data(path: &Path) -> Option<Analysis> {
132132
pub fn name_space_for_def_kind(dk: DefKind) -> char {
133133
match dk {
134134
DefKind::Enum |
135-
DefKind::Tuple |
136135
DefKind::Struct |
137136
DefKind::Union |
138137
DefKind::Type |
@@ -143,6 +142,7 @@ pub fn name_space_for_def_kind(dk: DefKind) -> char {
143142
DefKind::Local |
144143
DefKind::Static |
145144
DefKind::Const |
145+
DefKind::Tuple |
146146
DefKind::Field => 'v',
147147
DefKind::Macro => 'm',
148148
}

src/test/mod.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,115 @@ impl AnalysisLoader for TestAnalysisLoader {
4040
}
4141
}
4242

43+
#[test]
44+
fn doc_urls_resolve_correctly() {
45+
let host = AnalysisHost::new_with_loader(TestAnalysisLoader::new(
46+
Path::new("test_data/rust-analysis").to_owned(),
47+
));
48+
host.reload(
49+
Path::new("test_data/rust-analysis"),
50+
Path::new("test_data/rust-analysis"),
51+
).unwrap();
52+
53+
fn assert_url_for_type<S: Into<Option<&'static str>>>(
54+
host: &AnalysisHost<TestAnalysisLoader>,
55+
type_: &str,
56+
qualname: S,
57+
url: &str,
58+
) {
59+
let qualname = qualname.into();
60+
let ids = host.search_for_id(type_).unwrap();
61+
let defs: Vec<_> = ids.into_iter()
62+
.map(|id| host.get_def(id).unwrap())
63+
.filter(|def| {
64+
qualname.is_none() || def.qualname == qualname.unwrap()
65+
})
66+
.collect();
67+
println!("{:#?}", defs);
68+
assert_eq!(defs.len(), 1);
69+
assert_eq!(host.doc_url(&defs[0].span), Ok(url.into()));
70+
}
71+
72+
// FIXME This test cannot work for some values
73+
// Primitives like i64. i64 is shown with type mod but requires name "primitive".
74+
// All methods (instead of trait methods, see as_mut), seem to only be available for generic qualname
75+
// Unions like ManuallyDrop are not in the analysis file, just methods implemented for them or methods using them
76+
77+
assert_url_for_type(
78+
&host,
79+
"MAIN_SEPARATOR",
80+
None,
81+
"https://doc.rust-lang.org/nightly/std/path/MAIN_SEPARATOR.v.html",
82+
);
83+
// the parent has a qualname which is not represented in the usage, the ip part
84+
assert_url_for_type(
85+
&host,
86+
"Ipv4Addr",
87+
None,
88+
"https://doc.rust-lang.org/nightly/std/net/ip/Ipv4Addr.t.html",
89+
);
90+
assert_url_for_type(
91+
&host,
92+
"VarError",
93+
None,
94+
"https://doc.rust-lang.org/nightly/std/env/VarError.t.html",
95+
);
96+
assert_url_for_type(
97+
&host,
98+
"NotPresent",
99+
None,
100+
"https://doc.rust-lang.org/nightly/std/env/VarError.t.html#NotPresent.v",
101+
);
102+
assert_url_for_type(
103+
&host,
104+
"Result",
105+
"std::thread::Result",
106+
"https://doc.rust-lang.org/nightly/std/thread/Result.t.html",
107+
);
108+
assert_url_for_type(
109+
&host,
110+
"args",
111+
"std::env::args",
112+
"https://doc.rust-lang.org/nightly/std/env/args.v.html",
113+
);
114+
assert_url_for_type(
115+
&host,
116+
"AsciiExt",
117+
None,
118+
"https://doc.rust-lang.org/nightly/std/ascii/AsciiExt.t.html",
119+
);
120+
assert_url_for_type(
121+
&host,
122+
"is_ascii",
123+
None,
124+
"https://doc.rust-lang.org/nightly/std/ascii/AsciiExt.t.html#is_ascii.v",
125+
);
126+
assert_url_for_type(
127+
&host,
128+
"status",
129+
"std::process::Output::status",
130+
"https://doc.rust-lang.org/nightly/std/process/Output.t.html#status.v",
131+
);
132+
assert_url_for_type(
133+
&host,
134+
"copy",
135+
"std::fs::copy",
136+
"https://doc.rust-lang.org/nightly/std/fs/copy.v.html",
137+
);
138+
// prelude and fs are both mod, but the parent once has a trailing / and once not
139+
assert_url_for_type(
140+
&host,
141+
"prelude",
142+
"std::io::prelude",
143+
"https://doc.rust-lang.org/nightly/std/io/prelude/",
144+
);
145+
assert_url_for_type(
146+
&host,
147+
"fs",
148+
"std::fs",
149+
"https://doc.rust-lang.org/nightly/std/fs/",
150+
);
151+
}
43152

44153
#[test]
45154
fn smoke() {

test_data/rust-analysis/libstd-f594a99642bc74c0.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)