rewrap long lines

This commit is contained in:
Brent Westbrook 2025-12-10 14:44:14 -05:00
parent 94e0418006
commit b00bbf43ee
No known key found for this signature in database
12 changed files with 60 additions and 43 deletions

View File

@ -2,7 +2,8 @@ use ruff_python_ast::AnyNodeRef;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
/// Used as key into the [`MultiMap`](super::MultiMap) storing the comments per node by [`Comments`](super::Comments). /// Used as key into the [`MultiMap`](super::MultiMap) storing the comments per node by
/// [`Comments`](super::Comments).
/// ///
/// Implements equality and hashing based on the address of the [`AnyNodeRef`] to get fast and cheap /// Implements equality and hashing based on the address of the [`AnyNodeRef`] to get fast and cheap
/// hashing/equality comparison. /// hashing/equality comparison.

View File

@ -1974,8 +1974,8 @@ fn handle_unary_op_comment<'a>(
/// ) /// )
/// ``` /// ```
/// ///
/// The comment will be attached to the [`Arguments`](ast::Arguments) node as a dangling comment, to ensure /// The comment will be attached to the [`Arguments`](ast::Arguments) node as a dangling comment, to
/// that it remains on the same line as open parenthesis. /// ensure that it remains on the same line as open parenthesis.
/// ///
/// Similarly, given: /// Similarly, given:
/// ```python /// ```python
@ -1984,8 +1984,8 @@ fn handle_unary_op_comment<'a>(
/// ] = ... /// ] = ...
/// ``` /// ```
/// ///
/// The comment will be attached to the [`TypeParams`](ast::TypeParams) node as a dangling comment, to ensure /// The comment will be attached to the [`TypeParams`](ast::TypeParams) node as a dangling comment,
/// that it remains on the same line as open bracket. /// to ensure that it remains on the same line as open bracket.
fn handle_bracketed_end_of_line_comment<'a>( fn handle_bracketed_end_of_line_comment<'a>(
comment: DecoratedComment<'a>, comment: DecoratedComment<'a>,
source: &str, source: &str,

View File

@ -174,7 +174,8 @@ impl<'ast> SourceOrderVisitor<'ast> for CommentsVisitor<'ast, '_> {
/// A comment decorated with additional information about its surrounding context in the source document. /// A comment decorated with additional information about its surrounding context in the source document.
/// ///
/// Used by [`place_comment`] to determine if this should become a [leading](self#leading-comments), [dangling](self#dangling-comments), or [trailing](self#trailing-comments) comment. /// Used by [`place_comment`] to determine if this should become a [leading](self#leading-comments),
/// [dangling](self#dangling-comments), or [trailing](self#trailing-comments) comment.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct DecoratedComment<'a> { pub(crate) struct DecoratedComment<'a> {
enclosing: AnyNodeRef<'a>, enclosing: AnyNodeRef<'a>,

View File

@ -1095,9 +1095,9 @@ impl OperandIndex {
} }
} }
/// Returns the index of the operand's right operator. The method always returns an index /// Returns the index of the operand's right operator. The method always returns an index even
/// even if the operand has no right operator. Use [`FlatBinaryExpressionSlice::get_operator`] to test if /// if the operand has no right operator. Use [`FlatBinaryExpressionSlice::get_operator`] to
/// the operand has a right operator. /// test if the operand has a right operator.
fn right_operator(self) -> OperatorIndex { fn right_operator(self) -> OperatorIndex {
OperatorIndex::new(self.0 + 1) OperatorIndex::new(self.0 + 1)
} }

View File

