Skip to content

Commit

Permalink
Comments outside expression parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Oct 10, 2023
1 parent 61a4133 commit 55b2da1
Show file tree
Hide file tree
Showing 12 changed files with 589 additions and 95 deletions.
4 changes: 4 additions & 0 deletions crates/ruff_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ where
context: PhantomData,
}
}

pub fn rule(&self) -> &R {
&self.rule
}
}

impl<T, R, O, C> FormatRefWithRule<'_, T, R, C>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,14 @@
+ "WARNING: Removing listed files. Do you really want to continue. yes/n)? "
):
pass

# https://github.com/astral-sh/ruff/issues/7448
x = (
# a
not # b
# c
( # d
# e
True
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
list_with_parenthesized_elements1 = [
# comment leading outer
(
# comment leading inner
1 + 2 # comment trailing inner
) # comment trailing outer
]

list_with_parenthesized_elements2 = [
# leading outer
(1 + 2)
]
list_with_parenthesized_elements3 = [
# leading outer
(1 + 2) # trailing outer
]
list_with_parenthesized_elements4 = [
# leading outer
(1 + 2), # trailing outer
]
list_with_parenthesized_elements5 = [
(1), # trailing outer
(2), # trailing outer
]

nested_parentheses1 = (
(
(
1
) # i
) # j
) # k
nested_parentheses2 = [
(
(
(
1
) # i
# i2
) # j
# j2
) # k
# k2
]
nested_parentheses3 = (
( # a
( # b
1
) # i
) # j
) # k
nested_parentheses4 = [
# a
( # b
# c
( # d
# e
( #f
1
) # i
# i2
) # j
# j2
) # k
# k2
]


x = (
# unary comment
not
# in-between comment
(
# leading inner
"a"
),
not # in-between comment
(
# leading inner
"b"
),
not
( # in-between comment
# leading inner
"c"
),
# 1
not # 2
( # 3
# 4
"d"
)
)

if (
# unary comment
not
# in-between comment
(
# leading inner
1
)
):
pass

# Make sure we keep a inside the parentheses
# https://github.com/astral-sh/ruff/issues/7892
x = (
# a
( # b
1
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@
)
): ...

with (a # trailing same line comment
# trailing own line comment
) as b: ...

with (
a # trailing same line comment
# trailing own line comment
Expand Down
40 changes: 20 additions & 20 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,8 +1878,7 @@ fn handle_lambda_comment<'a>(
CommentPlacement::Default(comment)
}

/// Attach trailing end-of-line comments on the operator as dangling comments on the enclosing
/// node.
/// Move comment between a unary op and its operand before the unary op by marking them as trailing.
///
/// For example, given:
/// ```python
Expand All @@ -1896,26 +1895,27 @@ fn handle_unary_op_comment<'a>(
unary_op: &'a ast::ExprUnaryOp,
locator: &Locator,
) -> CommentPlacement<'a> {
if comment.line_position().is_own_line() {
return CommentPlacement::Default(comment);
}

if comment.start() > unary_op.operand.start() {
return CommentPlacement::Default(comment);
}

let tokenizer = SimpleTokenizer::new(
let mut tokenizer = SimpleTokenizer::new(
locator.contents(),
TextRange::new(comment.start(), unary_op.operand.start()),
);
if tokenizer
.skip_trivia()
.any(|token| token.kind == SimpleTokenKind::LParen)
{
return CommentPlacement::Default(comment);
TextRange::new(unary_op.start(), unary_op.operand.start()),
)
.skip_trivia();
let op_token = tokenizer.next();
debug_assert!(op_token.is_some_and(|token| matches!(
token.kind,
SimpleTokenKind::Tilde
| SimpleTokenKind::Not
| SimpleTokenKind::Plus
| SimpleTokenKind::Minus
)));
let up_to = tokenizer
.find(|token| token.kind == SimpleTokenKind::LParen)
.map_or(unary_op.operand.start(), |lparen| lparen.start());
if comment.end() < up_to {
CommentPlacement::leading(unary_op, comment)
} else {
CommentPlacement::Default(comment)
}

CommentPlacement::dangling(comment.enclosing_node(), comment)
}

/// Attach an end-of-line comment immediately following an open bracket as a dangling comment on
Expand Down
Loading

0 comments on commit 55b2da1

Please sign in to comment.