Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use parenthesized_with_dangling_comments in arguments formatter #6376

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ def f(*args, **kwargs):
1
# abc
)

threshold_date = datetime.datetime.now() - datetime.timedelta( # comment
days=threshold_days_threshold_days
)
33 changes: 18 additions & 15 deletions crates/ruff_python_formatter/src/other/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{TextRange, TextSize};

use crate::builders::empty_parenthesized_with_dangling_comments;
use crate::comments::trailing_comments;
use crate::expression::expr_generator_exp::GeneratorExpParentheses;
use crate::expression::parentheses::{parenthesized, Parentheses};
use crate::expression::parentheses::{parenthesized_with_dangling_comments, Parentheses};
use crate::prelude::*;
use crate::FormatNodeRule;

Expand Down Expand Up @@ -35,18 +34,6 @@ impl FormatNodeRule<Arguments> for FormatArguments {
);
}

// If the arguments are non-empty, then a dangling comment indicates a comment on the
// same line as the opening parenthesis, e.g.:
// ```python
// f( # This call has a dangling comment.
// a,
// b,
// c,
// )
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
write!(f, [trailing_comments(dangling_comments)])?;

let all_arguments = format_with(|f: &mut PyFormatter| {
let source = f.context().source();
let mut joiner = f.join_comma_separated(item.end());
Expand Down Expand Up @@ -84,6 +71,17 @@ impl FormatNodeRule<Arguments> for FormatArguments {
joiner.finish()
});

// If the arguments are non-empty, then a dangling comment indicates a comment on the
// same line as the opening parenthesis, e.g.:
// ```python
// f( # This call has a dangling comment.
// a,
// b,
// c,
// )
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());

write!(
f,
[
Expand All @@ -103,7 +101,12 @@ impl FormatNodeRule<Arguments> for FormatArguments {
// )
// ```
// TODO(konstin): Doesn't work see wrongly formatted test
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@konstin - Is this still applicable? I can't tell what it applies to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think think so

parenthesized("(", &group(&all_arguments), ")")
parenthesized_with_dangling_comments(
"(",
dangling_comments,
&group(&all_arguments),
")"
)
Comment on lines +104 to +109
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we change the builder to parenthesized("(", &group(&all_arguments)), ")").with_dangling_comments(dangling_comments) to be more in line with how other builders work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm thinking about parenthesized and parenthesized_without_dangling_comments since the latter should be the exception

Copy link
Member Author

@charliermarsh charliermarsh Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will handle separately since it would be unrelated to this change.

]
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ f (
1
# abc
)

threshold_date = datetime.datetime.now() - datetime.timedelta( # comment
days=threshold_days_threshold_days
)
```

## Output
Expand Down Expand Up @@ -230,6 +234,10 @@ f(
1
# abc
)

threshold_date = datetime.datetime.now() - datetime.timedelta( # comment
days=threshold_days_threshold_days
)
```


Expand Down
Loading