mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
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:
parent
bb96647d66
commit
8919b6ad9a
11 changed files with 60 additions and 82 deletions
|
@ -6,9 +6,7 @@ use ruff_text_size::TextRange;
|
||||||
|
|
||||||
use crate::builders::empty_parenthesized_with_dangling_comments;
|
use crate::builders::empty_parenthesized_with_dangling_comments;
|
||||||
use crate::comments::leading_comments;
|
use crate::comments::leading_comments;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
|
||||||
|
@ -86,7 +84,9 @@ impl FormatNodeRule<ExprDict> for FormatExprDict {
|
||||||
joiner.finish()
|
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<()> {
|
fn fmt_dangling_comments(&self, _node: &ExprDict, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
|
|
|
@ -6,9 +6,7 @@ use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::ExprDictComp;
|
use ruff_python_ast::ExprDictComp;
|
||||||
|
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
|
|
||||||
};
|
|
||||||
use crate::AsFormat;
|
use crate::AsFormat;
|
||||||
use crate::{FormatNodeRule, FormattedIterExt, PyFormatter};
|
use crate::{FormatNodeRule, FormattedIterExt, PyFormatter};
|
||||||
|
|
||||||
|
@ -35,9 +33,8 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[parenthesized_with_dangling_comments(
|
[parenthesized(
|
||||||
"{",
|
"{",
|
||||||
dangling,
|
|
||||||
&group(&format_args!(
|
&group(&format_args!(
|
||||||
group(&key.format()),
|
group(&key.format()),
|
||||||
text(":"),
|
text(":"),
|
||||||
|
@ -47,7 +44,8 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
|
||||||
&joined
|
&joined
|
||||||
)),
|
)),
|
||||||
"}"
|
"}"
|
||||||
)]
|
)
|
||||||
|
.with_dangling_comments(dangling)]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,7 @@ use ruff_python_ast::ExprGeneratorExp;
|
||||||
|
|
||||||
use crate::comments::leading_comments;
|
use crate::comments::leading_comments;
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::parenthesized_with_dangling_comments;
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::AsFormat;
|
use crate::AsFormat;
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
|
@ -66,16 +65,16 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
|
||||||
} else {
|
} else {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[parenthesized_with_dangling_comments(
|
[parenthesized(
|
||||||
"(",
|
"(",
|
||||||
dangling,
|
|
||||||
&group(&format_args!(
|
&group(&format_args!(
|
||||||
group(&elt.format()),
|
group(&elt.format()),
|
||||||
soft_line_break_or_space(),
|
soft_line_break_or_space(),
|
||||||
&joined
|
&joined
|
||||||
)),
|
)),
|
||||||
")"
|
")"
|
||||||
)]
|
)
|
||||||
|
.with_dangling_comments(dangling)]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,7 @@ use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::{ExprList, Ranged};
|
use ruff_python_ast::{ExprList, Ranged};
|
||||||
|
|
||||||
use crate::builders::empty_parenthesized_with_dangling_comments;
|
use crate::builders::empty_parenthesized_with_dangling_comments;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
|
||||||
|
@ -34,7 +32,9 @@ impl FormatNodeRule<ExprList> for FormatExprList {
|
||||||
.finish()
|
.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<()> {
|
fn fmt_dangling_comments(&self, _node: &ExprList, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
|
|
|
@ -3,9 +3,7 @@ use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::ExprListComp;
|
use ruff_python_ast::ExprListComp;
|
||||||
|
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::AsFormat;
|
use crate::AsFormat;
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
|
@ -32,16 +30,16 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[parenthesized_with_dangling_comments(
|
[parenthesized(
|
||||||
"[",
|
"[",
|
||||||
dangling,
|
|
||||||
&group(&format_args![
|
&group(&format_args![
|
||||||
group(&elt.format()),
|
group(&elt.format()),
|
||||||
soft_line_break_or_space(),
|
soft_line_break_or_space(),
|
||||||
&joined
|
&joined
|
||||||
]),
|
]),
|
||||||
"]"
|
"]"
|
||||||
)]
|
)
|
||||||
|
.with_dangling_comments(dangling)]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::{ExprSet, Ranged};
|
use ruff_python_ast::{ExprSet, Ranged};
|
||||||
|
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
|
||||||
|
@ -25,7 +23,9 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
|
||||||
let comments = f.context().comments().clone();
|
let comments = f.context().comments().clone();
|
||||||
let dangling = comments.dangling_comments(item);
|
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<()> {
|
fn fmt_dangling_comments(&self, _node: &ExprSet, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
|
|
|
@ -3,9 +3,7 @@ use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::ExprSetComp;
|
use ruff_python_ast::ExprSetComp;
|
||||||
|
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::AsFormat;
|
use crate::AsFormat;
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
|
@ -32,16 +30,16 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[parenthesized_with_dangling_comments(
|
[parenthesized(
|
||||||
"{",
|
"{",
|
||||||
dangling,
|
|
||||||
&group(&format_args!(
|
&group(&format_args!(
|
||||||
group(&elt.format()),
|
group(&elt.format()),
|
||||||
soft_line_break_or_space(),
|
soft_line_break_or_space(),
|
||||||
&joined
|
&joined
|
||||||
)),
|
)),
|
||||||
"}"
|
"}"
|
||||||
)]
|
)
|
||||||
|
.with_dangling_comments(dangling)]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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::ExprTuple;
|
||||||
use ruff_python_ast::{Expr, Ranged};
|
use ruff_python_ast::{Expr, Ranged};
|
||||||
use ruff_text_size::TextRange;
|
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::builders::{empty_parenthesized_with_dangling_comments, parenthesize_if_expands};
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||||
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
|
|
||||||
};
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug, Default)]
|
#[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
|
// A single element tuple always needs parentheses and a trailing comma, except when inside of a subscript
|
||||||
{
|
{
|
||||||
parenthesized_with_dangling_comments(
|
parenthesized("(", &format_args![single.format(), text(",")], ")")
|
||||||
"(",
|
.with_dangling_comments(dangling)
|
||||||
dangling,
|
.fmt(f)
|
||||||
&format_args![single.format(), text(",")],
|
|
||||||
")",
|
|
||||||
)
|
|
||||||
.fmt(f)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// If the tuple has parentheses, we generally want to keep them. The exception are for
|
// 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
|
&& !(self.parentheses == TupleParentheses::NeverPreserve
|
||||||
&& dangling.is_empty()) =>
|
&& dangling.is_empty()) =>
|
||||||
{
|
{
|
||||||
parenthesized_with_dangling_comments("(", dangling, &ExprSequence::new(item), ")")
|
parenthesized("(", &ExprSequence::new(item), ")")
|
||||||
|
.with_dangling_comments(dangling)
|
||||||
.fmt(f)
|
.fmt(f)
|
||||||
}
|
}
|
||||||
_ => match self.parentheses {
|
_ => match self.parentheses {
|
||||||
|
|
|
@ -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> {
|
pub(crate) struct FormatParenthesized<'content, 'ast> {
|
||||||
left: &'static str,
|
left: &'static str,
|
||||||
comments: &'content [SourceComment],
|
comments: &'content [SourceComment],
|
||||||
|
@ -142,6 +123,24 @@ pub(crate) struct FormatParenthesized<'content, 'ast> {
|
||||||
right: &'static str,
|
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> {
|
impl<'ast> Format<PyFormatContext<'ast>> for FormatParenthesized<'_, 'ast> {
|
||||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
||||||
let inner = format_with(|f| {
|
let inner = format_with(|f| {
|
||||||
|
|
|
@ -6,7 +6,7 @@ use ruff_text_size::{TextRange, TextSize};
|
||||||
|
|
||||||
use crate::builders::empty_parenthesized_with_dangling_comments;
|
use crate::builders::empty_parenthesized_with_dangling_comments;
|
||||||
use crate::expression::expr_generator_exp::GeneratorExpParentheses;
|
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::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
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
|
// hey_this_is_a_very_long_call, it_has_funny_attributes_asdf_asdf, really=True
|
||||||
// )
|
// )
|
||||||
// ```
|
// ```
|
||||||
parenthesized_with_dangling_comments(
|
parenthesized("(", &group(&all_arguments), ")")
|
||||||
"(",
|
.with_dangling_comments(dangling_comments)
|
||||||
dangling_comments,
|
|
||||||
&group(&all_arguments),
|
|
||||||
")"
|
|
||||||
)
|
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ use crate::comments::{
|
||||||
leading_comments, leading_node_comments, trailing_comments, CommentLinePosition, SourceComment,
|
leading_comments, leading_node_comments, trailing_comments, CommentLinePosition, SourceComment,
|
||||||
};
|
};
|
||||||
use crate::context::{NodeLevel, WithNodeLevel};
|
use crate::context::{NodeLevel, WithNodeLevel};
|
||||||
use crate::expression::parentheses::parenthesized_with_dangling_comments;
|
use crate::expression::parentheses::parenthesized;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
|
||||||
|
@ -256,12 +256,8 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
||||||
} else {
|
} else {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[parenthesized_with_dangling_comments(
|
[parenthesized("(", &group(&format_inner), ")")
|
||||||
"(",
|
.with_dangling_comments(parenthesis_dangling)]
|
||||||
parenthesis_dangling,
|
|
||||||
&group(&format_inner),
|
|
||||||
")"
|
|
||||||
)]
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue