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

Unified Diff: pkg/analysis_server/lib/src/computer/import_elements_computer.dart

Issue 3002643002: Fix insertion of imports when there are no existing directives (issue 30430) (Closed)
Patch Set: Created 3 years, 4 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
Index: pkg/analysis_server/lib/src/computer/import_elements_computer.dart
diff --git a/pkg/analysis_server/lib/src/computer/import_elements_computer.dart b/pkg/analysis_server/lib/src/computer/import_elements_computer.dart
index c30639db624cc769d0d17e934eb579383e3279b2..11491075f5c3541a0f6db4ae2806ba0765a59afb 100644
--- a/pkg/analysis_server/lib/src/computer/import_elements_computer.dart
+++ b/pkg/analysis_server/lib/src/computer/import_elements_computer.dart
@@ -77,9 +77,11 @@ class ImportElementsComputer {
Source importedSource = importedFile.createSource(uri);
String importUri =
_getLibrarySourceUri(libraryElement, importedSource);
- int offset = _offsetForInsertion(importUri);
- builder.addInsertion(offset, (DartEditBuilder builder) {
- builder.writeln();
+ _InsertionDescription description = _offsetForInsertion(importUri);
+ builder.addInsertion(description.offset, (DartEditBuilder builder) {
+ for (int i = 0; i < description.newLinesBefore; i++) {
+ builder.writeln();
+ }
builder.write("import '");
builder.write(importUri);
builder.write("'");
@@ -88,6 +90,9 @@ class ImportElementsComputer {
builder.write(importedElements.prefix);
}
builder.write(';');
+ for (int i = 0; i < description.newLinesAfter; i++) {
+ builder.writeln();
+ }
});
} else {
//
@@ -345,26 +350,33 @@ class ImportElementsComputer {
*
* Partially copied from DartFileEditBuilderImpl.
*/
- int _offsetForInsertion(String importUri) {
- // TODO(brianwilkerson) Fix this to find the right location.
- // See DartFileEditBuilderImpl._addLibraryImports for inspiration.
+ _InsertionDescription _offsetForInsertion(String importUri) {
scheglov 2017/08/14 18:37:51 Maybe rename the method, it returns not just the o
Brian Wilkerson 2017/08/14 19:59:20 Done
CompilationUnit unit = libraryResult.unit;
LibraryDirective libraryDirective;
List<ImportDirective> importDirectives = <ImportDirective>[];
+ List<Directive> otherDirectives = <Directive>[];
for (Directive directive in unit.directives) {
if (directive is LibraryDirective) {
libraryDirective = directive;
} else if (directive is ImportDirective) {
importDirectives.add(directive);
+ } else {
+ otherDirectives.add(directive);
}
}
if (importDirectives.isEmpty) {
if (libraryDirective == null) {
- return 0;
+ if (otherDirectives.isEmpty) {
+ // TODO(brianwilkerson) Insert after any non-doc comments.
+ return new _InsertionDescription(0, 0, 2);
+ }
+ return new _InsertionDescription(otherDirectives[0].offset, 0, 2);
}
- return libraryDirective.end;
+ return new _InsertionDescription(libraryDirective.end, 2, 0);
}
- return importDirectives.last.end;
+ // TODO(brianwilkerson) Fix this to find the right location.
+ // See DartFileEditBuilderImpl._addLibraryImports for inspiration.
+ return new _InsertionDescription(importDirectives.last.end, 1, 0);
}
}
@@ -408,3 +420,11 @@ class _ImportUpdate {
namesToUnhide.add(name);
}
}
+
+class _InsertionDescription {
+ final int newLinesBefore;
+ final int offset;
+ final int newLinesAfter;
+
+ _InsertionDescription(this.offset, this.newLinesBefore, this.newLinesAfter);
scheglov 2017/08/14 18:37:51 Maybe reorder the parameters to correspond to the
Brian Wilkerson 2017/08/14 19:59:20 Reworked to use named parameters
+}

Powered by Google App Engine
This is Rietveld 408576698