Switch to Rust 2024 edition (#18129)

This commit is contained in:
Micha Reiser 2025-05-16 13:25:28 +02:00 committed by GitHub
parent e67b35743a
commit 9ae698fe30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
1082 changed files with 4211 additions and 3300 deletions

View file

@ -1,4 +1,4 @@
use ruff_formatter::{write, Argument, Arguments};
use ruff_formatter::{Argument, Arguments, write};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::context::{NodeLevel, WithNodeLevel};

View file

@ -3,16 +3,16 @@
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use clap::{command, Parser, ValueEnum};
use clap::{Parser, ValueEnum, command};
use ruff_formatter::SourceCode;
use ruff_python_ast::PySourceType;
use ruff_python_parser::{parse, ParseOptions};
use ruff_python_parser::{ParseOptions, parse};
use ruff_python_trivia::CommentRanges;
use ruff_text_size::Ranged;
use crate::comments::collect_comments;
use crate::{format_module_ast, MagicTrailingComma, PreviewMode, PyFormatOptions};
use crate::{MagicTrailingComma, PreviewMode, PyFormatOptions, format_module_ast};
#[derive(ValueEnum, Clone, Debug)]
pub enum Emit {

View file

@ -1,9 +1,9 @@
use std::borrow::Cow;
use ruff_formatter::{format_args, write, FormatError, FormatOptions, SourceCode};
use ruff_formatter::{FormatError, FormatOptions, SourceCode, format_args, write};
use ruff_python_ast::{AnyNodeRef, NodeKind, PySourceType};
use ruff_python_trivia::{
is_pragma_comment, lines_after, lines_after_ignoring_trivia, lines_before, CommentLinePosition,
CommentLinePosition, is_pragma_comment, lines_after, lines_after_ignoring_trivia, lines_before,
};
use ruff_text_size::{Ranged, TextLen, TextRange};
@ -381,14 +381,13 @@ impl Format<PyFormatContext<'_>> for FormatTrailingEndOfLineComment<'_> {
0
} else {
// Start with 2 because of the two leading spaces.
let width = 2u32.saturating_add(
2u32.saturating_add(
TextWidth::from_text(&normalized_comment, f.options().indent_width())
.width()
.expect("Expected comment not to contain any newlines")
.value(),
);
width
)
};
write!(

View file

@ -514,7 +514,7 @@ mod tests {
use ruff_formatter::SourceCode;
use ruff_python_ast::{Mod, PySourceType};
use ruff_python_parser::{parse, ParseOptions, Parsed};
use ruff_python_parser::{ParseOptions, Parsed, parse};
use ruff_python_trivia::CommentRanges;
use crate::comments::Comments;

View file

@ -4,15 +4,15 @@ use ruff_python_ast::{
self as ast, AnyNodeRef, Comprehension, Expr, ModModule, Parameter, Parameters, StringLike,
};
use ruff_python_trivia::{
find_only_token_in_range, first_non_trivia_token, indentation_at_offset, BackwardsTokenizer,
CommentRanges, SimpleToken, SimpleTokenKind, SimpleTokenizer,
BackwardsTokenizer, CommentRanges, SimpleToken, SimpleTokenKind, SimpleTokenizer,
find_only_token_in_range, first_non_trivia_token, indentation_at_offset,
};
use ruff_source_file::LineRanges;
use ruff_text_size::{Ranged, TextLen, TextRange};
use std::cmp::Ordering;
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
use crate::expression::expr_slice::{assign_comment_in_slice, ExprSliceCommentSection};
use crate::expression::expr_slice::{ExprSliceCommentSection, assign_comment_in_slice};
use crate::expression::parentheses::is_expression_parenthesized;
use crate::other::parameters::{
assign_argument_separator_comment_placement, find_parameter_separators,
@ -2256,7 +2256,9 @@ mod tests {
);
assert_eq!(
max_empty_lines("# trailing comment\n\n# own line comment\n\n\n# an other own line comment\n# block"),
max_empty_lines(
"# trailing comment\n\n# own line comment\n\n\n# an other own line comment\n# block"
),
2
);

View file

@ -5,9 +5,9 @@ use ruff_formatter::{Buffer, FormatContext, GroupId, IndentWidth, SourceCode};
use ruff_python_ast::str::Quote;
use ruff_python_parser::Tokens;
use crate::PyFormatOptions;
use crate::comments::Comments;
use crate::other::f_string_element::FStringExpressionElementContext;
use crate::PyFormatOptions;
pub struct PyFormatContext<'a> {
options: PyFormatOptions,

View file

@ -1,4 +1,4 @@
use ruff_db::{files::File, Db as SourceDb, Upcast};
use ruff_db::{Db as SourceDb, Upcast, files::File};
use crate::PyFormatOptions;

View file

@ -11,14 +11,14 @@ use ruff_python_trivia::CommentRanges;
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{leading_comments, trailing_comments, Comments, SourceComment};
use crate::comments::{Comments, SourceComment, leading_comments, trailing_comments};
use crate::expression::OperatorPrecedence;
use crate::expression::parentheses::{
in_parentheses_only_group, in_parentheses_only_if_group_breaks,
Parentheses, in_parentheses_only_group, in_parentheses_only_if_group_breaks,
in_parentheses_only_soft_line_break, in_parentheses_only_soft_line_break_or_space,
is_expression_parenthesized, write_in_parentheses_only_group_end_tag,
write_in_parentheses_only_group_start_tag, Parentheses,
write_in_parentheses_only_group_start_tag,
};
use crate::expression::OperatorPrecedence;
use crate::prelude::*;
use crate::string::implicit::FormatImplicitConcatenatedString;

View file

@ -1,14 +1,14 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{Expr, ExprAttribute, ExprNumberLiteral, Number};
use ruff_python_trivia::{find_only_token_in_range, SimpleTokenKind, SimpleTokenizer};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer, find_only_token_in_range};
use ruff_text_size::{Ranged, TextRange};
use crate::comments::dangling_comments;
use crate::expression::parentheses::{
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
};
use crate::expression::CallChainLayout;
use crate::expression::parentheses::{
NeedsParentheses, OptionalParentheses, Parentheses, is_expression_parenthesized,
};
use crate::prelude::*;
#[derive(Default)]

View file

@ -4,7 +4,7 @@ use ruff_python_ast::ExprAwait;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::{
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parenthesize,
NeedsParentheses, OptionalParentheses, Parenthesize, is_expression_parenthesized,
};
use crate::prelude::*;

View file

@ -2,11 +2,11 @@ use ruff_python_ast::ExprBytesLiteral;
use ruff_python_ast::{AnyNodeRef, StringLike};
use crate::expression::parentheses::{
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, in_parentheses_only_group,
};
use crate::prelude::*;
use crate::string::implicit::FormatImplicitConcatenatedStringFlat;
use crate::string::{implicit::FormatImplicitConcatenatedString, StringLikeExtensions};
use crate::string::{StringLikeExtensions, implicit::FormatImplicitConcatenatedString};
#[derive(Default)]
pub struct FormatExprBytesLiteral;

View file

@ -3,10 +3,10 @@ use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{Expr, ExprCall};
use crate::comments::dangling_comments;
use crate::expression::parentheses::{
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
};
use crate::expression::CallChainLayout;
use crate::expression::parentheses::{
NeedsParentheses, OptionalParentheses, Parentheses, is_expression_parenthesized,
};
use crate::prelude::*;
#[derive(Default)]

View file

@ -2,9 +2,9 @@ use ruff_formatter::{format_args, write};
use ruff_python_ast::{AnyNodeRef, DictItem, Expr, ExprDict};
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{dangling_comments, leading_comments, SourceComment};
use crate::comments::{SourceComment, dangling_comments, leading_comments};
use crate::expression::parentheses::{
empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, empty_parenthesized, parenthesized,
};
use crate::prelude::*;

View file

@ -4,7 +4,7 @@ use ruff_python_ast::ExprDictComp;
use ruff_text_size::Ranged;
use crate::comments::dangling_comments;
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, parenthesized};
use crate::prelude::*;
#[derive(Default)]

View file

@ -1,14 +1,14 @@
use ruff_python_ast::{AnyNodeRef, ExprFString, StringLike};
use crate::expression::parentheses::{
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, in_parentheses_only_group,
};
use crate::other::f_string::FStringLayout;
use crate::prelude::*;
use crate::string::StringLikeExtensions;
use crate::string::implicit::{
FormatImplicitConcatenatedString, FormatImplicitConcatenatedStringFlat,
};
use crate::string::StringLikeExtensions;
#[derive(Default)]
pub struct FormatExprFString;

View file

@ -1,8 +1,8 @@
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, format_args, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprGenerator;
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, parenthesized};
use crate::prelude::*;
#[derive(Eq, PartialEq, Debug, Default)]

View file

@ -1,11 +1,11 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{Expr, ExprIf};
use crate::comments::leading_comments;
use crate::expression::parentheses::{
in_parentheses_only_group, in_parentheses_only_soft_line_break_or_space,
is_expression_parenthesized, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, in_parentheses_only_group,
in_parentheses_only_soft_line_break_or_space, is_expression_parenthesized,
};
use crate::prelude::*;

View file

@ -4,7 +4,7 @@ use ruff_python_ast::ExprList;
use ruff_text_size::Ranged;
use crate::expression::parentheses::{
empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, empty_parenthesized, parenthesized,
};
use crate::prelude::*;

View file

@ -1,8 +1,8 @@
use ruff_formatter::{format_args, write, FormatResult};
use ruff_formatter::{FormatResult, format_args, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprListComp;
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, parenthesized};
use crate::prelude::*;
#[derive(Default)]

View file

@ -4,7 +4,7 @@ use ruff_python_ast::ExprNamed;
use crate::comments::dangling_comments;
use crate::expression::parentheses::{
in_parentheses_only_soft_line_break_or_space, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, in_parentheses_only_soft_line_break_or_space,
};
use crate::prelude::*;

View file

@ -2,7 +2,7 @@ use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprSet;
use ruff_text_size::Ranged;
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, parenthesized};
use crate::prelude::*;
#[derive(Default)]

View file

@ -1,8 +1,8 @@
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use ruff_formatter::{Buffer, FormatResult, format_args, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprSetComp;
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, parenthesized};
use crate::prelude::*;
#[derive(Default)]

View file

@ -1,10 +1,10 @@
use ruff_formatter::{write, FormatError};
use ruff_formatter::{FormatError, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{Expr, ExprSlice, ExprUnaryOp, UnaryOp};
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{dangling_comments, SourceComment};
use crate::comments::{SourceComment, dangling_comments};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::prelude::*;

View file

@ -1,6 +1,6 @@
use crate::builders::parenthesize_if_expands;
use crate::expression::parentheses::{
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, in_parentheses_only_group,
};
use crate::other::string_literal::StringLiteralKind;
use crate::prelude::*;
@ -8,7 +8,7 @@ use crate::string::implicit::{
FormatImplicitConcatenatedStringExpanded, FormatImplicitConcatenatedStringFlat,
ImplicitConcatenatedLayout,
};
use crate::string::{implicit::FormatImplicitConcatenatedString, StringLikeExtensions};
use crate::string::{StringLikeExtensions, implicit::FormatImplicitConcatenatedString};
use ruff_formatter::FormatRuleWithOptions;
use ruff_python_ast::{AnyNodeRef, ExprStringLiteral, StringLike};

View file

@ -1,12 +1,12 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{Expr, ExprSubscript};
use crate::expression::CallChainLayout;
use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::parentheses::{
is_expression_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
NeedsParentheses, OptionalParentheses, Parentheses, is_expression_parenthesized, parenthesized,
};
use crate::expression::CallChainLayout;
use crate::prelude::*;
#[derive(Default)]

View file

@ -1,11 +1,11 @@
use ruff_formatter::{format_args, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, format_args};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprTuple;
use ruff_text_size::{Ranged, TextRange};
use crate::builders::parenthesize_if_expands;
use crate::expression::parentheses::{
empty_parenthesized, optional_parentheses, parenthesized, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, empty_parenthesized, optional_parentheses, parenthesized,
};
use crate::other::commas::has_trailing_comma;
use crate::prelude::*;

View file

@ -4,7 +4,7 @@ use ruff_python_ast::UnaryOp;
use crate::comments::trailing_comments;
use crate::expression::parentheses::{
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
NeedsParentheses, OptionalParentheses, Parentheses, is_expression_parenthesized,
};
use crate::prelude::*;

View file

@ -5,7 +5,7 @@ use ruff_text_size::{Ranged, TextRange};
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::{
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parenthesize,
NeedsParentheses, OptionalParentheses, Parenthesize, is_expression_parenthesized,
};
use crate::prelude::*;

View file

@ -2,21 +2,21 @@ use std::cmp::Ordering;
use std::slice;
use ruff_formatter::{
write, FormatOwnedWithRule, FormatRefWithRule, FormatRule, FormatRuleWithOptions,
FormatOwnedWithRule, FormatRefWithRule, FormatRule, FormatRuleWithOptions, write,
};
use ruff_python_ast::parenthesize::parentheses_iterator;
use ruff_python_ast::visitor::source_order::{walk_expr, SourceOrderVisitor};
use ruff_python_ast::visitor::source_order::{SourceOrderVisitor, walk_expr};
use ruff_python_ast::{self as ast};
use ruff_python_ast::{AnyNodeRef, Expr, ExprRef, Operator};
use ruff_python_trivia::CommentRanges;
use ruff_text_size::Ranged;
use crate::builders::parenthesize_if_expands;
use crate::comments::{leading_comments, trailing_comments, LeadingDanglingTrailingComments};
use crate::comments::{LeadingDanglingTrailingComments, leading_comments, trailing_comments};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::parentheses::{
is_expression_parenthesized, optional_parentheses, parenthesized, NeedsParentheses,
OptionalParentheses, Parentheses, Parenthesize,
NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize, is_expression_parenthesized,
optional_parentheses, parenthesized,
};
use crate::prelude::*;
use crate::preview::is_hug_parens_with_braces_and_square_brackets_enabled;
@ -391,7 +391,7 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
.fmt(f)
} else {
expression.format().with_options(Parentheses::Never).fmt(f)
}
};
}
needs_parentheses => needs_parentheses,
};

