Refactor and rename skip_trailing_trivia (#6312)

Based on feedback here:
https://github.com/astral-sh/ruff/pull/6274#discussion_r1282747964.
This commit is contained in:
Charlie Marsh 2023-08-04 09:30:53 -04:00 committed by GitHub
parent 38a96c88c1
commit 1e3fe67ca5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 42 deletions

View file

@ -1,6 +1,6 @@
use ruff_formatter::{write, Buffer};
use ruff_python_ast::{Ranged, StmtClassDef};
use ruff_python_trivia::{lines_after, skip_trailing_trivia};
use ruff_python_trivia::lines_after_ignoring_trivia;
use crate::comments::{leading_comments, trailing_comments};
use crate::prelude::*;
@ -41,14 +41,13 @@ impl FormatNodeRule<StmtClassDef> for FormatStmtClassDef {
// Write any leading definition comments (between last decorator and the header)
// while maintaining the right amount of empty lines between the comment
// and the last decorator.
let decorator_end =
skip_trailing_trivia(last_decorator.end(), f.context().source());
let leading_line = if lines_after(decorator_end, f.context().source()) <= 1 {
hard_line_break()
} else {
empty_line()
};
let leading_line =
if lines_after_ignoring_trivia(last_decorator.end(), f.context().source()) <= 1
{
hard_line_break()
} else {
empty_line()
};
write!(
f,

View file

@ -2,7 +2,7 @@ use ruff_python_ast::{Ranged, StmtFunctionDef};
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
use ruff_python_ast::function::AnyFunctionDefinition;
use ruff_python_trivia::{lines_after, skip_trailing_trivia};
use ruff_python_trivia::lines_after_ignoring_trivia;
use crate::comments::{leading_comments, trailing_comments};
@ -54,14 +54,13 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
// Write any leading definition comments (between last decorator and the header)
// while maintaining the right amount of empty lines between the comment
// and the last decorator.
let decorator_end =
skip_trailing_trivia(last_decorator.end(), f.context().source());
let leading_line = if lines_after(decorator_end, f.context().source()) <= 1 {
hard_line_break()
} else {
empty_line()
};
let leading_line =
if lines_after_ignoring_trivia(last_decorator.end(), f.context().source()) <= 1
{
hard_line_break()
} else {
empty_line()
};
write!(
f,

View file

@ -1,7 +1,7 @@
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
use ruff_python_ast::helpers::is_compound_statement;
use ruff_python_ast::{self as ast, Constant, Expr, Ranged, Stmt, Suite};
use ruff_python_trivia::{lines_after, lines_before, skip_trailing_trivia};
use ruff_python_trivia::{lines_after_ignoring_trivia, lines_before};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::prelude::*;
@ -180,8 +180,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
// it then counts the lines between the statement and the trailing comment, which is
// always 0. This is why it skips any trailing trivia (trivia that's on the same line)
// and counts the lines after.
let after_trailing_trivia = skip_trailing_trivia(offset, source);
lines_after(after_trailing_trivia, source)
lines_after_ignoring_trivia(offset, source)
};
match node_level {