Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Unified Diff: pkg/analysis_server/lib/src/services/correction/sort_members.dart

Issue 1843763002: Fix for sorting directives with documentation comments. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Keep the documentation comment of the first directive before sorting attached to the first directiv… Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/correction/sort_members_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/services/correction/sort_members.dart
diff --git a/pkg/analysis_server/lib/src/services/correction/sort_members.dart b/pkg/analysis_server/lib/src/services/correction/sort_members.dart
index f1d788c228b3c86e4903290e35c6cdcea669fecb..d5bb1265409d9b23d838802a1e28e82aabe5c562 100644
--- a/pkg/analysis_server/lib/src/services/correction/sort_members.dart
+++ b/pkg/analysis_server/lib/src/services/correction/sort_members.dart
@@ -7,7 +7,6 @@ library services.src.refactoring.sort_members;
import 'package:analysis_server/plugin/protocol/protocol.dart' hide Element;
import 'package:analysis_server/src/services/correction/strings.dart';
import 'package:analyzer/dart/ast/ast.dart';
-import 'package:analyzer/dart/ast/token.dart';
/**
* Sorter for unit/class members.
@@ -161,8 +160,12 @@ class MemberSorter {
* Sorts all [Directive]s.
*/
void _sortUnitDirectives() {
+ bool hasLibraryDirective = false;
List<_DirectiveInfo> directives = [];
for (Directive directive in unit.directives) {
+ if (directive is LibraryDirective) {
+ hasLibraryDirective = true;
+ }
if (directive is! UriBasedDirective) {
continue;
}
@@ -195,10 +198,17 @@ class MemberSorter {
kind = _DirectivePriority.PART;
}
if (kind != null) {
- int offset = directive.offset;
- int length = directive.length;
+ String documentationText;
+ if (directive.documentationComment != null) {
+ documentationText = code.substring(
+ directive.documentationComment.offset,
+ directive.documentationComment.end);
+ }
+ int offset = directive.firstTokenAfterCommentAndMetadata.offset;
+ int length = directive.end - offset;
String text = code.substring(offset, offset + length);
- directives.add(new _DirectiveInfo(directive, kind, uriContent, text));
+ directives.add(new _DirectiveInfo(
+ directive, kind, uriContent, documentationText, text));
}
}
// nothing to do
@@ -207,6 +217,12 @@ class MemberSorter {
}
int firstDirectiveOffset = directives[0].directive.offset;
int lastDirectiveEnd = directives[directives.length - 1].directive.end;
+ // Without a library directive, the library comment is the comment of the
+ // first directive.
+ _DirectiveInfo libraryDocumentationDirective;
+ if (!hasLibraryDirective && directives.isNotEmpty) {
+ libraryDocumentationDirective = directives.first;
+ }
// do sort
directives.sort();
// append directives with grouping
@@ -215,6 +231,7 @@ class MemberSorter {
StringBuffer sb = new StringBuffer();
String endOfLine = this.endOfLine;
_DirectivePriority currentPriority = null;
+ bool firstOutputDirective = true;
for (_DirectiveInfo directive in directives) {
if (currentPriority != directive.priority) {
if (sb.length != 0) {
@@ -222,35 +239,25 @@ class MemberSorter {
}
currentPriority = directive.priority;
}
+ if (directive != libraryDocumentationDirective &&
+ directive.documentationText != null) {
+ sb.write(directive.documentationText);
+ sb.write(endOfLine);
+ }
+ if (firstOutputDirective) {
+ firstOutputDirective = false;
+ if (libraryDocumentationDirective != null &&
+ libraryDocumentationDirective.documentationText != null) {
+ sb.write(libraryDocumentationDirective.documentationText);
+ sb.write(endOfLine);
+ }
+ }
sb.write(directive.text);
sb.write(endOfLine);
}
directivesCode = sb.toString();
directivesCode = directivesCode.trimRight();
}
- // append comment tokens which otherwise would be removed completely
- {
- bool firstCommentToken = true;
- Token token = unit.beginToken;
- while (token != null &&
- token.type != TokenType.EOF &&
- token.end < lastDirectiveEnd) {
- Token commentToken = token.precedingComments;
- while (commentToken != null) {
- int offset = commentToken.offset;
- int end = commentToken.end;
- if (offset > firstDirectiveOffset && offset < lastDirectiveEnd) {
- if (firstCommentToken) {
- directivesCode += endOfLine;
- firstCommentToken = false;
- }
- directivesCode += code.substring(offset, end) + endOfLine;
- }
- commentToken = commentToken.next;
- }
- token = token.next;
- }
- }
// prepare code
String beforeDirectives = code.substring(0, firstDirectiveOffset);
String afterDirectives = code.substring(lastDirectiveEnd);
@@ -368,9 +375,11 @@ class _DirectiveInfo implements Comparable<_DirectiveInfo> {
final Directive directive;
final _DirectivePriority priority;
final String uri;
+ final String documentationText;
final String text;
- _DirectiveInfo(this.directive, this.priority, this.uri, this.text);
+ _DirectiveInfo(this.directive, this.priority, this.uri,
+ this.documentationText, this.text);
@override
int compareTo(_DirectiveInfo other) {
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/correction/sort_members_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698