Add a with_dangling_comments to the parenthesized formatter (#6402)

See: https://github.com/astral-sh/ruff/pull/6376#discussion_r1285514328.
This commit is contained in:
Charlie Marsh 2023-08-07 15:12:12 -04:00 committed by GitHub
parent bb96647d66
commit 8919b6ad9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 60 additions and 82 deletions

View file

@ -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;
@ -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<()> {

View file

@ -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};
@ -35,9 +33,8 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
write!(
f,
[parenthesized_with_dangling_comments(
[parenthesized(
"{",
dangling,
&group(&format_args!(
group(&key.format()),
text(":"),
@ -47,7 +44,8 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
&joined
)),
"}"
)]
)
.with_dangling_comments(dangling)]
)
}
}

View file

@ -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};
@ -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)]
)
}
}

View file

@ -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;
@ -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<()> {

View file

@ -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};
@ -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)]
)
}

View file

@ -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;
@ -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<()> {

View file

@ -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};
@ -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)]
)
}

View file

@ -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)]
@ -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
@ -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 {

View file

@ -116,25 +116,6 @@ 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],
@ -142,6 +123,24 @@ pub(crate) struct FormatParenthesized<'content, '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| {

View file

@ -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;
@ -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)
]
)
}

View file

@ -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;
@ -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)]
)
}
}