Format Attribute Expression (#5259)

This commit is contained in:
Micha Reiser 2023-06-21 23:33:53 +02:00 committed by GitHub
parent 341b12d918
commit ccf34aae8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 394 additions and 329 deletions

View file

@ -31,6 +31,7 @@ pub(super) fn place_comment<'a>(
handle_leading_function_with_decorators_comment,
handle_dict_unpacking_comment,
handle_slice_comments,
handle_attribute_comment,
];
for handler in HANDLERS {
comment = match handler(comment, locator) {
@ -1005,6 +1006,43 @@ fn handle_dict_unpacking_comment<'a>(
CommentPlacement::Default(comment)
}
// Own line comments coming after the node are always dangling comments
// ```python
// (
// a
// # trailing a comment
// . # dangling comment
// # or this
// b
// )
// ```
fn handle_attribute_comment<'a>(
comment: DecoratedComment<'a>,
locator: &Locator,
) -> CommentPlacement<'a> {
let Some(attribute) = comment.enclosing_node().expr_attribute() else {
return CommentPlacement::Default(comment);
};
// It must be a comment AFTER the name
if comment.preceding_node().is_none() {
return CommentPlacement::Default(comment);
}
let between_value_and_attr = TextRange::new(attribute.value.end(), attribute.attr.start());
let dot = SimpleTokenizer::new(locator.contents(), between_value_and_attr)
.skip_trivia()
.next()
.expect("Expected the `.` character after the value");
if TextRange::new(dot.end(), attribute.attr.start()).contains(comment.slice().start()) {
CommentPlacement::dangling(attribute.into(), comment)
} else {
CommentPlacement::Default(comment)
}
}
/// Returns `true` if `right` is `Some` and `left` and `right` are referentially equal.
fn are_same_optional<'a, T>(left: AnyNodeRef, right: Option<T>) -> bool
where