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

Update doc urls #85

Merged
merged 1 commit into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct AnalysisHost<L: AnalysisLoader = CargoAnalysisLoader> {

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

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum AError {
MutexPoison,
Unclassified,
Expand Down Expand Up @@ -469,25 +469,45 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
}

match def.parent {
Some(p) => analysis.with_defs(p, |parent| {
let parent_qualpath = parent.qualname.replace("::", "/");
let ns = name_space_for_def_kind(def.kind);
format!(
"{}/{}.t.html#{}.{}",
analysis.doc_url_base,
parent_qualpath,
def.name,
ns
)
}),
Some(p) => {
analysis.with_defs(p, |parent| match def.kind {
DefKind::Field | DefKind::Method | DefKind::Tuple => {
let ns = name_space_for_def_kind(def.kind);
let mut res = AnalysisHost::<L>::mk_doc_url(&parent, analysis)
.unwrap_or_else(|| "".into());
res.push_str(&format!("#{}.{}", def.name, ns));
res
}
DefKind::Mod => {
let parent_qualpath = parent.qualname.replace("::", "/");
format!(
"{}/{}/{}/",
analysis.doc_url_base,
parent_qualpath.trim_right_matches('/'),
def.name,
)
}
_ => {
let parent_qualpath = parent.qualname.replace("::", "/");
let ns = name_space_for_def_kind(def.kind);
format!(
"{}/{}/{}.{}.html",
analysis.doc_url_base,
parent_qualpath,
def.name,
ns,
)
}
})
}
None => {
let qualpath = def.qualname.replace("::", "/");
let ns = name_space_for_def_kind(def.kind);
Some(format!(
"{}/{}.{}.html",
analysis.doc_url_base,
qualpath,
ns
ns,
))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ fn read_crate_data(path: &Path) -> Option<Analysis> {
pub fn name_space_for_def_kind(dk: DefKind) -> char {
match dk {
DefKind::Enum |
DefKind::Tuple |
DefKind::Struct |
DefKind::Union |
DefKind::Type |
Expand All @@ -143,6 +142,7 @@ pub fn name_space_for_def_kind(dk: DefKind) -> char {
DefKind::Local |
DefKind::Static |
DefKind::Const |
DefKind::Tuple |
DefKind::Field => 'v',
DefKind::Macro => 'm',
}
Expand Down
109 changes: 109 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,115 @@ impl AnalysisLoader for TestAnalysisLoader {
}
}

#[test]
fn doc_urls_resolve_correctly() {
let host = AnalysisHost::new_with_loader(TestAnalysisLoader::new(
Path::new("test_data/rust-analysis").to_owned(),
));
host.reload(
Path::new("test_data/rust-analysis"),
Path::new("test_data/rust-analysis"),
).unwrap();

fn assert_url_for_type<S: Into<Option<&'static str>>>(
host: &AnalysisHost<TestAnalysisLoader>,
type_: &str,
qualname: S,
url: &str,
) {
let qualname = qualname.into();
let ids = host.search_for_id(type_).unwrap();
let defs: Vec<_> = ids.into_iter()
.map(|id| host.get_def(id).unwrap())
.filter(|def| {
qualname.is_none() || def.qualname == qualname.unwrap()
})
.collect();
println!("{:#?}", defs);
assert_eq!(defs.len(), 1);
assert_eq!(host.doc_url(&defs[0].span), Ok(url.into()));
}

// FIXME This test cannot work for some values
// Primitives like i64. i64 is shown with type mod but requires name "primitive".
// All methods (instead of trait methods, see as_mut), seem to only be available for generic qualname
// Unions like ManuallyDrop are not in the analysis file, just methods implemented for them or methods using them

assert_url_for_type(
&host,
"MAIN_SEPARATOR",
None,
"https://doc.rust-lang.org/nightly/std/path/MAIN_SEPARATOR.v.html",
);
// the parent has a qualname which is not represented in the usage, the ip part
assert_url_for_type(
&host,
"Ipv4Addr",
None,
"https://doc.rust-lang.org/nightly/std/net/ip/Ipv4Addr.t.html",
);
assert_url_for_type(
&host,
"VarError",
None,
"https://doc.rust-lang.org/nightly/std/env/VarError.t.html",
);
assert_url_for_type(
&host,
"NotPresent",
None,
"https://doc.rust-lang.org/nightly/std/env/VarError.t.html#NotPresent.v",
);
assert_url_for_type(
&host,
"Result",
"std::thread::Result",
"https://doc.rust-lang.org/nightly/std/thread/Result.t.html",
);
assert_url_for_type(
&host,
"args",
"std::env::args",
"https://doc.rust-lang.org/nightly/std/env/args.v.html",
);
assert_url_for_type(
&host,
"AsciiExt",
None,
"https://doc.rust-lang.org/nightly/std/ascii/AsciiExt.t.html",
);
assert_url_for_type(
&host,
"is_ascii",
None,
"https://doc.rust-lang.org/nightly/std/ascii/AsciiExt.t.html#is_ascii.v",
);
assert_url_for_type(
&host,
"status",
"std::process::Output::status",
"https://doc.rust-lang.org/nightly/std/process/Output.t.html#status.v",
);
assert_url_for_type(
&host,
"copy",
"std::fs::copy",
"https://doc.rust-lang.org/nightly/std/fs/copy.v.html",
);
// prelude and fs are both mod, but the parent once has a trailing / and once not
assert_url_for_type(
&host,
"prelude",
"std::io::prelude",
"https://doc.rust-lang.org/nightly/std/io/prelude/",
);
assert_url_for_type(
&host,
"fs",
"std::fs",
"https://doc.rust-lang.org/nightly/std/fs/",
);
}

#[test]
fn smoke() {
Expand Down
1 change: 1 addition & 0 deletions test_data/rust-analysis/libstd-f594a99642bc74c0.json

Large diffs are not rendered by default.