mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +00:00
Format Attribute Expression (#5259)
This commit is contained in:
parent
341b12d918
commit
ccf34aae8c
20 changed files with 394 additions and 329 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue