Skip to content

Commit

Permalink
Match Black's formatting of trailing comments containing NBSP (#7030)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Sep 1, 2023
1 parent 60132da commit 0489bbc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 10 additions & 9 deletions crates/ruff_python_formatter/src/comments/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ i = "" #   type: Add space before leading NBSP followed by spaces
i = "" # type: A space is added
i = "" #   type: Add space before leading NBSP followed by a space
i = "" #  type: Add space before leading NBSP
i = "" #  type: Add space before two leading NBSP
i = "" #   type: Add space before two leading NBSP
# A noqa as `#\u{A0}\u{A0}noqa` becomes `# \u{A0}noqa`
i = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # noqa
i = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" #  noqa
```


Expand Down

0 comments on commit 0489bbc

Please sign in to comment.