View file

@ -1,15 +1,15 @@
use ruff_formatter::prelude::tag::Condition;
use ruff_formatter::{format_args, write, Argument, Arguments};
use ruff_formatter::{Argument, Arguments, format_args, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprRef;
use ruff_python_trivia::CommentRanges;
use ruff_python_trivia::{
first_non_trivia_token, BackwardsTokenizer, SimpleToken, SimpleTokenKind,
BackwardsTokenizer, SimpleToken, SimpleTokenKind, first_non_trivia_token,
};
use ruff_text_size::Ranged;
use crate::comments::{
dangling_comments, dangling_open_parenthesis_comments, trailing_comments, SourceComment,
SourceComment, dangling_comments, dangling_open_parenthesis_comments, trailing_comments,
};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::prelude::*;
@ -422,9 +422,11 @@ impl Format<PyFormatContext<'_>> for FormatEmptyParenthesized<'_> {
let end_of_line_split = self
.comments
.partition_point(|comment| comment.line_position().is_end_of_line());
debug_assert!(self.comments[end_of_line_split..]
.iter()
.all(|comment| comment.line_position().is_own_line()));
debug_assert!(
self.comments[end_of_line_split..]
.iter()
.all(|comment| comment.line_position().is_own_line())
);
group(&format_args![
token(self.left),
// end-of-line comments

View file

@ -6,14 +6,14 @@ use tracing::Level;
pub use range::format_range;
use ruff_formatter::prelude::*;
use ruff_formatter::{format, write, FormatError, Formatted, PrintError, Printed, SourceCode};
use ruff_formatter::{FormatError, Formatted, PrintError, Printed, SourceCode, format, write};
use ruff_python_ast::{AnyNodeRef, Mod};
use ruff_python_parser::{parse, ParseError, ParseOptions, Parsed};
use ruff_python_parser::{ParseError, ParseOptions, Parsed, parse};
use ruff_python_trivia::CommentRanges;
use ruff_text_size::Ranged;
use crate::comments::{
has_skip_comment, leading_comments, trailing_comments, Comments, SourceComment,
Comments, SourceComment, has_skip_comment, leading_comments, trailing_comments,
};
pub use crate::context::PyFormatContext;
pub use crate::db::Db;
@ -77,7 +77,13 @@ where
self.fmt_fields(node, f)?;
debug_assert!(node_comments.dangling.iter().all(SourceComment::is_formatted), "The node has dangling comments that need to be formatted manually. Add the special dangling comments handling to `fmt_fields`.");
debug_assert!(
node_comments
.dangling
.iter()
.all(SourceComment::is_formatted),
"The node has dangling comments that need to be formatted manually. Add the special dangling comments handling to `fmt_fields`."
);
write!(
f,
@ -194,11 +200,11 @@ mod tests {
use insta::assert_snapshot;
use ruff_python_ast::PySourceType;
use ruff_python_parser::{parse, ParseOptions};
use ruff_python_parser::{ParseOptions, parse};
use ruff_python_trivia::CommentRanges;
use ruff_text_size::{TextRange, TextSize};
use crate::{format_module_ast, format_module_source, format_range, PyFormatOptions};
use crate::{PyFormatOptions, format_module_ast, format_module_source, format_range};
/// Very basic test intentionally kept very similar to the CLI
#[test]

View file

@ -1,11 +1,11 @@
use std::io::{stdout, Read, Write};
use std::io::{Read, Write, stdout};
use std::path::Path;
use std::{fs, io};
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result, bail};
use clap::Parser as ClapParser;
use ruff_python_formatter::cli::{format_and_debug_print, Cli, Emit};
use ruff_python_formatter::cli::{Cli, Emit, format_and_debug_print};
/// Read a `String` from `stdin`.
pub(crate) fn read_from_stdin() -> Result<String> {

View file

@ -2,9 +2,9 @@ use ruff_formatter::write;
use ruff_python_ast::ModModule;
use ruff_python_trivia::lines_after;
use crate::FormatNodeRule;
use crate::prelude::*;
use crate::statement::suite::SuiteKind;
use crate::FormatNodeRule;
#[derive(Default)]
pub struct FormatModModule;

View file

@ -398,7 +398,7 @@ pub enum DocstringCodeLineWidth {
#[cfg(feature = "schemars")]
mod schema {
use ruff_formatter::LineWidth;
use schemars::gen::SchemaGenerator;
use schemars::r#gen::SchemaGenerator;
use schemars::schema::{Metadata, Schema, SubschemaValidation};
/// A dummy type that is used to generate a schema for `DocstringCodeLineWidth::Dynamic`.
@ -415,8 +415,8 @@ mod schema {
//
// The only difference to the automatically derived schema is that we use `oneOf` instead of
// `allOf`. There's no semantic difference between `allOf` and `oneOf` for single element lists.
pub(super) fn fixed(gen: &mut SchemaGenerator) -> Schema {
let schema = gen.subschema_for::<LineWidth>();
pub(super) fn fixed(generator: &mut SchemaGenerator) -> Schema {
let schema = generator.subschema_for::<LineWidth>();
Schema::Object(schemars::schema::SchemaObject {
metadata: Some(Box::new(Metadata {
description: Some(
@ -457,7 +457,7 @@ fn deserialize_docstring_code_line_width_dynamic<'de, D>(d: D) -> Result<(), D::
where
D: serde::Deserializer<'de>,
{
use serde::{de::Error, Deserialize};
use serde::{Deserialize, de::Error};
let value = String::deserialize(d)?;
match &*value {

View file

@ -1,11 +1,11 @@
use ruff_formatter::{write, FormatContext};
use ruff_formatter::{FormatContext, write};
use ruff_python_ast::{ArgOrKeyword, Arguments, Expr, StringFlags, StringLike};
use ruff_python_trivia::{PythonWhitespace, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use crate::expression::expr_generator::GeneratorExpParentheses;
use crate::expression::is_expression_huggable;
use crate::expression::parentheses::{empty_parenthesized, parenthesized, Parentheses};
use crate::expression::parentheses::{Parentheses, empty_parenthesized, parenthesized};
use crate::other::commas;
use crate::prelude::*;
use crate::string::StringLikeExtensions;

View file

@ -2,8 +2,8 @@ use ruff_formatter::FormatContext;
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::TextRange;
use crate::prelude::*;
use crate::MagicTrailingComma;
use crate::prelude::*;
/// Returns `true` if the range ends with a magic trailing comma (and the magic trailing comma
/// should be respected).

View file

@ -1,6 +1,6 @@
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use ruff_formatter::{Buffer, FormatResult, format_args, write};
use ruff_python_ast::{Comprehension, Expr};
use ruff_python_trivia::{find_only_token_in_range, SimpleTokenKind};
use ruff_python_trivia::{SimpleTokenKind, find_only_token_in_range};
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{leading_comments, trailing_comments};

View file

@ -1,11 +1,11 @@
use ruff_formatter::write;
use ruff_formatter::FormatRuleWithOptions;
use ruff_formatter::write;
use ruff_python_ast::ExceptHandlerExceptHandler;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
#[derive(Copy, Clone, Default)]

View file

@ -1,6 +1,6 @@
use std::borrow::Cow;
use ruff_formatter::{format_args, write, Buffer, RemoveSoftLinesBuffer};
use ruff_formatter::{Buffer, RemoveSoftLinesBuffer, format_args, write};
use ruff_python_ast::{
AnyStringFlags, ConversionFlag, Expr, FStringElement, FStringExpressionElement,
FStringLiteralElement, StringFlags,

View file

@ -1,11 +1,11 @@
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, format_args, write};
use ruff_python_ast::MatchCase;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::pattern::maybe_parenthesize_pattern;
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
#[derive(Default)]

View file

@ -38,20 +38,26 @@ impl FormatNodeRule<ParameterWithDefault> for FormatParameterWithDefault {
// ```
let needs_line_break_trailing = f.context().comments().has_trailing(parameter);
let default_first_comment = f.context().comments().leading(default.as_ref()).first();
let needs_line_break_leading = default_first_comment.is_some_and(|default_leading_comment| {
let mut tokenizer = SimpleTokenizer::new(
f.context().source(),
TextRange::new(parameter.end(), default_leading_comment.start()),
)
.skip_trivia()
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
let equals = tokenizer.next();
debug_assert!(equals.is_some_and(|token| token.kind == SimpleTokenKind::Equals));
let lparens = tokenizer.next();
debug_assert!(lparens
.as_ref().is_none_or(|token| token.kind == SimpleTokenKind::LParen));
lparens.is_none()
});
let needs_line_break_leading =
default_first_comment.is_some_and(|default_leading_comment| {
let mut tokenizer = SimpleTokenizer::new(
f.context().source(),
TextRange::new(parameter.end(), default_leading_comment.start()),
)
.skip_trivia()
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
let equals = tokenizer.next();
debug_assert!(
equals.is_some_and(|token| token.kind == SimpleTokenKind::Equals)
);
let lparens = tokenizer.next();
debug_assert!(
lparens
.as_ref()
.is_none_or(|token| token.kind == SimpleTokenKind::LParen)
);
lparens.is_none()
});
let needs_line_break = needs_line_break_trailing || needs_line_break_leading;
write!(

View file

@ -1,11 +1,11 @@
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, format_args, write};
use ruff_python_ast::{AnyNodeRef, Parameters};
use ruff_python_trivia::{CommentLinePosition, SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::comments::{
dangling_comments, dangling_open_parenthesis_comments, leading_comments, leading_node_comments,
trailing_comments, SourceComment,
SourceComment, dangling_comments, dangling_open_parenthesis_comments, leading_comments,
leading_node_comments, trailing_comments,
};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::parentheses::empty_parenthesized;
@ -670,10 +670,28 @@ fn has_trailing_comma(
// The slash lacks its own node
if ends_with_pos_only_argument_separator {
let comma = tokens.next();
assert!(matches!(comma, Some(SimpleToken { kind: SimpleTokenKind::Comma, .. })), "The last positional only argument must be separated by a `,` from the positional only parameters separator `/` but found '{comma:?}'.");
assert!(
matches!(
comma,
Some(SimpleToken {
kind: SimpleTokenKind::Comma,
..
})
),
"The last positional only argument must be separated by a `,` from the positional only parameters separator `/` but found '{comma:?}'."
);
let slash = tokens.next();
assert!(matches!(slash, Some(SimpleToken { kind: SimpleTokenKind::Slash, .. })), "The positional argument separator must be present for a function that has positional only parameters but found '{slash:?}'.");
assert!(
matches!(
slash,
Some(SimpleToken {
kind: SimpleTokenKind::Slash,
..
})
),
"The positional argument separator must be present for a function that has positional only parameters but found '{slash:?}'."
);
}
tokens

View file

@ -1,9 +1,9 @@
use ruff_formatter::FormatRuleWithOptions;
use ruff_python_ast::StringLiteral;
use crate::prelude::*;
use crate::string::{docstring, StringNormalizer};
use crate::QuoteStyle;
use crate::prelude::*;
use crate::string::{StringNormalizer, docstring};
#[derive(Default)]
pub struct FormatStringLiteral {

View file

@ -1,9 +1,9 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, write};
use ruff_python_ast::WithItem;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::{
is_expression_parenthesized, parenthesized, Parentheses, Parenthesize,
Parentheses, Parenthesize, is_expression_parenthesized, parenthesized,
};
use crate::prelude::*;

View file

@ -3,7 +3,7 @@ use ruff_python_ast::{AnyNodeRef, Expr};
use ruff_python_ast::{MatchCase, Pattern};
use ruff_python_trivia::CommentRanges;
use ruff_python_trivia::{
first_non_trivia_token, BackwardsTokenizer, SimpleToken, SimpleTokenKind,
BackwardsTokenizer, SimpleToken, SimpleTokenKind, first_non_trivia_token,
};
use ruff_text_size::Ranged;
use std::cmp::Ordering;
@ -11,7 +11,7 @@ use std::cmp::Ordering;
use crate::builders::parenthesize_if_expands;
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::parentheses::{
optional_parentheses, parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
NeedsParentheses, OptionalParentheses, Parentheses, optional_parentheses, parenthesized,
};
use crate::prelude::*;

View file

@ -3,7 +3,7 @@ use ruff_python_ast::{Pattern, PatternArguments};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::expression::parentheses::{empty_parenthesized, parenthesized, Parentheses};
use crate::expression::parentheses::{Parentheses, empty_parenthesized, parenthesized};
use crate::prelude::*;
#[derive(Default)]

View file

@ -5,9 +5,9 @@ use ruff_python_ast::{Expr, Identifier, Pattern};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{leading_comments, trailing_comments, SourceComment};
use crate::comments::{SourceComment, leading_comments, trailing_comments};
use crate::expression::parentheses::{
empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, empty_parenthesized, parenthesized,
};
use crate::prelude::*;

View file

@ -4,8 +4,8 @@ use ruff_python_ast::PatternMatchOr;
use crate::comments::leading_comments;
use crate::expression::parentheses::{
in_parentheses_only_group, in_parentheses_only_soft_line_break_or_space, NeedsParentheses,
OptionalParentheses,
NeedsParentheses, OptionalParentheses, in_parentheses_only_group,
in_parentheses_only_soft_line_break_or_space,
};
use crate::prelude::*;

View file

@ -1,11 +1,11 @@
use ruff_formatter::{format_args, Format, FormatResult};
use ruff_formatter::{Format, FormatResult, format_args};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::PatternMatchSequence;
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};
use crate::expression::parentheses::{
empty_parenthesized, optional_parentheses, parenthesized, NeedsParentheses, OptionalParentheses,
NeedsParentheses, OptionalParentheses, empty_parenthesized, optional_parentheses, parenthesized,
};
use crate::prelude::*;
@ -25,7 +25,7 @@ impl FormatNodeRule<PatternMatchSequence> for FormatPatternMatchSequence {
// If the sequence is empty, format the empty parentheses, along with any dangling
// comments.
([], SequenceType::Tuple | SequenceType::TupleNoParens) => {
return empty_parenthesized("(", dangling, ")").fmt(f)
return empty_parenthesized("(", dangling, ")").fmt(f);
}
([], SequenceType::List) => return empty_parenthesized("[", dangling, "]").fmt(f),
@ -34,7 +34,7 @@ impl FormatNodeRule<PatternMatchSequence> for FormatPatternMatchSequence {
([elt], SequenceType::Tuple | SequenceType::TupleNoParens) => {
return parenthesized("(", &format_args![elt.format(), token(",")], ")")
.with_dangling_comments(dangling)
.fmt(f)
.fmt(f);
}
_ => {}

View file

@ -1,5 +1,5 @@
pub(crate) use crate::{
builders::PyFormatterExtensions, AsFormat, FormatNodeRule, FormattedIterExt as _, IntoFormat,
PyFormatContext, PyFormatter,
AsFormat, FormatNodeRule, FormattedIterExt as _, IntoFormat, PyFormatContext, PyFormatter,
builders::PyFormatterExtensions,
};
pub(crate) use ruff_formatter::prelude::*;

View file

@ -2,13 +2,13 @@ use tracing::Level;
use ruff_formatter::printer::SourceMapGeneration;
use ruff_formatter::{
format, FormatContext, FormatError, FormatOptions, IndentStyle, PrintedRange, SourceCode,
FormatContext, FormatError, FormatOptions, IndentStyle, PrintedRange, SourceCode, format,
};
use ruff_python_ast::visitor::source_order::{walk_body, SourceOrderVisitor, TraversalSignal};
use ruff_python_ast::visitor::source_order::{SourceOrderVisitor, TraversalSignal, walk_body};
use ruff_python_ast::{AnyNodeRef, Stmt, StmtMatch, StmtTry};
use ruff_python_parser::{parse, ParseOptions};
use ruff_python_parser::{ParseOptions, parse};
use ruff_python_trivia::{
indentation_at_offset, BackwardsTokenizer, CommentRanges, SimpleToken, SimpleTokenKind,
BackwardsTokenizer, CommentRanges, SimpleToken, SimpleTokenKind, indentation_at_offset,
};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
@ -17,7 +17,7 @@ use crate::context::{IndentLevel, NodeLevel};
use crate::prelude::*;
use crate::statement::suite::DocstringStmt;
use crate::verbatim::{ends_suppression, starts_suppression};
use crate::{format_module_source, FormatModuleError, PyFormatOptions};
use crate::{FormatModuleError, PyFormatOptions, format_module_source};
/// Formats the given `range` in source rather than the entire file.
///

View file

@ -1,4 +1,4 @@
use ruff_formatter::{write, Argument, Arguments, FormatError};
use ruff_formatter::{Argument, Arguments, FormatError, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{
ElifElseClause, ExceptHandlerExceptHandler, MatchCase, StmtClassDef, StmtFor, StmtFunctionDef,
@ -7,8 +7,8 @@ use ruff_python_ast::{
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
use crate::statement::suite::{contains_only_an_ellipsis, SuiteKind};
use crate::comments::{SourceComment, leading_alternate_branch_comments, trailing_comments};
use crate::statement::suite::{SuiteKind, contains_only_an_ellipsis};
use crate::verbatim::write_suppressed_clause_header;
use crate::{has_skip_comment, prelude::*};
@ -473,12 +473,22 @@ fn colon_range(after_keyword_or_condition: TextSize, source: &str) -> FormatResu
range,
}) => Ok(range),
Some(token) => {
debug_assert!(false, "Expected the colon marking the end of the case header but found {token:?} instead.");
Err(FormatError::syntax_error("Expected colon marking the end of the case header but found another token instead."))
debug_assert!(
false,
"Expected the colon marking the end of the case header but found {token:?} instead."
);
Err(FormatError::syntax_error(
"Expected colon marking the end of the case header but found another token instead.",
))
}
None => {
debug_assert!(false, "Expected the colon marking the end of the case header but found the end of the range.");
Err(FormatError::syntax_error("Expected the colon marking the end of the case header but found the end of the range."))
debug_assert!(
false,
"Expected the colon marking the end of the case header but found the end of the range."
);
Err(FormatError::syntax_error(
"Expected the colon marking the end of the case header but found the end of the range.",
))
}
}
}

View file

@ -1,4 +1,4 @@
use ruff_formatter::{format_args, write, FormatError, RemoveSoftLinesBuffer};
use ruff_formatter::{FormatError, RemoveSoftLinesBuffer, format_args, write};
use ruff_python_ast::{
AnyNodeRef, Expr, ExprAttribute, ExprCall, FString, Operator, StmtAssign, StringLike,
TypeParams,
@ -6,12 +6,12 @@ use ruff_python_ast::{
use crate::builders::parenthesize_if_expands;
use crate::comments::{
trailing_comments, Comments, LeadingDanglingTrailingComments, SourceComment,
Comments, LeadingDanglingTrailingComments, SourceComment, trailing_comments,
};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::parentheses::{
is_expression_parenthesized, optional_parentheses, NeedsParentheses, OptionalParentheses,
Parentheses, Parenthesize,
NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize, is_expression_parenthesized,
optional_parentheses,
};
use crate::expression::{
can_omit_optional_parentheses, has_own_parentheses, has_parentheses,
@ -19,11 +19,11 @@ use crate::expression::{
};
use crate::other::f_string::FStringLayout;
use crate::statement::trailing_semicolon;
use crate::string::StringLikeExtensions;
use crate::string::implicit::{
FormatImplicitConcatenatedStringExpanded, FormatImplicitConcatenatedStringFlat,
ImplicitConcatenatedLayout,
};
use crate::string::StringLikeExtensions;
use crate::{has_skip_comment, prelude::*};
#[derive(Default)]
@ -1153,7 +1153,10 @@ impl<'a> OptionalParenthesesInlinedComments<'a> {
let (expression_inline_comments, trailing_own_line_comments) =
expression_comments.trailing.split_at(after_end_of_line);
debug_assert!(trailing_own_line_comments.is_empty(), "The method should have returned early if the expression has trailing own line comments");
debug_assert!(
trailing_own_line_comments.is_empty(),
"The method should have returned early if the expression has trailing own line comments"
);
Some(OptionalParenthesesInlinedComments {
expression: expression_inline_comments,

View file

@ -4,12 +4,12 @@ use ruff_python_ast::StmtAugAssign;
use crate::comments::SourceComment;
use crate::expression::parentheses::is_expression_parenthesized;
use crate::statement::stmt_assign::{
has_target_own_parentheses, AnyAssignmentOperator, AnyBeforeOperator,
FormatStatementsLastExpression,
AnyAssignmentOperator, AnyBeforeOperator, FormatStatementsLastExpression,
has_target_own_parentheses,
};
use crate::statement::trailing_semicolon;
use crate::{has_skip_comment, prelude::*};
use crate::{AsFormat, FormatNodeRule};
use crate::{has_skip_comment, prelude::*};
#[derive(Default)]
pub struct FormatStmtAugAssign;

View file

@ -6,9 +6,9 @@ use ruff_text_size::Ranged;
use crate::comments::format::{
empty_lines_after_leading_comments, empty_lines_before_trailing_comments,
};
use crate::comments::{leading_comments, trailing_comments, SourceComment};
use crate::comments::{SourceComment, leading_comments, trailing_comments};
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
#[derive(Default)]

View file

@ -2,8 +2,8 @@ use ruff_formatter::write;
use ruff_python_ast::StmtDelete;
use ruff_text_size::Ranged;
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
use crate::comments::{dangling_node_comments, SourceComment};
use crate::builders::{PyFormatterExtensions, parenthesize_if_expands};
use crate::comments::{SourceComment, dangling_node_comments};
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::{has_skip_comment, prelude::*};

View file

@ -6,7 +6,7 @@ use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader, ElseClause};
use crate::statement::clause::{ClauseHeader, ElseClause, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
#[derive(Debug)]

View file

@ -4,7 +4,7 @@ use crate::comments::format::{
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::{Parentheses, Parenthesize};
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
use crate::statement::stmt_class_def::FormatDecorators;
use crate::statement::suite::SuiteKind;
use ruff_formatter::write;

View file

@ -5,7 +5,7 @@ use ruff_text_size::Ranged;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
#[derive(Default)]

View file

@ -2,7 +2,7 @@ use ruff_formatter::write;
use ruff_python_ast::StmtImportFrom;
use ruff_text_size::Ranged;
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions, TrailingComma};
use crate::builders::{PyFormatterExtensions, TrailingComma, parenthesize_if_expands};
use crate::comments::SourceComment;
use crate::expression::parentheses::parenthesized;
use crate::has_skip_comment;

View file

@ -6,7 +6,7 @@ use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::statement::clause::{clause_header, ClauseHeader};
use crate::statement::clause::{ClauseHeader, clause_header};
#[derive(Default)]
pub struct FormatStmtMatch;

View file

@ -1,15 +1,15 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_formatter::{FormatRuleWithOptions, write};
use ruff_python_ast::{ExceptHandler, StmtTry};
use ruff_text_size::Ranged;
use crate::comments;
use crate::comments::leading_alternate_branch_comments;
use crate::comments::SourceComment;
use crate::comments::leading_alternate_branch_comments;
use crate::other::except_handler_except_handler::{
ExceptHandlerKind, FormatExceptHandlerExceptHandler,
};
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader, ElseClause};
use crate::statement::clause::{ClauseHeader, ElseClause, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
use crate::statement::{FormatRefWithRule, Stmt};

View file

@ -5,7 +5,7 @@ use ruff_text_size::Ranged;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader, ElseClause};
use crate::statement::clause::{ClauseHeader, ElseClause, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
#[derive(Default)]

View file

@ -1,4 +1,4 @@
use ruff_formatter::{format_args, write, FormatContext, FormatError};
use ruff_formatter::{FormatContext, FormatError, format_args, write};
use ruff_python_ast::PythonVersion;
use ruff_python_ast::{StmtWith, WithItem};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
@ -13,7 +13,7 @@ use crate::expression::parentheses::{
use crate::other::commas;
use crate::other::with_item::WithItemLayout;
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
#[derive(Default)]

View file

@ -1,5 +1,5 @@
use ruff_formatter::{
write, FormatContext, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions,
FormatContext, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions, write,
};
use ruff_python_ast::helpers::is_compound_statement;
use ruff_python_ast::{self as ast, Expr, PySourceType, Stmt, Suite};
@ -8,7 +8,7 @@ use ruff_python_trivia::{lines_after, lines_after_ignoring_end_of_line_trivia, l
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{
leading_comments, trailing_comments, Comments, LeadingDanglingTrailingComments,
Comments, LeadingDanglingTrailingComments, leading_comments, trailing_comments,
};
use crate::context::{NodeLevel, TopLevelStatementPosition, WithIndentLevel, WithNodeLevel};
use crate::other::string_literal::StringLiteralKind;
@ -916,10 +916,10 @@ mod tests {
use ruff_python_parser::parse_module;
use ruff_python_trivia::CommentRanges;
use crate::PyFormatOptions;
use crate::comments::Comments;
use crate::prelude::*;
use crate::statement::suite::SuiteKind;
use crate::PyFormatOptions;
fn format_suite(level: SuiteKind) -> String {
let source = r"

View file

@ -10,19 +10,19 @@ use itertools::Itertools;
use regex::Regex;
use ruff_formatter::printer::SourceMapGeneration;
use ruff_python_ast::{str::Quote, AnyStringFlags, StringFlags};
use ruff_python_ast::{AnyStringFlags, StringFlags, str::Quote};
use ruff_python_parser::ParseOptions;
use ruff_python_trivia::CommentRanges;
use {
ruff_formatter::{write, FormatOptions, IndentStyle, LineWidth, Printed},
ruff_python_trivia::{is_python_whitespace, PythonWhitespace},
ruff_formatter::{FormatOptions, IndentStyle, LineWidth, Printed, write},
ruff_python_trivia::{PythonWhitespace, is_python_whitespace},
ruff_text_size::{Ranged, TextLen, TextRange, TextSize},
};
use super::NormalizedString;
use crate::preview::is_no_chaperone_for_escaped_quote_in_triple_quoted_docstring_enabled;
use crate::string::StringQuotes;
use crate::{prelude::*, DocstringCodeLineWidth, FormatModuleError};
use crate::{DocstringCodeLineWidth, FormatModuleError, prelude::*};
/// Format a docstring by trimming whitespace and adjusting the indentation.
///
@ -1796,7 +1796,7 @@ impl Indentation {
}),
_ => None,
}
};
}
Self::Mixed { .. } => return None,
};

View file

@ -1,5 +1,5 @@
use itertools::Itertools;
use ruff_formatter::{format_args, write, FormatContext};
use ruff_formatter::{FormatContext, format_args, write};
use ruff_python_ast::str::{Quote, TripleQuotes};
use ruff_python_ast::str_prefix::{
AnyStringPrefix, ByteStringPrefix, FStringPrefix, StringLiteralPrefix,
@ -16,10 +16,11 @@ use crate::other::f_string_element::FormatFStringExpressionElement;
use crate::prelude::*;
use crate::string::docstring::needs_chaperone_space;
use crate::string::normalize::{
is_fstring_with_quoted_debug_expression, is_fstring_with_quoted_format_spec_and_debug,
is_fstring_with_triple_quoted_literal_expression_containing_quotes, QuoteMetadata,
QuoteMetadata, is_fstring_with_quoted_debug_expression,
is_fstring_with_quoted_format_spec_and_debug,
is_fstring_with_triple_quoted_literal_expression_containing_quotes,
};
use crate::string::{normalize_string, StringLikeExtensions, StringNormalizer, StringQuotes};
use crate::string::{StringLikeExtensions, StringNormalizer, StringQuotes, normalize_string};
/// Formats any implicitly concatenated string. This could be any valid combination
/// of string, bytes or f-string literals.

View file

@ -1,17 +1,16 @@
use memchr::memchr2;
pub(crate) use normalize::{normalize_string, NormalizedString, StringNormalizer};
use ruff_python_ast::str::{Quote, TripleQuotes};
pub(crate) use normalize::{NormalizedString, StringNormalizer, normalize_string};
use ruff_python_ast::StringLikePart;
use ruff_python_ast::str::{Quote, TripleQuotes};
use ruff_python_ast::{
self as ast,
self as ast, AnyStringFlags, StringFlags,
str_prefix::{AnyStringPrefix, StringLiteralPrefix},
AnyStringFlags, StringFlags,
};
use ruff_source_file::LineRanges;
use ruff_text_size::Ranged;
use crate::prelude::*;
use crate::QuoteStyle;
use crate::prelude::*;
pub(crate) mod docstring;
pub(crate) mod implicit;

View file

@ -5,16 +5,16 @@ use std::iter::FusedIterator;
use ruff_formatter::FormatContext;
use ruff_python_ast::visitor::source_order::SourceOrderVisitor;
use ruff_python_ast::{
str::{Quote, TripleQuotes},
AnyStringFlags, BytesLiteral, FString, FStringElement, FStringElements, FStringFlags,
StringFlags, StringLikePart, StringLiteral,
str::{Quote, TripleQuotes},
};
use ruff_text_size::{Ranged, TextRange, TextSlice};
use crate::QuoteStyle;
use crate::context::FStringState;
use crate::prelude::*;
use crate::string::StringQuotes;
use crate::QuoteStyle;
pub(crate) struct StringNormalizer<'a, 'src> {
preferred_quote_style: Option<QuoteStyle>,
@ -1057,9 +1057,9 @@ mod tests {
use std::borrow::Cow;
use ruff_python_ast::{
AnyStringFlags,
str::{Quote, TripleQuotes},
str_prefix::{AnyStringPrefix, ByteStringPrefix},
AnyStringFlags,
};
use crate::string::normalize_string;

View file

@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::iter::FusedIterator;
use std::slice::Iter;
use ruff_formatter::{write, FormatError};
use ruff_formatter::{FormatError, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::Stmt;
use ruff_python_parser::{self as parser, TokenKind};
@ -11,7 +11,7 @@ use ruff_source_file::LineRanges;
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::comments::format::{empty_lines, format_comment};
use crate::comments::{leading_comments, trailing_comments, SourceComment};
use crate::comments::{SourceComment, leading_comments, trailing_comments};
use crate::prelude::*;
use crate::statement::clause::ClauseHeader;
use crate::statement::suite::SuiteChildStatement;
@ -798,13 +798,13 @@ impl Iterator for LogicalLinesIter<'_> {
Some(token) if token.kind() == TokenKind::Unknown => {
return Some(Err(FormatError::syntax_error(
"Unexpected token when lexing verbatim statement range.",
)))
)));
}
Some(token) => match token.kind() {
TokenKind::Newline => break (token.start(), token.end()),
// Ignore if inside an expression
TokenKind::NonLogicalNewline if parens == 0 => {
break (token.start(), token.end())
break (token.start(), token.end());
}
TokenKind::Lbrace | TokenKind::Lpar | TokenKind::Lsqb => {
parens = parens.saturating_add(1);