[analysis_server] Enable updating imports for folder renames via LSP

Change-Id: Idbc1745570f19e372eee17089ebdc7db0d0b4fdb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247548
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2022-06-08 21:57:19 +00:00 committed by Commit Bot
parent f111a2df54
commit a5d2b9156e
3 changed files with 52 additions and 6 deletions

View file

@ -21,11 +21,10 @@ class WillRenameFilesHandler
Future<ErrorOr<WorkspaceEdit?>> handle(RenameFilesParams params,
MessageInfo message, CancellationToken token) async {
final files = params.files;
// Only single-file rename/moves are currently supported.
// TODO(dantup): Tweak this when VS Code can correctly pass us cancellation
// requests to not check for .dart to also support folders (although we
// may still only support a single entry initially).
if (files.length > 1 || files.any((f) => !f.oldUri.endsWith('.dart'))) {
// Although we support folders, currently only a single item in the list
// is supported (eg. although you can rename a folder, you can't drag
// multiple files between folders).
if (files.length > 1) {
return success(null);
}

View file

@ -127,6 +127,13 @@ class ServerCapabilitiesComputer {
glob: '**/*.dart',
matches: FileOperationPatternKind.file,
),
),
FileOperationFilter(
scheme: 'file',
pattern: FileOperationPattern(
glob: '**/',
matches: FileOperationPatternKind.folder,
),
)
],
);

View file

@ -86,7 +86,7 @@ class WillRenameFilesTest extends AbstractLspAnalysisServerTest {
);
}
Future<void> test_rename_updatesImports() async {
Future<void> test_renameFile_updatesImports() async {
final otherFilePath = join(projectFolderPath, 'lib', 'other.dart');
final otherFileUri = Uri.file(otherFilePath);
final otherFileNewPath = join(projectFolderPath, 'lib', 'other_new.dart');
@ -125,4 +125,44 @@ final a = A();
applyChanges(contents, edit.changes!);
expect(contents[mainFilePath], equals(expectedMainContent));
}
Future<void> test_renameFolder_updatesImports() async {
final oldFolderPath = join(projectFolderPath, 'lib', 'folder');
final newFolderPath = join(projectFolderPath, 'lib', 'folder_new');
final otherFilePath = join(oldFolderPath, 'other.dart');
final otherFileUri = Uri.file(otherFilePath);
final mainContent = '''
import 'folder/other.dart';
final a = A();
''';
final otherContent = '''
class A {}
''';
final expectedMainContent = '''
import 'folder_new/other.dart';
final a = A();
''';
await initialize();
await openFile(mainFileUri, mainContent);
await openFile(otherFileUri, otherContent);
final edit = await onWillRename([
FileRename(
oldUri: Uri.file(oldFolderPath).toString(),
newUri: Uri.file(newFolderPath).toString(),
),
]);
// Ensure applying the edit will give us the expected content.
final contents = {
mainFilePath: withoutMarkers(mainContent),
};
applyChanges(contents, edit.changes!);
expect(contents[mainFilePath], equals(expectedMainContent));
}
}