Skip to content

Commit

Permalink
Add a with_dangling_comments to the parenthesized formatter (astral…
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored and durumu committed Aug 12, 2023
1 parent bcc7d8d commit b90d94a
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 82 deletions.
8 changes: 4 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use ruff_text_size::TextRange;

use crate::builders::empty_parenthesized_with_dangling_comments;
use crate::comments::leading_comments;
use crate::expression::parentheses::{
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
use crate::FormatNodeRule;

Expand Down Expand Up @@ -86,7 +84,9 @@ impl FormatNodeRule<ExprDict> for FormatExprDict {
joiner.finish()
});

parenthesized_with_dangling_comments("{", dangling, &format_pairs, "}").fmt(f)
parenthesized("{", &format_pairs, "}")
.with_dangling_comments(dangling)
.fmt(f)
}

fn fmt_dangling_comments(&self, _node: &ExprDict, _f: &mut PyFormatter) -> FormatResult<()> {
Expand Down
10 changes: 4 additions & 6 deletions crates/ruff_python_formatter/src/expression/expr_dict_comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::ExprDictComp;

use crate::context::PyFormatContext;
use crate::expression::parentheses::{
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::AsFormat;
use crate::{FormatNodeRule, FormattedIterExt, PyFormatter};

Expand All @@ -35,9 +33,8 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {

write!(
f,
[parenthesized_with_dangling_comments(
[parenthesized(
"{",
dangling,
&group(&format_args!(
group(&key.format()),
text(":"),
Expand All @@ -47,7 +44,8 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
&joined
)),
"}"
)]
)
.with_dangling_comments(dangling)]
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use ruff_python_ast::ExprGeneratorExp;

use crate::comments::leading_comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::parenthesized_with_dangling_comments;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
use crate::AsFormat;
use crate::{FormatNodeRule, PyFormatter};
Expand Down Expand Up @@ -66,16 +65,16 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
} else {
write!(
f,
[parenthesized_with_dangling_comments(
[parenthesized(
"(",
dangling,
&group(&format_args!(
group(&elt.format()),
soft_line_break_or_space(),
&joined
)),
")"
)]
)
.with_dangling_comments(dangling)]
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_list.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::{ExprList, Ranged};

use crate::builders::empty_parenthesized_with_dangling_comments;
use crate::expression::parentheses::{
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
use crate::FormatNodeRule;

Expand Down Expand Up @@ -34,7 +32,9 @@ impl FormatNodeRule<ExprList> for FormatExprList {
.finish()
});

parenthesized_with_dangling_comments("[", dangling, &items, "]").fmt(f)
parenthesized("[", &items, "]")
.with_dangling_comments(dangling)
.fmt(f)
}

fn fmt_dangling_comments(&self, _node: &ExprList, _f: &mut PyFormatter) -> FormatResult<()> {
Expand Down
10 changes: 4 additions & 6 deletions crates/ruff_python_formatter/src/expression/expr_list_comp.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::ExprListComp;

use crate::context::PyFormatContext;
use crate::expression::parentheses::{
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
use crate::AsFormat;
use crate::{FormatNodeRule, PyFormatter};
Expand All @@ -32,16 +30,16 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {

write!(
f,
[parenthesized_with_dangling_comments(
[parenthesized(
"[",
dangling,
&group(&format_args![
group(&elt.format()),
soft_line_break_or_space(),
&joined
]),
"]"
)]
)
.with_dangling_comments(dangling)]
)
}

Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_set.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{ExprSet, Ranged};

use crate::expression::parentheses::{
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
use crate::FormatNodeRule;

Expand All @@ -25,7 +23,9 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);

parenthesized_with_dangling_comments("{", dangling, &joined, "}").fmt(f)
parenthesized("{", &joined, "}")
.with_dangling_comments(dangling)
.fmt(f)
}

fn fmt_dangling_comments(&self, _node: &ExprSet, _f: &mut PyFormatter) -> FormatResult<()> {
Expand Down
10 changes: 4 additions & 6 deletions crates/ruff_python_formatter/src/expression/expr_set_comp.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::ExprSetComp;

use crate::context::PyFormatContext;
use crate::expression::parentheses::{
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
use crate::AsFormat;
use crate::{FormatNodeRule, PyFormatter};
Expand All @@ -32,16 +30,16 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {

write!(
f,
[parenthesized_with_dangling_comments(
[parenthesized(
"{",
dangling,
&group(&format_args!(
group(&elt.format()),
soft_line_break_or_space(),
&joined
)),
"}"
)]
)
.with_dangling_comments(dangling)]
)
}

Expand Down
22 changes: 8 additions & 14 deletions crates/ruff_python_formatter/src/expression/expr_tuple.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::ExprTuple;
use ruff_python_ast::{Expr, Ranged};
use ruff_text_size::TextRange;

use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;

use crate::builders::{empty_parenthesized_with_dangling_comments, parenthesize_if_expands};
use crate::expression::parentheses::{
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;

#[derive(Eq, PartialEq, Debug, Default)]
Expand Down Expand Up @@ -132,13 +129,9 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
_ =>
// A single element tuple always needs parentheses and a trailing comma, except when inside of a subscript
{
parenthesized_with_dangling_comments(
"(",
dangling,
&format_args![single.format(), text(",")],
")",
)
.fmt(f)
parenthesized("(", &format_args![single.format(), text(",")], ")")
.with_dangling_comments(dangling)
.fmt(f)
}
},
// If the tuple has parentheses, we generally want to keep them. The exception are for
Expand All @@ -150,7 +143,8 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
&& !(self.parentheses == TupleParentheses::NeverPreserve
&& dangling.is_empty()) =>
{
parenthesized_with_dangling_comments("(", dangling, &ExprSequence::new(item), ")")
parenthesized("(", &ExprSequence::new(item), ")")
.with_dangling_comments(dangling)
.fmt(f)
}
_ => match self.parentheses {
Expand Down
37 changes: 18 additions & 19 deletions crates/ruff_python_formatter/src/expression/parentheses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,32 +116,31 @@ where
}
}

/// Formats `content` enclosed by the `left` and `right` parentheses, along with any dangling
/// comments on the opening parenthesis itself.
pub(crate) fn parenthesized_with_dangling_comments<'content, 'ast, Content>(
left: &'static str,
comments: &'content [SourceComment],
content: &'content Content,
right: &'static str,
) -> FormatParenthesized<'content, 'ast>
where
Content: Format<PyFormatContext<'ast>>,
{
FormatParenthesized {
left,
comments,
content: Argument::new(content),
right,
}
}

pub(crate) struct FormatParenthesized<'content, 'ast> {
left: &'static str,
comments: &'content [SourceComment],
content: Argument<'content, PyFormatContext<'ast>>,
right: &'static str,
}

impl<'content, 'ast> FormatParenthesized<'content, 'ast> {
/// Inserts any dangling comments that should be placed immediately after the open parenthesis.
/// For example:
/// ```python
/// [ # comment
/// 1,
/// 2,
/// 3,
/// ]
/// ```
pub(crate) fn with_dangling_comments(
self,
comments: &'content [SourceComment],
) -> FormatParenthesized<'content, 'ast> {
FormatParenthesized { comments, ..self }
}
}

impl<'ast> Format<PyFormatContext<'ast>> for FormatParenthesized<'_, 'ast> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
let inner = format_with(|f| {
Expand Down
10 changes: 3 additions & 7 deletions crates/ruff_python_formatter/src/other/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_text_size::{TextRange, TextSize};

use crate::builders::empty_parenthesized_with_dangling_comments;
use crate::expression::expr_generator_exp::GeneratorExpParentheses;
use crate::expression::parentheses::{parenthesized_with_dangling_comments, Parentheses};
use crate::expression::parentheses::{parenthesized, Parentheses};
use crate::prelude::*;
use crate::FormatNodeRule;

Expand Down Expand Up @@ -100,12 +100,8 @@ impl FormatNodeRule<Arguments> for FormatArguments {
// hey_this_is_a_very_long_call, it_has_funny_attributes_asdf_asdf, really=True
// )
// ```
parenthesized_with_dangling_comments(
"(",
dangling_comments,
&group(&all_arguments),
")"
)
parenthesized("(", &group(&all_arguments), ")")
.with_dangling_comments(dangling_comments)
]
)
}
Expand Down
10 changes: 3 additions & 7 deletions crates/ruff_python_formatter/src/other/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::comments::{
leading_comments, leading_node_comments, trailing_comments, CommentLinePosition, SourceComment,
};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::parentheses::parenthesized_with_dangling_comments;
use crate::expression::parentheses::parenthesized;
use crate::prelude::*;
use crate::FormatNodeRule;

Expand Down Expand Up @@ -256,12 +256,8 @@ impl FormatNodeRule<Parameters> for FormatParameters {
} else {
write!(
f,
[parenthesized_with_dangling_comments(
"(",
parenthesis_dangling,
&group(&format_inner),
")"
)]
[parenthesized("(", &group(&format_inner), ")")
.with_dangling_comments(parenthesis_dangling)]
)
}
}
Expand Down

0 comments on commit b90d94a

Please sign in to comment.