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

Unified Diff: pkg/analysis_server/lib/src/services/refactoring/move_file.dart

Issue 586863002: Try to restore 'package:' URI reference to the file being moved. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/move_file_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/services/refactoring/move_file.dart
diff --git a/pkg/analysis_server/lib/src/services/refactoring/move_file.dart b/pkg/analysis_server/lib/src/services/refactoring/move_file.dart
index e22d79f64bc7af7f2c8f3125b1110e20650437a2..66562d7916989ea36bcb3d07f719ffa1cd72ddd4 100644
--- a/pkg/analysis_server/lib/src/services/refactoring/move_file.dart
+++ b/pkg/analysis_server/lib/src/services/refactoring/move_file.dart
@@ -62,30 +62,24 @@ class MoveFileRefactoringImpl extends RefactoringImpl implements
CompilationUnitElement unitElement =
context.getCompilationUnitElement(source, librarySource);
if (unitElement != null) {
- // update reference to the unit
- searchEngine.searchReferences(unitElement).then((matches) {
- List<SourceReference> references = getSourceReferences(matches);
- for (SourceReference reference in references) {
- String refDir = pathos.dirname(reference.file);
- String newUri = pathos.relative(newFile, from: refDir);
- change.addElementEdit(
- reference.element,
- createReferenceEdit(reference, "'$newUri'"));
- }
- });
// if a defining unit, update outgoing references
library = unitElement.library;
if (library.definingCompilationUnit == unitElement) {
oldLibraryDir = pathos.dirname(oldFile);
newLibraryDir = pathos.dirname(newFile);
- // update references to the imported/exported libraries
_updateUriReferences(library.imports);
_updateUriReferences(library.exports);
- // update references to the sources units
- for (CompilationUnitElement unit in library.parts) {
- _updateUriReference(unit);
- }
+ _updateUriReferences(library.parts);
}
+ // update reference to the unit
+ return searchEngine.searchReferences(unitElement).then((matches) {
+ List<SourceReference> references = getSourceReferences(matches);
+ for (SourceReference reference in references) {
+ String newUri = _computeNewUri(reference);
+ SourceEdit edit = createReferenceEdit(reference, "'$newUri'");
+ change.addElementEdit(reference.element, edit);
+ }
+ });
}
}).then((_) {
return change;
@@ -95,6 +89,30 @@ class MoveFileRefactoringImpl extends RefactoringImpl implements
@override
bool requiresPreview() => false;
+ /**
+ * Computes the URI to use to reference [newFile] from [reference].
+ */
+ String _computeNewUri(SourceReference reference) {
+ String refDir = pathos.dirname(reference.file);
+ // try to keep package: URI
+ if (_isPackageReference(reference)) {
+ Source newSource = new NonExistingSource(newFile, UriKind.FILE_URI);
+ Uri restoredUri = context.sourceFactory.restoreUri(newSource);
+ if (restoredUri != null) {
+ return restoredUri.toString();
+ }
+ }
+ // if no package: URI, prepare relative
+ return pathos.relative(newFile, from: refDir);
+ }
+
+ bool _isPackageReference(SourceReference reference) {
+ Source source = reference.element.source;
+ int offset = reference.range.offset + "'".length;
+ String content = context.getContents(source).data;
+ return content.startsWith('package:', offset);
+ }
+
void _updateUriReference(UriReferencedElement element) {
if (!element.isSynthetic) {
String elementUri = element.uri;
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/move_file_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698