Skip to content

Commit

Permalink
Change attribute comment placement to fix stability
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Aug 2, 2023
1 parent 127b851 commit c8c8c50
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
aaaa
)

blogs1 = Blog.objects.filter(entry__headline__contains="Lennon").filter(
a1 = Blog.objects.filter(entry__headline__contains="Lennon").filter(
entry__pub_date__year=2008
)

blogs2 = Blog.objects.filter(
a2 = Blog.objects.filter(
entry__headline__contains="Lennon",
).filter(
entry__pub_date__year=2008,
Expand Down Expand Up @@ -47,15 +47,15 @@
)

# Break only after calls and indexing
result = (
b1 = (
session.query(models.Customer.id)
.filter(
models.Customer.account_id == account_id, models.Customer.email == email_address
)
.count()
)

raise (
b2 = (
Blog.objects.filter(
entry__headline__contains="Lennon",
)
Expand All @@ -66,7 +66,7 @@
)

# Nested call chains
blogs = (
c1 = (
Blog.objects.filter(
entry__headline__contains="Lennon",
).filter(
Expand All @@ -80,3 +80,30 @@
entry__pub_date__year=2010,
)
).all()

# Test different cases with trailing end of line comments:
# * fluent style, fits: no parentheses -> ignore the expand_parent
# * fluent style, doesn't fit: break all soft line breaks
# * default, fits: no parentheses
# * default, doesn't fit: parentheses but no soft line breaks

# Fits, either style
d11 = x.e().e().e() #
d12 = (x.e().e().e()) #
d13 = (
x.e() #
.e()
.e()
)

# Doesn't fit, default
d2 = (
x.e().esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() #
)

# Doesn't fit, fluent style
d3 = (
x.e() #
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
)
4 changes: 2 additions & 2 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,15 @@ fn handle_attribute_comment<'a>(
.contains(comment.slice().start())
);
if comment.line_position().is_end_of_line() {
// Attach to node with b
// Attach as trailing comment to a. The specific placement is only relevant for fluent style
// ```python
// x322 = (
// a
// . # end-of-line dot comment 2
// b
// )
// ```
CommentPlacement::trailing(comment.enclosing_node(), comment)
CommentPlacement::trailing(attribute.value.as_ref(), comment)
} else {
CommentPlacement::dangling(attribute, comment)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ raise OsError(
aaaa
)
blogs1 = Blog.objects.filter(entry__headline__contains="Lennon").filter(
a1 = Blog.objects.filter(entry__headline__contains="Lennon").filter(
entry__pub_date__year=2008
)
blogs2 = Blog.objects.filter(
a2 = Blog.objects.filter(
entry__headline__contains="Lennon",
).filter(
entry__pub_date__year=2008,
Expand Down Expand Up @@ -53,15 +53,15 @@ raise OsError("sökdjffffsldkfjlhsakfjhalsökafhsöfdahsödfjösaaksjdllllllllll
)
# Break only after calls and indexing
result = (
b1 = (
session.query(models.Customer.id)
.filter(
models.Customer.account_id == account_id, models.Customer.email == email_address
)
.count()
)
raise (
b2 = (
Blog.objects.filter(
entry__headline__contains="Lennon",
)
Expand All @@ -72,7 +72,7 @@ raise (
)
# Nested call chains
blogs = (
c1 = (
Blog.objects.filter(
entry__headline__contains="Lennon",
).filter(
Expand All @@ -86,6 +86,33 @@ blogs = (
entry__pub_date__year=2010,
)
).all()
# Test different cases with trailing end of line comments:
# * fluent style, fits: no parentheses -> ignore the expand_parent
# * fluent style, doesn't fit: break all soft line breaks
# * default, fits: no parentheses
# * default, doesn't fit: parentheses but no soft line breaks
# Fits, either style
d11 = x.e().e().e() #
d12 = (x.e().e().e()) #
d13 = (
x.e() #
.e()
.e()
)
# Doesn't fit, default
d2 = (
x.e().esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() #
)
# Doesn't fit, fluent style
d3 = (
x.e() #
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
)
```

## Output
Expand All @@ -102,11 +129,11 @@ raise OsError(
aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa
).a(aaaa)
blogs1 = Blog.objects.filter(entry__headline__contains="Lennon").filter(
a1 = Blog.objects.filter(entry__headline__contains="Lennon").filter(
entry__pub_date__year=2008
)
blogs2 = Blog.objects.filter(
a2 = Blog.objects.filter(
entry__headline__contains="Lennon",
).filter(
entry__pub_date__year=2008,
Expand Down Expand Up @@ -137,15 +164,15 @@ raise OsError("sökdjffffsldkfjlhsakfjhalsökafhsöfdahsödfjösaaksjdllllllllll
)
# Break only after calls and indexing
result = (
b1 = (
session.query(models.Customer.id)
.filter(
models.Customer.account_id == account_id, models.Customer.email == email_address
)
.count()
)
raise (
b2 = (
Blog.objects.filter(
entry__headline__contains="Lennon",
)
Expand All @@ -156,7 +183,7 @@ raise (
)
# Nested call chains
blogs = (
c1 = (
Blog.objects.filter(
entry__headline__contains="Lennon",
).filter(
Expand All @@ -170,6 +197,31 @@ blogs = (
entry__pub_date__year=2010,
)
).all()
# Test different cases with trailing end of line comments:
# * fluent style, fits: no parentheses -> ignore the expand_parent
# * fluent style, doesn't fit: break all soft line breaks
# * default, fits: no parentheses
# * default, doesn't fit: parentheses but no soft line breaks
# Fits, either style
d11 = x.e().e().e() #
d12 = x.e().e().e() #
d13 = (
x.e() #
.e()
.e()
)
# Doesn't fit, default
d2 = x.e().esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() #
# Doesn't fit, fluent style
d3 = (
x.e() #
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
)
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ a = Namespace()
(
a.
a. # trailing dot comment
# comment
# in between
b # trailing dot comment # trailing identifier comment
b # trailing identifier comment
)
Expand All @@ -155,10 +155,10 @@ a.aaaaaaaaaaaaaaaaaaaaa.lllllllllllllllllllllllllllloooooooooong.chaaaaaaaaaaaaa
# chain if and only if we need them, that is if there are own line comments inside
# the chain.
x1 = (
a.b.
a.b. # comment 2
# comment 1
# comment 3
c.d # comment 2
c.d
)
x20 = a.b
Expand Down

0 comments on commit c8c8c50

Please sign in to comment.