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

Side by Side Diff: pkg/analyzer_plugin/lib/src/utilities/change_builder/change_builder_dart.dart

Issue 2972463003: Use DartEditBuilder to write types and remove dead code (Closed)
Patch Set: Created 3 years, 5 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 unified diff | Download patch
OLDNEW
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:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/token.dart'; 8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/dart/element/type.dart'; 10 import 'package:analyzer/dart/element/type.dart';
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 {bool addSupertypeProposals: false, 479 {bool addSupertypeProposals: false,
480 String groupName, 480 String groupName,
481 ExecutableElement methodBeingCopied, 481 ExecutableElement methodBeingCopied,
482 bool required: false}) { 482 bool required: false}) {
483 if (type != null && !type.isDynamic) { 483 if (type != null && !type.isDynamic) {
484 _EnclosingElementFinder finder = new _EnclosingElementFinder(); 484 _EnclosingElementFinder finder = new _EnclosingElementFinder();
485 finder.find(dartFileEditBuilder.unit, offset); 485 finder.find(dartFileEditBuilder.unit, offset);
486 String typeSource = _getTypeSource( 486 String typeSource = _getTypeSource(
487 type, finder.enclosingClass, finder.enclosingExecutable, 487 type, finder.enclosingClass, finder.enclosingExecutable,
488 methodBeingCopied: methodBeingCopied); 488 methodBeingCopied: methodBeingCopied);
489 if (typeSource != 'dynamic') { 489 if (typeSource.isNotEmpty && typeSource != 'dynamic') {
490 if (groupName != null) { 490 if (groupName != null) {
491 addLinkedEdit(groupName, (LinkedEditBuilder builder) { 491 addLinkedEdit(groupName, (LinkedEditBuilder builder) {
492 write(typeSource); 492 write(typeSource);
493 if (addSupertypeProposals) { 493 if (addSupertypeProposals) {
494 _addSuperTypeProposals(builder, type, new Set<DartType>()); 494 _addSuperTypeProposals(builder, type, new Set<DartType>());
495 } 495 }
496 }); 496 });
497 } else { 497 } else {
498 write(typeSource); 498 write(typeSource);
499 } 499 }
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 if (type is TypeParameterType) { 950 if (type is TypeParameterType) {
951 TypeParameterElement parameterElement = type.element; 951 TypeParameterElement parameterElement = type.element;
952 Element parameterParent = parameterElement.enclosingElement; 952 Element parameterParent = parameterElement.enclosingElement;
953 // TODO(brianwilkerson) This needs to compare the parameterParent with 953 // TODO(brianwilkerson) This needs to compare the parameterParent with
954 // each of the parents of the enclosingElement. (That means that we only 954 // each of the parents of the enclosingElement. (That means that we only
955 // need the most closely enclosing element.) 955 // need the most closely enclosing element.)
956 return parameterParent == enclosingExecutable || 956 return parameterParent == enclosingExecutable ||
957 parameterParent == enclosingClass || 957 parameterParent == enclosingClass ||
958 parameterParent == methodBeingCopied; 958 parameterParent == methodBeingCopied;
959 } 959 }
960 Element element = type.element;
961 if (element == null) {
962 return true;
963 }
964 LibraryElement definingLibrary = element.library;
965 LibraryElement importingLibrary = dartFileEditBuilder.unit.element.library;
966 if (definingLibrary != null && definingLibrary != importingLibrary) {
967 if (element.isPrivate) {
968 return false;
969 }
970 }
960 return true; 971 return true;
961 } 972 }
962 } 973 }
963 974
964 /** 975 /**
965 * A [FileEditBuilder] used to build edits for Dart files. 976 * A [FileEditBuilder] used to build edits for Dart files.
966 */ 977 */
967 class DartFileEditBuilderImpl extends FileEditBuilderImpl 978 class DartFileEditBuilderImpl extends FileEditBuilderImpl
968 implements DartFileEditBuilder { 979 implements DartFileEditBuilder {
969 /** 980 /**
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1008 _replaceReturnTypeWithFuture(body, typeProvider); 1019 _replaceReturnTypeWithFuture(body, typeProvider);
1009 } 1020 }
1010 1021
1011 @override 1022 @override
1012 DartEditBuilderImpl createEditBuilder(int offset, int length) { 1023 DartEditBuilderImpl createEditBuilder(int offset, int length) {
1013 return new DartEditBuilderImpl(this, offset, length); 1024 return new DartEditBuilderImpl(this, offset, length);
1014 } 1025 }
1015 1026
1016 @override 1027 @override
1017 void finalize() { 1028 void finalize() {
1018 _addLibraryImports( 1029 CompilationUnitElement unitElement = unit.element;
1019 changeBuilder.sourceChange, unit.element.library, librariesToImport); 1030 LibraryElement libraryElement = unitElement.library;
1031 CompilationUnitElement definingUnitElement =
1032 libraryElement.definingCompilationUnit;
1033 if (definingUnitElement == unitElement) {
1034 _addLibraryImports(libraryElement, librariesToImport);
1035 } else {
1036 (changeBuilder as DartChangeBuilder).addFileEdit(
1037 definingUnitElement.source.fullName, (DartFileEditBuilder builder) {
1038 (builder as DartFileEditBuilderImpl)
1039 ._addLibraryImports(libraryElement, librariesToImport);
1040 });
1041 }
1020 } 1042 }
1021 1043
1022 @override 1044 @override
1023 void importLibraries(Iterable<Source> libraries) { 1045 void importLibraries(Iterable<Source> libraries) {
1024 librariesToImport.addAll(libraries); 1046 librariesToImport.addAll(libraries);
1025 } 1047 }
1026 1048
1027 @override 1049 @override
1028 void replaceTypeWithFuture( 1050 void replaceTypeWithFuture(
1029 TypeAnnotation typeAnnotation, TypeProvider typeProvider) { 1051 TypeAnnotation typeAnnotation, TypeProvider typeProvider) {
(...skipping 14 matching lines...) Expand all
1044 } 1066 }
1045 // prepare code for the types 1067 // prepare code for the types
1046 addReplacement(range.node(typeAnnotation), (EditBuilder builder) { 1068 addReplacement(range.node(typeAnnotation), (EditBuilder builder) {
1047 if (!(builder as DartEditBuilder).writeType(futureType)) { 1069 if (!(builder as DartEditBuilder).writeType(futureType)) {
1048 builder.write('void'); 1070 builder.write('void');
1049 } 1071 }
1050 }); 1072 });
1051 } 1073 }
1052 1074
1053 /** 1075 /**
1054 * Adds edits to the given [change] that ensure that all the [libraries] are 1076 * Adds edits ensure that all the [libraries] are imported into the given
1055 * imported into the given [targetLibrary]. 1077 * [targetLibrary].
1056 */ 1078 */
1057 void _addLibraryImports(SourceChange change, LibraryElement targetLibrary, 1079 void _addLibraryImports(LibraryElement targetLibrary, Set<Source> libraries) {
1058 Set<Source> libraries) {
1059 // Prepare information about existing imports. 1080 // Prepare information about existing imports.
1060 LibraryDirective libraryDirective; 1081 LibraryDirective libraryDirective;
1061 List<ImportDirective> importDirectives = <ImportDirective>[]; 1082 List<ImportDirective> importDirectives = <ImportDirective>[];
1062 for (Directive directive in unit.directives) { 1083 for (Directive directive in unit.directives) {
1063 if (directive is LibraryDirective) { 1084 if (directive is LibraryDirective) {
1064 libraryDirective = directive; 1085 libraryDirective = directive;
1065 } else if (directive is ImportDirective) { 1086 } else if (directive is ImportDirective) {
1066 importDirectives.add(directive); 1087 importDirectives.add(directive);
1067 } 1088 }
1068 } 1089 }
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
1366 } 1387 }
1367 } 1388 }
1368 1389
1369 class _InsertionDescription { 1390 class _InsertionDescription {
1370 final int offset; 1391 final int offset;
1371 final bool insertEmptyLineBefore; 1392 final bool insertEmptyLineBefore;
1372 final bool insertEmptyLineAfter; 1393 final bool insertEmptyLineAfter;
1373 _InsertionDescription( 1394 _InsertionDescription(
1374 this.offset, this.insertEmptyLineBefore, this.insertEmptyLineAfter); 1395 this.offset, this.insertEmptyLineBefore, this.insertEmptyLineAfter);
1375 } 1396 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698