Skip to content

Commit

Permalink
deduplicate code.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoel-bagard committed Mar 22, 2024
1 parent 559b030 commit a2cea4a
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,31 +440,25 @@ impl<'a> Iterator for LinePreprocessor<'a> {
}
// At the start of the line...
else {
// Check if we are at the beginning of a cell in a notebook.
if let Some(ref mut cell_offsets) = self.cell_offsets {
if cell_offsets
.peek()
.is_some_and(|offset| offset == &&self.line_start)
{
self.is_beginning_of_cell = true;
cell_offsets.next();
blank_lines = BlankLines::Zero;
self.max_preceding_blank_lines = BlankLines::Zero;
}
}

// An empty line
if token_kind == TokenKind::NonLogicalNewline {
blank_lines.add(*range);

self.line_start = range.end();

// Check if we are at the beginning of a cell in a notebook.
if let Some(ref mut cell_offsets) = self.cell_offsets {
if cell_offsets
.peek()
.is_some_and(|offset| offset == &&self.line_start)
{
self.is_beginning_of_cell = true;
cell_offsets.next();
blank_lines = BlankLines::Many {
count: NonZeroU32::new(1).unwrap(),
range: *range,
};
self.max_preceding_blank_lines = BlankLines::Many {
count: NonZeroU32::new(1).unwrap(),
range: *range,
};
}
}

continue;
}

Expand Down Expand Up @@ -538,18 +532,8 @@ impl<'a> Iterator for LinePreprocessor<'a> {
// Set the start for the next logical line.
self.line_start = range.end();

// Check if we are at the beginning of a cell in a notebook.
if let Some(ref mut cell_offsets) = self.cell_offsets {
if cell_offsets
.peek()
.is_some_and(|offset| offset == &&self.line_start)
{
self.is_beginning_of_cell = true;
cell_offsets.next();
self.max_preceding_blank_lines = BlankLines::Zero;
} else if !line_is_comment_only {
self.is_beginning_of_cell = false;
}
if self.cell_offsets.is_some() && !line_is_comment_only {
self.is_beginning_of_cell = false;
}

return Some(logical_line);
Expand Down

0 comments on commit a2cea4a

Please sign in to comment.