Chromium Code Reviews| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:core'; | 6 import 'dart:core'; |
| 7 | 7 |
| 8 import 'package:analysis_server/src/protocol_server.dart' hide Element; | 8 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| 9 import 'package:analysis_server/src/services/correction/status.dart'; | 9 import 'package:analysis_server/src/services/correction/status.dart'; |
| 10 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 10 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 11 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da rt'; | 11 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da rt'; |
| 12 import 'package:analysis_server/src/services/search/search_engine.dart'; | 12 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 13 import 'package:analyzer/dart/element/element.dart'; | 13 import 'package:analyzer/dart/element/element.dart'; |
| 14 import 'package:analyzer/file_system/file_system.dart'; | 14 import 'package:analyzer/file_system/file_system.dart'; |
| 15 import 'package:analyzer/src/generated/engine.dart'; | 15 import 'package:analyzer/src/generated/engine.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:path/path.dart' as pathos; | 17 import 'package:path/path.dart' as pathos; |
| 18 import 'package:source_span/src/span.dart'; | |
| 19 import 'package:yaml/yaml.dart'; | |
| 20 | 18 |
| 21 /** | 19 /** |
| 22 * [ExtractLocalRefactoring] implementation. | 20 * [ExtractLocalRefactoring] implementation. |
| 23 */ | 21 */ |
| 24 class MoveFileRefactoringImpl extends RefactoringImpl | 22 class MoveFileRefactoringImpl extends RefactoringImpl |
| 25 implements MoveFileRefactoring { | 23 implements MoveFileRefactoring { |
| 26 final ResourceProvider resourceProvider; | 24 final ResourceProvider resourceProvider; |
| 27 final pathos.Context pathContext; | 25 final pathos.Context pathContext; |
| 28 final SearchEngine searchEngine; | 26 final SearchEngine searchEngine; |
| 29 final AnalysisContext context; | 27 final AnalysisContext context; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 56 } | 54 } |
| 57 | 55 |
| 58 @override | 56 @override |
| 59 Future<RefactoringStatus> checkInitialConditions() { | 57 Future<RefactoringStatus> checkInitialConditions() { |
| 60 RefactoringStatus result = new RefactoringStatus(); | 58 RefactoringStatus result = new RefactoringStatus(); |
| 61 return new Future.value(result); | 59 return new Future.value(result); |
| 62 } | 60 } |
| 63 | 61 |
| 64 @override | 62 @override |
| 65 Future<SourceChange> createChange() async { | 63 Future<SourceChange> createChange() async { |
| 66 // move file | 64 // // move file |
|
Paul Berry
2017/07/18 21:01:06
Can you add a TODO comment explaining why the code
| |
| 67 if (source != null) { | 65 // if (source != null) { |
| 68 return _createFileChange(); | 66 // return _createFileChange(); |
| 69 } | 67 // } |
| 70 // rename project | 68 // // rename project |
| 71 if (oldFile != null) { | 69 // if (oldFile != null) { |
| 72 Resource projectFolder = resourceProvider.getResource(oldFile); | 70 // Resource projectFolder = resourceProvider.getResource(oldFile); |
| 73 if (projectFolder is Folder && projectFolder.exists) { | 71 // if (projectFolder is Folder && projectFolder.exists) { |
| 74 Resource pubspecFile = projectFolder.getChild('pubspec.yaml'); | 72 // Resource pubspecFile = projectFolder.getChild('pubspec.yaml'); |
| 75 if (pubspecFile is File && pubspecFile.exists) { | 73 // if (pubspecFile is File && pubspecFile.exists) { |
| 76 return _createProjectChange(projectFolder, pubspecFile); | 74 // return _createProjectChange(projectFolder, pubspecFile); |
| 77 } | 75 // } |
| 78 } | 76 // } |
| 79 } | 77 // } |
| 80 // no change | 78 // no change |
| 81 return null; | 79 return null; |
| 82 } | 80 } |
| 83 | 81 |
| 84 @override | 82 @override |
| 85 bool requiresPreview() => false; | 83 bool requiresPreview() => false; |
| 86 | 84 |
| 87 /** | 85 // /** |
| 88 * Computes the URI to use to reference [newFile] from [reference]. | 86 // * Computes the URI to use to reference [newFile] from [reference]. |
| 89 */ | 87 // */ |
| 90 String _computeNewUri(SourceReference reference) { | 88 // String _computeNewUri(SourceReference reference) { |
| 91 String refDir = pathContext.dirname(reference.file); | 89 // String refDir = pathContext.dirname(reference.file); |
| 92 // try to keep package: URI | 90 // // try to keep package: URI |
| 93 if (_isPackageReference(reference)) { | 91 // if (_isPackageReference(reference)) { |
| 94 Source newSource = new NonExistingSource( | 92 // Source newSource = new NonExistingSource( |
| 95 newFile, pathos.toUri(newFile), UriKind.FILE_URI); | 93 // newFile, pathos.toUri(newFile), UriKind.FILE_URI); |
| 96 Uri restoredUri = context.sourceFactory.restoreUri(newSource); | 94 // Uri restoredUri = context.sourceFactory.restoreUri(newSource); |
| 97 if (restoredUri != null) { | 95 // if (restoredUri != null) { |
| 98 return restoredUri.toString(); | 96 // return restoredUri.toString(); |
| 99 } | 97 // } |
| 100 } | 98 // } |
| 101 // if no package: URI, prepare relative | 99 // // if no package: URI, prepare relative |
| 102 return _getRelativeUri(newFile, refDir); | 100 // return _getRelativeUri(newFile, refDir); |
| 103 } | 101 // } |
| 104 | 102 |
| 105 Future<SourceChange> _createFileChange() async { | 103 // Future<SourceChange> _createFileChange() async { |
| 106 change = new SourceChange('Update File References'); | 104 // change = new SourceChange('Update File References'); |
| 107 List<Source> librarySources = context.getLibrariesContaining(source); | 105 // List<Source> librarySources = context.getLibrariesContaining(source); |
| 108 await Future.forEach(librarySources, (Source librarySource) async { | 106 // await Future.forEach(librarySources, (Source librarySource) async { |
| 109 CompilationUnitElement unitElement = | 107 // CompilationUnitElement unitElement = |
| 110 context.getCompilationUnitElement(source, librarySource); | 108 // context.getCompilationUnitElement(source, librarySource); |
| 111 if (unitElement != null) { | 109 // if (unitElement != null) { |
| 112 // if a defining unit, update outgoing references | 110 // // if a defining unit, update outgoing references |
| 113 library = unitElement.library; | 111 // library = unitElement.library; |
| 114 if (library.definingCompilationUnit == unitElement) { | 112 // if (library.definingCompilationUnit == unitElement) { |
| 115 oldLibraryDir = pathContext.dirname(oldFile); | 113 // oldLibraryDir = pathContext.dirname(oldFile); |
| 116 newLibraryDir = pathContext.dirname(newFile); | 114 // newLibraryDir = pathContext.dirname(newFile); |
| 117 _updateUriReferences(library.imports); | 115 // _updateUriReferences(library.imports); |
| 118 _updateUriReferences(library.exports); | 116 // _updateUriReferences(library.exports); |
| 119 _updateUriReferences(library.parts); | 117 // _updateUriReferences(library.parts); |
| 120 } | 118 // } |
| 121 // update reference to the unit | 119 // // update reference to the unit |
| 122 List<SearchMatch> matches = | 120 // List<SearchMatch> matches = |
| 123 await searchEngine.searchReferences(unitElement); | 121 // await searchEngine.searchReferences(unitElement); |
| 124 List<SourceReference> references = getSourceReferences(matches); | 122 // List<SourceReference> references = getSourceReferences(matches); |
| 125 for (SourceReference reference in references) { | 123 // for (SourceReference reference in references) { |
| 126 String newUri = _computeNewUri(reference); | 124 // String newUri = _computeNewUri(reference); |
| 127 reference.addEdit(change, "'$newUri'"); | 125 // reference.addEdit(change, "'$newUri'"); |
| 128 } | 126 // } |
| 129 } | 127 // } |
| 130 }); | 128 // }); |
| 131 return change; | 129 // return change; |
| 132 } | 130 // } |
| 133 | 131 |
| 134 Future<SourceChange> _createProjectChange( | 132 // Future<SourceChange> _createProjectChange( |
| 135 Folder project, File pubspecFile) async { | 133 // Folder project, File pubspecFile) async { |
| 136 change = new SourceChange('Rename project'); | 134 // change = new SourceChange('Rename project'); |
| 137 String oldPackageName = pathContext.basename(oldFile); | 135 // String oldPackageName = pathContext.basename(oldFile); |
| 138 String newPackageName = pathContext.basename(newFile); | 136 // String newPackageName = pathContext.basename(newFile); |
| 139 // add pubspec.yaml change | 137 // // add pubspec.yaml change |
| 140 { | 138 // { |
| 141 // prepare "name" field value location | 139 // // prepare "name" field value location |
| 142 SourceSpan nameSpan; | 140 // SourceSpan nameSpan; |
| 143 { | 141 // { |
| 144 String pubspecString = pubspecFile.readAsStringSync(); | 142 // String pubspecString = pubspecFile.readAsStringSync(); |
| 145 YamlMap pubspecNode = loadYamlNode(pubspecString); | 143 // YamlMap pubspecNode = loadYamlNode(pubspecString); |
| 146 YamlNode nameNode = pubspecNode.nodes['name']; | 144 // YamlNode nameNode = pubspecNode.nodes['name']; |
| 147 nameSpan = nameNode.span; | 145 // nameSpan = nameNode.span; |
| 148 } | 146 // } |
| 149 int nameOffset = nameSpan.start.offset; | 147 // int nameOffset = nameSpan.start.offset; |
| 150 int nameLength = nameSpan.length; | 148 // int nameLength = nameSpan.length; |
| 151 // add edit | 149 // // add edit |
| 152 change.addEdit(pubspecFile.path, pubspecFile.modificationStamp, | 150 // change.addEdit(pubspecFile.path, pubspecFile.modificationStamp, |
| 153 new SourceEdit(nameOffset, nameLength, newPackageName)); | 151 // new SourceEdit(nameOffset, nameLength, newPackageName)); |
| 154 } | 152 // } |
| 155 // check all local libraries | 153 // // check all local libraries |
| 156 for (Source librarySource in context.librarySources) { | 154 // for (Source librarySource in context.librarySources) { |
| 157 // should be a local library | 155 // // should be a local library |
| 158 if (!project.contains(librarySource.fullName)) { | 156 // if (!project.contains(librarySource.fullName)) { |
| 159 continue; | 157 // continue; |
| 160 } | 158 // } |
| 161 // we need LibraryElement | 159 // // we need LibraryElement |
| 162 LibraryElement library = context.getLibraryElement(librarySource); | 160 // LibraryElement library = context.getLibraryElement(librarySource); |
| 163 if (library == null) { | 161 // if (library == null) { |
| 164 continue; | 162 // continue; |
| 165 } | 163 // } |
| 166 // update all imports | 164 // // update all imports |
| 167 updateUriElements(List<UriReferencedElement> uriElements) { | 165 // updateUriElements(List<UriReferencedElement> uriElements) { |
| 168 for (UriReferencedElement element in uriElements) { | 166 // for (UriReferencedElement element in uriElements) { |
| 169 String uri = element.uri; | 167 // String uri = element.uri; |
| 170 if (uri != null) { | 168 // if (uri != null) { |
| 171 String oldPrefix = 'package:$oldPackageName/'; | 169 // String oldPrefix = 'package:$oldPackageName/'; |
| 172 if (uri.startsWith(oldPrefix)) { | 170 // if (uri.startsWith(oldPrefix)) { |
| 173 doSourceChange_addElementEdit( | 171 // doSourceChange_addElementEdit( |
| 174 change, | 172 // change, |
| 175 library, | 173 // library, |
| 176 new SourceEdit(element.uriOffset + 1, oldPrefix.length, | 174 // new SourceEdit(element.uriOffset + 1, oldPrefix.length, |
| 177 'package:$newPackageName/')); | 175 // 'package:$newPackageName/')); |
| 178 } | 176 // } |
| 179 } | 177 // } |
| 180 } | 178 // } |
| 181 } | 179 // } |
| 182 | 180 // |
| 183 updateUriElements(library.imports); | 181 // updateUriElements(library.imports); |
| 184 updateUriElements(library.exports); | 182 // updateUriElements(library.exports); |
| 185 } | 183 // } |
| 186 // done | 184 // // done |
| 187 return change; | 185 // return change; |
| 188 } | 186 // } |
| 189 | 187 // |
| 190 String _getRelativeUri(String path, String from) { | 188 // String _getRelativeUri(String path, String from) { |
| 191 String uri = pathContext.relative(path, from: from); | 189 // String uri = pathContext.relative(path, from: from); |
| 192 List<String> parts = pathContext.split(uri); | 190 // List<String> parts = pathContext.split(uri); |
| 193 return pathos.posix.joinAll(parts); | 191 // return pathos.posix.joinAll(parts); |
| 194 } | 192 // } |
| 195 | 193 // |
| 196 bool _isPackageReference(SourceReference reference) { | 194 // bool _isPackageReference(SourceReference reference) { |
| 197 Source source = reference.element.source; | 195 // Source source = reference.element.source; |
| 198 int offset = reference.range.offset + "'".length; | 196 // int offset = reference.range.offset + "'".length; |
| 199 String content = context.getContents(source).data; | 197 // String content = context.getContents(source).data; |
| 200 return content.startsWith('package:', offset); | 198 // return content.startsWith('package:', offset); |
| 201 } | 199 // } |
| 202 | 200 // |
| 203 /** | 201 // /** |
| 204 * Checks if the given [path] represents a relative URI. | 202 // * Checks if the given [path] represents a relative URI. |
| 205 * | 203 // * |
| 206 * The following URI's are not relative: | 204 // * The following URI's are not relative: |
| 207 * `/absolute/path/file.dart` | 205 // * `/absolute/path/file.dart` |
| 208 * `dart:math` | 206 // * `dart:math` |
| 209 */ | 207 // */ |
| 210 bool _isRelativeUri(String path) { | 208 // bool _isRelativeUri(String path) { |
| 211 // absolute URI | 209 // // absolute URI |
| 212 if (Uri.parse(path).isAbsolute) { | 210 // if (Uri.parse(path).isAbsolute) { |
| 213 return false; | 211 // return false; |
| 214 } | 212 // } |
| 215 // absolute path | 213 // // absolute path |
| 216 if (pathContext.isAbsolute(path)) { | 214 // if (pathContext.isAbsolute(path)) { |
| 217 return false; | 215 // return false; |
| 218 } | 216 // } |
| 219 // OK | 217 // // OK |
| 220 return true; | 218 // return true; |
| 221 } | 219 // } |
| 222 | 220 // |
| 223 void _updateUriReference(UriReferencedElement element) { | 221 // void _updateUriReference(UriReferencedElement element) { |
| 224 if (!element.isSynthetic) { | 222 // if (!element.isSynthetic) { |
| 225 String elementUri = element.uri; | 223 // String elementUri = element.uri; |
| 226 if (_isRelativeUri(elementUri)) { | 224 // if (_isRelativeUri(elementUri)) { |
| 227 String elementPath = pathContext.join(oldLibraryDir, elementUri); | 225 // String elementPath = pathContext.join(oldLibraryDir, elementUri); |
| 228 String newUri = _getRelativeUri(elementPath, newLibraryDir); | 226 // String newUri = _getRelativeUri(elementPath, newLibraryDir); |
| 229 int uriOffset = element.uriOffset; | 227 // int uriOffset = element.uriOffset; |
| 230 int uriLength = element.uriEnd - uriOffset; | 228 // int uriLength = element.uriEnd - uriOffset; |
| 231 doSourceChange_addElementEdit( | 229 // doSourceChange_addElementEdit( |
| 232 change, library, new SourceEdit(uriOffset, uriLength, "'$newUri'")); | 230 // change, library, new SourceEdit(uriOffset, uriLength, "'$newUri'") ); |
| 233 } | 231 // } |
| 234 } | 232 // } |
| 235 } | 233 // } |
| 236 | 234 // |
| 237 void _updateUriReferences(List<UriReferencedElement> elements) { | 235 // void _updateUriReferences(List<UriReferencedElement> elements) { |
| 238 for (UriReferencedElement element in elements) { | 236 // for (UriReferencedElement element in elements) { |
| 239 _updateUriReference(element); | 237 // _updateUriReference(element); |
| 240 } | 238 // } |
| 241 } | 239 // } |
| 242 } | 240 } |
| OLD | NEW |