Skip to content

Commit

Permalink
Preserve a blank line after a leading top-level comment.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
munificent committed May 13, 2024
1 parent bdc8a8c commit 0c3e4fc
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
1 change: 1 addition & 0 deletions benchmark/case/large.expect
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library pub.solver.backtracking_solver;

import 'dart:async';
Expand Down
11 changes: 10 additions & 1 deletion lib/src/front_end/sequence_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class SequenceBuilder {

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

// Write a blank before the token if there should be one.
if (comments.linesBeforeNextToken > 1) addBlank();
if (comments.linesBeforeNextToken > 1) {
// If we just wrote a comment, then allow a blank line between it and the
// element.
if (comments.isNotEmpty) _allowBlank = true;

addBlank();
}
}

void _add(int indent, Piece piece) {
Expand Down
20 changes: 20 additions & 0 deletions test/tall/regression/1400/1415.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
>>>
// Comment.

main() {}
<<<
// Comment.

main() {}
>>>
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
<<<
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
17 changes: 17 additions & 0 deletions test/tall/statement/block_comment.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,21 @@ line */
a:
b:
c;
}
>>> Preserve one blank line between a leading comment and statement.
{



// comment



a;
}
<<<
{
// comment

a;
}
48 changes: 47 additions & 1 deletion test/tall/top_level/comment.unit
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,50 @@ void b() => body;
/// Doc function.
void c() {
;
}
}
>>> Preserve one blank line between a leading comment and declaration.
// comment



var a = 1;
<<<
// comment

var a = 1;
>>> Preserve one blank line between a leading comment and directive.
// comment



import 'x.dart';
<<<
// comment

import 'x.dart';
>>> Preserve one blank line between comments and declarations.
// comment
var a = 1;

// comment

var b = 2;



// comment



var c = 3;
<<<
// comment
var a = 1;

// comment

var b = 2;

// comment

var c = 3;

0 comments on commit 0c3e4fc

Please sign in to comment.