| OLD | NEW |
| 1 // Copyright (c) 2015, 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 hide Element, ElementKind; | 8 hide Element, ElementKind; |
| 9 import 'package:analysis_server/src/provisional/edit/utilities/change_builder_co
re.dart'; | |
| 10 import 'package:analysis_server/src/provisional/edit/utilities/change_builder_da
rt.dart'; | |
| 11 import 'package:analysis_server/src/services/correction/name_suggestion.dart'; | 9 import 'package:analysis_server/src/services/correction/name_suggestion.dart'; |
| 12 import 'package:analysis_server/src/services/correction/source_range.dart'; | 10 import 'package:analysis_server/src/services/correction/source_range.dart'; |
| 13 import 'package:analysis_server/src/services/correction/util.dart'; | 11 import 'package:analysis_server/src/services/correction/util.dart'; |
| 14 import 'package:analysis_server/src/utilities/change_builder_core.dart'; | |
| 15 import 'package:analyzer/dart/ast/ast.dart'; | 12 import 'package:analyzer/dart/ast/ast.dart'; |
| 16 import 'package:analyzer/dart/ast/token.dart'; | 13 import 'package:analyzer/dart/ast/token.dart'; |
| 17 import 'package:analyzer/dart/element/element.dart'; | 14 import 'package:analyzer/dart/element/element.dart'; |
| 18 import 'package:analyzer/dart/element/type.dart'; | 15 import 'package:analyzer/dart/element/type.dart'; |
| 19 import 'package:analyzer/src/dart/analysis/driver.dart'; | 16 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 20 import 'package:analyzer/src/dart/ast/utilities.dart'; | 17 import 'package:analyzer/src/dart/ast/utilities.dart'; |
| 21 import 'package:analyzer/src/generated/engine.dart' hide AnalysisResult; | |
| 22 import 'package:analyzer/src/generated/resolver.dart'; | 18 import 'package:analyzer/src/generated/resolver.dart'; |
| 23 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| 24 import 'package:analyzer/src/generated/utilities_dart.dart'; | 20 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 21 import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_core
.dart'; |
| 22 import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dar
t'; |
| 23 import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dar
t'; |
| 25 | 24 |
| 26 /** | 25 /** |
| 27 * A [ChangeBuilder] used to build changes in Dart files. | 26 * A [ChangeBuilder] used to build changes in Dart files. |
| 28 */ | 27 */ |
| 29 class DartChangeBuilderImpl extends ChangeBuilderImpl | 28 class DartChangeBuilderImpl extends ChangeBuilderImpl |
| 30 implements DartChangeBuilder { | 29 implements DartChangeBuilder { |
| 31 /** | 30 /** |
| 32 * The analysis driver in which the files being edited were analyzed. | 31 * The analysis driver in which the files being edited were analyzed. |
| 33 */ | 32 */ |
| 34 final AnalysisDriver driver; | 33 final AnalysisDriver driver; |
| (...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 766 * The compilation unit to which the code will be added. | 765 * The compilation unit to which the code will be added. |
| 767 */ | 766 */ |
| 768 CompilationUnit unit; | 767 CompilationUnit unit; |
| 769 | 768 |
| 770 /** | 769 /** |
| 771 * A set containing the sources of the libraries that need to be imported in | 770 * A set containing the sources of the libraries that need to be imported in |
| 772 * order to make visible the names used in generated code. | 771 * order to make visible the names used in generated code. |
| 773 */ | 772 */ |
| 774 Set<Source> librariesToImport = new Set<Source>(); | 773 Set<Source> librariesToImport = new Set<Source>(); |
| 775 | 774 |
| 776 /** | 775 // /** |
| 777 * The content of the file being edited. | 776 // * The content of the file being edited. |
| 778 */ | 777 // */ |
| 779 String _content; | 778 // String _content; |
| 780 | 779 |
| 781 /** | 780 /** |
| 782 * Initialize a newly created builder to build a source file edit within the | 781 * Initialize a newly created builder to build a source file edit within the |
| 783 * change being built by the given [changeBuilder]. The file being edited has | 782 * change being built by the given [changeBuilder]. The file being edited has |
| 784 * the given [source] and [timeStamp], and the given fully resolved [unit]. | 783 * the given [source] and [timeStamp], and the given fully resolved [unit]. |
| 785 */ | 784 */ |
| 786 DartFileEditBuilderImpl(DartChangeBuilderImpl changeBuilder, String path, | 785 DartFileEditBuilderImpl(DartChangeBuilderImpl changeBuilder, String path, |
| 787 int timeStamp, this.unit) | 786 int timeStamp, this.unit) |
| 788 : super(changeBuilder, path, timeStamp); | 787 : super(changeBuilder, path, timeStamp); |
| 789 | 788 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 804 DartEditBuilderImpl createEditBuilder(int offset, int length) { | 803 DartEditBuilderImpl createEditBuilder(int offset, int length) { |
| 805 return new DartEditBuilderImpl(this, offset, length); | 804 return new DartEditBuilderImpl(this, offset, length); |
| 806 } | 805 } |
| 807 | 806 |
| 808 @override | 807 @override |
| 809 void finalize() { | 808 void finalize() { |
| 810 addLibraryImports( | 809 addLibraryImports( |
| 811 changeBuilder.sourceChange, unit.element.library, librariesToImport); | 810 changeBuilder.sourceChange, unit.element.library, librariesToImport); |
| 812 } | 811 } |
| 813 | 812 |
| 814 /** | 813 // /** |
| 815 * Return the content of the file being edited. | 814 // * Return the content of the file being edited. |
| 816 */ | 815 // */ |
| 817 String getContent() { | 816 // String getContent() { |
| 818 if (_content == null) { | 817 // if (_content == null) { |
| 819 CompilationUnitElement unitElement = unit.element; | 818 // CompilationUnitElement unitElement = unit.element; |
| 820 AnalysisContext context = unitElement.context; | 819 // AnalysisContext context = unitElement.context; |
| 821 if (context == null) { | 820 // if (context == null) { |
| 822 throw new CancelCorrectionException(); | 821 // throw new CancelCorrectionException(); |
| 823 } | 822 // } |
| 824 _content = context.getContents(unitElement.source).data; | 823 // _content = context.getContents(unitElement.source).data; |
| 825 } | 824 // } |
| 826 return _content; | 825 // return _content; |
| 827 } | 826 // } |
| 828 | 827 |
| 829 @override | 828 @override |
| 830 void importLibraries(Iterable<Source> libraries) { | 829 void importLibraries(Iterable<Source> libraries) { |
| 831 librariesToImport.addAll(libraries); | 830 librariesToImport.addAll(libraries); |
| 832 } | 831 } |
| 833 | 832 |
| 834 @override | 833 @override |
| 835 void replaceTypeWithFuture( | 834 void replaceTypeWithFuture( |
| 836 TypeAnnotation typeAnnotation, TypeProvider typeProvider) { | 835 TypeAnnotation typeAnnotation, TypeProvider typeProvider) { |
| 837 InterfaceType futureType = typeProvider.futureType; | 836 InterfaceType futureType = typeProvider.futureType; |
| 838 // | 837 // |
| 839 // Check whether the type needs to be replaced. | 838 // Check whether the type needs to be replaced. |
| 840 // | 839 // |
| 841 DartType type = typeAnnotation?.type; | 840 DartType type = typeAnnotation?.type; |
| 842 if (type == null || | 841 if (type == null || |
| 843 type.isDynamic || | 842 type.isDynamic || |
| 844 type is InterfaceType && type.element == futureType.element) { | 843 type is InterfaceType && type.element == futureType.element) { |
| 845 return; | 844 return; |
| 846 } | 845 } |
| 847 futureType = futureType.instantiate(<DartType>[type]); | 846 futureType = futureType.instantiate(<DartType>[type]); |
| 848 // prepare code for the types | 847 // prepare code for the types |
| 849 addReplacement(rangeNode(typeAnnotation), (EditBuilder builder) { | 848 addReplacement(rangeNode(typeAnnotation), (EditBuilder builder) { |
| 850 if (!(builder as DartEditBuilder).writeType(futureType)) { | 849 if (!(builder as DartEditBuilder).writeType(futureType)) { |
| 851 builder.write('void'); | 850 builder.write('void'); |
| 852 } | 851 } |
| 853 }); | 852 }); |
| 854 } | 853 } |
| 855 | 854 |
| 856 /** | 855 // /** |
| 857 * Returns the text of the given [AstNode] in the unit. | 856 // * Returns the text of the given [AstNode] in the unit. |
| 858 */ | 857 // */ |
| 859 String _getNodeText(AstNode node) { | 858 // String _getNodeText(AstNode node) { |
| 860 return _getText(node.offset, node.length); | 859 // return _getText(node.offset, node.length); |
| 861 } | 860 // } |
| 862 | 861 // |
| 863 /** | 862 // /** |
| 864 * Returns the text of the given range in the unit. | 863 // * Returns the text of the given range in the unit. |
| 865 */ | 864 // */ |
| 866 String _getText(int offset, int length) { | 865 // String _getText(int offset, int length) { |
| 867 return getContent().substring(offset, offset + length); | 866 // return getContent().substring(offset, offset + length); |
| 868 } | 867 // } |
| 869 | 868 |
| 870 /** | 869 /** |
| 871 * Create an edit to replace the return type of the innermost function | 870 * Create an edit to replace the return type of the innermost function |
| 872 * containing the given [node] with the type `Future`. The [typeProvider] is | 871 * containing the given [node] with the type `Future`. The [typeProvider] is |
| 873 * used to check the current return type, because if it is already `Future` no | 872 * used to check the current return type, because if it is already `Future` no |
| 874 * edit will be added. | 873 * edit will be added. |
| 875 */ | 874 */ |
| 876 void _replaceReturnTypeWithFuture(AstNode node, TypeProvider typeProvider) { | 875 void _replaceReturnTypeWithFuture(AstNode node, TypeProvider typeProvider) { |
| 877 while (node != null) { | 876 while (node != null) { |
| 878 node = node.parent; | 877 node = node.parent; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 enclosingExecutable = node.element; | 933 enclosingExecutable = node.element; |
| 935 } else if (node is MethodDeclaration) { | 934 } else if (node is MethodDeclaration) { |
| 936 enclosingExecutable = node.element; | 935 enclosingExecutable = node.element; |
| 937 } else if (node is FunctionDeclaration) { | 936 } else if (node is FunctionDeclaration) { |
| 938 enclosingExecutable = node.element; | 937 enclosingExecutable = node.element; |
| 939 } | 938 } |
| 940 node = node.parent; | 939 node = node.parent; |
| 941 } | 940 } |
| 942 } | 941 } |
| 943 } | 942 } |
| OLD | NEW |