Skip to content

Commit

Permalink
Call chain formatting in fluent style
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Aug 3, 2023
1 parent 5fb1550 commit 58fc27c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
20 changes: 10 additions & 10 deletions crates/ruff_python_formatter/src/expression/expr_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{Constant, Expr, ExprAttribute, ExprConstant};

use crate::comments::{leading_comments, trailing_comments};
use crate::expression::parentheses::{
NeedsParentheses, OptionalParentheses, Parentheses,
};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
use crate::prelude::*;
use crate::FormatNodeRule;

Expand Down Expand Up @@ -49,23 +47,25 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {

if needs_parentheses {
value.format().with_options(Parentheses::Always).fmt(f)?;
} else if self.fluent_style {
} else {
match value.as_ref() {
Expr::Attribute(expr) => expr.format().with_options(self.fluent_style).fmt(f)?,
Expr::Call(expr) => {
expr.format().with_options(self.fluent_style).fmt(f)?;
// Format the dot on its own line
soft_line_break().fmt(f)?;
if self.fluent_style {
// Format the dot on its own line
soft_line_break().fmt(f)?;
}
}
Expr::Subscript(expr) => {
expr.format().with_options(self.fluent_style).fmt(f)?;
// Format the dot on its own line
soft_line_break().fmt(f)?;
if self.fluent_style {
// Format the dot on its own line
soft_line_break().fmt(f)?;
}
}
_ => value.format().fmt(f)?,
}
} else {
value.format().fmt(f)?;
}

if comments.has_trailing_own_line_comments(value.as_ref()) {
Expand Down
14 changes: 5 additions & 9 deletions crates/ruff_python_formatter/src/expression/expr_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ impl FormatNodeRule<ExprCall> for FormatExprCall {
arguments,
} = item;

if self.fluent_style {
match func.as_ref() {
Expr::Attribute(expr) => expr.format().with_options(self.fluent_style).fmt(f)?,
Expr::Call(expr) => expr.format().with_options(self.fluent_style).fmt(f)?,
Expr::Subscript(expr) => expr.format().with_options(self.fluent_style).fmt(f)?,
_ => func.format().fmt(f)?,
}
} else {
func.format().fmt(f)?;
match func.as_ref() {
Expr::Attribute(expr) => expr.format().with_options(self.fluent_style).fmt(f)?,
Expr::Call(expr) => expr.format().with_options(self.fluent_style).fmt(f)?,
Expr::Subscript(expr) => expr.format().with_options(self.fluent_style).fmt(f)?,
_ => func.format().fmt(f)?,
}

write!(f, [arguments.format()])
Expand Down
16 changes: 5 additions & 11 deletions crates/ruff_python_formatter/src/expression/expr_subscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
"A subscript expression can only have a single dangling comment, the one after the bracket"
);

let format_value = format_with(|f| {
if self.fluent_style {
match value.as_ref() {
Expr::Attribute(expr) => expr.format().with_options(self.fluent_style).fmt(f),
Expr::Call(expr) => expr.format().with_options(self.fluent_style).fmt(f),
Expr::Subscript(expr) => expr.format().with_options(self.fluent_style).fmt(f),
_ => value.format().fmt(f),
}
} else {
value.format().fmt(f)
}
let format_value = format_with(|f| match value.as_ref() {
Expr::Attribute(expr) => expr.format().with_options(self.fluent_style).fmt(f),
Expr::Call(expr) => expr.format().with_options(self.fluent_style).fmt(f),
Expr::Subscript(expr) => expr.format().with_options(self.fluent_style).fmt(f),
_ => value.format().fmt(f),
});

if let NodeLevel::Expression(Some(_)) = f.context().node_level() {
Expand Down

0 comments on commit 58fc27c

Please sign in to comment.