mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 04:55:09 +00:00
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:
parent
38a96c88c1
commit
1e3fe67ca5
5 changed files with 32 additions and 42 deletions
|
@ -3,7 +3,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize};
|
|||
|
||||
use ruff_formatter::{format_args, write, FormatError, SourceCode};
|
||||
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||
use ruff_python_trivia::{lines_after, lines_before, skip_trailing_trivia};
|
||||
use ruff_python_trivia::{lines_after, lines_after_ignoring_trivia, lines_before};
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::context::NodeLevel;
|
||||
|
@ -90,10 +90,9 @@ impl Format<PyFormatContext<'_>> for FormatLeadingAlternateBranchComments<'_> {
|
|||
|
||||
write!(f, [leading_comments(self.comments)])?;
|
||||
} else if let Some(last_preceding) = self.last_node {
|
||||
let full_end = skip_trailing_trivia(last_preceding.end(), f.context().source());
|
||||
// The leading comments formatting ensures that it preserves the right amount of lines after
|
||||
// We need to take care of this ourselves, if there's no leading `else` comment.
|
||||
if lines_after(full_end, f.context().source()) > 1 {
|
||||
if lines_after_ignoring_trivia(last_preceding.end(), f.context().source()) > 1 {
|
||||
write!(f, [empty_line()])?;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue