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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/move_file_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.move_file; 5 library services.src.refactoring.move_file;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/protocol.dart' hide Element; 9 import 'package:analysis_server/src/protocol.dart' hide Element;
10 import 'package:analysis_server/src/services/correction/status.dart'; 10 import 'package:analysis_server/src/services/correction/status.dart';
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 } 55 }
56 56
57 @override 57 @override
58 Future<SourceChange> createChange() { 58 Future<SourceChange> createChange() {
59 change = new SourceChange(refactoringName); 59 change = new SourceChange(refactoringName);
60 List<Source> librarySources = context.getLibrariesContaining(source); 60 List<Source> librarySources = context.getLibrariesContaining(source);
61 return Future.forEach(librarySources, (Source librarySource) { 61 return Future.forEach(librarySources, (Source librarySource) {
62 CompilationUnitElement unitElement = 62 CompilationUnitElement unitElement =
63 context.getCompilationUnitElement(source, librarySource); 63 context.getCompilationUnitElement(source, librarySource);
64 if (unitElement != null) { 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 65 // if a defining unit, update outgoing references
77 library = unitElement.library; 66 library = unitElement.library;
78 if (library.definingCompilationUnit == unitElement) { 67 if (library.definingCompilationUnit == unitElement) {
79 oldLibraryDir = pathos.dirname(oldFile); 68 oldLibraryDir = pathos.dirname(oldFile);
80 newLibraryDir = pathos.dirname(newFile); 69 newLibraryDir = pathos.dirname(newFile);
81 // update references to the imported/exported libraries
82 _updateUriReferences(library.imports); 70 _updateUriReferences(library.imports);
83 _updateUriReferences(library.exports); 71 _updateUriReferences(library.exports);
84 // update references to the sources units 72 _updateUriReferences(library.parts);
85 for (CompilationUnitElement unit in library.parts) { 73 }
86 _updateUriReference(unit); 74 // update reference to the unit
75 return searchEngine.searchReferences(unitElement).then((matches) {
76 List<SourceReference> references = getSourceReferences(matches);
77 for (SourceReference reference in references) {
78 String newUri = _computeNewUri(reference);
79 SourceEdit edit = createReferenceEdit(reference, "'$newUri'");
80 change.addElementEdit(reference.element, edit);
87 } 81 }
88 } 82 });
89 } 83 }
90 }).then((_) { 84 }).then((_) {
91 return change; 85 return change;
92 }); 86 });
93 } 87 }
94 88
95 @override 89 @override
96 bool requiresPreview() => false; 90 bool requiresPreview() => false;
97 91
92 /**
93 * Computes the URI to use to reference [newFile] from [reference].
94 */
95 String _computeNewUri(SourceReference reference) {
96 String refDir = pathos.dirname(reference.file);
97 // try to keep package: URI
98 if (_isPackageReference(reference)) {
99 Source newSource = new NonExistingSource(newFile, UriKind.FILE_URI);
100 Uri restoredUri = context.sourceFactory.restoreUri(newSource);
101 if (restoredUri != null) {
102 return restoredUri.toString();
103 }
104 }
105 // if no package: URI, prepare relative
106 return pathos.relative(newFile, from: refDir);
107 }
108
109 bool _isPackageReference(SourceReference reference) {
110 Source source = reference.element.source;
111 int offset = reference.range.offset + "'".length;
112 String content = context.getContents(source).data;
113 return content.startsWith('package:', offset);
114 }
115
98 void _updateUriReference(UriReferencedElement element) { 116 void _updateUriReference(UriReferencedElement element) {
99 if (!element.isSynthetic) { 117 if (!element.isSynthetic) {
100 String elementUri = element.uri; 118 String elementUri = element.uri;
101 if (_isRelativeUri(elementUri)) { 119 if (_isRelativeUri(elementUri)) {
102 String elementPath = pathos.join(oldLibraryDir, elementUri); 120 String elementPath = pathos.join(oldLibraryDir, elementUri);
103 String newUri = pathos.relative(elementPath, from: newLibraryDir); 121 String newUri = pathos.relative(elementPath, from: newLibraryDir);
104 int uriOffset = element.uriOffset; 122 int uriOffset = element.uriOffset;
105 int uriLength = element.uriEnd - uriOffset; 123 int uriLength = element.uriEnd - uriOffset;
106 change.addElementEdit( 124 change.addElementEdit(
107 library, 125 library,
(...skipping 21 matching lines...) Expand all
129 return false; 147 return false;
130 } 148 }
131 // absolute URI 149 // absolute URI
132 if (Uri.parse(path).isAbsolute) { 150 if (Uri.parse(path).isAbsolute) {
133 return false; 151 return false;
134 } 152 }
135 // OK 153 // OK
136 return true; 154 return true;
137 } 155 }
138 } 156 }
OLDNEW
« 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