Skip to content

Commit

Permalink
Use new IR to format expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jul 8, 2023
1 parent 7135143 commit c97caf1
Show file tree
Hide file tree
Showing 20 changed files with 293 additions and 783 deletions.
29 changes: 24 additions & 5 deletions crates/ruff_python_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,31 @@ pub(crate) struct OptionalParentheses<'a, 'ast> {
inner: Argument<'a, PyFormatContext<'ast>>,
}

// TODO move this advanced logic to `FormatExpr` instead? I dont think it should apply to other nodes
impl<'ast> Format<PyFormatContext<'ast>> for OptionalParentheses<'_, 'ast> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
group(&format_args![
let saved_level = f.context().node_level();

let parens_id = f.group_id("optional_parentheses");

f.context_mut()
.set_node_level(NodeLevel::Expression(Some(parens_id)));

let result = group(&format_args![
if_group_breaks(&text("(")),
soft_block_indent(&Arguments::from(&self.inner)),
indent_if_group_breaks(
&format_args![soft_line_break(), Arguments::from(&self.inner)],
parens_id
),
soft_line_break(),
if_group_breaks(&text(")"))
])
.fmt(f)
.with_group_id(Some(parens_id))
.fmt(f);

f.context_mut().set_node_level(saved_level);

result
}
}

Expand Down Expand Up @@ -113,7 +130,9 @@ impl<'fmt, 'ast, 'buf> JoinNodesBuilder<'fmt, 'ast, 'buf> {
0 | 1 => hard_line_break().fmt(self.fmt),
_ => empty_line().fmt(self.fmt),
},
NodeLevel::Expression => hard_line_break().fmt(self.fmt),
NodeLevel::Expression(_) | NodeLevel::ParenthesizedExpression => {
hard_line_break().fmt(self.fmt)
}
}?;
}

Expand Down Expand Up @@ -353,7 +372,7 @@ no_leading_newline = 30"#
// Removes all empty lines
#[test]
fn ranged_builder_parenthesized_level() {
let printed = format_ranged(NodeLevel::Expression);
let printed = format_ranged(NodeLevel::Expression(None));

assert_eq!(
&printed,
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_python_formatter/src/comments/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLines {
},

// Remove all whitespace in parenthesized expressions
NodeLevel::Expression => write!(f, [hard_line_break()]),
NodeLevel::Expression(_) | NodeLevel::ParenthesizedExpression => {
write!(f, [hard_line_break()])
}
}
}
}
9 changes: 6 additions & 3 deletions crates/ruff_python_formatter/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::comments::Comments;
use crate::PyFormatOptions;
use ruff_formatter::{FormatContext, SourceCode};
use ruff_formatter::{FormatContext, GroupId, SourceCode};
use ruff_python_ast::source_code::Locator;
use std::fmt::{Debug, Formatter};

Expand Down Expand Up @@ -78,6 +78,9 @@ pub(crate) enum NodeLevel {
/// (`if`, `while`, `match`, etc.).
CompoundStatement,

/// Formatting nodes that are enclosed in a parenthesized expression.
Expression,
/// The root or any sub-expression.
Expression(Option<GroupId>),

/// Formatting nodes that are enclosed by a parenthesized (any `[]`, `{}` or `()`) expression.
ParenthesizedExpression,
}
216 changes: 0 additions & 216 deletions crates/ruff_python_formatter/src/expression/binary_like.rs

This file was deleted.

Loading

0 comments on commit c97caf1

Please sign in to comment.