@ -56,18 +56,20 @@ pub(crate) enum Parenthesize {
/// Adding parentheses is desired to prevent the comments from wandering. /// Adding parentheses is desired to prevent the comments from wandering.
IfRequired, IfRequired,
/// Same as [`Self::IfBreaks`] except that it uses [`parenthesize_if_expands`](crate::builders::parenthesize_if_expands) for expressions /// Same as [`Self::IfBreaks`] except that it uses
/// with the layout [`OptionalParentheses::BestFit`] which is used by non-splittable /// [`parenthesize_if_expands`](crate::builders::parenthesize_if_expands) for expressions with
/// expressions like literals, name, and strings. /// the layout [`OptionalParentheses::BestFit`] which is used by non-splittable expressions like
/// literals, name, and strings.
/// ///
/// Use this layout over `IfBreaks` when there's a sequence of `maybe_parenthesize_expression` /// Use this layout over `IfBreaks` when there's a sequence of `maybe_parenthesize_expression`
/// in a single logical-line and you want to break from right-to-left. Use `IfBreaks` for the /// in a single logical-line and you want to break from right-to-left. Use `IfBreaks` for the
/// first expression and `IfBreaksParenthesized` for the rest. /// first expression and `IfBreaksParenthesized` for the rest.
IfBreaksParenthesized, IfBreaksParenthesized,
/// Same as [`Self::IfBreaksParenthesized`] but uses [`parenthesize_if_expands`](crate::builders::parenthesize_if_expands) for nested /// Same as [`Self::IfBreaksParenthesized`] but uses
/// [`maybe_parenthesized_expression`](crate::expression::maybe_parenthesize_expression) calls unlike other layouts that always omit parentheses /// [`parenthesize_if_expands`](crate::builders::parenthesize_if_expands) for nested
/// when outer parentheses are present. /// [`maybe_parenthesized_expression`](crate::expression::maybe_parenthesize_expression) calls
/// unlike other layouts that always omit parentheses when outer parentheses are present.
IfBreaksParenthesizedNested, IfBreaksParenthesizedNested,
} }

View File

@ -214,8 +214,9 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizePattern<'_> {
} }
} }
/// This function is very similar to [`can_omit_optional_parentheses`](crate::expression::can_omit_optional_parentheses) with the only difference that it is for patterns /// This function is very similar to
/// and not expressions. /// [`can_omit_optional_parentheses`](crate::expression::can_omit_optional_parentheses)
/// with the only difference that it is for patterns and not expressions.
/// ///
/// The base idea of the omit optional parentheses layout is to prefer using parentheses of sub-patterns /// The base idea of the omit optional parentheses layout is to prefer using parentheses of sub-patterns
/// when splitting the pattern over introducing new patterns. For example, prefer splitting the sequence pattern in /// when splitting the pattern over introducing new patterns. For example, prefer splitting the sequence pattern in

View File

@ -72,8 +72,9 @@ impl FormatNodeRule<PatternArguments> for FormatPatternArguments {
} }
} }
/// Returns `true` if the pattern (which is the only argument to a [`PatternMatchClass`](ruff_python_ast::PatternMatchClass)) is /// Returns `true` if the pattern (which is the only argument to a
/// parenthesized. Used to avoid falsely assuming that `x` is parenthesized in cases like: /// [`PatternMatchClass`](ruff_python_ast::PatternMatchClass)) is parenthesized.
/// Used to avoid falsely assuming that `x` is parenthesized in cases like:
/// ```python /// ```python
/// case Point2D(x): ... /// case Point2D(x): ...
/// ``` /// ```

View File

