| 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.move_file; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_server/src/protocol.dart' hide Element; |
| 10 import 'package:analysis_server/src/services/correction/status.dart'; |
| 11 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 12 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; |
| 13 import 'package:analysis_server/src/services/refactoring/rename.dart'; |
| 14 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 15 import 'package:analyzer/src/generated/element.dart'; |
| 16 import 'package:analyzer/src/generated/engine.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart'; |
| 18 import 'package:path/path.dart' as pathos; |
| 19 |
| 20 |
| 21 /** |
| 22 * [ExtractLocalRefactoring] implementation. |
| 23 */ |
| 24 class MoveFileRefactoringImpl extends RefactoringImpl implements |
| 25 MoveFileRefactoring { |
| 26 final SearchEngine searchEngine; |
| 27 final AnalysisContext context; |
| 28 final Source source; |
| 29 |
| 30 String oldFile; |
| 31 String newFile; |
| 32 |
| 33 SourceChange change; |
| 34 LibraryElement library; |
| 35 String oldLibraryDir; |
| 36 String newLibraryDir; |
| 37 |
| 38 MoveFileRefactoringImpl(this.searchEngine, this.context, this.source) { |
| 39 oldFile = source.fullName; |
| 40 } |
| 41 |
| 42 @override |
| 43 String get refactoringName => 'Move File'; |
| 44 |
| 45 @override |
| 46 Future<RefactoringStatus> checkFinalConditions() { |
| 47 RefactoringStatus result = new RefactoringStatus(); |
| 48 return new Future.value(result); |
| 49 } |
| 50 |
| 51 @override |
| 52 Future<RefactoringStatus> checkInitialConditions() { |
| 53 RefactoringStatus result = new RefactoringStatus(); |
| 54 return new Future.value(result); |
| 55 } |
| 56 |
| 57 @override |
| 58 Future<SourceChange> createChange() { |
| 59 change = new SourceChange(refactoringName); |
| 60 List<Source> librarySources = context.getLibrariesContaining(source); |
| 61 return Future.forEach(librarySources, (Source librarySource) { |
| 62 CompilationUnitElement unitElement = |
| 63 context.getCompilationUnitElement(source, librarySource); |
| 64 if (unitElement != null) { |
| 65 // update reference to the unit |
| 66 searchEngine.searchReferences(unitElement).then((matches) { |
| 67 List<SourceReference> references = getSourceReferences(matches); |
| 68 for (SourceReference reference in references) { |
| 69 String refDir = pathos.dirname(reference.file); |
| 70 String newUri = pathos.relative(newFile, from: refDir); |
| 71 change.addElementEdit( |
| 72 reference.element, |
| 73 createReferenceEdit(reference, "'$newUri'")); |
| 74 } |
| 75 }); |
| 76 // if a defining unit, update outgoing references |
| 77 library = unitElement.library; |
| 78 if (library.definingCompilationUnit == unitElement) { |
| 79 oldLibraryDir = pathos.dirname(oldFile); |
| 80 newLibraryDir = pathos.dirname(newFile); |
| 81 // update references to the imported/exported libraries |
| 82 _updateUriReferences(library.imports); |
| 83 _updateUriReferences(library.exports); |
| 84 // update references to the sources units |
| 85 for (CompilationUnitElement unit in library.parts) { |
| 86 _updateUriReference(unit); |
| 87 } |
| 88 } |
| 89 } |
| 90 }).then((_) { |
| 91 return change; |
| 92 }); |
| 93 } |
| 94 |
| 95 @override |
| 96 bool requiresPreview() => false; |
| 97 |
| 98 void _updateUriReference(UriReferencedElement element) { |
| 99 if (!element.isSynthetic) { |
| 100 String elementUri = element.uri; |
| 101 if (pathos.isRelative(elementUri)) { |
| 102 String elementPath = pathos.join(oldLibraryDir, elementUri); |
| 103 String newUri = pathos.relative(elementPath, from: newLibraryDir); |
| 104 int uriOffset = element.uriOffset; |
| 105 int uriLength = element.uriEnd - uriOffset; |
| 106 change.addElementEdit( |
| 107 library, |
| 108 new SourceEdit(uriOffset, uriLength, "'$newUri'")); |
| 109 } |
| 110 } |
| 111 } |
| 112 |
| 113 void _updateUriReferences(List<UriReferencedElement> elements) { |
| 114 for (UriReferencedElement element in elements) { |
| 115 _updateUriReference(element); |
| 116 } |
| 117 } |
| 118 } |
| OLD | NEW |