mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-07 04:38:46 +00:00
Rollup merge of #86523 - LeSeulArtichaut:macros-disambiguators, r=jyn514
Improvements to intra-doc link macro disambiguators A few small improvements around macro disambiguators: - display the link text as it was entered: previously `[macro!()]` would be displayed without the parantheses (fixes #86309) - support `!{}` and `![]` as macro disambiguators (fixes #86310) r? `@jyn514` cc `@Manishearth` `@camelid`
This commit is contained in:
commit
f9ebf1edb5
@ -992,9 +992,9 @@ fn preprocess_link<'a>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse and strip the disambiguator from the link, if present.
|
// Parse and strip the disambiguator from the link, if present.
|
||||||
let (path_str, disambiguator) = match Disambiguator::from_str(&link) {
|
let (disambiguator, path_str, link_text) = match Disambiguator::from_str(&link) {
|
||||||
Ok(Some((d, path))) => (path.trim(), Some(d)),
|
Ok(Some((d, path, link_text))) => (Some(d), path.trim(), link_text.trim()),
|
||||||
Ok(None) => (link.trim(), None),
|
Ok(None) => (None, link.trim(), link.trim()),
|
||||||
Err((err_msg, relative_range)) => {
|
Err((err_msg, relative_range)) => {
|
||||||
// Only report error if we would not have ignored this link. See issue #83859.
|
// Only report error if we would not have ignored this link. See issue #83859.
|
||||||
if !should_ignore_link_with_disambiguators(link) {
|
if !should_ignore_link_with_disambiguators(link) {
|
||||||
@ -1012,11 +1012,6 @@ fn preprocess_link<'a>(
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We stripped `()` and `!` when parsing the disambiguator.
|
|
||||||
// Add them back to be displayed, but not prefix disambiguators.
|
|
||||||
let link_text =
|
|
||||||
disambiguator.map(|d| d.display_for(path_str)).unwrap_or_else(|| path_str.to_owned());
|
|
||||||
|
|
||||||
// Strip generics from the path.
|
// Strip generics from the path.
|
||||||
let path_str = if path_str.contains(['<', '>'].as_slice()) {
|
let path_str = if path_str.contains(['<', '>'].as_slice()) {
|
||||||
match strip_generics_from_path(&path_str) {
|
match strip_generics_from_path(&path_str) {
|
||||||
@ -1046,7 +1041,7 @@ fn preprocess_link<'a>(
|
|||||||
path_str,
|
path_str,
|
||||||
disambiguator,
|
disambiguator,
|
||||||
extra_fragment: extra_fragment.map(String::from),
|
extra_fragment: extra_fragment.map(String::from),
|
||||||
link_text,
|
link_text: link_text.to_owned(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1554,24 +1549,12 @@ enum Disambiguator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Disambiguator {
|
impl Disambiguator {
|
||||||
/// The text that should be displayed when the path is rendered as HTML.
|
/// Given a link, parse and return `(disambiguator, path_str, link_text)`.
|
||||||
///
|
|
||||||
/// NOTE: `path` is not the original link given by the user, but a name suitable for passing to `resolve`.
|
|
||||||
fn display_for(&self, path: &str) -> String {
|
|
||||||
match self {
|
|
||||||
// FIXME: this will have different output if the user had `m!()` originally.
|
|
||||||
Self::Kind(DefKind::Macro(MacroKind::Bang)) => format!("{}!", path),
|
|
||||||
Self::Kind(DefKind::Fn) => format!("{}()", path),
|
|
||||||
_ => path.to_owned(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Given a link, parse and return `(disambiguator, path_str)`.
|
|
||||||
///
|
///
|
||||||
/// This returns `Ok(Some(...))` if a disambiguator was found,
|
/// This returns `Ok(Some(...))` if a disambiguator was found,
|
||||||
/// `Ok(None)` if no disambiguator was found, or `Err(...)`
|
/// `Ok(None)` if no disambiguator was found, or `Err(...)`
|
||||||
/// if there was a problem with the disambiguator.
|
/// if there was a problem with the disambiguator.
|
||||||
fn from_str(link: &str) -> Result<Option<(Self, &str)>, (String, Range<usize>)> {
|
fn from_str(link: &str) -> Result<Option<(Self, &str, &str)>, (String, Range<usize>)> {
|
||||||
use Disambiguator::{Kind, Namespace as NS, Primitive};
|
use Disambiguator::{Kind, Namespace as NS, Primitive};
|
||||||
|
|
||||||
if let Some(idx) = link.find('@') {
|
if let Some(idx) = link.find('@') {
|
||||||
@ -1592,18 +1575,20 @@ impl Disambiguator {
|
|||||||
"prim" | "primitive" => Primitive,
|
"prim" | "primitive" => Primitive,
|
||||||
_ => return Err((format!("unknown disambiguator `{}`", prefix), 0..idx)),
|
_ => return Err((format!("unknown disambiguator `{}`", prefix), 0..idx)),
|
||||||
};
|
};
|
||||||
Ok(Some((d, &rest[1..])))
|
Ok(Some((d, &rest[1..], &rest[1..])))
|
||||||
} else {
|
} else {
|
||||||
let suffixes = [
|
let suffixes = [
|
||||||
("!()", DefKind::Macro(MacroKind::Bang)),
|
("!()", DefKind::Macro(MacroKind::Bang)),
|
||||||
|
("!{}", DefKind::Macro(MacroKind::Bang)),
|
||||||
|
("![]", DefKind::Macro(MacroKind::Bang)),
|
||||||
("()", DefKind::Fn),
|
("()", DefKind::Fn),
|
||||||
("!", DefKind::Macro(MacroKind::Bang)),
|
("!", DefKind::Macro(MacroKind::Bang)),
|
||||||
];
|
];
|
||||||
for (suffix, kind) in suffixes {
|
for (suffix, kind) in suffixes {
|
||||||
if let Some(link) = link.strip_suffix(suffix) {
|
if let Some(path_str) = link.strip_suffix(suffix) {
|
||||||
// Avoid turning `!` or `()` into an empty string
|
// Avoid turning `!` or `()` into an empty string
|
||||||
if !link.is_empty() {
|
if !path_str.is_empty() {
|
||||||
return Ok(Some((Kind(kind), link)));
|
return Ok(Some((Kind(kind), path_str, link)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
25
src/test/rustdoc/intra-doc/macros-disambiguators.rs
Normal file
25
src/test/rustdoc/intra-doc/macros-disambiguators.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#![crate_name = "foo"]
|
||||||
|
#![deny(rustdoc::broken_intra_doc_links)]
|
||||||
|
|
||||||
|
//! [foo!()]
|
||||||
|
// @has foo/index.html '//a[@href="macro.foo.html"]' 'foo!()'
|
||||||
|
|
||||||
|
//! [foo!{}]
|
||||||
|
// @has - '//a[@href="macro.foo.html"]' 'foo!{}'
|
||||||
|
|
||||||
|
//! [foo![]](foo![])
|
||||||
|
// @has - '//a[@href="macro.foo.html"]' 'foo![]'
|
||||||
|
|
||||||
|
//! [foo1](foo!())
|
||||||
|
// @has - '//a[@href="macro.foo.html"]' 'foo1'
|
||||||
|
|
||||||
|
//! [foo2](foo!{})
|
||||||
|
// @has - '//a[@href="macro.foo.html"]' 'foo2'
|
||||||
|
|
||||||
|
//! [foo3](foo![])
|
||||||
|
// @has - '//a[@href="macro.foo.html"]' 'foo3'
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! foo {
|
||||||
|
() => {};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user