Skip to content

Commit acbe67d

Browse files
authored
Preserve a blank line after a leading top-level comment. (#1481)
Preserving blank lines between comments and code was mostly working correctly except for one edge case: A blank line between the *first* comment at the top level of a file and the subsequent code would get discarded. This fixes that. Fix #1415.
1 parent c189908 commit acbe67d

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed

benchmark/case/large.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4+
45
library pub.solver.backtracking_solver;
56

67
import 'dart:async';

lib/src/front_end/sequence_builder.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class SequenceBuilder {
107107

108108
/// Writes any comments appearing before [token] to the sequence.
109109
///
110+
/// Also handles blank lines between preceding comments and elements and the
111+
/// subsequent element.
112+
///
110113
/// Comments between sequence elements get special handling where comments
111114
/// on their own line become standalone sequence elements.
112115
void addCommentsBefore(Token token, {int? indent}) {
@@ -152,7 +155,13 @@ class SequenceBuilder {
152155
if (comments.requiresNewline) _mustSplit = true;
153156

154157
// Write a blank before the token if there should be one.
155-
if (comments.linesBeforeNextToken > 1) addBlank();
158+
if (comments.linesBeforeNextToken > 1) {
159+
// If we just wrote a comment, then allow a blank line between it and the
160+
// element.
161+
if (comments.isNotEmpty) _allowBlank = true;
162+
163+
addBlank();
164+
}
156165
}
157166

158167
void _add(int indent, Piece piece) {

test/tall/regression/1400/1415.unit

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
>>>
2+
// Comment.
3+
4+
main() {}
5+
<<<
6+
// Comment.
7+
8+
main() {}
9+
>>>
10+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
11+
// for details. All rights reserved. Use of this source code is governed by a
12+
// BSD-style license that can be found in the LICENSE file.
13+
14+
import 'package:analyzer/dart/ast/ast.dart';
15+
<<<
16+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
17+
// for details. All rights reserved. Use of this source code is governed by a
18+
// BSD-style license that can be found in the LICENSE file.
19+
20+
import 'package:analyzer/dart/ast/ast.dart';

test/tall/statement/block_comment.stmt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,21 @@ line */
9696
a:
9797
b:
9898
c;
99+
}
100+
>>> Preserve one blank line between a leading comment and statement.
101+
{
102+
103+
104+
105+
// comment
106+
107+
108+
109+
a;
110+
}
111+
<<<
112+
{
113+
// comment
114+
115+
a;
99116
}

test/tall/top_level/comment.unit

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,50 @@ void b() => body;
100100
/// Doc function.
101101
void c() {
102102
;
103-
}
103+
}
104+
>>> Preserve one blank line between a leading comment and declaration.
105+
// comment
106+
107+
108+
109+
var a = 1;
110+
<<<
111+
// comment
112+
113+
var a = 1;
114+
>>> Preserve one blank line between a leading comment and directive.
115+
// comment
116+
117+
118+
119+
import 'x.dart';
120+
<<<
121+
// comment
122+
123+
import 'x.dart';
124+
>>> Preserve one blank line between comments and declarations.
125+
// comment
126+
var a = 1;
127+
128+
// comment
129+
130+
var b = 2;
131+
132+
133+
134+
// comment
135+
136+
137+
138+
var c = 3;
139+
<<<
140+
// comment
141+
var a = 1;
142+
143+
// comment
144+
145+
var b = 2;
146+
147+
// comment
148+
149+
var c = 3;

0 commit comments

Comments
 (0)