mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 04:55:09 +00:00
Use single lookup for leading, dangling, and trailing comments (#6589)
This commit is contained in:
parent
81b1176f99
commit
29c0b9f91c
39 changed files with 319 additions and 387 deletions
|
@ -2,7 +2,7 @@ use ruff_formatter::{write, Buffer};
|
|||
use ruff_python_ast::{Ranged, StmtClassDef};
|
||||
use ruff_python_trivia::lines_after_ignoring_trivia;
|
||||
|
||||
use crate::comments::{leading_comments, trailing_comments};
|
||||
use crate::comments::{leading_comments, trailing_comments, SourceComment};
|
||||
use crate::prelude::*;
|
||||
use crate::statement::suite::SuiteKind;
|
||||
use crate::FormatNodeRule;
|
||||
|
@ -129,7 +129,7 @@ impl FormatNodeRule<StmtClassDef> for FormatStmtClassDef {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtClassDef,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// handled in fmt_fields
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
|
||||
use crate::comments::dangling_node_comments;
|
||||
use crate::comments::{dangling_node_comments, SourceComment};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
|
@ -53,7 +53,11 @@ impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
|
|||
}
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtDelete, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
||||
use ruff_python_ast::{Expr, Ranged, Stmt, StmtFor};
|
||||
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
|
||||
use crate::expression::expr_tuple::TupleParentheses;
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
|
@ -86,7 +86,11 @@ impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtFor, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use ruff_formatter::write;
|
|||
use ruff_python_ast::{Parameters, Ranged, StmtFunctionDef};
|
||||
use ruff_python_trivia::{lines_after_ignoring_trivia, SimpleTokenKind, SimpleTokenizer};
|
||||
|
||||
use crate::comments::{leading_comments, trailing_comments};
|
||||
use crate::comments::{leading_comments, trailing_comments, SourceComment};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::{Parentheses, Parenthesize};
|
||||
use crate::prelude::*;
|
||||
|
@ -147,7 +147,7 @@ impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtFunctionDef,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
|
@ -43,7 +43,11 @@ impl FormatNodeRule<StmtIf> for FormatStmtIf {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtIf, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled by `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ use ruff_python_ast::node::AstNode;
|
|||
use ruff_python_ast::{Ranged, StmtImportFrom};
|
||||
|
||||
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions, TrailingComma};
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::parentheses::parenthesized;
|
||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||
|
||||
|
@ -71,7 +72,7 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtImportFrom,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
||||
use ruff_python_ast::StmtMatch;
|
||||
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
|
||||
use crate::context::{NodeLevel, WithNodeLevel};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
|
@ -64,7 +64,11 @@ impl FormatNodeRule<StmtMatch> for FormatStmtMatch {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtMatch, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled as part of `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -93,7 +93,11 @@ impl FormatNodeRule<StmtTry> for FormatStmtTry {
|
|||
write!(f, [comments::dangling_comments(dangling_comments)])
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtTry, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// dangling comments are formatted as part of AnyStatementTry::fmt
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
|
@ -62,7 +62,11 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtWhile, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use ruff_python_ast::{Ranged, StmtWith};
|
|||
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
use crate::comments::trailing_comments;
|
||||
use crate::comments::{trailing_comments, SourceComment};
|
||||
use crate::expression::parentheses::{
|
||||
in_parentheses_only_soft_line_break_or_space, optional_parentheses, parenthesized,
|
||||
};
|
||||
|
@ -88,7 +88,11 @@ impl FormatNodeRule<StmtWith> for FormatStmtWith {
|
|||
)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtWith, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -117,8 +117,10 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
|||
SuiteKind::TopLevel => SuiteChildStatement::Other(first),
|
||||
};
|
||||
|
||||
let (mut preceding, mut after_class_docstring) = if comments
|
||||
.leading_comments(first)
|
||||
let first_comments = comments.leading_dangling_trailing_comments(first);
|
||||
|
||||
let (mut preceding, mut after_class_docstring) = if first_comments
|
||||
.leading
|
||||
.iter()
|
||||
.any(|comment| comment.is_suppression_off_comment(source))
|
||||
{
|
||||
|
@ -128,8 +130,8 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
|||
)?,
|
||||
false,
|
||||
)
|
||||
} else if comments
|
||||
.trailing_comments(first)
|
||||
} else if first_comments
|
||||
.trailing
|
||||
.iter()
|
||||
.any(|comment| comment.is_suppression_off_comment(source))
|
||||
{
|
||||
|
@ -291,8 +293,10 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
|||
}
|
||||
}
|
||||
|
||||
if comments
|
||||
.leading_comments(following)
|
||||
let following_comments = comments.leading_dangling_trailing_comments(following);
|
||||
|
||||
if following_comments
|
||||
.leading
|
||||
.iter()
|
||||
.any(|comment| comment.is_suppression_off_comment(source))
|
||||
{
|
||||
|
@ -301,8 +305,8 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
|||
&mut iter,
|
||||
f,
|
||||
)?;
|
||||
} else if comments
|
||||
.trailing_comments(following)
|
||||
} else if following_comments
|
||||
.trailing
|
||||
.iter()
|
||||
.any(|comment| comment.is_suppression_off_comment(source))
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue