| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library services.src.refactoring.rename_local; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:analysis_services/correction/change.dart'; | |
| 10 import 'package:analysis_services/correction/status.dart'; | |
| 11 import 'package:analysis_services/refactoring/refactoring.dart'; | |
| 12 import 'package:analysis_services/search/hierarchy.dart'; | |
| 13 import 'package:analysis_services/search/search_engine.dart'; | |
| 14 import 'package:analysis_services/src/correction/util.dart'; | |
| 15 import 'package:analysis_services/src/refactoring/naming_conventions.dart'; | |
| 16 import 'package:analysis_services/src/refactoring/rename.dart'; | |
| 17 import 'package:analyzer/src/generated/ast.dart'; | |
| 18 import 'package:analyzer/src/generated/element.dart'; | |
| 19 import 'package:analyzer/src/generated/source.dart'; | |
| 20 | |
| 21 | |
| 22 /** | |
| 23 * A [Refactoring] for renaming [LocalElement]s. | |
| 24 */ | |
| 25 class RenameLocalRefactoringImpl extends RenameRefactoringImpl { | |
| 26 RenameLocalRefactoringImpl(SearchEngine searchEngine, LocalElement element) | |
| 27 : super(searchEngine, element); | |
| 28 | |
| 29 @override | |
| 30 LocalElement get element => super.element as LocalElement; | |
| 31 | |
| 32 @override | |
| 33 String get refactoringName { | |
| 34 if (element is ParameterElement) { | |
| 35 return "Rename Parameter"; | |
| 36 } | |
| 37 if (element is FunctionElement) { | |
| 38 return "Rename Local Function"; | |
| 39 } | |
| 40 return "Rename Local Variable"; | |
| 41 } | |
| 42 | |
| 43 @override | |
| 44 Future<RefactoringStatus> checkFinalConditions() { | |
| 45 RefactoringStatus result = new RefactoringStatus(); | |
| 46 // checks the resolved CompilationUnit(s) | |
| 47 Source unitSource = element.source; | |
| 48 List<Source> librarySources = context.getLibrariesContaining(unitSource); | |
| 49 for (Source librarySource in librarySources) { | |
| 50 _analyzePossibleConflicts_inLibrary(result, unitSource, librarySource); | |
| 51 } | |
| 52 return new Future.value(result); | |
| 53 } | |
| 54 | |
| 55 @override | |
| 56 RefactoringStatus checkNewName() { | |
| 57 RefactoringStatus result = super.checkNewName(); | |
| 58 if (element is LocalVariableElement) { | |
| 59 LocalVariableElement variableElement = element; | |
| 60 if (variableElement.isConst) { | |
| 61 result.addStatus(validateConstantName(newName)); | |
| 62 } else { | |
| 63 result.addStatus(validateVariableName(newName)); | |
| 64 } | |
| 65 } else if (element is ParameterElement) { | |
| 66 result.addStatus(validateParameterName(newName)); | |
| 67 } else if (element is FunctionElement) { | |
| 68 result.addStatus(validateFunctionName(newName)); | |
| 69 } | |
| 70 return result; | |
| 71 } | |
| 72 | |
| 73 @override | |
| 74 Future<Change> createChange() { | |
| 75 Change change = new Change(refactoringName); | |
| 76 // update declaration | |
| 77 addDeclarationEdit(change, element); | |
| 78 // update references | |
| 79 return searchEngine.searchReferences(element).then((refMatches) { | |
| 80 List<SourceReference> references = getSourceReferences(refMatches); | |
| 81 for (SourceReference reference in references) { | |
| 82 addReferenceEdit(change, reference); | |
| 83 } | |
| 84 return change; | |
| 85 }); | |
| 86 } | |
| 87 | |
| 88 void _analyzePossibleConflicts_inLibrary(RefactoringStatus result, | |
| 89 Source unitSource, Source librarySource) { | |
| 90 // prepare resolved unit | |
| 91 CompilationUnit unit = null; | |
| 92 try { | |
| 93 unit = context.resolveCompilationUnit2(unitSource, librarySource); | |
| 94 } catch (e) { | |
| 95 } | |
| 96 if (unit == null) { | |
| 97 return; | |
| 98 } | |
| 99 // check for conflicts in the unit | |
| 100 SourceRange elementRange = element.visibleRange; | |
| 101 unit.accept(new _ConflictValidatorVisitor(this, result, elementRange)); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 | |
| 106 class _ConflictValidatorVisitor extends RecursiveAstVisitor<Object> { | |
| 107 final RenameLocalRefactoringImpl refactoring; | |
| 108 final RefactoringStatus result; | |
| 109 final SourceRange elementRange; | |
| 110 | |
| 111 _ConflictValidatorVisitor(this.refactoring, this.result, this.elementRange); | |
| 112 | |
| 113 @override | |
| 114 Object visitSimpleIdentifier(SimpleIdentifier node) { | |
| 115 Element nodeElement = node.bestElement; | |
| 116 String newName = refactoring.newName; | |
| 117 if (nodeElement != null && nodeElement.name == newName) { | |
| 118 // duplicate declaration | |
| 119 if (haveIntersectingRanges(refactoring.element, nodeElement)) { | |
| 120 String nodeKind = nodeElement.kind.displayName; | |
| 121 String message = "Duplicate ${nodeKind} '$newName'."; | |
| 122 result.addError( | |
| 123 message, | |
| 124 new RefactoringStatusContext.forElement(nodeElement)); | |
| 125 return null; | |
| 126 } | |
| 127 // shadowing referenced element | |
| 128 if (elementRange.contains(node.offset) && !node.isQualified) { | |
| 129 nodeElement = getSyntheticAccessorVariable(nodeElement); | |
| 130 String nodeKind = nodeElement.kind.displayName; | |
| 131 String nodeName = getElementQualifiedName(nodeElement); | |
| 132 String nameElementSourceName = nodeElement.source.shortName; | |
| 133 String refKind = refactoring.element.kind.displayName; | |
| 134 String message = | |
| 135 'Usage of $nodeKind "$nodeName" declared in ' | |
| 136 '"$nameElementSourceName" will be shadowed by renamed $refKind.'
; | |
| 137 result.addError(message, new RefactoringStatusContext.forNode(node)); | |
| 138 } | |
| 139 } | |
| 140 return null; | |
| 141 } | |
| 142 } | |
| OLD | NEW |