-
Notifications
You must be signed in to change notification settings - Fork 27.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype update import paths on file rename/move for JS/TS (#50074)
* Prototype of updating paths on rename file * Fix apply edits * Hook up to normal rename * Fix unit test * Remove timeout * Adding prompt * Bail early if user has set 'never'
- Loading branch information
Showing
13 changed files
with
285 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
extensions/typescript-language-features/src/features/updatePathsOnRename.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import * as nls from 'vscode-nls'; | ||
import * as Proto from '../protocol'; | ||
import { ITypeScriptServiceClient } from '../typescriptService'; | ||
import * as languageIds from '../utils/languageModeIds'; | ||
import * as typeConverters from '../utils/typeConverters'; | ||
import BufferSyncSupport from './bufferSyncSupport'; | ||
import FileConfigurationManager from './fileConfigurationManager'; | ||
|
||
const localize = nls.loadMessageBundle(); | ||
|
||
const updateImportsOnFileMoveName = 'updateImportsOnFileMove.enabled'; | ||
|
||
enum UpdateImportsOnFileMoveSetting { | ||
Prompt = 'prompt', | ||
Always = 'always', | ||
Never = 'never', | ||
} | ||
|
||
export class UpdateImportsOnFileRenameHandler { | ||
private readonly _onDidRenameSub: vscode.Disposable; | ||
|
||
public constructor( | ||
private readonly client: ITypeScriptServiceClient, | ||
private readonly bufferSyncSupport: BufferSyncSupport, | ||
private readonly fileConfigurationManager: FileConfigurationManager, | ||
private readonly handles: (uri: vscode.Uri) => Promise<boolean>, | ||
) { | ||
this._onDidRenameSub = vscode.workspace.onDidRenameResource(e => { | ||
this.doRename(e.oldResource, e.newResource); | ||
}); | ||
} | ||
|
||
public dispose() { | ||
this._onDidRenameSub.dispose(); | ||
} | ||
|
||
private async doRename( | ||
oldResource: vscode.Uri, | ||
newResource: vscode.Uri, | ||
): Promise<void> { | ||
if (!this.client.apiVersion.has290Features) { | ||
return; | ||
} | ||
|
||
if (!await this.handles(newResource)) { | ||
return; | ||
} | ||
|
||
const newFile = this.client.normalizePath(newResource); | ||
if (!newFile) { | ||
return; | ||
} | ||
|
||
const oldFile = this.client.normalizePath(oldResource); | ||
if (!oldFile) { | ||
return; | ||
} | ||
|
||
const document = await vscode.workspace.openTextDocument(newResource); | ||
|
||
const config = this.getConfiguration(document); | ||
const setting = config.get<UpdateImportsOnFileMoveSetting>(updateImportsOnFileMoveName); | ||
if (setting === UpdateImportsOnFileMoveSetting.Never) { | ||
return; | ||
} | ||
|
||
// Make sure TS knows about file | ||
this.bufferSyncSupport.openTextDocument(document); | ||
|
||
const edits = await this.getEditsForFileRename(document, oldFile, newFile); | ||
if (!edits || !edits.size) { | ||
return; | ||
} | ||
|
||
if (await this.confirmActionWithUser(document)) { | ||
await vscode.workspace.applyEdit(edits); | ||
} | ||
} | ||
|
||
private async confirmActionWithUser( | ||
newDocument: vscode.TextDocument | ||
): Promise<boolean> { | ||
const config = this.getConfiguration(newDocument); | ||
const setting = config.get<UpdateImportsOnFileMoveSetting>(updateImportsOnFileMoveName); | ||
switch (setting) { | ||
case UpdateImportsOnFileMoveSetting.Always: | ||
return true; | ||
case UpdateImportsOnFileMoveSetting.Never: | ||
return false; | ||
case UpdateImportsOnFileMoveSetting.Prompt: | ||
default: | ||
return this.promptUser(newDocument); | ||
} | ||
} | ||
|
||
private getConfiguration(newDocument: vscode.TextDocument) { | ||
return vscode.workspace.getConfiguration(isTypeScriptDocument(newDocument) ? 'typescript' : 'javascript', newDocument.uri); | ||
} | ||
|
||
private async promptUser( | ||
newDocument: vscode.TextDocument | ||
): Promise<boolean> { | ||
enum Choice { | ||
None = 0, | ||
Accept = 1, | ||
Reject = 2, | ||
Always = 3, | ||
Never = 4, | ||
} | ||
|
||
interface Item extends vscode.QuickPickItem { | ||
choice: Choice; | ||
} | ||
|
||
const response = await vscode.window.showQuickPick<Item>([ | ||
{ | ||
label: localize('accept.label', "Yes"), | ||
description: localize('accept.description', "Update imports."), | ||
choice: Choice.Accept, | ||
}, | ||
{ | ||
label: localize('reject.label', "No"), | ||
description: localize('reject.description', "Do not update imports."), | ||
choice: Choice.Reject, | ||
}, | ||
{ | ||
label: localize('always.label', "Always"), | ||
description: localize('always.description', "Yes, and always automatically update imports."), | ||
choice: Choice.Always, | ||
}, | ||
{ | ||
label: localize('never.label', "Never"), | ||
description: localize('never.description', "No, and do not prompt me again."), | ||
choice: Choice.Never, | ||
}, | ||
], { | ||
placeHolder: localize('prompt', "Update import paths?"), | ||
ignoreFocusOut: true, | ||
}); | ||
|
||
if (!response) { | ||
return false; | ||
} | ||
|
||
switch (response.choice) { | ||
case Choice.Accept: | ||
{ | ||
return true; | ||
} | ||
case Choice.Reject: | ||
{ | ||
return false; | ||
} | ||
case Choice.Always: | ||
{ | ||
const config = this.getConfiguration(newDocument); | ||
config.update( | ||
updateImportsOnFileMoveName, | ||
UpdateImportsOnFileMoveSetting.Always, | ||
vscode.ConfigurationTarget.Global); | ||
return true; | ||
} | ||
case Choice.Never: | ||
{ | ||
const config = this.getConfiguration(newDocument); | ||
config.update( | ||
updateImportsOnFileMoveName, | ||
UpdateImportsOnFileMoveSetting.Never, | ||
vscode.ConfigurationTarget.Global); | ||
return false; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private async getEditsForFileRename( | ||
document: vscode.TextDocument, | ||
oldFile: string, | ||
newFile: string, | ||
) { | ||
await this.fileConfigurationManager.ensureConfigurationForDocument(document, undefined); | ||
|
||
const args: Proto.GetEditsForFileRenameRequestArgs = { | ||
file: newFile, | ||
oldFilePath: oldFile, | ||
newFilePath: newFile, | ||
}; | ||
const response = await this.client.execute('getEditsForFileRename', args); | ||
if (!response || !response.body) { | ||
return; | ||
} | ||
|
||
return typeConverters.WorkspaceEdit.fromFromFileCodeEdits(this.client, response.body); | ||
} | ||
} | ||
|
||
function isTypeScriptDocument(document: vscode.TextDocument) { | ||
return document.languageId === languageIds.typescript || document.languageId === languageIds.typescriptreact; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.