| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library services.src.refactoring.rename_local; | 5 library services.src.refactoring.rename_local; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol_server.dart' hide Element; | 9 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| 10 import 'package:analysis_server/src/services/correction/status.dart'; | 10 import 'package:analysis_server/src/services/correction/status.dart'; |
| 11 import 'package:analysis_server/src/services/correction/util.dart'; | 11 import 'package:analysis_server/src/services/correction/util.dart'; |
| 12 import 'package:analysis_server/src/services/refactoring/naming_conventions.dart
'; | 12 import 'package:analysis_server/src/services/refactoring/naming_conventions.dart
'; |
| 13 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 13 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 14 import 'package:analysis_server/src/services/refactoring/rename.dart'; | 14 import 'package:analysis_server/src/services/refactoring/rename.dart'; |
| 15 import 'package:analysis_server/src/services/search/hierarchy.dart'; | 15 import 'package:analysis_server/src/services/search/hierarchy.dart'; |
| 16 import 'package:analysis_server/src/services/search/search_engine.dart'; | 16 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 17 import 'package:analyzer/dart/ast/ast.dart'; | 17 import 'package:analyzer/dart/ast/ast.dart'; |
| 18 import 'package:analyzer/dart/ast/visitor.dart'; | 18 import 'package:analyzer/dart/ast/visitor.dart'; |
| 19 import 'package:analyzer/dart/element/element.dart'; | 19 import 'package:analyzer/dart/element/element.dart'; |
| 20 import 'package:analyzer/src/dart/element/ast_provider.dart'; |
| 20 import 'package:analyzer/src/generated/source.dart'; | 21 import 'package:analyzer/src/generated/source.dart'; |
| 21 import 'package:analyzer/src/generated/utilities_dart.dart'; | 22 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 22 | 23 |
| 23 /** | 24 /** |
| 24 * A [Refactoring] for renaming [LocalElement]s. | 25 * A [Refactoring] for renaming [LocalElement]s. |
| 25 */ | 26 */ |
| 26 class RenameLocalRefactoringImpl extends RenameRefactoringImpl { | 27 class RenameLocalRefactoringImpl extends RenameRefactoringImpl { |
| 28 final AstProvider astProvider; |
| 29 final ResolvedUnitCache unitCache; |
| 30 |
| 27 Set<LocalElement> elements = new Set<LocalElement>(); | 31 Set<LocalElement> elements = new Set<LocalElement>(); |
| 28 | 32 |
| 29 RenameLocalRefactoringImpl(SearchEngine searchEngine, LocalElement element) | 33 RenameLocalRefactoringImpl( |
| 30 : super(searchEngine, element); | 34 SearchEngine searchEngine, this.astProvider, LocalElement element) |
| 35 : unitCache = new ResolvedUnitCache(astProvider), |
| 36 super(searchEngine, element); |
| 31 | 37 |
| 32 @override | 38 @override |
| 33 LocalElement get element => super.element as LocalElement; | 39 LocalElement get element => super.element as LocalElement; |
| 34 | 40 |
| 35 @override | 41 @override |
| 36 String get refactoringName { | 42 String get refactoringName { |
| 37 if (element is ParameterElement) { | 43 if (element is ParameterElement) { |
| 38 return "Rename Parameter"; | 44 return "Rename Parameter"; |
| 39 } | 45 } |
| 40 if (element is FunctionElement) { | 46 if (element is FunctionElement) { |
| 41 return "Rename Local Function"; | 47 return "Rename Local Function"; |
| 42 } | 48 } |
| 43 return "Rename Local Variable"; | 49 return "Rename Local Variable"; |
| 44 } | 50 } |
| 45 | 51 |
| 46 @override | 52 @override |
| 47 Future<RefactoringStatus> checkFinalConditions() async { | 53 Future<RefactoringStatus> checkFinalConditions() async { |
| 48 RefactoringStatus result = new RefactoringStatus(); | 54 RefactoringStatus result = new RefactoringStatus(); |
| 49 // prepare all elements (usually one) | |
| 50 await _prepareElements(); | 55 await _prepareElements(); |
| 51 // checks the resolved CompilationUnit(s) | |
| 52 for (LocalElement element in elements) { | 56 for (LocalElement element in elements) { |
| 53 Source unitSource = element.source; | 57 CompilationUnit unit = await unitCache.getUnit(element); |
| 54 List<Source> librarySources = context.getLibrariesContaining(unitSource); | 58 if (unit != null) { |
| 55 for (Source librarySource in librarySources) { | 59 SourceRange elementRange = element.visibleRange; |
| 56 _analyzePossibleConflicts_inLibrary( | 60 unit.accept(new _ConflictValidatorVisitor(this, result, elementRange)); |
| 57 result, unitSource, librarySource, element); | |
| 58 } | 61 } |
| 59 } | 62 } |
| 60 // done | |
| 61 return result; | 63 return result; |
| 62 } | 64 } |
| 63 | 65 |
| 64 @override | 66 @override |
| 65 RefactoringStatus checkNewName() { | 67 RefactoringStatus checkNewName() { |
| 66 RefactoringStatus result = super.checkNewName(); | 68 RefactoringStatus result = super.checkNewName(); |
| 67 if (element is LocalVariableElement) { | 69 if (element is LocalVariableElement) { |
| 68 result.addStatus(validateVariableName(newName)); | 70 result.addStatus(validateVariableName(newName)); |
| 69 } else if (element is ParameterElement) { | 71 } else if (element is ParameterElement) { |
| 70 result.addStatus(validateParameterName(newName)); | 72 result.addStatus(validateParameterName(newName)); |
| 71 } else if (element is FunctionElement) { | 73 } else if (element is FunctionElement) { |
| 72 result.addStatus(validateFunctionName(newName)); | 74 result.addStatus(validateFunctionName(newName)); |
| 73 } | 75 } |
| 74 return result; | 76 return result; |
| 75 } | 77 } |
| 76 | 78 |
| 77 @override | 79 @override |
| 78 Future fillChange() async { | 80 Future fillChange() async { |
| 79 for (Element element in elements) { | 81 for (Element element in elements) { |
| 80 addDeclarationEdit(element); | 82 addDeclarationEdit(element); |
| 81 await searchEngine.searchReferences(element).then(addReferenceEdits); | 83 await searchEngine.searchReferences(element).then(addReferenceEdits); |
| 82 } | 84 } |
| 83 } | 85 } |
| 84 | 86 |
| 85 void _analyzePossibleConflicts_inLibrary(RefactoringStatus result, | |
| 86 Source unitSource, Source librarySource, LocalElement element) { | |
| 87 // prepare resolved unit | |
| 88 CompilationUnit unit = null; | |
| 89 try { | |
| 90 unit = context.resolveCompilationUnit2(unitSource, librarySource); | |
| 91 } catch (e) {} | |
| 92 if (unit == null) { | |
| 93 return; | |
| 94 } | |
| 95 // check for conflicts in the unit | |
| 96 SourceRange elementRange = element.visibleRange; | |
| 97 unit.accept(new _ConflictValidatorVisitor(this, result, elementRange)); | |
| 98 } | |
| 99 | |
| 100 /** | 87 /** |
| 101 * Fills [elements] with [Element]s to rename. | 88 * Fills [elements] with [Element]s to rename. |
| 102 */ | 89 */ |
| 103 Future _prepareElements() async { | 90 Future _prepareElements() async { |
| 104 Element enclosing = element.enclosingElement; | 91 Element enclosing = element.enclosingElement; |
| 105 if (enclosing is MethodElement && | 92 if (enclosing is MethodElement && |
| 106 element is ParameterElement && | 93 element is ParameterElement && |
| 107 (element as ParameterElement).parameterKind == ParameterKind.NAMED) { | 94 (element as ParameterElement).parameterKind == ParameterKind.NAMED) { |
| 108 // prepare hierarchy methods | 95 // prepare hierarchy methods |
| 109 Set<ClassMemberElement> methods = | 96 Set<ClassMemberElement> methods = |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 '"$nameElementSourceName" will be shadowed by renamed $refKind.'; | 151 '"$nameElementSourceName" will be shadowed by renamed $refKind.'; |
| 165 result.addError(message, newLocation_fromNode(node)); | 152 result.addError(message, newLocation_fromNode(node)); |
| 166 } | 153 } |
| 167 } | 154 } |
| 168 } | 155 } |
| 169 | 156 |
| 170 static bool _isNamedExpressionName(SimpleIdentifier node) { | 157 static bool _isNamedExpressionName(SimpleIdentifier node) { |
| 171 return node.parent is Label && node.parent.parent is NamedExpression; | 158 return node.parent is Label && node.parent.parent is NamedExpression; |
| 172 } | 159 } |
| 173 } | 160 } |
| OLD | NEW |