Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analysis_server/protocol/protocol_generated.dart'; | 7 import 'package:analysis_server/protocol/protocol_generated.dart'; |
| 8 import 'package:analyzer/dart/analysis/results.dart'; | 8 import 'package:analyzer/dart/analysis/results.dart'; |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/ast_factory.dart'; | 10 import 'package:analyzer/dart/ast/ast_factory.dart'; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 if (matchingImports.isEmpty) { | 70 if (matchingImports.isEmpty) { |
| 71 // | 71 // |
| 72 // The required library is not being imported with a matching prefix, | 72 // The required library is not being imported with a matching prefix, |
| 73 // so we need to add an import. | 73 // so we need to add an import. |
| 74 // | 74 // |
| 75 File importedFile = resourceProvider.getFile(importedElements.path); | 75 File importedFile = resourceProvider.getFile(importedElements.path); |
| 76 Uri uri = sourceFactory.restoreUri(importedFile.createSource()); | 76 Uri uri = sourceFactory.restoreUri(importedFile.createSource()); |
| 77 Source importedSource = importedFile.createSource(uri); | 77 Source importedSource = importedFile.createSource(uri); |
| 78 String importUri = | 78 String importUri = |
| 79 _getLibrarySourceUri(libraryElement, importedSource); | 79 _getLibrarySourceUri(libraryElement, importedSource); |
| 80 int offset = _offsetForInsertion(importUri); | 80 _InsertionDescription description = _offsetForInsertion(importUri); |
| 81 builder.addInsertion(offset, (DartEditBuilder builder) { | 81 builder.addInsertion(description.offset, (DartEditBuilder builder) { |
| 82 builder.writeln(); | 82 for (int i = 0; i < description.newLinesBefore; i++) { |
| 83 builder.writeln(); | |
| 84 } | |
| 83 builder.write("import '"); | 85 builder.write("import '"); |
| 84 builder.write(importUri); | 86 builder.write(importUri); |
| 85 builder.write("'"); | 87 builder.write("'"); |
| 86 if (importedElements.prefix.isNotEmpty) { | 88 if (importedElements.prefix.isNotEmpty) { |
| 87 builder.write(' as '); | 89 builder.write(' as '); |
| 88 builder.write(importedElements.prefix); | 90 builder.write(importedElements.prefix); |
| 89 } | 91 } |
| 90 builder.write(';'); | 92 builder.write(';'); |
| 93 for (int i = 0; i < description.newLinesAfter; i++) { | |
| 94 builder.writeln(); | |
| 95 } | |
| 91 }); | 96 }); |
| 92 } else { | 97 } else { |
| 93 // | 98 // |
| 94 // There are some imports of the library with a matching prefix. We | 99 // There are some imports of the library with a matching prefix. We |
| 95 // need to determine whether the names are already visible or whether | 100 // need to determine whether the names are already visible or whether |
| 96 // we need to make edits to make them visible. | 101 // we need to make edits to make them visible. |
| 97 // | 102 // |
| 98 // Compute the edits that need to be made. | 103 // Compute the edits that need to be made. |
| 99 // | 104 // |
| 100 Map<ImportDirective, _ImportUpdate> updateMap = | 105 Map<ImportDirective, _ImportUpdate> updateMap = |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 importedElements.path && | 343 importedElements.path && |
| 339 (import.prefix?.name ?? '') == importedElements.prefix; | 344 (import.prefix?.name ?? '') == importedElements.prefix; |
| 340 } | 345 } |
| 341 | 346 |
| 342 /** | 347 /** |
| 343 * Return the offset at which an import of the given [importUri] should be | 348 * Return the offset at which an import of the given [importUri] should be |
| 344 * inserted. | 349 * inserted. |
| 345 * | 350 * |
| 346 * Partially copied from DartFileEditBuilderImpl. | 351 * Partially copied from DartFileEditBuilderImpl. |
| 347 */ | 352 */ |
| 348 int _offsetForInsertion(String importUri) { | 353 _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
| |
| 349 // TODO(brianwilkerson) Fix this to find the right location. | |
| 350 // See DartFileEditBuilderImpl._addLibraryImports for inspiration. | |
| 351 CompilationUnit unit = libraryResult.unit; | 354 CompilationUnit unit = libraryResult.unit; |
| 352 LibraryDirective libraryDirective; | 355 LibraryDirective libraryDirective; |
| 353 List<ImportDirective> importDirectives = <ImportDirective>[]; | 356 List<ImportDirective> importDirectives = <ImportDirective>[]; |
| 357 List<Directive> otherDirectives = <Directive>[]; | |
| 354 for (Directive directive in unit.directives) { | 358 for (Directive directive in unit.directives) { |
| 355 if (directive is LibraryDirective) { | 359 if (directive is LibraryDirective) { |
| 356 libraryDirective = directive; | 360 libraryDirective = directive; |
| 357 } else if (directive is ImportDirective) { | 361 } else if (directive is ImportDirective) { |
| 358 importDirectives.add(directive); | 362 importDirectives.add(directive); |
| 363 } else { | |
| 364 otherDirectives.add(directive); | |
| 359 } | 365 } |
| 360 } | 366 } |
| 361 if (importDirectives.isEmpty) { | 367 if (importDirectives.isEmpty) { |
| 362 if (libraryDirective == null) { | 368 if (libraryDirective == null) { |
| 363 return 0; | 369 if (otherDirectives.isEmpty) { |
| 370 // TODO(brianwilkerson) Insert after any non-doc comments. | |
| 371 return new _InsertionDescription(0, 0, 2); | |
| 372 } | |
| 373 return new _InsertionDescription(otherDirectives[0].offset, 0, 2); | |
| 364 } | 374 } |
| 365 return libraryDirective.end; | 375 return new _InsertionDescription(libraryDirective.end, 2, 0); |
| 366 } | 376 } |
| 367 return importDirectives.last.end; | 377 // TODO(brianwilkerson) Fix this to find the right location. |
| 378 // See DartFileEditBuilderImpl._addLibraryImports for inspiration. | |
| 379 return new _InsertionDescription(importDirectives.last.end, 1, 0); | |
| 368 } | 380 } |
| 369 } | 381 } |
| 370 | 382 |
| 371 /** | 383 /** |
| 372 * Information about how a given import directive needs to be updated in order | 384 * Information about how a given import directive needs to be updated in order |
| 373 * to make the required names visible. | 385 * to make the required names visible. |
| 374 */ | 386 */ |
| 375 class _ImportUpdate { | 387 class _ImportUpdate { |
| 376 /** | 388 /** |
| 377 * The import directive to be updated. | 389 * The import directive to be updated. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 401 namesToShow.add(name); | 413 namesToShow.add(name); |
| 402 } | 414 } |
| 403 | 415 |
| 404 /** | 416 /** |
| 405 * Record that the given [name] needs to be removed from hide combinators. | 417 * Record that the given [name] needs to be removed from hide combinators. |
| 406 */ | 418 */ |
| 407 void unhide(String name) { | 419 void unhide(String name) { |
| 408 namesToUnhide.add(name); | 420 namesToUnhide.add(name); |
| 409 } | 421 } |
| 410 } | 422 } |
| 423 | |
| 424 class _InsertionDescription { | |
| 425 final int newLinesBefore; | |
| 426 final int offset; | |
| 427 final int newLinesAfter; | |
| 428 | |
| 429 _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
| |
| 430 } | |
| OLD | NEW |