forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype TS/JS Refactoring Provider
Fixes microsoft#25739, from microsoft/TypeScript#15569 Prototype of refactoring support for ts 2.4
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import { CodeActionProvider, TextDocument, Range, CancellationToken, CodeActionContext, Command, commands, workspace, WorkspaceEdit } from 'vscode'; | ||
|
||
import * as Proto from '../protocol'; | ||
import { ITypescriptServiceClient } from '../typescriptService'; | ||
|
||
|
||
export default class TypeScriptRefactorProvider implements CodeActionProvider { | ||
private commandId: string; | ||
|
||
constructor( | ||
private readonly client: ITypescriptServiceClient, | ||
mode: string | ||
) { | ||
this.commandId = `_typescript.applyRefactoring.${mode}`; | ||
commands.registerCommand(this.commandId, this.onCodeAction, this); | ||
} | ||
|
||
public async provideCodeActions( | ||
document: TextDocument, | ||
range: Range, | ||
_context: CodeActionContext, | ||
token: CancellationToken | ||
): Promise<Command[]> { | ||
if (!this.client.apiVersion.has240Features()) { | ||
return []; | ||
} | ||
|
||
const file = this.client.normalizePath(document.uri); | ||
if (!file) { | ||
return []; | ||
} | ||
|
||
const args: Proto.GetApplicableRefactorsRequestArgs = { | ||
file: file, | ||
startLine: range.start.line + 1, | ||
startOffset: range.start.character + 1, | ||
endLine: range.end.line + 1, | ||
endOffset: range.end.character + 1 | ||
}; | ||
|
||
const response = await this.client.execute('getApplicableRefactors', args, token); | ||
if (!response || !response.body) { | ||
return []; | ||
} | ||
|
||
return response.body.map(action => ({ | ||
title: action.description, | ||
command: this.commandId, | ||
arguments: [file, action.name, range] | ||
})); | ||
} | ||
|
||
private actionsToEdit(actions: Proto.CodeAction[]): WorkspaceEdit { | ||
const workspaceEdit = new WorkspaceEdit(); | ||
for (const action of actions) { | ||
for (const change of action.changes) { | ||
for (const textChange of change.textChanges) { | ||
workspaceEdit.replace(this.client.asUrl(change.fileName), | ||
new Range( | ||
textChange.start.line - 1, textChange.start.offset - 1, | ||
textChange.end.line - 1, textChange.end.offset - 1), | ||
textChange.newText); | ||
} | ||
} | ||
} | ||
return workspaceEdit; | ||
} | ||
|
||
private async onCodeAction(file: string, refactorName: string, range: Range): Promise<boolean> { | ||
const args: Proto.GetRefactorCodeActionsRequestArgs = { | ||
file, | ||
refactorName, | ||
startLine: range.start.line + 1, | ||
startOffset: range.start.character + 1, | ||
endLine: range.end.line + 1, | ||
endOffset: range.end.character + 1 | ||
}; | ||
|
||
const response = await this.client.execute('getRefactorCodeActions', args); | ||
if (!response || !response.body || !response.body.actions.length) { | ||
return false; | ||
} | ||
|
||
const edit = this.actionsToEdit(response.body.actions); | ||
return workspace.applyEdit(edit); | ||
} | ||
} |
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