Skip to content

Commit

Permalink
Use width instead of byte length, small normalize comment cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Aug 30, 2023
1 parent aa8a607 commit c8f4514
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def function(a:int=42):
a
b
"""
#  There's a NBSP + 3 spaces before
   There's a NBSP + 3 spaces before
# And 4 spaces on the next line
pass
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,3 @@

i1 = ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",) # This should break

# As of adding this fixture Black adds a space before the non-breaking space if part of a type pragma.
# https://github.com/psf/black/blob/b4dca26c7d93f930bbd5a7b552807370b60d4298/src/black/comments.py#L122-L129
i2 = ("",) #  type: Add space before leading NBSP followed by spaces
i3 = ("",) #type: A space is added
i4 = ("",) #  type: Add space before leading NBSP followed by a space
i5 = ("",) # type: Add space before leading NBSP
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# As of adding this fixture Black adds a space before the non-breaking space if part of a type pragma.
# https://github.com/psf/black/blob/b4dca26c7d93f930bbd5a7b552807370b60d4298/src/black/comments.py#L122-L129
i2 = "" #  type: Add space before leading NBSP followed by spaces
i3 = "" #type: A space is added
i4 = "" #  type: Add space before leading NBSP followed by a space
i5 = "" # type: Add space before leading NBSP
53 changes: 31 additions & 22 deletions crates/ruff_python_formatter/src/comments/format.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::borrow::Cow;
use unicode_width::UnicodeWidthChar;

use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_text_size::{Ranged, TextLen, TextRange};

use ruff_formatter::{format_args, write, FormatError, SourceCode};
use ruff_formatter::{format_args, write, FormatError, FormatOptions, SourceCode};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_trivia::{lines_after, lines_after_ignoring_trivia, lines_before};

Expand Down Expand Up @@ -294,7 +295,7 @@ impl Format<PyFormatContext<'_>> for FormatComment<'_> {

let normalized_comment = normalize_comment(self.comment, source)?;

format_normalized_comment(normalized_comment, slice.start()).fmt(f)
format_normalized_comment(normalized_comment, slice.range()).fmt(f)
}
}

Expand Down Expand Up @@ -355,7 +356,16 @@ impl Format<PyFormatContext<'_>> for FormatTrailingEndOfLineComment<'_> {
let source = SourceCode::new(f.context().source());

let normalized_comment = normalize_comment(self.comment, source)?;
let reserved_width = normalized_comment.text_len().to_u32() + 2; // Account for two added spaces

// Start with 2 because of the two leading spaces.
let mut reserved_width = 2;

for c in normalized_comment.chars() {
reserved_width += match c {
'\t' => f.options().tab_width().value(),
c => c.width().unwrap_or(0) as u32,
}
}

write!(
f,
Expand All @@ -364,7 +374,7 @@ impl Format<PyFormatContext<'_>> for FormatTrailingEndOfLineComment<'_> {
&format_args![
space(),
space(),
format_normalized_comment(normalized_comment, slice.start())
format_normalized_comment(normalized_comment, slice.range())
],
reserved_width
),
Expand All @@ -383,29 +393,34 @@ impl Format<PyFormatContext<'_>> for FormatTrailingEndOfLineComment<'_> {
/// a dynamic text element at the original slice's start position.
pub(crate) const fn format_normalized_comment(
comment: Cow<'_, str>,
start_position: TextSize,
range: TextRange,
) -> FormatNormalizedComment<'_> {
FormatNormalizedComment {
comment,
start_position,
}
FormatNormalizedComment { comment, range }
}

pub(crate) struct FormatNormalizedComment<'a> {
comment: Cow<'a, str>,
start_position: TextSize,
range: TextRange,
}

impl Format<PyFormatContext<'_>> for FormatNormalizedComment<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext>) -> FormatResult<()> {
match self.comment {
Cow::Borrowed(borrowed) => source_text_slice(
TextRange::at(self.start_position, borrowed.text_len()),
TextRange::at(self.range.start(), borrowed.text_len()),
ContainsNewlines::No,
)
.fmt(f),

Cow::Owned(ref owned) => dynamic_text(owned, Some(self.start_position)).fmt(f),
Cow::Owned(ref owned) => {
write!(
f,
[
dynamic_text(owned, Some(self.range.start())),
source_position(self.range.end())
]
)
}
}
}
}
Expand All @@ -424,7 +439,6 @@ fn normalize_comment<'a>(
let comment_text = slice.text(source);

let trimmed = comment_text.trim_end();
let trailing_whitespace_len = comment_text.text_len() - trimmed.text_len();

let Some(content) = trimmed.strip_prefix('#') else {
return Err(FormatError::syntax_error(
Expand All @@ -439,8 +453,8 @@ fn normalize_comment<'a>(
// Fast path for correctly formatted comments:
// * Start with a `# '.
// * Have no trailing whitespace.
if trailing_whitespace_len == TextSize::new(0) && content.starts_with(' ') {
return Ok(Cow::Borrowed(comment_text));
if content.starts_with([' ', '!', ':', '#', '\'']) {
return Ok(Cow::Borrowed(trimmed));
}

if content.starts_with('\u{A0}') {
Expand All @@ -457,10 +471,5 @@ fn normalize_comment<'a>(
}
}

// We normalize the leading space if we can.
if !content.starts_with([' ', '!', ':', '#', '\'']) {
return Ok(Cow::Owned(std::format!("# {}", content.trim_start())));
}

Ok(Cow::Owned(std::format!("#{content}")))
Ok(Cow::Owned(std::format!("# {}", content.trim_start())))
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ h3 = 1, "qweiurpoiqwurepqiurpqirpuqoiwrupqoirupqoirupqoiurpqiorupwqiourpqurpqurp
i1 = ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",) # This should break
# As of adding this fixture Black adds a space before the non-breaking space if part of a type pragma.
# https://github.com/psf/black/blob/b4dca26c7d93f930bbd5a7b552807370b60d4298/src/black/comments.py#L122-L129
i2 = ("",) #  type: Add space before leading NBSP followed by spaces
i3 = ("",) #type: A space is added
i4 = ("",) #  type: Add space before leading NBSP followed by a space
i5 = ("",) # type: Add space before leading NBSP
```

## Output
Expand Down Expand Up @@ -288,13 +282,6 @@ h3 = (
i1 = (
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
) # This should break
# As of adding this fixture Black adds a space before the non-breaking space if part of a type pragma.
# https://github.com/psf/black/blob/b4dca26c7d93f930bbd5a7b552807370b60d4298/src/black/comments.py#L122-L129
i2 = ("",) #   type: Add space before leading NBSP followed by spaces
i3 = ("",) # type: A space is added
i4 = ("",) #   type: Add space before leading NBSP followed by a space
i5 = ("",) #  type: Add space before leading NBSP
```


Expand Down

0 comments on commit c8f4514

Please sign in to comment.