[analysis_server] Fix loss of comments containing newlines when sorting imports
Change-Id: Ie264c16d4ec8254ba0394691625a161598e73f5d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252180
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/organize_imports.dart b/pkg/analysis_server/lib/src/services/correction/organize_imports.dart
index c0bcd6e..8206e04 100644
--- a/pkg/analysis_server/lib/src/services/correction/organize_imports.dart
+++ b/pkg/analysis_server/lib/src/services/correction/organize_imports.dart
@@ -132,7 +132,8 @@
final leadingToken =
lastLibraryAnnotation == null ? directive.beginToken : null;
final leadingComment = leadingToken != null
- ? getLeadingComment(unit, leadingToken, lineInfo)
+ ? getLeadingComment(unit, leadingToken, lineInfo,
+ isPseudoLibraryDirective: isPseudoLibraryDirective)
: null;
final trailingComment = getTrailingComment(unit, directive, lineInfo);
@@ -205,14 +206,16 @@
/// Gets the first comment token considered to be the leading comment for this
/// token.
///
- /// Leading comments for the first directive in a file are considered library
- /// comments and not returned unless they contain blank lines, in which case
- /// only the last part of the comment will be returned (unless it is a
- /// language directive comment, in which case it will also be skipped) or an
- /// '// ignore:' comment which should always be treated as attached to the
- /// import.
+ /// Leading comments for the first directive in a file with no library
+ /// directive (indicated with [isPseudoLibraryDirective]) are considered
+ /// library comments and not included unless they contain blank lines, in
+ /// which case only the last part of the comment will be returned (unless it
+ /// is a language directive comment, in which case it will also be skipped),
+ /// or an '// ignore:' comment which should always be treated as attached to
+ /// the import.
static Token? getLeadingComment(
- CompilationUnit unit, Token beginToken, LineInfo lineInfo) {
+ CompilationUnit unit, Token beginToken, LineInfo lineInfo,
+ {required bool isPseudoLibraryDirective}) {
if (beginToken.precedingComments == null) {
return null;
}
@@ -220,8 +223,9 @@
Token? firstComment = beginToken.precedingComments;
var comment = firstComment;
var nextComment = comment?.next;
- // Don't connect comments that have a blank line between them
- while (comment != null && nextComment != null) {
+ // Don't connect comments that have a blank line between them if this is
+ // a psuedo-library directive.
+ while (isPseudoLibraryDirective && comment != null && nextComment != null) {
var currentLine = lineInfo.getLocation(comment.offset).lineNumber;
var nextLine = lineInfo.getLocation(nextComment.offset).lineNumber;
if (nextLine - currentLine > 1) {
diff --git a/pkg/analysis_server/test/services/correction/organize_directives_test.dart b/pkg/analysis_server/test/services/correction/organize_directives_test.dart
index 686b25d..3d59aee 100644
--- a/pkg/analysis_server/test/services/correction/organize_directives_test.dart
+++ b/pkg/analysis_server/test/services/correction/organize_directives_test.dart
@@ -561,19 +561,25 @@
''');
}
- Future<void>
- test_sort_imports_dontConnectFirstCommentsWithBlankLinesBetween() async {
+ Future<void> test_sort_imports_blankLinesInImportComments() async {
+ // Only the blank line in the first import is treated specially and split.
await _computeUnitAndErrors(r'''
-// Copyright...
+// Import 1 comment 1
-// Some comment related to the line below
+// Import 1 comment 2
import 'package:b/a.dart';
+// Import 2 comment 1
+
+// Import 2 comment 2
import 'package:a/b.dart';''');
_assertOrganize(r'''
-// Copyright...
+// Import 1 comment 1
+// Import 2 comment 1
+
+// Import 2 comment 2
import 'package:a/b.dart';
-// Some comment related to the line below
+// Import 1 comment 2
import 'package:b/a.dart';''');
}
@@ -658,7 +664,8 @@
''');
}
- Future<void> test_sort_imports_with_library_keepPrecedingComments() async {
+ Future<void>
+ test_sort_imports_with_library_blankLineInImportComments() async {
await _computeUnitAndErrors(r'''
/// Copyright...
library lib;
@@ -677,11 +684,11 @@
/// Copyright...
library lib;
-// Test comment
-
// Comment for a
import 'package:a/b.dart';
+// Test comment
+
// We are keeping this because ... l1
// We are keeping this because ... l2
// We are keeping this because ... l3