Match Black's formatting of trailing comments containing NBSP (#7030)

This commit is contained in:
Chris Pryer 2023-09-01 08:52:59 -04:00 committed by GitHub
parent 60132da7bb
commit 0489bbc54c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View file

@ -474,18 +474,19 @@ fn normalize_comment<'a>(
if content.starts_with('\u{A0}') {
let trimmed = content.trim_start_matches('\u{A0}');
// Black adds a space before the non-breaking space if part of a type pragma.
if trimmed.trim_start().starts_with("type:") {
return Ok(Cow::Owned(std::format!("# \u{A0}{trimmed}")));
}
// Black replaces the non-breaking space with a space if followed by a space.
if trimmed.starts_with(' ') {
return Ok(Cow::Owned(std::format!("# {trimmed}")));
// Black adds a space before the non-breaking space if part of a type pragma.
Ok(Cow::Owned(std::format!("# {content}")))
} else if trimmed.starts_with(' ') {
// Black replaces the non-breaking space with a space if followed by a space.
Ok(Cow::Owned(std::format!("# {trimmed}")))
} else {
// Otherwise we replace the first non-breaking space with a regular space.
Ok(Cow::Owned(std::format!("# {}", &content["\u{A0}".len()..])))
}
} else {
Ok(Cow::Owned(std::format!("# {}", content.trim_start())))
}
Ok(Cow::Owned(std::format!("# {}", content.trim_start())))
}
/// A helper for stripping '#' from comments.