Skip to content

Commit

Permalink
Indent blocks in initializers of multiple variable declarations.
Browse files Browse the repository at this point in the history
This fixes the example in the second comment on issue #900, but not
the main issue.
  • Loading branch information
munificent committed Mar 20, 2020
1 parent 71c3833 commit cce1060
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Split outer nested control flow elements (#869).
* Always place a blank line after script tags (#782).
* Don't add unneeded splits on if elements near comments (#888).
* Indent blocks in initializers of multiple-variable declarations.

# 1.3.3

Expand Down
9 changes: 8 additions & 1 deletion lib/src/source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2698,9 +2698,16 @@ class SourceVisitor extends ThrowingAstVisitor {
// possible, we split after *every* declaration so that each is on its own
// line.
builder.startRule();

// If there are multiple declarations split across lines, then we want any
// blocks in the initializers to indent past the variables.
if (node.variables.length > 1) builder.startBlockArgumentNesting();

visitCommaSeparatedNodes(node.variables, between: split);
builder.endRule();

if (node.variables.length > 1) builder.endBlockArgumentNesting();

builder.endRule();
_endPossibleConstContext(node.keyword);
}

Expand Down
22 changes: 22 additions & 0 deletions test/regression/0900/0900.stmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
>>>
void main() {
final f = stdin.readLineSync,
p = int.parse,
n = p(f()),
ranges = List.generate(n, (_) {
final t = f().split(' '), a = p(t[0]), b = a + p(t[1]);
return [a, b];
}),
dp = List<int>(n);
}
<<<
void main() {
final f = stdin.readLineSync,
p = int.parse,
n = p(f()),
ranges = List.generate(n, (_) {
final t = f().split(' '), a = p(t[0]), b = a + p(t[1]);
return [a, b];
}),
dp = List<int>(n);
}
7 changes: 7 additions & 0 deletions test/splitting/variables.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ var x = new XXXXXXXXXXXXXXXXXXXXXXXXXXXXX();
<<<
var x =
new XXXXXXXXXXXXXXXXXXXXXXXXXXXXX();
>>> nest blocks when variables split
SomeType a = () {;}, b;
<<<
SomeType a = () {
;
},
b;

0 comments on commit cce1060

Please sign in to comment.