Skip to content
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

[rustdoc] Add no-hidden-lines codeblock attribute #118711

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions src/doc/rustdoc/src/write-documentation/what-to-include.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,34 @@ The example is the same on the doc page, but has that extra information
available to anyone trying to use your crate. More about tests in the
upcoming [Documentation tests] chapter.

In case you're writing documentation for a macro, it might be useful to use
the `no-hidden-lines` attribute in order to prevent some `#` characters to be
stripped:

``````rust
//! ```rust,no-hidden-lines
//!
//! test!(
//! # a,
//! ##b,
//! ###c
//! );
//! ```
macro_rules! test {
(# a,##b,###c) => {}
}
``````

Without using the `no-hidden-lines` attribute, the generated code example would
look like this:

```text
test!(
#b,
##c
);
```

## What to Exclude

Certain parts of your public interface may be included by default in the output
Expand Down
14 changes: 13 additions & 1 deletion src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,13 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
};

let added_classes = parse_result.added_classes;
let lines = original_text.lines().filter_map(|l| map_line(l).for_html());
let lines = original_text.lines().filter_map(|l| {
if parse_result.no_hidden_lines {
Some(Cow::Borrowed(l))
} else {
map_line(l).for_html()
}
});
let text = lines.intersperse("\n".into()).collect::<String>();

compile_fail = parse_result.compile_fail;
Expand Down Expand Up @@ -879,6 +885,7 @@ pub(crate) struct LangString {
pub(crate) edition: Option<Edition>,
pub(crate) added_classes: Vec<String>,
pub(crate) unknown: Vec<String>,
pub(crate) no_hidden_lines: bool,
}

#[derive(Eq, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -1213,6 +1220,7 @@ impl Default for LangString {
edition: None,
added_classes: Vec::new(),
unknown: Vec::new(),
no_hidden_lines: false,
}
}
}
Expand Down Expand Up @@ -1274,6 +1282,10 @@ impl LangString {
data.rust = true;
seen_rust_tags = true;
}
LangStringToken::LangToken("no_hidden_lines") => {
data.no_hidden_lines = true;
seen_rust_tags = !seen_other_tags;
}
LangStringToken::LangToken("custom") => {
if custom_code_classes_in_docs {
seen_custom_tag = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<code><span class="macro">macro_rules! </span>test {
(# a,##b,###c) =&gt; {}
}

<span class="macro">test!</span>(
#b,
##c
);</code>
9 changes: 9 additions & 0 deletions tests/rustdoc/no-hidden-lines-codeblock-format.codeblock.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<code><span class="macro">macro_rules! </span>test {
(# a,##b,###c) =&gt; {}
}

<span class="macro">test!</span>(
# a,
##b,
###c
);</code>
54 changes: 54 additions & 0 deletions tests/rustdoc/no-hidden-lines-codeblock-format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Test that the `no_hidden_lines` codeblock attribute prevents lines starting with `#` to
// be stripped.

#![crate_name = "foo"]

// @has 'foo/index.html'
// @matches - '//*[@class="rust rust-example-rendered"]/code' '\(\s+# a,\s+##b,\s+###c\s+\);'
// @snapshot 'codeblock' - '//*[@class="rust rust-example-rendered"]/code'

//! ```rust,no_hidden_lines
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this isn't gated behind a feature flag. Is that intentional?

(I'm fine either way, but if it's going to be insta-stable then it's going to need to go through an FCP.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's what I want indeed but in any case I'd like us to go through FCP so every one can check it.

//! macro_rules! test {
//! (# a,##b,###c) => {}
//! }
//!
//! test!(
//! # a,
//! ##b,
//! ###c
//! );
//! ```

// @has 'foo/fn.foo.html'
// @matches - '//*[@class="rust rust-example-rendered"]/code' '\(\s+#b,\s+##c\s+\);'
// @snapshot 'codeblock-non-raw' - '//*[@class="rust rust-example-rendered"]/code'

/// ```
/// macro_rules! test {
/// (# a,##b,###c) => {}
/// }
///
/// test!(
/// # a,
/// ##b,
/// ###c
/// );
/// ```
pub fn foo() {}

// Testing that `raw` means that it is a rust code block by default.
// @has 'foo/fn.bar.html'
// @matches - '//*[@class="rust rust-example-rendered"]/code' '\(\s+#a,\s+##b,\s+###c\s+\);'

/// ```no_hidden_lines
/// macro_rules! test {
/// (#a,##b,###c) => {}
/// }
///
/// test!(
/// #a,
/// ##b,
/// ###c
/// );
/// ```
pub fn bar() {}
Loading