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

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