Rollup merge of #131370 - notriddle:notriddle/screaming-camel-case, r=GuillaumeGomez

rustdoc: improve `<wbr>`-insertion for SCREAMING_CAMEL_CASE
This commit is contained in:
Stuart Cook 2024-10-08 13:19:45 +11:00 committed by GitHub
commit ad96cf3685
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -108,7 +108,17 @@ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|| pk.map_or(true, |(_, t)| t.chars().any(|c| c.is_uppercase())); || pk.map_or(true, |(_, t)| t.chars().any(|c| c.is_uppercase()));
let next_is_underscore = || pk.map_or(true, |(_, t)| t.contains('_')); let next_is_underscore = || pk.map_or(true, |(_, t)| t.contains('_'));
let next_is_colon = || pk.map_or(true, |(_, t)| t.contains(':')); let next_is_colon = || pk.map_or(true, |(_, t)| t.contains(':'));
if i - last > 3 && is_uppercase() && !next_is_uppercase() { // Check for CamelCase.
//
// `i - last > 3` avoids turning FmRadio into Fm<wbr>Radio, which is technically
// correct, but needlessly bloated.
//
// is_uppercase && !next_is_uppercase checks for camelCase. HTTPSProxy,
// for example, should become HTTPS<wbr>Proxy.
//
// !next_is_underscore avoids turning TEST_RUN into TEST<wbr>_<wbr>RUN, which is also
// needlessly bloated.
if i - last > 3 && is_uppercase() && !next_is_uppercase() && !next_is_underscore() {
EscapeBodyText(&text[last..i]).fmt(fmt)?; EscapeBodyText(&text[last..i]).fmt(fmt)?;
fmt.write_str("<wbr>")?; fmt.write_str("<wbr>")?;
last = i; last = i;

View File

@ -24,6 +24,10 @@ fn escape_body_text_with_wbr() {
assert_eq!(&E("first:second").to_string(), "first:<wbr>second"); assert_eq!(&E("first:second").to_string(), "first:<wbr>second");
assert_eq!(&E("first::second").to_string(), "first::<wbr>second"); assert_eq!(&E("first::second").to_string(), "first::<wbr>second");
assert_eq!(&E("MY_CONSTANT").to_string(), "MY_<wbr>CONSTANT"); assert_eq!(&E("MY_CONSTANT").to_string(), "MY_<wbr>CONSTANT");
assert_eq!(
&E("_SIDD_MASKED_NEGATIVE_POLARITY").to_string(),
"_SIDD_<wbr>MASKED_<wbr>NEGATIVE_<wbr>POLARITY"
);
// a string won't get wrapped if it's less than 8 bytes // a string won't get wrapped if it's less than 8 bytes
assert_eq!(&E("HashSet").to_string(), "HashSet"); assert_eq!(&E("HashSet").to_string(), "HashSet");
// an individual word won't get wrapped if it's less than 4 bytes // an individual word won't get wrapped if it's less than 4 bytes