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

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

Issue 1019993002: Issue 22851. Append comment tokens after sorted directives. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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 a7270c2055cbbf147aa138f2a8d33beaef58b502..c66c52183d588212a099d8351dacd63306b47510 100644
--- a/pkg/analysis_server/lib/src/services/correction/sort_members.dart
+++ b/pkg/analysis_server/lib/src/services/correction/sort_members.dart
@@ -7,6 +7,7 @@ library services.src.refactoring.sort_members;
import 'package:analysis_server/src/protocol.dart' hide Element;
import 'package:analysis_server/src/services/correction/strings.dart';
import 'package:analyzer/src/generated/ast.dart';
+import 'package:analyzer/src/generated/scanner.dart';
/**
* Sorter for unit/class members.
@@ -41,20 +42,11 @@ class MemberSorter {
final String initialCode;
final CompilationUnit unit;
String code;
+ String endOfLine;
MemberSorter(this.initialCode, this.unit) {
this.code = initialCode;
- }
-
- /**
- * Return the EOL to use for [code].
- */
- String get endOfLine {
- if (code.contains('\r\n')) {
- return '\r\n';
- } else {
- return '\n';
- }
+ this.endOfLine = getEOL(code);
}
/**
@@ -233,6 +225,29 @@ class MemberSorter {
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);
@@ -297,6 +312,17 @@ class MemberSorter {
_sortAndReorderMembers(members);
}
+ /**
+ * Return the EOL to use for [code].
+ */
+ static String getEOL(String code) {
+ if (code.contains('\r\n')) {
+ return '\r\n';
+ } else {
+ return '\n';
+ }
+ }
+
static int _getPriority(_PriorityItem item) {
for (int i = 0; i < _PRIORITY_ITEMS.length; i++) {
if (_PRIORITY_ITEMS[i] == item) {
« 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