Skip to content

Commit

Permalink
Extract format_call_chain_layout function
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Aug 4, 2023
1 parent 9a2ef45 commit 9d7bfff
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 36 deletions.
15 changes: 3 additions & 12 deletions crates/ruff_python_formatter/src/expression/expr_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ 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::CallChainLayout;
use crate::expression::{format_call_chain_layout, CallChainLayout};
use crate::prelude::*;
use crate::FormatNodeRule;
use crate::{FormatNodeRule};

#[derive(Default)]
pub struct FormatExprAttribute {
Expand All @@ -31,16 +31,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
ctx: _,
} = item;

let call_chain_layout = match self.call_chain_layout {
CallChainLayout::Default => {
if f.context().node_level().is_parenthesized() {
CallChainLayout::from_expression(AnyNodeRef::from(item), f.context().source())
} else {
CallChainLayout::NonFluent
}
}
layout @ (CallChainLayout::Fluent | CallChainLayout::NonFluent) => layout,
};
let call_chain_layout = format_call_chain_layout(f, self.call_chain_layout, item);

let needs_parentheses = matches!(
value.as_ref(),
Expand Down
16 changes: 3 additions & 13 deletions crates/ruff_python_formatter/src/expression/expr_call.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::expression::CallChainLayout;
use ruff_formatter::{write, FormatRuleWithOptions};
use crate::expression::{format_call_chain_layout, CallChainLayout};
use ruff_formatter::FormatRuleWithOptions;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::Expr::Call;
use ruff_python_ast::{Expr, ExprCall};

use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
Expand Down Expand Up @@ -30,16 +29,7 @@ impl FormatNodeRule<ExprCall> for FormatExprCall {
arguments,
} = item;

let call_chain_layout = match self.call_chain_layout {
CallChainLayout::Default => {
if f.context().node_level().is_parenthesized() {
CallChainLayout::from_expression(AnyNodeRef::from(item), f.context().source())
} else {
CallChainLayout::NonFluent
}
}
layout @ (CallChainLayout::Fluent | CallChainLayout::NonFluent) => layout,
};
let call_chain_layout = format_call_chain_layout(f, self.call_chain_layout, item);

let fmt_inner = format_with(|f| {
match func.as_ref() {
Expand Down
13 changes: 2 additions & 11 deletions crates/ruff_python_formatter/src/expression/expr_subscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::context::PyFormatContext;
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::expression::CallChainLayout;
use crate::expression::{format_call_chain_layout, CallChainLayout};
use crate::prelude::*;
use crate::FormatNodeRule;

Expand All @@ -34,16 +34,7 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
ctx: _,
} = item;

let call_chain_layout = match self.call_chain_layout {
CallChainLayout::Default => {
if f.context().node_level().is_parenthesized() {
CallChainLayout::from_expression(AnyNodeRef::from(item), f.context().source())
} else {
CallChainLayout::NonFluent
}
}
layout @ (CallChainLayout::Fluent | CallChainLayout::NonFluent) => layout,
};
let call_chain_layout = format_call_chain_layout(f, self.call_chain_layout, item);

let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
Expand Down
18 changes: 18 additions & 0 deletions crates/ruff_python_formatter/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,21 @@ impl From<ast::Operator> for OperatorPriority {
}
}
}

/// Determine whether to actually apply fluent layout in attribute, call and subscript formatting
pub(crate) fn format_call_chain_layout<'a>(
f: &mut PyFormatter,
layout: CallChainLayout,
item: impl Into<AnyNodeRef<'a>>,
) -> CallChainLayout {
match layout {
CallChainLayout::Default => {
if f.context().node_level().is_parenthesized() {
CallChainLayout::from_expression(item.into(), f.context().source())
} else {
CallChainLayout::NonFluent
}
}
layout @ (CallChainLayout::Fluent | CallChainLayout::NonFluent) => layout,
}
}

0 comments on commit 9d7bfff

Please sign in to comment.