@ -23,7 +23,8 @@ use crate::{FormatModuleError, PyFormatOptions, format_module_source};
/// ///
/// The returned formatted range guarantees to cover at least `range` (excluding whitespace), but the range might be larger. /// The returned formatted range guarantees to cover at least `range` (excluding whitespace), but the range might be larger.
/// Some cases in which the returned range is larger than `range` are: /// Some cases in which the returned range is larger than `range` are:
/// * The logical lines in `range` use a indentation different from the configured [`IndentStyle`] and [`IndentWidth`](ruff_formatter::IndentWidth). /// * The logical lines in `range` use a indentation different from the configured [`IndentStyle`]
/// and [`IndentWidth`](ruff_formatter::IndentWidth).
/// * `range` is smaller than a logical lines and the formatter needs to format the entire logical line. /// * `range` is smaller than a logical lines and the formatter needs to format the entire logical line.
/// * `range` falls on a single line body. /// * `range` falls on a single line body.
/// ///
@ -129,16 +130,19 @@ pub fn format_range(
/// b) formatting a sub-expression has fewer split points than formatting the entire expressions. /// b) formatting a sub-expression has fewer split points than formatting the entire expressions.
/// ///
/// ### Possible docstrings /// ### Possible docstrings
/// Strings that are suspected to be docstrings are excluded from the search to format the enclosing suite instead /// Strings that are suspected to be docstrings are excluded from the search to format the enclosing
/// so that the formatter's docstring detection in [`FormatSuite`](crate::statement::suite::FormatSuite) correctly detects and formats the docstrings. /// suite instead so that the formatter's docstring detection in
/// [`FormatSuite`](crate::statement::suite::FormatSuite) correctly detects and formats the
/// docstrings.
/// ///
/// ### Compound statements with a simple statement body /// ### Compound statements with a simple statement body
/// Don't include simple-statement bodies of compound statements `if True: pass` because the formatter /// Don't include simple-statement bodies of compound statements `if True: pass` because the formatter
/// must run `FormatClauseBody` to determine if the body should be collapsed or not. /// must run `FormatClauseBody` to determine if the body should be collapsed or not.
/// ///
/// ### Incorrectly indented code /// ### Incorrectly indented code
/// Code that uses indentations that don't match the configured [`IndentStyle`] and [`IndentWidth`](ruff_formatter::IndentWidth) are excluded from the search, /// Code that uses indentations that don't match the configured [`IndentStyle`] and
/// because formatting such nodes on their own can lead to indentation mismatch with its sibling nodes. /// [`IndentWidth`](ruff_formatter::IndentWidth) are excluded from the search, because formatting
/// such nodes on their own can lead to indentation mismatch with its sibling nodes.
/// ///
/// ## Suppression comments /// ## Suppression comments
/// The search ends when `range` falls into a suppressed range because there's nothing to format. It also avoids that the /// The search ends when `range` falls into a suppressed range because there's nothing to format. It also avoids that the
@ -282,10 +286,12 @@ enum EnclosingNode<'a> {
/// with the body. This ensures that the formatter runs `FormatClauseBody` that determines if the body should be indented. /// with the body. This ensures that the formatter runs `FormatClauseBody` that determines if the body should be indented.
/// ///
/// ## Non-standard indentation /// ## Non-standard indentation
/// Node's that use an indentation that doesn't match the configured [`IndentStyle`] and [`IndentWidth`](ruff_formatter::IndentWidth) are excluded from the search. /// Nodes that use an indentation that doesn't match the configured [`IndentStyle`] and
/// This is because the formatter always uses the configured [`IndentStyle`] and [`IndentWidth`](ruff_formatter::IndentWidth), resulting in the /// [`IndentWidth`](ruff_formatter::IndentWidth) are excluded from the search. This is because the
/// formatted nodes using a different indentation than the unformatted sibling nodes. This would be tolerable /// formatter always uses the configured [`IndentStyle`] and
/// in non whitespace sensitive languages like JavaScript but results in lexical errors in Python. /// [`IndentWidth`](ruff_formatter::IndentWidth), resulting in the formatted nodes using a different
/// indentation than the unformatted sibling nodes. This would be tolerable in non whitespace
/// sensitive languages like JavaScript but results in lexical errors in Python.
/// ///
/// ## Implementation /// ## Implementation
/// It would probably be possible to merge this visitor with [`FindEnclosingNode`] but they are separate because /// It would probably be possible to merge this visitor with [`FindEnclosingNode`] but they are separate because
@ -713,9 +719,11 @@ impl Format<PyFormatContext<'_>> for FormatEnclosingNode<'_> {
} }
} }
/// Computes the level of indentation for `indentation` when using the configured [`IndentStyle`] and [`IndentWidth`](ruff_formatter::IndentWidth). /// Computes the level of indentation for `indentation` when using the configured [`IndentStyle`]
/// and [`IndentWidth`](ruff_formatter::IndentWidth).
/// ///
/// Returns `None` if the indentation doesn't conform to the configured [`IndentStyle`] and [`IndentWidth`](ruff_formatter::IndentWidth). /// Returns `None` if the indentation doesn't conform to the configured [`IndentStyle`] and
/// [`IndentWidth`](ruff_formatter::IndentWidth).
/// ///
/// # Panics /// # Panics
/// If `offset` is outside of `source`. /// If `offset` is outside of `source`.

