Add updated support for example-analyzer
Move rendering of examples into Finalize design Cleanup, rename found -> scraped Softer yellow Clean up dead code Document scrape_examples More simplification and documentation Remove extra css Test
This commit is contained in:
parent
0eabf25b90
commit
4b3f82ad03
12 changed files with 609 additions and 21 deletions
|
@ -39,6 +39,7 @@ crate use span_map::{collect_spans_and_sources, LinkFromSrc};
|
|||
use std::collections::VecDeque;
|
||||
use std::default::Default;
|
||||
use std::fmt;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::str;
|
||||
use std::string::ToString;
|
||||
|
@ -68,6 +69,8 @@ use crate::html::format::{
|
|||
print_generic_bounds, print_where_clause, Buffer, HrefError, PrintWithSpace,
|
||||
};
|
||||
use crate::html::markdown::{HeadingOffset, Markdown, MarkdownHtml, MarkdownSummaryLine};
|
||||
use crate::html::sources;
|
||||
use crate::scrape_examples::FnCallLocations;
|
||||
|
||||
/// A pair of name and its optional document.
|
||||
crate type NameDoc = (String, Option<String>);
|
||||
|
@ -584,6 +587,13 @@ fn document_full_inner(
|
|||
render_markdown(w, cx, &s, item.links(cx), heading_offset);
|
||||
}
|
||||
}
|
||||
|
||||
match &*item.kind {
|
||||
clean::ItemKind::FunctionItem(f) | clean::ItemKind::MethodItem(f, _) => {
|
||||
render_call_locations(w, cx, &f.call_locations);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Add extra information about an item such as:
|
||||
|
@ -2440,3 +2450,88 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
|
|||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn render_call_locations(
|
||||
w: &mut Buffer,
|
||||
cx: &Context<'_>,
|
||||
call_locations: &Option<FnCallLocations>,
|
||||
) {
|
||||
let call_locations = match call_locations.as_ref() {
|
||||
Some(call_locations) => call_locations,
|
||||
None => {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let filtered_locations: Vec<_> = call_locations
|
||||
.iter()
|
||||
.filter_map(|(file, locs)| {
|
||||
// TODO(wcrichto): file I/O should be cached
|
||||
let mut contents = match fs::read_to_string(&file) {
|
||||
Ok(contents) => contents,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to read file {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
// Remove the utf-8 BOM if any
|
||||
if contents.starts_with('\u{feff}') {
|
||||
contents.drain(..3);
|
||||
}
|
||||
|
||||
Some((file, contents, locs))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let n_examples = filtered_locations.len();
|
||||
if n_examples == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let id = cx.id_map.borrow_mut().derive("scraped-examples");
|
||||
write!(
|
||||
w,
|
||||
r##"<div class="docblock scraped-example-list">
|
||||
<h1 id="scraped-examples" class="small-section-header">
|
||||
<a href="#{}">Uses found in <code>examples/</code></a>
|
||||
</h1>"##,
|
||||
id
|
||||
);
|
||||
|
||||
let write_example = |w: &mut Buffer, (file, contents, locs): (&String, String, _)| {
|
||||
let ex_title = match cx.shared.repository_url.as_ref() {
|
||||
Some(url) => format!(
|
||||
r#"<a href="{url}/{file}" target="_blank">{file}</a>"#,
|
||||
file = file,
|
||||
url = url
|
||||
),
|
||||
None => file.clone(),
|
||||
};
|
||||
let edition = cx.shared.edition();
|
||||
write!(
|
||||
w,
|
||||
r#"<div class="scraped-example" data-code="{code}" data-locs="{locations}">
|
||||
<strong>{title}</strong>
|
||||
<div class="code-wrapper">"#,
|
||||
code = contents.replace("\"", """),
|
||||
locations = serde_json::to_string(&locs).unwrap(),
|
||||
title = ex_title,
|
||||
);
|
||||
write!(w, r#"<span class="prev">≺</span> <span class="next">≻</span>"#);
|
||||
write!(w, r#"<span class="expand">↕</span>"#);
|
||||
sources::print_src(w, &contents, edition);
|
||||
write!(w, "</div></div>");
|
||||
};
|
||||
|
||||
let mut it = filtered_locations.into_iter();
|
||||
write_example(w, it.next().unwrap());
|
||||
|
||||
if n_examples > 1 {
|
||||
write!(w, r#"<div class="more-scraped-examples hidden">"#);
|
||||
it.for_each(|ex| write_example(w, ex));
|
||||
write!(w, "</div>");
|
||||
}
|
||||
|
||||
write!(w, "</div>");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue