-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve Rustdoc UI for scraped examples with multiline arguments, fix overflow in line numbers #93217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Improve Rustdoc UI for scraped examples with multiline arguments, fix overflow in line numbers #93217
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7e81b0a
Improve Rustdoc UI for scraped examples with multiline arguments, fix
willcrichton d58c9df
Only highlight identifier in scraped examples, not arguments
willcrichton f1e3e2c
Use PathSegment::ident for scraping method calls
willcrichton ae5d0cb
Improve alignment of additional scraped examples, add scrape examples…
willcrichton 7cca693
Fix markdown issue, remove hard-coded rust-lang.org url
willcrichton bb3ed6f
Improve styling on scrape examples help button
willcrichton 318e457
Clarify that scrape examples is unstable
willcrichton d1416d5
Add scrape examples help page
willcrichton b9ecdca
Don't panic when scraping invalid calls
willcrichton 6a18b68
Add Rustdoc book link to scrape examples help. Remove remaining panic
willcrichton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Scraped examples | ||
|
||
Rustdoc has an unstable feature where it can automatically scrape examples of items being documented from the `examples/` directory of a Cargo workspace. These examples will be included within the generated documentation for that item. For example, if your library contains a public function: | ||
|
||
```rust,ignore (needs-other-file) | ||
// a_crate/src/lib.rs | ||
pub fn a_func() {} | ||
``` | ||
|
||
And you have an example calling this function: | ||
|
||
```rust,ignore (needs-other-file) | ||
// a_crate/examples/ex.rs | ||
fn main() { | ||
a_crate::a_func(); | ||
} | ||
``` | ||
|
||
Then this code snippet will be included in the documentation for `a_func`. This documentation is inserted by Rustdoc and cannot be manually edited by the crate author. | ||
|
||
|
||
## How to use this feature | ||
|
||
This feature is unstable, so you can enable it by calling Rustdoc with the unstable `rustdoc-scrape-examples` flag: | ||
|
||
```bash | ||
cargo doc -Zunstable-options -Zrustdoc-scrape-examples=examples | ||
``` | ||
|
||
To enable this feature on [docs.rs](https://docs.rs), add this to your Cargo.toml: | ||
|
||
```toml | ||
[package.metadata.docs.rs] | ||
cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples=examples"] | ||
``` | ||
|
||
|
||
## How it works | ||
|
||
When you run `cargo doc`, Rustdoc will analyze all the crates that match Cargo's `--examples` filter for instances of items being documented. Then Rustdoc will include the source code of these instances in the generated documentation. | ||
|
||
Rustdoc has a few techniques to ensure these examples don't overwhelm documentation readers, and that it doesn't blow up the page size: | ||
|
||
1. For a given item, a maximum of 5 examples are included in the page. The remaining examples are just links to source code. | ||
2. Only one example is shown by default, and the remaining examples are hidden behind a toggle. | ||
3. For a given file that contains examples, only the item containing the examples will be included in the generated documentation. | ||
|
||
For a given item, Rustdoc sorts its examples based on the size of the example — smaller ones are shown first. | ||
|
||
|
||
## FAQ | ||
|
||
### My example is not showing up in the documentation | ||
|
||
This feature uses Cargo's convention for finding examples. You should ensure that `cargo check --examples` includes your example file. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,8 +76,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>); | ||
|
@@ -461,6 +463,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\">\ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
<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<'_>, | ||
|
@@ -2671,7 +2701,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 | ||
); | ||
|
||
|
@@ -2723,9 +2755,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); | ||
|
||
|
@@ -2841,6 +2874,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>\ | ||
GuillaumeGomez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<div class=\"more-scraped-examples\">\ | ||
<div class=\"toggle-line\"><div class=\"toggle-line-inner\"></div></div>\ | ||
<div class=\"more-scraped-examples-inner\">" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.