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

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

Issue 2972463003: Use DartEditBuilder to write types and remove dead code (Closed)
Patch Set: Created 3 years, 6 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/services/correction/assist_internal.dart
diff --git a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
index 007b926eb3621e9ba6a1cc4880e7997394b4f92e..3f007f82d3860aa999e4f77144dec08ccfe59267 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
@@ -221,26 +221,26 @@ class AssistProcessor {
return;
}
_configureTargetLocation(node);
- Set<Source> librariesToImport = new Set<Source>();
- String typeSource = utils.getTypeSource(type, librariesToImport);
- if (typeSource == null) {
- // The type source might be null if the type is private.
- _coverageMarker();
- return;
- }
DartChangeBuilder changeBuilder = new DartChangeBuilder(driver);
+ bool validChange = true;
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
Token keyword = declaredIdentifier.keyword;
if (keyword.keyword == Keyword.VAR) {
- builder.addSimpleReplacement(range.token(keyword), typeSource);
+ builder.addReplacement(range.token(keyword), (DartEditBuilder builder) {
+ validChange = builder.writeType(type);
+ });
} else {
- builder.addSimpleInsertion(
- declaredIdentifier.identifier.offset, '$typeSource ');
+ builder.addInsertion(declaredIdentifier.identifier.offset,
+ (DartEditBuilder builder) {
+ validChange = builder.writeType(type);
+ builder.write(' ');
+ });
}
- builder.importLibraries(librariesToImport);
});
- _addAssistFromBuilder(changeBuilder, DartAssistKind.ADD_TYPE_ANNOTATION);
+ if (validChange) {
+ _addAssistFromBuilder(changeBuilder, DartAssistKind.ADD_TYPE_ANNOTATION);
+ }
}
Future<Null> _addProposal_addTypeAnnotation_SimpleFormalParameter() async {
@@ -269,20 +269,18 @@ class AssistProcessor {
}
// prepare type source
_configureTargetLocation(node);
- Set<Source> librariesToImport = new Set<Source>();
- String typeSource = utils.getTypeSource(type, librariesToImport);
- // type source might be null, if the type is private
- if (typeSource == null) {
- _coverageMarker();
- return;
- }
DartChangeBuilder changeBuilder = new DartChangeBuilder(driver);
+ bool validChange = true;
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
- builder.addSimpleInsertion(name.offset, '$typeSource ');
- builder.importLibraries(librariesToImport);
+ builder.addInsertion(name.offset, (DartEditBuilder builder) {
+ validChange = builder.writeType(type);
+ builder.write(' ');
+ });
});
- _addAssistFromBuilder(changeBuilder, DartAssistKind.ADD_TYPE_ANNOTATION);
+ if (validChange) {
+ _addAssistFromBuilder(changeBuilder, DartAssistKind.ADD_TYPE_ANNOTATION);
+ }
}
Future<Null> _addProposal_addTypeAnnotation_VariableDeclaration() async {
@@ -325,42 +323,25 @@ class AssistProcessor {
return;
}
_configureTargetLocation(node);
- Set<Source> librariesToImport = new Set<Source>();
- String typeSource = utils.getTypeSource(type, librariesToImport);
- // type source might be null, if the type is private
- if (typeSource == null) {
- _coverageMarker();
- return;
- }
DartChangeBuilder changeBuilder = new DartChangeBuilder(driver);
- if (unitLibraryFile == file) {
- // TODO(brianwilkerson) Make ChangeBuilder merge multiple edits to the
- // same file so that only the else block is necessary.
- await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
- Token keyword = declarationList.keyword;
- if (keyword?.keyword == Keyword.VAR) {
- builder.addSimpleReplacement(range.token(keyword), typeSource);
- } else {
- builder.addSimpleInsertion(variable.offset, '$typeSource ');
- }
- builder.importLibraries(librariesToImport);
- });
- } else {
- await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
- Token keyword = declarationList.keyword;
- if (keyword?.keyword == Keyword.VAR) {
- builder.addSimpleReplacement(range.token(keyword), typeSource);
- } else {
- builder.addSimpleInsertion(variable.offset, '$typeSource ');
- }
- });
- await changeBuilder.addFileEdit(unitLibraryFile,
- (DartFileEditBuilder builder) {
- builder.importLibraries(librariesToImport);
- });
+ bool validChange = true;
+ await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
+ Token keyword = declarationList.keyword;
+ if (keyword?.keyword == Keyword.VAR) {
+ builder.addReplacement(range.token(keyword), (DartEditBuilder builder) {
+ validChange = builder.writeType(type);
+ });
+ } else {
+ builder.addInsertion(variable.offset, (DartEditBuilder builder) {
+ validChange = builder.writeType(type);
+ builder.write(' ');
+ });
+ }
+ });
+ if (validChange) {
+ _addAssistFromBuilder(changeBuilder, DartAssistKind.ADD_TYPE_ANNOTATION);
}
- _addAssistFromBuilder(changeBuilder, DartAssistKind.ADD_TYPE_ANNOTATION);
}
Future<Null> _addProposal_assignToLocalVariable() async {
@@ -1091,8 +1072,6 @@ class AssistProcessor {
String name = (node as SimpleIdentifier).name;
// prepare type
DartType type = parameterElement.type;
- Set<Source> librariesToImport = new Set<Source>();
- String typeCode = utils.getTypeSource(type, librariesToImport);
DartChangeBuilder changeBuilder = new DartChangeBuilder(driver);
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
@@ -1100,8 +1079,12 @@ class AssistProcessor {
if (type.isDynamic) {
builder.addSimpleReplacement(range.node(parameter), name);
} else {
- builder.addSimpleReplacement(
- range.node(parameter), '$typeCode $name');
+ builder.addReplacement(range.node(parameter),
+ (DartEditBuilder builder) {
+ builder.writeType(type);
+ builder.write(' ');
+ builder.write(name);
+ });
}
// add field initializer
List<ConstructorInitializer> initializers = constructor.initializers;

Powered by Google App Engine
This is Rietveld 408576698