mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 02:12:22 +00:00
Pass parent to NeedsParentheses
(#5708)
This commit is contained in:
parent
30702c2977
commit
067b2a6ce6
55 changed files with 562 additions and 606 deletions
|
@ -1,11 +1,12 @@
|
||||||
use crate::comments::{leading_comments, trailing_comments, Comments};
|
use rustpython_parser::ast::{Constant, Expr, ExprAttribute, ExprConstant};
|
||||||
use crate::expression::parentheses::{
|
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
use ruff_formatter::write;
|
||||||
};
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
|
|
||||||
|
use crate::comments::{leading_comments, trailing_comments};
|
||||||
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::write;
|
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprAttribute, ExprConstant};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FormatExprAttribute;
|
pub struct FormatExprAttribute;
|
||||||
|
@ -35,7 +36,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
|
||||||
dangling_comments.split_at(leading_attribute_comments_start);
|
dangling_comments.split_at(leading_attribute_comments_start);
|
||||||
|
|
||||||
if needs_parentheses {
|
if needs_parentheses {
|
||||||
value.format().with_options(Parenthesize::Always).fmt(f)?;
|
value.format().with_options(Parentheses::Always).fmt(f)?;
|
||||||
} else if let Expr::Attribute(expr_attribute) = value.as_ref() {
|
} else if let Expr::Attribute(expr_attribute) = value.as_ref() {
|
||||||
// We're in a attribute chain (`a.b.c`). The outermost node adds parentheses if
|
// We're in a attribute chain (`a.b.c`). The outermost node adds parentheses if
|
||||||
// required, the inner ones don't need them so we skip the `Expr` formatting that
|
// required, the inner ones don't need them so we skip the `Expr` formatting that
|
||||||
|
@ -71,41 +72,23 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if there are any own line comments in an attribute chain (a.b.c). This method is
|
|
||||||
/// recursive up to the innermost expression that the attribute chain starts behind.
|
|
||||||
fn has_breaking_comments_attribute_chain(
|
|
||||||
expr_attribute: &ExprAttribute,
|
|
||||||
comments: &Comments,
|
|
||||||
) -> bool {
|
|
||||||
if comments
|
|
||||||
.dangling_comments(expr_attribute)
|
|
||||||
.iter()
|
|
||||||
.any(|comment| comment.line_position().is_own_line())
|
|
||||||
|| comments.has_trailing_own_line_comments(expr_attribute)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Expr::Attribute(inner) = expr_attribute.value.as_ref() {
|
|
||||||
return has_breaking_comments_attribute_chain(inner, comments);
|
|
||||||
}
|
|
||||||
|
|
||||||
return comments.has_trailing_own_line_comments(expr_attribute.value.as_ref());
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NeedsParentheses for ExprAttribute {
|
impl NeedsParentheses for ExprAttribute {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
if has_breaking_comments_attribute_chain(self, context.comments()) {
|
// Checks if there are any own line comments in an attribute chain (a.b.c).
|
||||||
return Parentheses::Always;
|
if context
|
||||||
}
|
.comments()
|
||||||
|
.dangling_comments(self)
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
.iter()
|
||||||
Parentheses::Optional => Parentheses::Never,
|
.any(|comment| comment.line_position().is_own_line())
|
||||||
parentheses => parentheses,
|
|| context.comments().has_trailing_own_line_comments(self)
|
||||||
|
{
|
||||||
|
OptionalParentheses::Always
|
||||||
|
} else {
|
||||||
|
self.value.needs_parentheses(parent, context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::prelude::{space, text};
|
use ruff_formatter::prelude::{space, text};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprAwait;
|
use rustpython_parser::ast::ExprAwait;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -20,9 +19,9 @@ impl FormatNodeRule<ExprAwait> for FormatExprAwait {
|
||||||
impl NeedsParentheses for ExprAwait {
|
impl NeedsParentheses for ExprAwait {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
use crate::comments::{trailing_comments, trailing_node_comments};
|
use crate::comments::{trailing_comments, trailing_node_comments};
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
default_expression_needs_parentheses, in_parentheses_only_group, is_expression_parenthesized,
|
in_parentheses_only_group, is_expression_parenthesized, NeedsParentheses, OptionalParentheses,
|
||||||
NeedsParentheses, Parenthesize,
|
|
||||||
};
|
};
|
||||||
use crate::expression::Parentheses;
|
use crate::expression::Parentheses;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
|
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
|
||||||
use ruff_python_ast::node::AstNode;
|
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||||
use rustpython_parser::ast::{
|
use rustpython_parser::ast::{
|
||||||
Constant, Expr, ExprAttribute, ExprBinOp, ExprConstant, ExprUnaryOp, Operator, UnaryOp,
|
Constant, Expr, ExprAttribute, ExprBinOp, ExprConstant, ExprUnaryOp, Operator, UnaryOp,
|
||||||
};
|
};
|
||||||
|
@ -175,9 +174,9 @@ impl FormatRule<Operator, PyFormatContext<'_>> for FormatOperator {
|
||||||
impl NeedsParentheses for ExprBinOp {
|
impl NeedsParentheses for ExprBinOp {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::comments::leading_comments;
|
use crate::comments::leading_comments;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
|
in_parentheses_only_group, NeedsParentheses, OptionalParentheses, Parentheses,
|
||||||
Parenthesize,
|
|
||||||
};
|
};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
|
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::{BoolOp, ExprBoolOp};
|
use rustpython_parser::ast::{BoolOp, ExprBoolOp};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -70,10 +70,10 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
|
||||||
impl NeedsParentheses for ExprBoolOp {
|
impl NeedsParentheses for ExprBoolOp {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
use crate::builders::PyFormatterExtensions;
|
|
||||||
use crate::comments::dangling_comments;
|
|
||||||
use crate::context::PyFormatContext;
|
|
||||||
use crate::expression::parentheses::{
|
|
||||||
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
|
|
||||||
Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::trivia::{SimpleTokenizer, TokenKind};
|
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
|
||||||
use ruff_formatter::prelude::{format_with, group, text};
|
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
use rustpython_parser::ast::{Expr, ExprCall, Ranged};
|
use rustpython_parser::ast::{Expr, ExprCall, Ranged};
|
||||||
|
|
||||||
|
use ruff_formatter::write;
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
|
|
||||||
|
use crate::comments::dangling_comments;
|
||||||
|
|
||||||
|
use crate::expression::parentheses::{
|
||||||
|
parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
|
||||||
|
};
|
||||||
|
use crate::prelude::*;
|
||||||
|
use crate::trivia::{SimpleTokenizer, TokenKind};
|
||||||
|
use crate::FormatNodeRule;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FormatExprCall;
|
pub struct FormatExprCall;
|
||||||
|
|
||||||
|
@ -52,14 +53,21 @@ impl FormatNodeRule<ExprCall> for FormatExprCall {
|
||||||
[argument] if keywords.is_empty() => {
|
[argument] if keywords.is_empty() => {
|
||||||
let parentheses =
|
let parentheses =
|
||||||
if is_single_argument_parenthesized(argument, item.end(), source) {
|
if is_single_argument_parenthesized(argument, item.end(), source) {
|
||||||
Parenthesize::Always
|
Parentheses::Always
|
||||||
} else {
|
} else {
|
||||||
Parenthesize::Never
|
Parentheses::Never
|
||||||
};
|
};
|
||||||
joiner.entry(argument, &argument.format().with_options(parentheses));
|
joiner.entry(argument, &argument.format().with_options(parentheses));
|
||||||
}
|
}
|
||||||
arguments => {
|
arguments => {
|
||||||
joiner.nodes(arguments).nodes(keywords.iter());
|
joiner
|
||||||
|
.entries(
|
||||||
|
// We have the parentheses from the call so the arguments never need any
|
||||||
|
arguments
|
||||||
|
.iter()
|
||||||
|
.map(|arg| (arg, arg.format().with_options(Parentheses::Preserve))),
|
||||||
|
)
|
||||||
|
.nodes(keywords.iter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,13 +108,10 @@ impl FormatNodeRule<ExprCall> for FormatExprCall {
|
||||||
impl NeedsParentheses for ExprCall {
|
impl NeedsParentheses for ExprCall {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
self.func.needs_parentheses(parent, context)
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::comments::leading_comments;
|
use crate::comments::leading_comments;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
|
in_parentheses_only_group, NeedsParentheses, OptionalParentheses, Parentheses,
|
||||||
Parenthesize,
|
|
||||||
};
|
};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
|
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::{CmpOp, ExprCompare};
|
use rustpython_parser::ast::{CmpOp, ExprCompare};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -73,10 +73,10 @@ impl FormatNodeRule<ExprCompare> for FormatExprCompare {
|
||||||
impl NeedsParentheses for ExprCompare {
|
impl NeedsParentheses for ExprCompare {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,17 @@
|
||||||
use crate::expression::parentheses::{
|
use ruff_text_size::{TextLen, TextRange};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
use rustpython_parser::ast::{Constant, ExprConstant, Ranged};
|
||||||
};
|
|
||||||
use crate::expression::string::{FormatString, StringLayout};
|
use ruff_formatter::write;
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
|
use ruff_python_ast::str::is_implicit_concatenation;
|
||||||
|
|
||||||
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
|
use crate::expression::string::{FormatString, StringPrefix, StringQuotes};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::{not_yet_implemented_custom_text, verbatim_text, FormatNodeRule};
|
use crate::{not_yet_implemented_custom_text, verbatim_text, FormatNodeRule};
|
||||||
use ruff_formatter::{write, FormatRuleWithOptions};
|
|
||||||
use rustpython_parser::ast::{Constant, ExprConstant};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FormatExprConstant {
|
pub struct FormatExprConstant;
|
||||||
string_layout: StringLayout,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FormatRuleWithOptions<ExprConstant, PyFormatContext<'_>> for FormatExprConstant {
|
|
||||||
type Options = StringLayout;
|
|
||||||
|
|
||||||
fn with_options(mut self, options: Self::Options) -> Self {
|
|
||||||
self.string_layout = options;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FormatNodeRule<ExprConstant> for FormatExprConstant {
|
impl FormatNodeRule<ExprConstant> for FormatExprConstant {
|
||||||
fn fmt_fields(&self, item: &ExprConstant, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_fields(&self, item: &ExprConstant, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
|
@ -39,7 +31,7 @@ impl FormatNodeRule<ExprConstant> for FormatExprConstant {
|
||||||
Constant::Int(_) | Constant::Float(_) | Constant::Complex { .. } => {
|
Constant::Int(_) | Constant::Float(_) | Constant::Complex { .. } => {
|
||||||
write!(f, [verbatim_text(item)])
|
write!(f, [verbatim_text(item)])
|
||||||
}
|
}
|
||||||
Constant::Str(_) => FormatString::new(item, self.string_layout).fmt(f),
|
Constant::Str(_) => FormatString::new(item).fmt(f),
|
||||||
Constant::Bytes(_) => {
|
Constant::Bytes(_) => {
|
||||||
not_yet_implemented_custom_text(r#"b"NOT_YET_IMPLEMENTED_BYTE_STRING""#).fmt(f)
|
not_yet_implemented_custom_text(r#"b"NOT_YET_IMPLEMENTED_BYTE_STRING""#).fmt(f)
|
||||||
}
|
}
|
||||||
|
@ -61,20 +53,32 @@ impl FormatNodeRule<ExprConstant> for FormatExprConstant {
|
||||||
impl NeedsParentheses for ExprConstant {
|
impl NeedsParentheses for ExprConstant {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
if self.value.is_str() {
|
||||||
Parentheses::Optional if self.value.is_str() && parenthesize.is_if_breaks() => {
|
let contents = context.locator().slice(self.range());
|
||||||
// Custom handling that only adds parentheses for implicit concatenated strings.
|
// Don't wrap triple quoted strings
|
||||||
if parenthesize.is_if_breaks() {
|
if is_multiline_string(self, context.source()) || !is_implicit_concatenation(contents) {
|
||||||
Parentheses::Custom
|
OptionalParentheses::Never
|
||||||
} else {
|
} else {
|
||||||
Parentheses::Optional
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
Parentheses::Optional => Parentheses::Never,
|
OptionalParentheses::Never
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn is_multiline_string(constant: &ExprConstant, source: &str) -> bool {
|
||||||
|
if constant.value.is_str() {
|
||||||
|
let contents = &source[constant.range()];
|
||||||
|
let prefix = StringPrefix::parse(contents);
|
||||||
|
let quotes =
|
||||||
|
StringQuotes::parse(&contents[TextRange::new(prefix.text_len(), contents.text_len())]);
|
||||||
|
|
||||||
|
quotes.map_or(false, StringQuotes::is_triple) && contents.contains(['\n', '\r'])
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
use crate::comments::{dangling_node_comments, leading_comments};
|
use crate::comments::{dangling_node_comments, leading_comments};
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
|
|
||||||
Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::{format_args, write};
|
use ruff_formatter::{format_args, write};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::Ranged;
|
use rustpython_parser::ast::Ranged;
|
||||||
use rustpython_parser::ast::{Expr, ExprDict};
|
use rustpython_parser::ast::{Expr, ExprDict};
|
||||||
|
@ -99,12 +97,9 @@ impl FormatNodeRule<ExprDict> for FormatExprDict {
|
||||||
impl NeedsParentheses for ExprDict {
|
impl NeedsParentheses for ExprDict {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
OptionalParentheses::Never
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprDictComp;
|
use rustpython_parser::ast::ExprDictComp;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -23,12 +22,9 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
|
||||||
impl NeedsParentheses for ExprDictComp {
|
impl NeedsParentheses for ExprDictComp {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
OptionalParentheses::Never
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprFormattedValue;
|
use rustpython_parser::ast::ExprFormattedValue;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -18,9 +17,9 @@ impl FormatNodeRule<ExprFormattedValue> for FormatExprFormattedValue {
|
||||||
impl NeedsParentheses for ExprFormattedValue {
|
impl NeedsParentheses for ExprFormattedValue {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprGeneratorExp;
|
use rustpython_parser::ast::ExprGeneratorExp;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -23,12 +22,9 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
|
||||||
impl NeedsParentheses for ExprGeneratorExp {
|
impl NeedsParentheses for ExprGeneratorExp {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
OptionalParentheses::Never
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::comments::leading_comments;
|
use crate::comments::leading_comments;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
|
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
|
||||||
Parenthesize,
|
|
||||||
};
|
};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::{format_args, write};
|
use ruff_formatter::{format_args, write};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprIfExp;
|
use rustpython_parser::ast::ExprIfExp;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -46,9 +46,9 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
|
||||||
impl NeedsParentheses for ExprIfExp {
|
impl NeedsParentheses for ExprIfExp {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprJoinedStr;
|
use rustpython_parser::ast::ExprJoinedStr;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -18,9 +17,9 @@ impl FormatNodeRule<ExprJoinedStr> for FormatExprJoinedStr {
|
||||||
impl NeedsParentheses for ExprJoinedStr {
|
impl NeedsParentheses for ExprJoinedStr {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprLambda;
|
use rustpython_parser::ast::ExprLambda;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -23,9 +22,9 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
|
||||||
impl NeedsParentheses for ExprLambda {
|
impl NeedsParentheses for ExprLambda {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
use crate::comments::{dangling_comments, CommentLinePosition};
|
use crate::comments::{dangling_comments, CommentLinePosition};
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
|
|
||||||
Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::{format_args, write};
|
use ruff_formatter::{format_args, write};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::{ExprList, Ranged};
|
use rustpython_parser::ast::{ExprList, Ranged};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -71,12 +69,9 @@ impl FormatNodeRule<ExprList> for FormatExprList {
|
||||||
impl NeedsParentheses for ExprList {
|
impl NeedsParentheses for ExprList {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
OptionalParentheses::Never
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
|
|
||||||
Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::AsFormat;
|
use crate::AsFormat;
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprListComp;
|
use rustpython_parser::ast::ExprListComp;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -44,12 +42,9 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {
|
||||||
impl NeedsParentheses for ExprListComp {
|
impl NeedsParentheses for ExprListComp {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
OptionalParentheses::Never
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::{write, FormatContext};
|
use ruff_formatter::{write, FormatContext};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprName;
|
use rustpython_parser::ast::ExprName;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -28,10 +27,10 @@ impl FormatNodeRule<ExprName> for FormatExprName {
|
||||||
impl NeedsParentheses for ExprName {
|
impl NeedsParentheses for ExprName {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Never
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::prelude::{space, text};
|
use ruff_formatter::prelude::{space, text};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprNamedExpr;
|
use rustpython_parser::ast::ExprNamedExpr;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -33,14 +32,11 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
|
||||||
impl NeedsParentheses for ExprNamedExpr {
|
impl NeedsParentheses for ExprNamedExpr {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
|
||||||
// Unlike tuples, named expression parentheses are not part of the range even when
|
// Unlike tuples, named expression parentheses are not part of the range even when
|
||||||
// mandatory. See [PEP 572](https://peps.python.org/pep-0572/) for details.
|
// mandatory. See [PEP 572](https://peps.python.org/pep-0572/) for details.
|
||||||
Parentheses::Optional => Parentheses::Always,
|
OptionalParentheses::Always
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
|
|
||||||
Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::format_args;
|
use ruff_formatter::format_args;
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprSet;
|
use rustpython_parser::ast::ExprSet;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -29,12 +27,9 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
|
||||||
impl NeedsParentheses for ExprSet {
|
impl NeedsParentheses for ExprSet {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
OptionalParentheses::Never
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprSetComp;
|
use rustpython_parser::ast::ExprSetComp;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -23,12 +22,9 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
|
||||||
impl NeedsParentheses for ExprSetComp {
|
impl NeedsParentheses for ExprSetComp {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
OptionalParentheses::Never
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
use crate::comments::{dangling_comments, SourceComment};
|
use crate::comments::{dangling_comments, SourceComment};
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::trivia::Token;
|
use crate::trivia::Token;
|
||||||
use crate::trivia::{first_non_trivia_token, TokenKind};
|
use crate::trivia::{first_non_trivia_token, TokenKind};
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::prelude::{hard_line_break, line_suffix_boundary, space, text};
|
use ruff_formatter::prelude::{hard_line_break, line_suffix_boundary, space, text};
|
||||||
use ruff_formatter::{write, Buffer, Format, FormatError, FormatResult};
|
use ruff_formatter::{write, Buffer, Format, FormatError, FormatResult};
|
||||||
use ruff_python_ast::node::AstNode;
|
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::ExprSlice;
|
use rustpython_parser::ast::ExprSlice;
|
||||||
use rustpython_parser::ast::{Expr, Ranged};
|
use rustpython_parser::ast::{Expr, Ranged};
|
||||||
|
@ -262,9 +260,9 @@ fn leading_comments_spacing(
|
||||||
impl NeedsParentheses for ExprSlice {
|
impl NeedsParentheses for ExprSlice {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
use rustpython_parser::ast::ExprStarred;
|
use rustpython_parser::ast::ExprStarred;
|
||||||
|
|
||||||
use ruff_formatter::write;
|
use ruff_formatter::write;
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
|
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
|
||||||
|
@ -33,9 +32,9 @@ impl FormatNodeRule<ExprStarred> for FormatExprStarred {
|
||||||
impl NeedsParentheses for ExprStarred {
|
impl NeedsParentheses for ExprStarred {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
use rustpython_parser::ast::{Expr, ExprSubscript};
|
use rustpython_parser::ast::{Expr, ExprSubscript};
|
||||||
|
|
||||||
use ruff_formatter::{format_args, write};
|
use ruff_formatter::{format_args, write};
|
||||||
use ruff_python_ast::node::AstNode;
|
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||||
|
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
use crate::context::NodeLevel;
|
use crate::context::NodeLevel;
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::expr_tuple::TupleParentheses;
|
use crate::expression::expr_tuple::TupleParentheses;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
|
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
|
||||||
Parenthesize,
|
|
||||||
};
|
};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
@ -87,12 +86,11 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
|
||||||
impl NeedsParentheses for ExprSubscript {
|
impl NeedsParentheses for ExprSubscript {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
{
|
||||||
Parentheses::Optional => Parentheses::Never,
|
OptionalParentheses::Never
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
use crate::builders::parenthesize_if_expands;
|
|
||||||
use crate::comments::{dangling_comments, CommentLinePosition};
|
|
||||||
use crate::expression::parentheses::{
|
|
||||||
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
|
|
||||||
Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
|
||||||
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
|
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::ExprTuple;
|
use rustpython_parser::ast::ExprTuple;
|
||||||
use rustpython_parser::ast::{Expr, Ranged};
|
use rustpython_parser::ast::{Expr, Ranged};
|
||||||
|
|
||||||
|
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
|
|
||||||
|
use crate::builders::parenthesize_if_expands;
|
||||||
|
use crate::comments::{dangling_comments, CommentLinePosition};
|
||||||
|
use crate::expression::parentheses::{
|
||||||
|
parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
|
||||||
|
};
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug, Default)]
|
#[derive(Eq, PartialEq, Debug, Default)]
|
||||||
pub enum TupleParentheses {
|
pub enum TupleParentheses {
|
||||||
/// Effectively `None` in `Option<Parentheses>`
|
/// Effectively `None` in `Option<Parentheses>`
|
||||||
|
@ -148,13 +150,10 @@ impl Format<PyFormatContext<'_>> for ExprSequence<'_> {
|
||||||
impl NeedsParentheses for ExprTuple {
|
impl NeedsParentheses for ExprTuple {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
OptionalParentheses::Never
|
||||||
Parentheses::Optional => Parentheses::Never,
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::trivia::{SimpleTokenizer, TokenKind};
|
use crate::trivia::{SimpleTokenizer, TokenKind};
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::prelude::{hard_line_break, space, text};
|
use ruff_formatter::prelude::{hard_line_break, space, text};
|
||||||
use ruff_formatter::{Format, FormatContext, FormatResult};
|
use ruff_formatter::{Format, FormatContext, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_text_size::{TextLen, TextRange};
|
use ruff_text_size::{TextLen, TextRange};
|
||||||
use rustpython_parser::ast::UnaryOp;
|
use rustpython_parser::ast::UnaryOp;
|
||||||
use rustpython_parser::ast::{ExprUnaryOp, Ranged};
|
use rustpython_parser::ast::{ExprUnaryOp, Ranged};
|
||||||
|
@ -70,19 +69,14 @@ impl FormatNodeRule<ExprUnaryOp> for FormatExprUnaryOp {
|
||||||
impl NeedsParentheses for ExprUnaryOp {
|
impl NeedsParentheses for ExprUnaryOp {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
|
||||||
Parentheses::Optional => {
|
|
||||||
// We preserve the parentheses of the operand. It should not be necessary to break this expression.
|
// We preserve the parentheses of the operand. It should not be necessary to break this expression.
|
||||||
if is_operand_parenthesized(self, context.source()) {
|
if is_operand_parenthesized(self, context.source()) {
|
||||||
Parentheses::Never
|
OptionalParentheses::Never
|
||||||
} else {
|
} else {
|
||||||
Parentheses::Optional
|
OptionalParentheses::Multiline
|
||||||
}
|
|
||||||
}
|
|
||||||
parentheses => parentheses,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprYield;
|
use rustpython_parser::ast::ExprYield;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -18,9 +17,9 @@ impl FormatNodeRule<ExprYield> for FormatExprYield {
|
||||||
impl NeedsParentheses for ExprYield {
|
impl NeedsParentheses for ExprYield {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
||||||
};
|
|
||||||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::ExprYieldFrom;
|
use rustpython_parser::ast::ExprYieldFrom;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -18,9 +17,9 @@ impl FormatNodeRule<ExprYieldFrom> for FormatExprYieldFrom {
|
||||||
impl NeedsParentheses for ExprYieldFrom {
|
impl NeedsParentheses for ExprYieldFrom {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
_parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
_context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
default_expression_needs_parentheses(self.into(), parenthesize, context)
|
OptionalParentheses::Multiline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
use rustpython_parser::ast;
|
|
||||||
use rustpython_parser::ast::{Expr, Operator};
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::builders::parenthesize_if_expands;
|
use rustpython_parser::ast;
|
||||||
|
use rustpython_parser::ast::{Expr, Operator};
|
||||||
|
|
||||||
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatRule, FormatRuleWithOptions};
|
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatRule, FormatRuleWithOptions};
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
|
use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
|
||||||
|
|
||||||
|
use crate::builders::parenthesize_if_expands;
|
||||||
use crate::context::NodeLevel;
|
use crate::context::NodeLevel;
|
||||||
use crate::expression::expr_tuple::TupleParentheses;
|
use crate::expression::expr_tuple::TupleParentheses;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
is_expression_parenthesized, optional_parentheses, parenthesized, NeedsParentheses,
|
is_expression_parenthesized, optional_parentheses, parenthesized, NeedsParentheses,
|
||||||
Parentheses, Parenthesize,
|
OptionalParentheses, Parentheses, Parenthesize,
|
||||||
};
|
};
|
||||||
use crate::expression::string::StringLayout;
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub(crate) mod expr_attribute;
|
pub(crate) mod expr_attribute;
|
||||||
|
@ -46,25 +46,25 @@ pub(crate) mod expr_yield_from;
|
||||||
pub(crate) mod parentheses;
|
pub(crate) mod parentheses;
|
||||||
pub(crate) mod string;
|
pub(crate) mod string;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Copy, Clone, PartialEq, Eq, Default)]
|
||||||
pub struct FormatExpr {
|
pub struct FormatExpr {
|
||||||
parenthesize: Parenthesize,
|
parentheses: Parentheses,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormatRuleWithOptions<Expr, PyFormatContext<'_>> for FormatExpr {
|
impl FormatRuleWithOptions<Expr, PyFormatContext<'_>> for FormatExpr {
|
||||||
type Options = Parenthesize;
|
type Options = Parentheses;
|
||||||
|
|
||||||
fn with_options(mut self, options: Self::Options) -> Self {
|
fn with_options(mut self, options: Self::Options) -> Self {
|
||||||
self.parenthesize = options;
|
self.parentheses = options;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
|
impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
|
||||||
fn fmt(&self, item: &Expr, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt(&self, expression: &Expr, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
let parentheses = item.needs_parentheses(self.parenthesize, f.context());
|
let parentheses = self.parentheses;
|
||||||
|
|
||||||
let format_expr = format_with(|f| match item {
|
let format_expr = format_with(|f| match expression {
|
||||||
Expr::BoolOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
|
Expr::BoolOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
|
||||||
Expr::NamedExpr(expr) => expr.format().fmt(f),
|
Expr::NamedExpr(expr) => expr.format().fmt(f),
|
||||||
Expr::BinOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
|
Expr::BinOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
|
||||||
|
@ -84,10 +84,7 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
|
||||||
Expr::Call(expr) => expr.format().fmt(f),
|
Expr::Call(expr) => expr.format().fmt(f),
|
||||||
Expr::FormattedValue(expr) => expr.format().fmt(f),
|
Expr::FormattedValue(expr) => expr.format().fmt(f),
|
||||||
Expr::JoinedStr(expr) => expr.format().fmt(f),
|
Expr::JoinedStr(expr) => expr.format().fmt(f),
|
||||||
Expr::Constant(expr) => expr
|
Expr::Constant(expr) => expr.format().fmt(f),
|
||||||
.format()
|
|
||||||
.with_options(StringLayout::Default(Some(parentheses)))
|
|
||||||
.fmt(f),
|
|
||||||
Expr::Attribute(expr) => expr.format().fmt(f),
|
Expr::Attribute(expr) => expr.format().fmt(f),
|
||||||
Expr::Subscript(expr) => expr.format().fmt(f),
|
Expr::Subscript(expr) => expr.format().fmt(f),
|
||||||
Expr::Starred(expr) => expr.format().fmt(f),
|
Expr::Starred(expr) => expr.format().fmt(f),
|
||||||
|
@ -100,74 +97,136 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
|
||||||
Expr::Slice(expr) => expr.format().fmt(f),
|
Expr::Slice(expr) => expr.format().fmt(f),
|
||||||
});
|
});
|
||||||
|
|
||||||
let result = match parentheses {
|
let parenthesize = match parentheses {
|
||||||
Parentheses::Always => parenthesized("(", &format_expr, ")").fmt(f),
|
Parentheses::Preserve => {
|
||||||
// Add optional parentheses. Ignore if the item renders parentheses itself.
|
is_expression_parenthesized(AnyNodeRef::from(expression), f.context().source())
|
||||||
Parentheses::Optional => {
|
|
||||||
if can_omit_optional_parentheses(item, f.context()) {
|
|
||||||
optional_parentheses(&format_expr).fmt(f)
|
|
||||||
} else {
|
|
||||||
parenthesize_if_expands(&format_expr).fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Parentheses::Custom | Parentheses::Never => {
|
|
||||||
let saved_level = f.context().node_level();
|
|
||||||
|
|
||||||
let new_level = match saved_level {
|
|
||||||
NodeLevel::TopLevel | NodeLevel::CompoundStatement => {
|
|
||||||
NodeLevel::Expression(None)
|
|
||||||
}
|
|
||||||
level @ (NodeLevel::Expression(_) | NodeLevel::ParenthesizedExpression) => {
|
|
||||||
level
|
|
||||||
}
|
}
|
||||||
|
Parentheses::Always => true,
|
||||||
|
Parentheses::Never => false,
|
||||||
};
|
};
|
||||||
|
|
||||||
f.context_mut().set_node_level(new_level);
|
if parenthesize {
|
||||||
|
parenthesized("(", &format_expr, ")").fmt(f)
|
||||||
|
} else {
|
||||||
|
let saved_level = match f.context().node_level() {
|
||||||
|
saved_level @ (NodeLevel::TopLevel | NodeLevel::CompoundStatement) => {
|
||||||
|
f.context_mut().set_node_level(NodeLevel::Expression(None));
|
||||||
|
Some(saved_level)
|
||||||
|
}
|
||||||
|
NodeLevel::Expression(_) | NodeLevel::ParenthesizedExpression => None,
|
||||||
|
};
|
||||||
|
|
||||||
let result = Format::fmt(&format_expr, f);
|
let result = Format::fmt(&format_expr, f);
|
||||||
|
|
||||||
|
if let Some(saved_level) = saved_level {
|
||||||
f.context_mut().set_node_level(saved_level);
|
f.context_mut().set_node_level(saved_level);
|
||||||
result
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wraps an expression in an optional parentheses except if its [`NeedsParentheses::needs_parentheses`] implementation
|
||||||
|
/// indicates that it is okay to omit the parentheses. For example, parentheses can always be omitted for lists,
|
||||||
|
/// because they already bring their own parentheses.
|
||||||
|
pub(crate) fn maybe_parenthesize_expression<'a, T>(
|
||||||
|
expression: &'a Expr,
|
||||||
|
parent: T,
|
||||||
|
parenthesize: Parenthesize,
|
||||||
|
) -> MaybeParenthesizeExpression<'a>
|
||||||
|
where
|
||||||
|
T: Into<AnyNodeRef<'a>>,
|
||||||
|
{
|
||||||
|
MaybeParenthesizeExpression {
|
||||||
|
expression,
|
||||||
|
parent: parent.into(),
|
||||||
|
parenthesize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct MaybeParenthesizeExpression<'a> {
|
||||||
|
expression: &'a Expr,
|
||||||
|
parent: AnyNodeRef<'a>,
|
||||||
|
parenthesize: Parenthesize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
|
||||||
|
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||||
|
let MaybeParenthesizeExpression {
|
||||||
|
expression,
|
||||||
|
parent,
|
||||||
|
parenthesize,
|
||||||
|
} = self;
|
||||||
|
|
||||||
|
let parenthesize = match parenthesize {
|
||||||
|
Parenthesize::Optional => {
|
||||||
|
is_expression_parenthesized(AnyNodeRef::from(*expression), f.context().source())
|
||||||
|
}
|
||||||
|
Parenthesize::IfBreaks => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
let parentheses =
|
||||||
|
if parenthesize || f.context().comments().has_leading_comments(*expression) {
|
||||||
|
OptionalParentheses::Always
|
||||||
|
} else {
|
||||||
|
expression.needs_parentheses(*parent, f.context())
|
||||||
|
};
|
||||||
|
|
||||||
|
match parentheses {
|
||||||
|
OptionalParentheses::Multiline => {
|
||||||
|
if can_omit_optional_parentheses(expression, f.context()) {
|
||||||
|
optional_parentheses(&expression.format().with_options(Parentheses::Never))
|
||||||
|
.fmt(f)
|
||||||
|
} else {
|
||||||
|
parenthesize_if_expands(&expression.format().with_options(Parentheses::Never))
|
||||||
|
.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
OptionalParentheses::Always => {
|
||||||
|
expression.format().with_options(Parentheses::Always).fmt(f)
|
||||||
|
}
|
||||||
|
OptionalParentheses::Never => {
|
||||||
|
expression.format().with_options(Parentheses::Never).fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl NeedsParentheses for Expr {
|
impl NeedsParentheses for Expr {
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
context: &PyFormatContext,
|
||||||
) -> Parentheses {
|
) -> OptionalParentheses {
|
||||||
match self {
|
match self {
|
||||||
Expr::BoolOp(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::BoolOp(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::NamedExpr(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::NamedExpr(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::BinOp(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::BinOp(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::UnaryOp(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::UnaryOp(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Lambda(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Lambda(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::IfExp(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::IfExp(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Dict(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Dict(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Set(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Set(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::ListComp(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::ListComp(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::SetComp(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::SetComp(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::DictComp(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::DictComp(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::GeneratorExp(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::GeneratorExp(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Await(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Await(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Yield(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Yield(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::YieldFrom(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::YieldFrom(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Compare(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Compare(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Call(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Call(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::FormattedValue(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::FormattedValue(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::JoinedStr(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::JoinedStr(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Constant(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Constant(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Attribute(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Attribute(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Subscript(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Subscript(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Starred(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Starred(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Name(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Name(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::List(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::List(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Tuple(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Tuple(expr) => expr.needs_parentheses(parent, context),
|
||||||
Expr::Slice(expr) => expr.needs_parentheses(parenthesize, context),
|
Expr::Slice(expr) => expr.needs_parentheses(parent, context),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,101 +6,50 @@ use ruff_formatter::{format_args, Argument, Arguments};
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use rustpython_parser::ast::Ranged;
|
use rustpython_parser::ast::Ranged;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||||
|
pub(crate) enum OptionalParentheses {
|
||||||
|
/// Add parentheses if the expression expands over multiple lines
|
||||||
|
Multiline,
|
||||||
|
|
||||||
|
/// Always set parentheses regardless if the expression breaks or if they were
|
||||||
|
/// present in the source.
|
||||||
|
Always,
|
||||||
|
|
||||||
|
/// Never add parentheses
|
||||||
|
Never,
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) trait NeedsParentheses {
|
pub(crate) trait NeedsParentheses {
|
||||||
|
/// Determines if this object needs optional parentheses or if it is safe to omit the parentheses.
|
||||||
fn needs_parentheses(
|
fn needs_parentheses(
|
||||||
&self,
|
&self,
|
||||||
parenthesize: Parenthesize,
|
parent: AnyNodeRef,
|
||||||
context: &PyFormatContext,
|
context: &PyFormatContext,
|
||||||
) -> Parentheses;
|
) -> OptionalParentheses;
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn default_expression_needs_parentheses(
|
|
||||||
node: AnyNodeRef,
|
|
||||||
parenthesize: Parenthesize,
|
|
||||||
context: &PyFormatContext,
|
|
||||||
) -> Parentheses {
|
|
||||||
debug_assert!(
|
|
||||||
node.is_expression(),
|
|
||||||
"Should only be called for expressions"
|
|
||||||
);
|
|
||||||
|
|
||||||
#[allow(clippy::if_same_then_else)]
|
|
||||||
if parenthesize.is_always() {
|
|
||||||
Parentheses::Always
|
|
||||||
} else if parenthesize.is_never() {
|
|
||||||
Parentheses::Never
|
|
||||||
}
|
|
||||||
// `Optional` or `Preserve` and expression has parentheses in source code.
|
|
||||||
else if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, context.source()) {
|
|
||||||
Parentheses::Always
|
|
||||||
}
|
|
||||||
// `Optional` or `IfBreaks`: Add parentheses if the expression doesn't fit on a line but enforce
|
|
||||||
// parentheses if the expression has leading comments
|
|
||||||
else if !parenthesize.is_preserve() {
|
|
||||||
if context.comments().has_leading_comments(node) {
|
|
||||||
Parentheses::Always
|
|
||||||
} else {
|
|
||||||
Parentheses::Optional
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//`Preserve` and expression has no parentheses in the source code
|
|
||||||
Parentheses::Never
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configures if the expression should be parenthesized.
|
/// Configures if the expression should be parenthesized.
|
||||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
pub enum Parenthesize {
|
pub(crate) enum Parenthesize {
|
||||||
/// Parenthesize the expression if it has parenthesis in the source.
|
|
||||||
#[default]
|
|
||||||
Preserve,
|
|
||||||
|
|
||||||
/// Parenthesizes the expression if it doesn't fit on a line OR if the expression is parenthesized in the source code.
|
/// Parenthesizes the expression if it doesn't fit on a line OR if the expression is parenthesized in the source code.
|
||||||
Optional,
|
Optional,
|
||||||
|
|
||||||
/// Parenthesizes the expression only if it doesn't fit on a line.
|
/// Parenthesizes the expression only if it doesn't fit on a line.
|
||||||
IfBreaks,
|
IfBreaks,
|
||||||
|
|
||||||
/// Always adds parentheses
|
|
||||||
Always,
|
|
||||||
|
|
||||||
/// Never adds parentheses. Parentheses are handled by the caller.
|
|
||||||
Never,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parenthesize {
|
|
||||||
pub(crate) const fn is_always(self) -> bool {
|
|
||||||
matches!(self, Parenthesize::Always)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) const fn is_never(self) -> bool {
|
|
||||||
matches!(self, Parenthesize::Never)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) const fn is_if_breaks(self) -> bool {
|
|
||||||
matches!(self, Parenthesize::IfBreaks)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) const fn is_preserve(self) -> bool {
|
|
||||||
matches!(self, Parenthesize::Preserve)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether it is necessary to add parentheses around an expression.
|
/// Whether it is necessary to add parentheses around an expression.
|
||||||
/// This is different from [`Parenthesize`] in that it is the resolved representation: It takes into account
|
/// This is different from [`Parenthesize`] in that it is the resolved representation: It takes into account
|
||||||
/// whether there are parentheses in the source code or not.
|
/// whether there are parentheses in the source code or not.
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
|
||||||
pub enum Parentheses {
|
pub enum Parentheses {
|
||||||
|
#[default]
|
||||||
|
Preserve,
|
||||||
|
|
||||||
/// Always set parentheses regardless if the expression breaks or if they were
|
/// Always set parentheses regardless if the expression breaks or if they were
|
||||||
/// present in the source.
|
/// present in the source.
|
||||||
Always,
|
Always,
|
||||||
|
|
||||||
/// Only add parentheses when necessary because the expression breaks over multiple lines.
|
|
||||||
Optional,
|
|
||||||
|
|
||||||
/// Custom handling by the node's formatter implementation
|
|
||||||
Custom,
|
|
||||||
|
|
||||||
/// Never add parentheses
|
/// Never add parentheses
|
||||||
Never,
|
Never,
|
||||||
}
|
}
|
||||||
|
@ -182,20 +131,20 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatParenthesized<'_, 'ast> {
|
||||||
/// a parentheses (`()`, `[]`, `{}`).
|
/// a parentheses (`()`, `[]`, `{}`).
|
||||||
pub(crate) fn optional_parentheses<'content, 'ast, Content>(
|
pub(crate) fn optional_parentheses<'content, 'ast, Content>(
|
||||||
content: &'content Content,
|
content: &'content Content,
|
||||||
) -> OptionalParentheses<'content, 'ast>
|
) -> FormatOptionalParentheses<'content, 'ast>
|
||||||
where
|
where
|
||||||
Content: Format<PyFormatContext<'ast>>,
|
Content: Format<PyFormatContext<'ast>>,
|
||||||
{
|
{
|
||||||
OptionalParentheses {
|
FormatOptionalParentheses {
|
||||||
content: Argument::new(content),
|
content: Argument::new(content),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct OptionalParentheses<'content, 'ast> {
|
pub(crate) struct FormatOptionalParentheses<'content, 'ast> {
|
||||||
content: Argument<'content, PyFormatContext<'ast>>,
|
content: Argument<'content, PyFormatContext<'ast>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast> Format<PyFormatContext<'ast>> for OptionalParentheses<'_, 'ast> {
|
impl<'ast> Format<PyFormatContext<'ast>> for FormatOptionalParentheses<'_, 'ast> {
|
||||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
||||||
let saved_level = f.context().node_level();
|
let saved_level = f.context().node_level();
|
||||||
|
|
||||||
|
|
|
@ -1,41 +1,27 @@
|
||||||
use crate::builders::parenthesize_if_expands;
|
use std::borrow::Cow;
|
||||||
use crate::comments::{leading_comments, trailing_comments};
|
|
||||||
use crate::expression::parentheses::Parentheses;
|
|
||||||
use crate::prelude::*;
|
|
||||||
use crate::QuoteStyle;
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use ruff_formatter::{format_args, write, FormatError};
|
|
||||||
use ruff_python_ast::str::is_implicit_concatenation;
|
|
||||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
use rustpython_parser::ast::{ExprConstant, Ranged};
|
use rustpython_parser::ast::{ExprConstant, Ranged};
|
||||||
use rustpython_parser::lexer::{lex_starts_at, LexicalError, LexicalErrorType};
|
use rustpython_parser::lexer::{lex_starts_at, LexicalError, LexicalErrorType};
|
||||||
use rustpython_parser::{Mode, Tok};
|
use rustpython_parser::{Mode, Tok};
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
use ruff_formatter::{format_args, write, FormatError};
|
||||||
pub enum StringLayout {
|
use ruff_python_ast::str::is_implicit_concatenation;
|
||||||
Default(Option<Parentheses>),
|
|
||||||
|
|
||||||
/// Enforces that implicit continuation strings are printed on a single line even if they exceed
|
use crate::comments::{leading_comments, trailing_comments};
|
||||||
/// the configured line width.
|
use crate::expression::parentheses::in_parentheses_only_group;
|
||||||
Flat,
|
use crate::prelude::*;
|
||||||
}
|
use crate::QuoteStyle;
|
||||||
|
|
||||||
impl Default for StringLayout {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::Default(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) struct FormatString<'a> {
|
pub(super) struct FormatString<'a> {
|
||||||
constant: &'a ExprConstant,
|
constant: &'a ExprConstant,
|
||||||
layout: StringLayout,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FormatString<'a> {
|
impl<'a> FormatString<'a> {
|
||||||
pub(super) fn new(constant: &'a ExprConstant, layout: StringLayout) -> Self {
|
pub(super) fn new(constant: &'a ExprConstant) -> Self {
|
||||||
debug_assert!(constant.value.is_str());
|
debug_assert!(constant.value.is_str());
|
||||||
Self { constant, layout }
|
Self { constant }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,13 +31,7 @@ impl<'a> Format<PyFormatContext<'_>> for FormatString<'a> {
|
||||||
let string_content = f.context().locator().slice(string_range);
|
let string_content = f.context().locator().slice(string_range);
|
||||||
|
|
||||||
if is_implicit_concatenation(string_content) {
|
if is_implicit_concatenation(string_content) {
|
||||||
let format_continuation = FormatStringContinuation::new(self.constant, self.layout);
|
in_parentheses_only_group(&FormatStringContinuation::new(self.constant)).fmt(f)
|
||||||
|
|
||||||
if let StringLayout::Default(Some(Parentheses::Custom)) = self.layout {
|
|
||||||
parenthesize_if_expands(&format_continuation).fmt(f)
|
|
||||||
} else {
|
|
||||||
format_continuation.fmt(f)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
FormatStringPart::new(string_range).fmt(f)
|
FormatStringPart::new(string_range).fmt(f)
|
||||||
}
|
}
|
||||||
|
@ -60,13 +40,12 @@ impl<'a> Format<PyFormatContext<'_>> for FormatString<'a> {
|
||||||
|
|
||||||
struct FormatStringContinuation<'a> {
|
struct FormatStringContinuation<'a> {
|
||||||
constant: &'a ExprConstant,
|
constant: &'a ExprConstant,
|
||||||
layout: StringLayout,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FormatStringContinuation<'a> {
|
impl<'a> FormatStringContinuation<'a> {
|
||||||
fn new(constant: &'a ExprConstant, layout: StringLayout) -> Self {
|
fn new(constant: &'a ExprConstant) -> Self {
|
||||||
debug_assert!(constant.value.is_str());
|
debug_assert!(constant.value.is_str());
|
||||||
Self { constant, layout }
|
Self { constant }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,12 +64,7 @@ impl Format<PyFormatContext<'_>> for FormatStringContinuation<'_> {
|
||||||
// because this is a black preview style.
|
// because this is a black preview style.
|
||||||
let lexer = lex_starts_at(string_content, Mode::Expression, string_range.start());
|
let lexer = lex_starts_at(string_content, Mode::Expression, string_range.start());
|
||||||
|
|
||||||
let separator = format_with(|f| match self.layout {
|
let mut joiner = f.join_with(soft_line_break_or_space());
|
||||||
StringLayout::Default(_) => soft_line_break_or_space().fmt(f),
|
|
||||||
StringLayout::Flat => space().fmt(f),
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut joiner = f.join_with(separator);
|
|
||||||
|
|
||||||
for token in lexer {
|
for token in lexer {
|
||||||
let (token, token_range) = match token {
|
let (token, token_range) = match token {
|
||||||
|
@ -220,7 +194,7 @@ impl Format<PyFormatContext<'_>> for FormatStringPart {
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
struct StringPrefix: u8 {
|
pub(super) struct StringPrefix: u8 {
|
||||||
const UNICODE = 0b0000_0001;
|
const UNICODE = 0b0000_0001;
|
||||||
/// `r"test"`
|
/// `r"test"`
|
||||||
const RAW = 0b0000_0010;
|
const RAW = 0b0000_0010;
|
||||||
|
@ -232,7 +206,7 @@ bitflags! {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StringPrefix {
|
impl StringPrefix {
|
||||||
fn parse(input: &str) -> StringPrefix {
|
pub(super) fn parse(input: &str) -> StringPrefix {
|
||||||
let chars = input.chars();
|
let chars = input.chars();
|
||||||
let mut prefix = StringPrefix::empty();
|
let mut prefix = StringPrefix::empty();
|
||||||
|
|
||||||
|
@ -257,7 +231,7 @@ impl StringPrefix {
|
||||||
prefix
|
prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
const fn text_len(self) -> TextSize {
|
pub(super) const fn text_len(self) -> TextSize {
|
||||||
TextSize::new(self.bits().count_ones())
|
TextSize::new(self.bits().count_ones())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -383,13 +357,13 @@ fn preferred_quotes(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
struct StringQuotes {
|
pub(super) struct StringQuotes {
|
||||||
triple: bool,
|
triple: bool,
|
||||||
style: QuoteStyle,
|
style: QuoteStyle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StringQuotes {
|
impl StringQuotes {
|
||||||
fn parse(input: &str) -> Option<StringQuotes> {
|
pub(super) fn parse(input: &str) -> Option<StringQuotes> {
|
||||||
let mut chars = input.chars();
|
let mut chars = input.chars();
|
||||||
|
|
||||||
let quote_char = chars.next()?;
|
let quote_char = chars.next()?;
|
||||||
|
@ -400,6 +374,10 @@ impl StringQuotes {
|
||||||
Some(Self { triple, style })
|
Some(Self { triple, style })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) const fn is_triple(self) -> bool {
|
||||||
|
self.triple
|
||||||
|
}
|
||||||
|
|
||||||
const fn text_len(self) -> TextSize {
|
const fn text_len(self) -> TextSize {
|
||||||
if self.triple {
|
if self.triple {
|
||||||
TextSize::new(3)
|
TextSize::new(3)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
@ -18,7 +19,7 @@ impl FormatNodeRule<Decorator> for FormatDecorator {
|
||||||
f,
|
f,
|
||||||
[
|
[
|
||||||
text("@"),
|
text("@"),
|
||||||
expression.format().with_options(Parenthesize::Optional)
|
maybe_parenthesize_expression(expression, item, Parenthesize::Optional)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
|
@ -60,7 +61,10 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
|
||||||
if let Some(type_) = type_ {
|
if let Some(type_) = type_ {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[space(), type_.format().with_options(Parenthesize::IfBreaks)]
|
[
|
||||||
|
space(),
|
||||||
|
maybe_parenthesize_expression(type_, item, Parenthesize::IfBreaks)
|
||||||
|
]
|
||||||
)?;
|
)?;
|
||||||
if let Some(name) = name {
|
if let Some(name) = name {
|
||||||
write!(f, [space(), text("as"), space(), name.format()])?;
|
write!(f, [space(), text("as"), space(), name.format()])?;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
|
@ -18,7 +19,11 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
|
||||||
let inner = format_with(|f| {
|
let inner = format_with(|f| {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[context_expr.format().with_options(Parenthesize::IfBreaks)]
|
[maybe_parenthesize_expression(
|
||||||
|
context_expr,
|
||||||
|
item,
|
||||||
|
Parenthesize::IfBreaks
|
||||||
|
)]
|
||||||
)?;
|
)?;
|
||||||
if let Some(optional_vars) = optional_vars {
|
if let Some(optional_vars) = optional_vars {
|
||||||
write!(f, [space(), text("as"), space(), optional_vars.format()])?;
|
write!(f, [space(), text("as"), space(), optional_vars.format()])?;
|
||||||
|
|
|
@ -2,6 +2,7 @@ use rustpython_parser::ast::StmtAssign;
|
||||||
|
|
||||||
use ruff_formatter::write;
|
use ruff_formatter::write;
|
||||||
|
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
@ -26,6 +27,13 @@ impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
|
||||||
write!(f, [target.format(), space(), text("="), space()])?;
|
write!(f, [target.format(), space(), text("="), space()])?;
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(f, [value.format().with_options(Parenthesize::IfBreaks)])
|
write!(
|
||||||
|
f,
|
||||||
|
[maybe_parenthesize_expression(
|
||||||
|
value,
|
||||||
|
item,
|
||||||
|
Parenthesize::IfBreaks
|
||||||
|
)]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
use rustpython_parser::ast::StmtAsyncFunctionDef;
|
||||||
|
|
||||||
|
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
|
||||||
use rustpython_parser::ast::StmtAsyncFunctionDef;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FormatStmtAsyncFunctionDef;
|
pub struct FormatStmtAsyncFunctionDef;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::prelude::{space, text};
|
use ruff_formatter::prelude::{space, text};
|
||||||
|
@ -23,7 +24,7 @@ impl FormatNodeRule<StmtAugAssign> for FormatStmtAugAssign {
|
||||||
op.format(),
|
op.format(),
|
||||||
text("="),
|
text("="),
|
||||||
space(),
|
space(),
|
||||||
value.format().with_options(Parenthesize::IfBreaks)
|
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
|
||||||
|
use crate::expression::parentheses::Parentheses;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::trivia::{SimpleTokenizer, TokenKind};
|
use crate::trivia::{SimpleTokenizer, TokenKind};
|
||||||
use ruff_formatter::{format_args, write};
|
use ruff_formatter::{format_args, write};
|
||||||
|
@ -100,13 +101,13 @@ impl Format<PyFormatContext<'_>> for FormatInheritanceClause<'_> {
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
// Ignore the first parentheses count
|
// Ignore the first parentheses count
|
||||||
let parenthesize = if left_paren_count > 1 {
|
let parentheses = if left_paren_count > 1 {
|
||||||
Parenthesize::Always
|
Parentheses::Always
|
||||||
} else {
|
} else {
|
||||||
Parenthesize::Never
|
Parentheses::Never
|
||||||
};
|
};
|
||||||
|
|
||||||
joiner.entry(first, &first.format().with_options(parenthesize));
|
joiner.entry(first, &first.format().with_options(parentheses));
|
||||||
joiner.nodes(rest.iter());
|
joiner.nodes(rest.iter());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
|
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
|
||||||
use crate::comments::dangling_node_comments;
|
use crate::comments::dangling_node_comments;
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::prelude::{block_indent, format_with, space, text};
|
use ruff_formatter::prelude::{block_indent, format_with, space, text};
|
||||||
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
||||||
use rustpython_parser::ast::{Ranged, StmtDelete};
|
use rustpython_parser::ast::{Ranged, StmtDelete};
|
||||||
|
@ -32,7 +33,14 @@ impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
[single] => {
|
[single] => {
|
||||||
write!(f, [single.format().with_options(Parenthesize::IfBreaks)])
|
write!(
|
||||||
|
f,
|
||||||
|
[maybe_parenthesize_expression(
|
||||||
|
single,
|
||||||
|
item,
|
||||||
|
Parenthesize::IfBreaks
|
||||||
|
)]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
targets => {
|
targets => {
|
||||||
let item = format_with(|f| {
|
let item = format_with(|f| {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use crate::expression::parentheses::{is_expression_parenthesized, Parenthesize};
|
use rustpython_parser::ast::StmtExpr;
|
||||||
use crate::expression::string::StringLayout;
|
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use rustpython_parser::ast::StmtExpr;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FormatStmtExpr;
|
pub struct FormatStmtExpr;
|
||||||
|
@ -11,14 +12,6 @@ impl FormatNodeRule<StmtExpr> for FormatStmtExpr {
|
||||||
fn fmt_fields(&self, item: &StmtExpr, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_fields(&self, item: &StmtExpr, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
let StmtExpr { value, .. } = item;
|
let StmtExpr { value, .. } = item;
|
||||||
|
|
||||||
if let Some(constant) = value.as_constant_expr() {
|
maybe_parenthesize_expression(value, item, Parenthesize::Optional).fmt(f)
|
||||||
if constant.value.is_str()
|
|
||||||
&& !is_expression_parenthesized(value.as_ref().into(), f.context().source())
|
|
||||||
{
|
|
||||||
return constant.format().with_options(StringLayout::Flat).fmt(f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
value.format().with_options(Parenthesize::Optional).fmt(f)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||||
use crate::expression::expr_tuple::TupleParentheses;
|
use crate::expression::expr_tuple::TupleParentheses;
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
|
@ -17,7 +18,7 @@ impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
|
||||||
.format()
|
.format()
|
||||||
.with_options(TupleParentheses::StripInsideForLoop)
|
.with_options(TupleParentheses::StripInsideForLoop)
|
||||||
.fmt(f),
|
.fmt(f),
|
||||||
other => other.format().with_options(Parenthesize::IfBreaks).fmt(f),
|
other => maybe_parenthesize_expression(other, self.0, Parenthesize::IfBreaks).fmt(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,7 +55,7 @@ impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
||||||
space(),
|
space(),
|
||||||
text("in"),
|
text("in"),
|
||||||
space(),
|
space(),
|
||||||
iter.format().with_options(Parenthesize::IfBreaks),
|
maybe_parenthesize_expression(iter, item, Parenthesize::IfBreaks),
|
||||||
text(":"),
|
text(":"),
|
||||||
trailing_comments(trailing_condition_comments),
|
trailing_comments(trailing_condition_comments),
|
||||||
block_indent(&body.format())
|
block_indent(&body.format())
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
|
use rustpython_parser::ast::{Ranged, StmtFunctionDef};
|
||||||
|
|
||||||
|
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
|
||||||
|
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||||
|
|
||||||
use crate::comments::{leading_comments, trailing_comments};
|
use crate::comments::{leading_comments, trailing_comments};
|
||||||
use crate::context::NodeLevel;
|
use crate::context::NodeLevel;
|
||||||
use crate::expression::parentheses::{optional_parentheses, Parenthesize};
|
use crate::expression::parentheses::{optional_parentheses, Parentheses};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::trivia::{lines_after, skip_trailing_trivia};
|
use crate::trivia::{lines_after, skip_trailing_trivia};
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
|
|
||||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
|
||||||
use rustpython_parser::ast::{Ranged, StmtFunctionDef};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FormatStmtFunctionDef;
|
pub struct FormatStmtFunctionDef;
|
||||||
|
@ -98,7 +100,7 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
||||||
text("->"),
|
text("->"),
|
||||||
space(),
|
space(),
|
||||||
optional_parentheses(
|
optional_parentheses(
|
||||||
&return_annotation.format().with_options(Parenthesize::Never)
|
&return_annotation.format().with_options(Parentheses::Never)
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)?;
|
)?;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
|
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
@ -48,7 +49,7 @@ impl FormatNodeRule<StmtIf> for FormatStmtIf {
|
||||||
[
|
[
|
||||||
text(current.keyword()),
|
text(current.keyword()),
|
||||||
space(),
|
space(),
|
||||||
test.format().with_options(Parenthesize::IfBreaks),
|
maybe_parenthesize_expression(test, current_statement, Parenthesize::IfBreaks),
|
||||||
text(":"),
|
text(":"),
|
||||||
trailing_comments(if_trailing_comments),
|
trailing_comments(if_trailing_comments),
|
||||||
block_indent(&body.format())
|
block_indent(&body.format())
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::prelude::{space, text};
|
use ruff_formatter::prelude::{space, text};
|
||||||
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
||||||
|
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use rustpython_parser::ast::StmtRaise;
|
use rustpython_parser::ast::StmtRaise;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -21,7 +22,10 @@ impl FormatNodeRule<StmtRaise> for FormatStmtRaise {
|
||||||
if let Some(value) = exc {
|
if let Some(value) = exc {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[space(), value.format().with_options(Parenthesize::Optional)]
|
[
|
||||||
|
space(),
|
||||||
|
maybe_parenthesize_expression(value, item, Parenthesize::Optional)
|
||||||
|
]
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +36,7 @@ impl FormatNodeRule<StmtRaise> for FormatStmtRaise {
|
||||||
space(),
|
space(),
|
||||||
text("from"),
|
text("from"),
|
||||||
space(),
|
space(),
|
||||||
value.format().with_options(Parenthesize::Optional)
|
maybe_parenthesize_expression(value, item, Parenthesize::Optional)
|
||||||
]
|
]
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::prelude::{space, text};
|
use ruff_formatter::prelude::{space, text};
|
||||||
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
||||||
use rustpython_parser::ast::StmtReturn;
|
use rustpython_parser::ast::StmtReturn;
|
||||||
|
@ -16,7 +17,7 @@ impl FormatNodeRule<StmtReturn> for FormatStmtReturn {
|
||||||
[
|
[
|
||||||
text("return"),
|
text("return"),
|
||||||
space(),
|
space(),
|
||||||
value.format().with_options(Parenthesize::IfBreaks)
|
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||||
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
@ -33,7 +34,7 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
|
||||||
[
|
[
|
||||||
text("while"),
|
text("while"),
|
||||||
space(),
|
space(),
|
||||||
test.format().with_options(Parenthesize::IfBreaks),
|
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
|
||||||
text(":"),
|
text(":"),
|
||||||
trailing_comments(trailing_condition_comments),
|
trailing_comments(trailing_condition_comments),
|
||||||
block_indent(&body.format())
|
block_indent(&body.format())
|
||||||
|
|
|
@ -304,17 +304,6 @@ long_unmergable_string_with_pragma = (
|
||||||
```diff
|
```diff
|
||||||
--- Black
|
--- Black
|
||||||
+++ Ruff
|
+++ Ruff
|
||||||
@@ -70,8 +70,8 @@
|
|
||||||
bad_split3 = (
|
|
||||||
"What if we have inline comments on " # First Comment
|
|
||||||
"each line of a bad split? In that " # Second Comment
|
|
||||||
- "case, we should just leave it alone." # Third Comment
|
|
||||||
-)
|
|
||||||
+ "case, we should just leave it alone."
|
|
||||||
+) # Third Comment
|
|
||||||
|
|
||||||
bad_split_func1(
|
|
||||||
"But what should happen when code has already "
|
|
||||||
@@ -143,9 +143,9 @@
|
@@ -143,9 +143,9 @@
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -458,8 +447,8 @@ bad_split2 = (
|
||||||
bad_split3 = (
|
bad_split3 = (
|
||||||
"What if we have inline comments on " # First Comment
|
"What if we have inline comments on " # First Comment
|
||||||
"each line of a bad split? In that " # Second Comment
|
"each line of a bad split? In that " # Second Comment
|
||||||
"case, we should just leave it alone."
|
"case, we should just leave it alone." # Third Comment
|
||||||
) # Third Comment
|
)
|
||||||
|
|
||||||
bad_split_func1(
|
bad_split_func1(
|
||||||
"But what should happen when code has already "
|
"But what should happen when code has already "
|
||||||
|
|
|
@ -122,11 +122,10 @@ def foo3(list_a, list_b):
|
||||||
- .order_by(User.created_at.desc())
|
- .order_by(User.created_at.desc())
|
||||||
- .with_for_update(key_share=True)
|
- .with_for_update(key_share=True)
|
||||||
- .all()
|
- .all()
|
||||||
- )
|
+ filter(db.not_(User.is_pending.astext.cast(db.Boolean).is_(True))).order_by(
|
||||||
+ filter
|
|
||||||
+ )(db.not_(User.is_pending.astext.cast(db.Boolean).is_(True))).order_by(
|
|
||||||
+ User.created_at.desc()
|
+ User.created_at.desc()
|
||||||
+ ).with_for_update(key_share=True).all()
|
+ ).with_for_update(key_share=True).all()
|
||||||
|
)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
@ -224,10 +223,10 @@ def foo(list_a, list_b):
|
||||||
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
|
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
|
||||||
).filter(User.xyz.is_(None)).
|
).filter(User.xyz.is_(None)).
|
||||||
# Another comment about the filtering on is_quux goes here.
|
# Another comment about the filtering on is_quux goes here.
|
||||||
filter
|
filter(db.not_(User.is_pending.astext.cast(db.Boolean).is_(True))).order_by(
|
||||||
)(db.not_(User.is_pending.astext.cast(db.Boolean).is_(True))).order_by(
|
|
||||||
User.created_at.desc()
|
User.created_at.desc()
|
||||||
).with_for_update(key_share=True).all()
|
).with_for_update(key_share=True).all()
|
||||||
|
)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -398,10 +398,11 @@ last_call()
|
||||||
(*starred,)
|
(*starred,)
|
||||||
{
|
{
|
||||||
"id": "1",
|
"id": "1",
|
||||||
@@ -208,24 +208,14 @@
|
@@ -207,25 +207,15 @@
|
||||||
|
)
|
||||||
what_is_up_with_those_new_coord_names = (coord_names | set(vars_to_create)) - set(
|
what_is_up_with_those_new_coord_names = (coord_names | set(vars_to_create)) - set(
|
||||||
vars_to_remove
|
vars_to_remove
|
||||||
)
|
-)
|
||||||
-result = (
|
-result = (
|
||||||
- session.query(models.Customer.id)
|
- session.query(models.Customer.id)
|
||||||
- .filter(
|
- .filter(
|
||||||
|
@ -409,7 +410,7 @@ last_call()
|
||||||
- )
|
- )
|
||||||
- .order_by(models.Customer.id.asc())
|
- .order_by(models.Customer.id.asc())
|
||||||
- .all()
|
- .all()
|
||||||
-)
|
)
|
||||||
-result = (
|
-result = (
|
||||||
- session.query(models.Customer.id)
|
- session.query(models.Customer.id)
|
||||||
- .filter(
|
- .filter(
|
||||||
|
@ -446,7 +447,7 @@ last_call()
|
||||||
|
|
||||||
|
|
||||||
async def f():
|
async def f():
|
||||||
@@ -248,18 +238,22 @@
|
@@ -248,18 +238,20 @@
|
||||||
|
|
||||||
|
|
||||||
print(*[] or [1])
|
print(*[] or [1])
|
||||||
|
@ -471,13 +472,11 @@ last_call()
|
||||||
for y in ():
|
for y in ():
|
||||||
...
|
...
|
||||||
-for z in (i for i in (1, 2, 3)):
|
-for z in (i for i in (1, 2, 3)):
|
||||||
+for (
|
+for z in (NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []):
|
||||||
+ z
|
|
||||||
+) in (NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []):
|
|
||||||
...
|
...
|
||||||
for i in call():
|
for i in call():
|
||||||
...
|
...
|
||||||
@@ -328,13 +322,18 @@
|
@@ -328,13 +320,18 @@
|
||||||
):
|
):
|
||||||
return True
|
return True
|
||||||
if (
|
if (
|
||||||
|
@ -499,7 +498,7 @@ last_call()
|
||||||
^ aaaaaaaa.i << aaaaaaaa.k >> aaaaaaaa.l**aaaaaaaa.m // aaaaaaaa.n
|
^ aaaaaaaa.i << aaaaaaaa.k >> aaaaaaaa.l**aaaaaaaa.m // aaaaaaaa.n
|
||||||
):
|
):
|
||||||
return True
|
return True
|
||||||
@@ -342,7 +341,8 @@
|
@@ -342,7 +339,8 @@
|
||||||
~aaaaaaaaaaaaaaaa.a
|
~aaaaaaaaaaaaaaaa.a
|
||||||
+ aaaaaaaaaaaaaaaa.b
|
+ aaaaaaaaaaaaaaaa.b
|
||||||
- aaaaaaaaaaaaaaaa.c * aaaaaaaaaaaaaaaa.d @ aaaaaaaaaaaaaaaa.e
|
- aaaaaaaaaaaaaaaa.c * aaaaaaaaaaaaaaaa.d @ aaaaaaaaaaaaaaaa.e
|
||||||
|
@ -767,9 +766,7 @@ for (x,) in (1,), (2,), (3,):
|
||||||
...
|
...
|
||||||
for y in ():
|
for y in ():
|
||||||
...
|
...
|
||||||
for (
|
for z in (NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []):
|
||||||
z
|
|
||||||
) in (NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []):
|
|
||||||
...
|
...
|
||||||
for i in call():
|
for i in call():
|
||||||
...
|
...
|
||||||
|
|
|
@ -96,18 +96,21 @@ elif unformatted:
|
||||||
```diff
|
```diff
|
||||||
--- Black
|
--- Black
|
||||||
+++ Ruff
|
+++ Ruff
|
||||||
@@ -5,8 +5,8 @@
|
@@ -3,10 +3,9 @@
|
||||||
|
entry_points={
|
||||||
|
# fmt: off
|
||||||
"console_scripts": [
|
"console_scripts": [
|
||||||
"foo-bar"
|
- "foo-bar"
|
||||||
"=foo.bar.:main",
|
- "=foo.bar.:main",
|
||||||
- # fmt: on
|
- # fmt: on
|
||||||
- ] # Includes an formatted indentation.
|
- ] # Includes an formatted indentation.
|
||||||
|
+ "foo-bar" "=foo.bar.:main",
|
||||||
+ # fmt: on
|
+ # fmt: on
|
||||||
+ ] # Includes an formatted indentation.
|
+ ] # Includes an formatted indentation.
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@
|
@@ -27,7 +26,7 @@
|
||||||
# Regression test for https://github.com/psf/black/issues/3026.
|
# Regression test for https://github.com/psf/black/issues/3026.
|
||||||
def test_func():
|
def test_func():
|
||||||
# yapf: disable
|
# yapf: disable
|
||||||
|
@ -116,7 +119,7 @@ elif unformatted:
|
||||||
return True
|
return True
|
||||||
# yapf: enable
|
# yapf: enable
|
||||||
elif b:
|
elif b:
|
||||||
@@ -39,10 +39,10 @@
|
@@ -39,10 +38,10 @@
|
||||||
# Regression test for https://github.com/psf/black/issues/2567.
|
# Regression test for https://github.com/psf/black/issues/2567.
|
||||||
if True:
|
if True:
|
||||||
# fmt: off
|
# fmt: off
|
||||||
|
@ -131,7 +134,7 @@ elif unformatted:
|
||||||
else:
|
else:
|
||||||
print("This will be formatted")
|
print("This will be formatted")
|
||||||
|
|
||||||
@@ -52,14 +52,12 @@
|
@@ -52,14 +51,12 @@
|
||||||
async def call(param):
|
async def call(param):
|
||||||
if param:
|
if param:
|
||||||
# fmt: off
|
# fmt: off
|
||||||
|
@ -149,7 +152,7 @@ elif unformatted:
|
||||||
|
|
||||||
print("This will be formatted")
|
print("This will be formatted")
|
||||||
|
|
||||||
@@ -68,13 +66,13 @@
|
@@ -68,13 +65,13 @@
|
||||||
class Named(t.Protocol):
|
class Named(t.Protocol):
|
||||||
# fmt: off
|
# fmt: off
|
||||||
@property
|
@property
|
||||||
|
@ -165,7 +168,7 @@ elif unformatted:
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
|
|
||||||
@@ -82,6 +80,6 @@
|
@@ -82,6 +79,6 @@
|
||||||
if x:
|
if x:
|
||||||
return x
|
return x
|
||||||
# fmt: off
|
# fmt: off
|
||||||
|
@ -183,8 +186,7 @@ setup(
|
||||||
entry_points={
|
entry_points={
|
||||||
# fmt: off
|
# fmt: off
|
||||||
"console_scripts": [
|
"console_scripts": [
|
||||||
"foo-bar"
|
"foo-bar" "=foo.bar.:main",
|
||||||
"=foo.bar.:main",
|
|
||||||
# fmt: on
|
# fmt: on
|
||||||
] # Includes an formatted indentation.
|
] # Includes an formatted indentation.
|
||||||
},
|
},
|
||||||
|
|
|
@ -476,9 +476,9 @@ if (
|
||||||
|
|
||||||
|
|
||||||
# Unstable formatting in https://github.com/realtyem/synapse-unraid/blob/unraid_develop/synapse/handlers/presence.py
|
# Unstable formatting in https://github.com/realtyem/synapse-unraid/blob/unraid_develop/synapse/handlers/presence.py
|
||||||
for user_id in (
|
for user_id in set(
|
||||||
set(target_user_ids) - {NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
|
target_user_ids
|
||||||
):
|
) - {NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}:
|
||||||
updates.append(UserPresenceState.default(user_id))
|
updates.append(UserPresenceState.default(user_id))
|
||||||
|
|
||||||
# Keeps parenthesized left hand sides
|
# Keeps parenthesized left hand sides
|
||||||
|
|
|
@ -203,7 +203,20 @@ String \"\"\"
|
||||||
|
|
||||||
"Let's" "start" "with" "a" "simple" "example"
|
"Let's" "start" "with" "a" "simple" "example"
|
||||||
|
|
||||||
"Let's" "start" "with" "a" "simple" "example" "now repeat after me:" "I am confident" "I am confident" "I am confident" "I am confident" "I am confident"
|
(
|
||||||
|
"Let's"
|
||||||
|
"start"
|
||||||
|
"with"
|
||||||
|
"a"
|
||||||
|
"simple"
|
||||||
|
"example"
|
||||||
|
"now repeat after me:"
|
||||||
|
"I am confident"
|
||||||
|
"I am confident"
|
||||||
|
"I am confident"
|
||||||
|
"I am confident"
|
||||||
|
"I am confident"
|
||||||
|
)
|
||||||
|
|
||||||
(
|
(
|
||||||
"Let's"
|
"Let's"
|
||||||
|
@ -351,7 +364,20 @@ String \"\"\"
|
||||||
|
|
||||||
"Let's" 'start' 'with' 'a' 'simple' 'example'
|
"Let's" 'start' 'with' 'a' 'simple' 'example'
|
||||||
|
|
||||||
"Let's" 'start' 'with' 'a' 'simple' 'example' 'now repeat after me:' 'I am confident' 'I am confident' 'I am confident' 'I am confident' 'I am confident'
|
(
|
||||||
|
"Let's"
|
||||||
|
'start'
|
||||||
|
'with'
|
||||||
|
'a'
|
||||||
|
'simple'
|
||||||
|
'example'
|
||||||
|
'now repeat after me:'
|
||||||
|
'I am confident'
|
||||||
|
'I am confident'
|
||||||
|
'I am confident'
|
||||||
|
'I am confident'
|
||||||
|
'I am confident'
|
||||||
|
)
|
||||||
|
|
||||||
(
|
(
|
||||||
"Let's"
|
"Let's"
|
||||||
|
|
|
@ -55,11 +55,7 @@ else: # trailing else comment
|
||||||
# trailing else body comment
|
# trailing else body comment
|
||||||
|
|
||||||
|
|
||||||
for (
|
for aVeryLongNameThatSpillsOverToTheNextLineBecauseItIsExtremelyLongAndGoesOnAndOnAndOnAndOnAndOnAndOnAndOnAndOnAndOn in anotherVeryLongNameThatSpillsOverToTheNextLineBecauseItIsExtremelyLongAndGoesOnAndOnAndOnAndOnAndOnAndOnAndOnAndOnAndOn: # trailing comment
|
||||||
aVeryLongNameThatSpillsOverToTheNextLineBecauseItIsExtremelyLongAndGoesOnAndOnAndOnAndOnAndOnAndOnAndOnAndOnAndOn
|
|
||||||
) in (
|
|
||||||
anotherVeryLongNameThatSpillsOverToTheNextLineBecauseItIsExtremelyLongAndGoesOnAndOnAndOnAndOnAndOnAndOnAndOnAndOnAndOn
|
|
||||||
): # trailing comment
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -105,9 +105,7 @@ raise a from OsError(
|
||||||
)
|
)
|
||||||
|
|
||||||
# some comment
|
# some comment
|
||||||
raise a from (
|
raise a from aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa # some comment
|
||||||
aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa
|
|
||||||
) # some comment
|
|
||||||
# some comment
|
# some comment
|
||||||
|
|
||||||
raise OsError(
|
raise OsError(
|
||||||
|
|
|
@ -51,9 +51,7 @@ else: # trailing else comment
|
||||||
# trailing else body comment
|
# trailing else body comment
|
||||||
|
|
||||||
|
|
||||||
while (
|
while aVeryLongConditionThatSpillsOverToTheNextLineBecauseItIsExtremelyLongAndGoesOnAndOnAndOnAndOnAndOnAndOnAndOnAndOnAndOn: # trailing comment
|
||||||
aVeryLongConditionThatSpillsOverToTheNextLineBecauseItIsExtremelyLongAndGoesOnAndOnAndOnAndOnAndOnAndOnAndOnAndOnAndOn
|
|
||||||
): # trailing comment
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -82,18 +82,14 @@ with (
|
||||||
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
(
|
a, # a # comma
|
||||||
a # a # comma
|
|
||||||
),
|
|
||||||
b, # c
|
b, # c
|
||||||
): # colon
|
): # colon
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
(
|
a as b, # a # as # b # comma
|
||||||
a # a # as
|
|
||||||
) as b, # b # comma
|
|
||||||
c, # c
|
c, # c
|
||||||
): # colon
|
): # colon
|
||||||
... # body
|
... # body
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue