Rename Comments methods (#6649)

This commit is contained in:
Micha Reiser 2023-08-18 08:37:01 +02:00 committed by GitHub
parent 3ceb6fbeb0
commit 0cea4975fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 128 additions and 172 deletions

View file

@ -45,7 +45,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
);
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item);
let dangling_comments = comments.dangling(item);
let leading_attribute_comments_start = dangling_comments
.partition_point(|comment| comment.line_position().is_end_of_line());
let (trailing_dot_comments, leading_attribute_comments) =
@ -88,7 +88,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
value.format().fmt(f)?;
}
if comments.has_trailing_own_line_comments(value.as_ref()) {
if comments.has_trailing_own_line(value.as_ref()) {
hard_line_break().fmt(f)?;
}
@ -171,10 +171,10 @@ impl NeedsParentheses for ExprAttribute {
OptionalParentheses::Multiline
} else if context
.comments()
.dangling_comments(self)
.dangling(self)
.iter()
.any(|comment| comment.line_position().is_own_line())
|| context.comments().has_trailing_own_line_comments(self)
|| context.comments().has_trailing_own_line(self)
{
OptionalParentheses::Always
} else {

View file

@ -29,10 +29,7 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
match Self::layout(item, f.context()) {
BinOpLayout::LeftString(expression) => {
let right_has_leading_comment = f
.context()
.comments()
.has_leading_comments(item.right.as_ref());
let right_has_leading_comment = comments.has_leading(item.right.as_ref());
let format_right_and_op = format_with(|f| {
if right_has_leading_comment {
@ -98,7 +95,7 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
right,
} = current;
let operator_comments = comments.dangling_comments(current);
let operator_comments = comments.dangling(current);
let needs_space = !is_simple_power_expression(current);
let before_operator_space = if needs_space {
@ -117,9 +114,7 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
)?;
// Format the operator on its own line if the right side has any leading comments.
if comments.has_leading_comments(right.as_ref())
|| !operator_comments.is_empty()
{
if comments.has_leading(right.as_ref()) || !operator_comments.is_empty() {
hard_line_break().fmt(f)?;
} else if needs_space {
space().fmt(f)?;
@ -171,8 +166,8 @@ impl FormatExprBinOp {
if bin_op.op == Operator::Mod
&& context.node_level().is_parenthesized()
&& !comments.has_dangling_comments(constant)
&& !comments.has_dangling_comments(bin_op)
&& !comments.has_dangling(constant)
&& !comments.has_dangling(bin_op)
{
BinOpLayout::LeftString(constant)
} else {

View file

@ -50,7 +50,7 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
FormatValue { value: first }.fmt(f)?;
for value in values {
let leading_value_comments = comments.leading_comments(value);
let leading_value_comments = comments.leading(value);
// Format the expressions leading comments **before** the operator
if leading_value_comments.is_empty() {
write!(f, [in_parentheses_only_soft_line_break_or_space()])?;

View file

@ -29,7 +29,7 @@ impl FormatNodeRule<ExprCompare> for FormatExprCompare {
assert_eq!(comparators.len(), ops.len());
for (operator, comparator) in ops.iter().zip(comparators) {
let leading_comparator_comments = comments.leading_comments(comparator);
let leading_comparator_comments = comments.leading(comparator);
if leading_comparator_comments.is_empty() {
write!(f, [in_parentheses_only_soft_line_break_or_space()])?;
} else {

View file

@ -43,7 +43,7 @@ impl Format<PyFormatContext<'_>> for KeyValuePair<'_> {
)
} else {
let comments = f.context().comments().clone();
let leading_value_comments = comments.leading_comments(self.value);
let leading_value_comments = comments.leading(self.value);
write!(
f,
[
@ -67,7 +67,7 @@ impl FormatNodeRule<ExprDict> for FormatExprDict {
debug_assert_eq!(keys.len(), values.len());
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
if values.is_empty() {
return empty_parenthesized("{", dangling, "}").fmt(f);

View file

@ -29,7 +29,7 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
});
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
write!(
f,

View file

@ -51,7 +51,7 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
});
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
if self.parentheses == GeneratorExpParentheses::StripIfOnlyFunctionArg
&& dangling.is_empty()

View file

@ -30,12 +30,12 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
[in_parentheses_only_group(&format_args![
body.format(),
in_parentheses_only_soft_line_break_or_space(),
leading_comments(comments.leading_comments(test.as_ref())),
leading_comments(comments.leading(test.as_ref())),
text("if"),
space(),
test.format(),
in_parentheses_only_soft_line_break_or_space(),
leading_comments(comments.leading_comments(orelse.as_ref())),
leading_comments(comments.leading(orelse.as_ref())),
text("else"),
space(),
orelse.format()

View file

@ -21,7 +21,7 @@ impl FormatNodeRule<ExprList> for FormatExprList {
} = item;
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
if elts.is_empty() {
return empty_parenthesized("[", dangling, "]").fmt(f);

View file

@ -27,7 +27,7 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {
});
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
write!(
f,

View file

@ -22,7 +22,7 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
// This context, a dangling comment is an end-of-line comment on the same line as the `:=`.
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
write!(
f,

View file

@ -22,7 +22,7 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
});
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
parenthesized("{", &joined, "}")
.with_dangling_comments(dangling)

View file

@ -27,7 +27,7 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
});
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
write!(
f,

View file

@ -39,7 +39,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
// to handle newlines and spacing, or the node is None and we insert the corresponding
// slice of dangling comments
let comments = f.context().comments().clone();
let slice_dangling_comments = comments.dangling_comments(item.as_any_node_ref());
let slice_dangling_comments = comments.dangling(item.as_any_node_ref());
// Put the dangling comments (where the nodes are missing) into buckets
let first_colon_partition_index =
slice_dangling_comments.partition_point(|x| x.slice().start() < first_colon.start());
@ -102,7 +102,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
// Upper
if let Some(upper) = upper {
let upper_leading_comments = comments.leading_comments(upper.as_ref());
let upper_leading_comments = comments.leading(upper.as_ref());
leading_comments_spacing(f, upper_leading_comments)?;
write!(f, [upper.format(), line_suffix_boundary()])?;
} else {
@ -134,7 +134,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
space().fmt(f)?;
}
if let Some(step) = step {
let step_leading_comments = comments.leading_comments(step.as_ref());
let step_leading_comments = comments.leading(step.as_ref());
leading_comments_spacing(f, step_leading_comments)?;
step.format().fmt(f)?;
} else if !dangling_step_comments.is_empty() {

View file

@ -21,7 +21,7 @@ impl FormatNodeRule<ExprStarred> for FormatExprStarred {
} = item;
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
write!(f, [text("*"), dangling_comments(dangling), value.format()])
}

View file

@ -37,7 +37,7 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
let call_chain_layout = self.call_chain_layout.apply_in_node(item, f);
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
let dangling_comments = comments.dangling(item.as_any_node_ref());
debug_assert!(
dangling_comments.len() <= 1,
"A subscript expression can only have a single dangling comment, the one after the bracket"

View file

@ -107,7 +107,7 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
} = item;
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);
// Handle the edge cases of an empty tuple and a tuple with one element
//

View file

@ -39,7 +39,7 @@ impl FormatNodeRule<ExprUnaryOp> for FormatExprUnaryOp {
// (not # comment
// a)
// ```
let leading_operand_comments = comments.leading_comments(operand.as_ref());
let leading_operand_comments = comments.leading(operand.as_ref());
let trailing_operator_comments_end =
leading_operand_comments.partition_point(|p| p.line_position().is_end_of_line());
let (trailing_operator_comments, leading_operand_comments) =

View file

@ -108,7 +108,20 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
if parenthesize {
let comments = f.context().comments().clone();
let open_parenthesis_comment = comments.open_parenthesis_comment(expression);
// Any comments on the open parenthesis of a `node`.
//
// For example, `# comment` in:
// ```python
// ( # comment
// foo.bar
// )
// ```
let open_parenthesis_comment = comments
.leading(expression)
.first()
.filter(|comment| comment.line_position().is_end_of_line());
parenthesized("(", &format_expr, ")")
.with_dangling_comments(
open_parenthesis_comment
@ -167,8 +180,8 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
let preserve_parentheses = parenthesize.is_optional()
&& is_expression_parenthesized((*expression).into(), f.context().source());
let has_comments = comments.has_leading_comments(*expression)
|| comments.has_trailing_own_line_comments(*expression);
let has_comments =
comments.has_leading(*expression) || comments.has_trailing_own_line(*expression);
// If the expression has comments, we always want to preserve the parentheses. This also
// ensures that we correctly handle parenthesized comments, and don't need to worry about
@ -655,11 +668,7 @@ pub(crate) fn has_own_parentheses(
Expr::List(ast::ExprList { elts, .. })
| Expr::Set(ast::ExprSet { elts, .. })
| Expr::Tuple(ast::ExprTuple { elts, .. }) => {
if !elts.is_empty()
|| context
.comments()
.has_dangling_comments(AnyNodeRef::from(expr))
{
if !elts.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) {
Some(OwnParentheses::NonEmpty)
} else {
Some(OwnParentheses::Empty)
@ -667,22 +676,14 @@ pub(crate) fn has_own_parentheses(
}
Expr::Dict(ast::ExprDict { keys, .. }) => {
if !keys.is_empty()
|| context
.comments()
.has_dangling_comments(AnyNodeRef::from(expr))
{
if !keys.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) {
Some(OwnParentheses::NonEmpty)
} else {
Some(OwnParentheses::Empty)
}
}
Expr::Call(ast::ExprCall { arguments, .. }) => {
if !arguments.is_empty()
|| context
.comments()
.has_dangling_comments(AnyNodeRef::from(expr))
{
if !arguments.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) {
Some(OwnParentheses::NonEmpty)
} else {
Some(OwnParentheses::Empty)

View file

@ -160,7 +160,7 @@ impl Format<PyFormatContext<'_>> for FormatStringContinuation<'_> {
let comments = f.context().comments().clone();
let locator = f.context().locator();
let quote_style = f.options().quote_style();
let mut dangling_comments = comments.dangling_comments(self.string);
let mut dangling_comments = comments.dangling(self.string);
let string_range = self.string.range();
let string_content = locator.slice(string_range);