View File

@ -202,8 +202,8 @@ pub(super) enum FormatStatementsLastExpression<'a> {
/// ] = some_long_value /// ] = some_long_value
/// ``` /// ```
/// ///
/// This layout is preferred over [`Self::RightToLeft`] if the left is unsplittable (single keyword like `return` or a Name) /// This layout is preferred over [`Self::RightToLeft`] if the left is unsplittable (single
/// because it has better performance characteristics. /// keyword like `return` or a Name) because it has better performance characteristics.
LeftToRight { LeftToRight {
/// The right side of an assignment or the value returned in a return statement. /// The right side of an assignment or the value returned in a return statement.
value: &'a Expr, value: &'a Expr,
@ -1083,11 +1083,10 @@ impl Format<PyFormatContext<'_>> for InterpolatedString<'_> {
/// For legibility, we discuss only the case of f-strings below, but the /// For legibility, we discuss only the case of f-strings below, but the
/// same comments apply to t-strings. /// same comments apply to t-strings.
/// ///
/// This is just a wrapper around [`FormatFString`](crate::other::f_string::FormatFString) while considering a special /// This is just a wrapper around [`FormatFString`](crate::other::f_string::FormatFString) while
/// case when the f-string is at an assignment statement's value position. /// considering a special case when the f-string is at an assignment statement's value position.
/// This is necessary to prevent an instability where an f-string contains a /// This is necessary to prevent an instability where an f-string contains a multiline expression
/// multiline expression and the f-string fits on the line, but only when it's /// and the f-string fits on the line, but only when it's surrounded by parentheses.
/// surrounded by parentheses.
/// ///
/// ```python /// ```python
/// aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{ /// aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{

View File

@ -177,8 +177,10 @@ enum WithItemsLayout<'a> {
/// ... /// ...
/// ``` /// ```
/// ///
/// In this case, use [`maybe_parenthesize_expression`](crate::expression::maybe_parenthesize_expression) to format the context expression /// In this case, use
/// to get the exact same formatting as when formatting an expression in any other clause header. /// [`maybe_parenthesize_expression`](crate::expression::maybe_parenthesize_expression) to
/// format the context expression to get the exact same formatting as when formatting an
/// expression in any other clause header.
/// ///
/// Only used for Python 3.9+ /// Only used for Python 3.9+
/// ///

View File

@ -38,8 +38,9 @@ impl<'a, 'src> StringNormalizer<'a, 'src> {
/// it can't because the string contains the preferred quotes OR /// it can't because the string contains the preferred quotes OR
/// it leads to more escaping. /// it leads to more escaping.
/// ///
/// Note: If you add more cases here where we return `QuoteStyle::Preserve`, /// Note: If you add more cases here where we return `QuoteStyle::Preserve`, make sure to also
/// make sure to also add them to [`FormatImplicitConcatenatedStringFlat::new`](crate::string::implicit::FormatImplicitConcatenatedStringFlat::new). /// add them to
/// [`FormatImplicitConcatenatedStringFlat::new`](crate::string::implicit::FormatImplicitConcatenatedStringFlat::new).
pub(super) fn preferred_quote_style(&self, string: StringLikePart) -> QuoteStyle { pub(super) fn preferred_quote_style(&self, string: StringLikePart) -> QuoteStyle {
let preferred_quote_style = self let preferred_quote_style = self
.preferred_quote_style .preferred_quote_style

View File

@ -679,8 +679,9 @@ impl Indentation {
/// Returns `true` for a space or tab character. /// Returns `true` for a space or tab character.
/// ///
/// This is different than [`is_python_whitespace`](ruff_python_trivia::is_python_whitespace) in that it returns `false` for a form feed character. /// This is different than [`is_python_whitespace`](ruff_python_trivia::is_python_whitespace) in
/// Form feed characters are excluded because they should be preserved in the suppressed output. /// that it returns `false` for a form feed character. Form feed characters are excluded because
/// they should be preserved in the suppressed output.
const fn is_indent_whitespace(c: char) -> bool { const fn is_indent_whitespace(c: char) -> bool {
matches!(c, ' ' | '\t') matches!(c, ' ' | '\t')
} }