From e4478d363d7a3054e77e7baa1dfe2d3b7a3cd103 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 23 Oct 2019 17:23:03 -0700 Subject: [PATCH] Batch renames --- .../src/features/updatePathsOnRename.ts | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/extensions/typescript-language-features/src/features/updatePathsOnRename.ts b/extensions/typescript-language-features/src/features/updatePathsOnRename.ts index 76ef25cd3dd..afd96852a4a 100644 --- a/extensions/typescript-language-features/src/features/updatePathsOnRename.ts +++ b/extensions/typescript-language-features/src/features/updatePathsOnRename.ts @@ -15,6 +15,7 @@ import { Disposable } from '../utils/dispose'; import * as fileSchemes from '../utils/fileSchemes'; import * as typeConverters from '../utils/typeConverters'; import FileConfigurationManager from './fileConfigurationManager'; +import { Delayer } from '../utils/async'; const localize = nls.loadMessageBundle(); @@ -49,6 +50,9 @@ function doesResourceLookLikeATypeScriptFile(resource: vscode.Uri): boolean { class UpdateImportsOnFileRenameHandler extends Disposable { public static readonly minVersion = API.v300; + private readonly _delayer = new Delayer(50); + private readonly _pendingRenames = new Set(); + public constructor( private readonly client: ITypeScriptServiceClient, private readonly fileConfigurationManager: FileConfigurationManager, @@ -80,29 +84,35 @@ class UpdateImportsOnFileRenameHandler extends Disposable { return; } - vscode.window.withProgress({ - location: vscode.ProgressLocation.Window, - title: localize('renameProgress.title', "Checking for update of JS/TS imports") - }, () => { - return this.doRename({ oldUri, newUri, newFilePath, oldFilePath, jsTsFileThatIsBeingMoved }); + this._pendingRenames.add({ oldUri, newUri, newFilePath, oldFilePath, jsTsFileThatIsBeingMoved }); + + this._delayer.trigger(() => { + vscode.window.withProgress({ + location: vscode.ProgressLocation.Window, + title: localize('renameProgress.title', "Checking for update of JS/TS imports") + }, () => this.flushRenames()); }); })); } - private async doRename({ oldUri, newUri, newFilePath, oldFilePath, jsTsFileThatIsBeingMoved }: RenameAction): Promise { - const document = await vscode.workspace.openTextDocument(jsTsFileThatIsBeingMoved); + private async flushRenames(): Promise { + const renames = Array.from(this._pendingRenames); + this._pendingRenames.clear(); + for (const { oldUri, newUri, newFilePath, oldFilePath, jsTsFileThatIsBeingMoved } of renames) { + const document = await vscode.workspace.openTextDocument(jsTsFileThatIsBeingMoved); - // Make sure TS knows about file - this.client.bufferSyncSupport.closeResource(oldUri); - this.client.bufferSyncSupport.openTextDocument(document); + // Make sure TS knows about file + this.client.bufferSyncSupport.closeResource(oldUri); + this.client.bufferSyncSupport.openTextDocument(document); - const edits = await this.getEditsForFileRename(document, oldFilePath, newFilePath); - if (!edits || !edits.size) { - return; - } + const edits = await this.getEditsForFileRename(document, oldFilePath, newFilePath); + if (!edits || !edits.size) { + return; + } - if (await this.confirmActionWithUser(newUri)) { - await vscode.workspace.applyEdit(edits); + if (await this.confirmActionWithUser(newUri)) { + await vscode.workspace.applyEdit(edits); + } } } -- GitLab