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

Format comment before parameter default correctly #7870

Merged
merged 1 commit into from
Oct 12, 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 @@ -384,3 +384,29 @@ def test():
#comment
except ImportError:
pass

# https://github.com/astral-sh/ruff/issues/7603
def default_arg_comments(
a: str = #a
"a",
b: str =
#b
"b",
c: str =( #c
"c"
),
d: str =(
#d
"d"
)
):
print(a, b, c, d)

def default_arg_comments2(#
x: int#=
= #
#
123#
#
):
print(x)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use ruff_formatter::write;
use ruff_python_ast::ParameterWithDefault;
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};

use crate::prelude::*;

Expand All @@ -18,7 +20,51 @@ impl FormatNodeRule<ParameterWithDefault> for FormatParameterWithDefault {

if let Some(default) = default {
let space = parameter.annotation.is_some().then_some(space());
write!(f, [space, token("="), space, group(&default.format())])?;
// ```python
// def f(
// a = # parameter trailing comment; needs line break
// 1,
// b =
// # default leading comment; needs line break
// 2,
// c = ( # the default leading can only be end-of-line with parentheses; no line break
// 3
// ),
// d = (
// # own line leading comment with parentheses; no line break
// 4
// )
// )
// ```
let needs_line_break_trailing = f.context().comments().has_trailing(parameter);
let default_first_comment = f.context().comments().leading(default.as_ref()).first();
let needs_line_break_leading = default_first_comment.is_some_and(|default_leading_comment| {
let mut tokenizer = SimpleTokenizer::new(
f.context().source(),
TextRange::new(parameter.end(), default_leading_comment.start()),
)
.skip_trivia()
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
let equals = tokenizer.next();
debug_assert!(equals.is_some_and(|token| token.kind == SimpleTokenKind::Equals));
let lparens = tokenizer.next();
debug_assert!(lparens
.as_ref()
.map_or(true, |token| token.kind == SimpleTokenKind::LParen));
lparens.is_none()
});
let needs_line_break = needs_line_break_trailing || needs_line_break_leading;

write!(
f,
[
space,
token("="),
(!needs_line_break).then_some(space),
needs_line_break.then_some(hard_line_break()),
Comment on lines +63 to +64
Copy link
Member Author

Choose a reason for hiding this comment

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

This was meant as an if-then-else, not sure if it's better to split the write! into parts

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine as-is.

group(&default.format())
]
)?;
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,32 @@ try:
#comment
except ImportError:
pass

# https://github.com/astral-sh/ruff/issues/7603
def default_arg_comments(
a: str = #a
"a",
b: str =
#b
"b",
c: str =( #c
"c"
),
d: str =(
#d
"d"
)
):
print(a, b, c, d)

def default_arg_comments2(#
x: int#=
= #
#
123#
#
):
print(x)
```

## Output
Expand Down Expand Up @@ -940,6 +966,33 @@ try:
# comment
except ImportError:
pass


# https://github.com/astral-sh/ruff/issues/7603
def default_arg_comments(
a: str = # a
"a",
b: str =
# b
"b",
c: str = ( # c
"c"
),
d: str = (
# d
"d"
),
):
print(a, b, c, d)


def default_arg_comments2( #
x: int = # = #
#
123, #
#
):
print(x)
```


Expand Down