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

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: address comments 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
« no previous file with comments | « no previous file | pkg/analysis_server/test/src/computer/import_elements_computer_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/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..0b3617e57df981606b29b8de212b371e8a3e1162 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,12 @@ 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 =
+ _getInsertionDescription(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 +91,9 @@ class ImportElementsComputer {
builder.write(importedElements.prefix);
}
builder.write(';');
+ for (int i = 0; i < description.newLinesAfter; i++) {
+ builder.writeln();
+ }
});
} else {
//
@@ -309,6 +315,41 @@ class ImportElementsComputer {
}
/**
+ * Return the offset at which an import of the given [importUri] should be
+ * inserted.
+ *
+ * Partially copied from DartFileEditBuilderImpl.
+ */
+ _InsertionDescription _getInsertionDescription(String importUri) {
+ 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) {
+ if (otherDirectives.isEmpty) {
+ // TODO(brianwilkerson) Insert after any non-doc comments.
+ return new _InsertionDescription(0, after: 2);
+ }
+ return new _InsertionDescription(otherDirectives[0].offset, after: 2);
+ }
+ return new _InsertionDescription(libraryDirective.end, before: 2);
+ }
+ // TODO(brianwilkerson) Fix this to find the right location.
+ // See DartFileEditBuilderImpl._addLibraryImports for inspiration.
+ return new _InsertionDescription(importDirectives.last.end, before: 1);
+ }
+
+ /**
* Computes the best URI to import [what] into [from].
*
* Copied from DartFileEditBuilderImpl.
@@ -338,34 +379,6 @@ class ImportElementsComputer {
importedElements.path &&
(import.prefix?.name ?? '') == importedElements.prefix;
}
-
- /**
- * Return the offset at which an import of the given [importUri] should be
- * inserted.
- *
- * Partially copied from DartFileEditBuilderImpl.
- */
- int _offsetForInsertion(String importUri) {
- // TODO(brianwilkerson) Fix this to find the right location.
- // See DartFileEditBuilderImpl._addLibraryImports for inspiration.
- CompilationUnit unit = libraryResult.unit;
- LibraryDirective libraryDirective;
- List<ImportDirective> importDirectives = <ImportDirective>[];
- for (Directive directive in unit.directives) {
- if (directive is LibraryDirective) {
- libraryDirective = directive;
- } else if (directive is ImportDirective) {
- importDirectives.add(directive);
- }
- }
- if (importDirectives.isEmpty) {
- if (libraryDirective == null) {
- return 0;
- }
- return libraryDirective.end;
- }
- return importDirectives.last.end;
- }
}
/**
@@ -408,3 +421,13 @@ class _ImportUpdate {
namesToUnhide.add(name);
}
}
+
+class _InsertionDescription {
+ final int newLinesBefore;
+ final int offset;
+ final int newLinesAfter;
+
+ _InsertionDescription(this.offset, {int before: 0, int after: 0})
+ : this.newLinesBefore = before,
+ this.newLinesAfter = after;
+}
« no previous file with comments | « no previous file | pkg/analysis_server/test/src/computer/import_elements_computer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698