diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-04-13 17:35:32 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-13 17:35:32 +0200 |
| commit | db61452b7a2ec92d90f4faebfa679c54ad3e1ab0 (patch) | |
| tree | 2b1f856f0aaba53b6c53b69669f807d523c8eb66 /src/librustdoc/html/render | |
| parent | f38c5c8e5d76ea9a87fece143425ef7c703c706a (diff) | |
| parent | 6a18b6865590704d42777fa7432436e55773d46b (diff) | |
| download | rust-db61452b7a2ec92d90f4faebfa679c54ad3e1ab0.tar.gz rust-db61452b7a2ec92d90f4faebfa679c54ad3e1ab0.zip | |
Rollup merge of #93217 - willcrichton:example-analyzer, r=GuillaumeGomez
Improve Rustdoc UI for scraped examples with multiline arguments, fix overflow in line numbers This PR improves a few aspects of the scrape examples feature in Rustdoc. * Only function names and not the full call expression are highlighted. * For call-sites with multiline arguments, the minimized code viewer will scroll to the top of the call-site rather than the middle if the argument is larger than the viewer size, ensuring that the function name is visible. * This fixes an issue where the line numbers column had a visible x-scroll bar. r? `@GuillaumeGomez`
Diffstat (limited to 'src/librustdoc/html/render')
| -rw-r--r-- | src/librustdoc/html/render/context.rs | 19 | ||||
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 36 |
2 files changed, 52 insertions, 3 deletions
diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 34784bbed0c..90123655758 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -17,8 +17,8 @@ use super::print_item::{full_path, item_path, print_item}; use super::search_index::build_index; use super::write_shared::write_shared; use super::{ - collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath, - BASIC_KEYWORDS, + collect_spans_and_sources, print_sidebar, scrape_examples_help, settings, AllTypes, + LinkFromSrc, NameDoc, StylePath, BASIC_KEYWORDS, }; use crate::clean::{self, types::ExternalLocation, ExternalCrate}; @@ -551,6 +551,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { let crate_name = self.tcx().crate_name(LOCAL_CRATE); let final_file = self.dst.join(crate_name.as_str()).join("all.html"); let settings_file = self.dst.join("settings.html"); + let scrape_examples_help_file = self.dst.join("scrape-examples-help.html"); let mut root_path = self.dst.to_str().expect("invalid path").to_owned(); if !root_path.ends_with('/') { @@ -606,6 +607,20 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { &self.shared.style_files, ); self.shared.fs.write(settings_file, v)?; + + if self.shared.layout.scrape_examples_extension { + page.title = "About scraped examples"; + page.description = "How the scraped examples feature works in Rustdoc"; + let v = layout::render( + &self.shared.layout, + &page, + "", + scrape_examples_help(&*self.shared), + &self.shared.style_files, + ); + self.shared.fs.write(scrape_examples_help_file, v)?; + } + if let Some(ref redirections) = self.shared.redirections { if !redirections.borrow().is_empty() { let redirect_map_path = diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 9891c4b676f..a4cc42e2a01 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -75,8 +75,10 @@ use crate::html::format::{ use crate::html::highlight; use crate::html::markdown::{HeadingOffset, IdMap, Markdown, MarkdownHtml, MarkdownSummaryLine}; use crate::html::sources; +use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD; use crate::scrape_examples::{CallData, CallLocation}; use crate::try_none; +use crate::DOC_RUST_LANG_ORG_CHANNEL; /// A pair of name and its optional document. crate type NameDoc = (String, Option<String>); @@ -460,6 +462,34 @@ fn settings(root_path: &str, suffix: &str, theme_names: Vec<String>) -> Result<S )) } +fn scrape_examples_help(shared: &SharedContext<'_>) -> String { + let mut content = SCRAPE_EXAMPLES_HELP_MD.to_owned(); + content.push_str(&format!( + "## More information\n\n\ + If you want more information about this feature, please read the [corresponding chapter in the Rustdoc book]({}/rustdoc/scraped-examples.html).", + DOC_RUST_LANG_ORG_CHANNEL)); + + let mut ids = IdMap::default(); + format!( + "<div class=\"main-heading\">\ + <h1 class=\"fqn\">\ + <span class=\"in-band\">About scraped examples</span>\ + </h1>\ + </div>\ + <div>{}</div>", + Markdown { + content: &content, + links: &[], + ids: &mut ids, + error_codes: shared.codes, + edition: shared.edition(), + playground: &shared.playground, + heading_offset: HeadingOffset::H1 + } + .into_string() + ) +} + fn document( w: &mut Buffer, cx: &Context<'_>, @@ -2743,7 +2773,9 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { <span></span>\ <h5 id=\"{id}\">\ <a href=\"#{id}\">Examples found in repository</a>\ + <a class=\"scrape-help\" href=\"{root_path}scrape-examples-help.html\">?</a>\ </h5>", + root_path = cx.root_path(), id = id ); @@ -2795,9 +2827,10 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { .locations .iter() .map(|loc| { - let (byte_lo, byte_hi) = loc.call_expr.byte_span; + let (byte_lo, byte_hi) = loc.call_ident.byte_span; let (line_lo, line_hi) = loc.call_expr.line_span; let byte_range = (byte_lo - byte_min, byte_hi - byte_min); + let line_range = (line_lo - line_min, line_hi - line_min); let (line_url, line_title) = link_to_loc(call_data, loc); @@ -2913,6 +2946,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { <summary class=\"hideme\">\ <span>More examples</span>\ </summary>\ + <div class=\"hide-more\">Hide additional examples</div>\ <div class=\"more-scraped-examples\">\ <div class=\"toggle-line\"><div class=\"toggle-line-inner\"></div></div>\ <div class=\"more-scraped-examples-inner\">" |
