Skip to content

Commit

Permalink
Move ExprSequence to sequence.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Jul 9, 2023
1 parent ce249f5 commit b5fe6d5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 58 deletions.
19 changes: 2 additions & 17 deletions crates/ruff_python_formatter/src/expression/expr_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use ruff_text_size::TextRange;
use rustpython_parser::ast::ExprTuple;
use rustpython_parser::ast::{Expr, Ranged};

use super::sequence::ExprSequence;

#[derive(Eq, PartialEq, Debug, Default)]
pub enum TupleParentheses {
/// Effectively `None` in `Option<Parentheses>`
Expand Down Expand Up @@ -93,23 +95,6 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
}
}

#[derive(Debug)]
struct ExprSequence<'a> {
elts: &'a [Expr],
}

impl<'a> ExprSequence<'a> {
const fn new(elts: &'a [Expr]) -> Self {
Self { elts }
}
}

impl Format<PyFormatContext<'_>> for ExprSequence<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
f.join_comma_separated().nodes(self.elts.iter()).finish()
}
}

impl NeedsParentheses for ExprTuple {
fn needs_parentheses(
&self,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_python_formatter/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(crate) mod expr_unary_op;
pub(crate) mod expr_yield;
pub(crate) mod expr_yield_from;
pub(crate) mod parentheses;
pub(crate) mod sequence;
pub(crate) mod string;

#[derive(Default)]
Expand Down
21 changes: 21 additions & 0 deletions crates/ruff_python_formatter/src/expression/sequence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use ruff_formatter::{prelude::Formatter, Format, FormatResult};
use rustpython_parser::ast::Expr;

use crate::{builders::PyFormatterExtensions, context::PyFormatContext};

#[derive(Debug)]
pub(crate) struct ExprSequence<'a> {
elts: &'a [Expr],
}

impl<'a> ExprSequence<'a> {
pub(crate) const fn new(elts: &'a [Expr]) -> Self {
Self { elts }
}
}

impl Format<PyFormatContext<'_>> for ExprSequence<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
f.join_comma_separated().nodes(self.elts.iter()).finish()
}
}
47 changes: 6 additions & 41 deletions crates/ruff_python_formatter/src/statement/stmt_delete.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::builders::{optional_parentheses, PyFormatterExtensions};
use crate::comments::{dangling_node_comments, Comments};
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::prelude::PyFormatContext;
use crate::builders::optional_parentheses;
use crate::comments::dangling_node_comments;
use crate::expression::parentheses::Parenthesize;
use crate::expression::sequence::ExprSequence;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{block_indent, space, text, Formatter};
use ruff_formatter::prelude::{block_indent, space, text};
use ruff_formatter::{write, Buffer, Format, FormatResult};
use rustpython_parser::ast::{Expr, StmtDelete};
use rustpython_parser::ast::StmtDelete;

#[derive(Default)]
pub struct FormatStmtDelete;
Expand Down Expand Up @@ -43,36 +41,3 @@ impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
}
}
}

// TODO(cnpryer): Shared `ExprSequence` (see expr_tuple.rs)
#[derive(Debug)]
struct ExprSequence<'a> {
targets: &'a [Expr],
}

impl<'a> ExprSequence<'a> {
const fn new(targets: &'a [Expr]) -> Self {
Self { targets }
}
}

impl Format<PyFormatContext<'_>> for ExprSequence<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
f.join_comma_separated().nodes(self.targets.iter()).finish()
}
}

// NOTE: `default_expression_needs_parentheses` is reserved for expression nodes.
impl NeedsParentheses for StmtDelete {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}
}
}

0 comments on commit b5fe6d5

Please sign in to comment.