Skip to content

Commit 662cb5b

Browse files
authored
Test that the new style doesn't keep line comments flush left. (#1397)
The old formatter style had a special rule that any line comment that was already at column 1 (i.e. the left edge) would not be indented, even if the surrounding code would otherwise indent it. So if you wrote: ```dart class C { void foo() { // comment } } ``` Then the formatter would output: ```dart class C { void foo() { // comment } } ``` And not: ```dart class C { void foo() { // comment } } ``` The intent was that if you commented out a block of code in your editor, like: ```dart class C { void unused() { // code(); } } ``` Then the formatter wouldn't then add *more* indentation: ```dart class C { void unused() { // code(); } } ``` But in the editors I've tested (VS Code, IntelliJ, Sublime Text, emacs), when they comment out code, they all put the line comment markers *after* the leading indentation, not before. So they will comment it out like: ```dart class C { void unused() { // code(); } } ``` That makes this feature, I think, not useful. Also, over the years, a number of users have found it surprising and unhelpful. It's really annoying if you are, for example, generating code, and *want* it to indent the comments with the surrounding structure but you don't want to have to generate some amount of leading indentation just to disable this weird behavior. The new style doesn't implement this feature. And with this PR, I'm testing that it doesn't implement it and claiming that we *shouldn't* implement it either.
1 parent f49c00b commit 662cb5b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

test/declaration/class_comment.unit

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ class C { // comment
66
class C {
77
// comment
88
}
9+
>>> Indent line comment in body.
10+
### Note: The old formatter had a special rule that line comments starting at
11+
### column 1 would not be indented even if the surrounding code otherwise
12+
### required it. The new style deliberately does not have that rule.
13+
class C {
14+
// comment
15+
}
16+
<<<
17+
class C {
18+
// comment
19+
}
920
>>> Empty class containing inline block comment.
1021
class C { /* comment */ }
1122
<<<

test/function/comment.unit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
40 columns |
2+
>>> Indent line comment in body.
3+
### Note: The old formatter had a special rule that line comments starting at
4+
### column 1 would not be indented even if the surrounding code otherwise
5+
### required it. The new style deliberately does not have that rule.
6+
main() {
7+
// comment
8+
{
9+
// block
10+
{
11+
// nested
12+
}
13+
}
14+
}
15+
<<<
16+
main() {
17+
// comment
18+
{
19+
// block
20+
{
21+
// nested
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)