-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110573 from microsoft/joh/tsQuickRename
Implement on-type-rename for TypeScript
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
extensions/typescript-language-features/src/languageFeatures/renameOnType.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,73 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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 { Kind } from '../protocol.const'; | ||
import { ClientCapability, ITypeScriptServiceClient } from '../typescriptService'; | ||
import { conditionalRegistration, requireSomeCapability } from '../utils/dependentRegistration'; | ||
import { DocumentSelector } from '../utils/documentSelector'; | ||
import * as typeConverters from '../utils/typeConverters'; | ||
|
||
|
||
class TypeScriptOnTypeRenameProvider implements vscode.OnTypeRenameRangeProvider { | ||
|
||
private static enabledKinds = new Set<string>([ | ||
Kind.let, Kind.const, Kind.localVariable, Kind.parameter, Kind.typeParameter | ||
]); | ||
|
||
public constructor( | ||
private readonly client: ITypeScriptServiceClient | ||
) { } | ||
|
||
public async provideOnTypeRenameRanges( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
token: vscode.CancellationToken | ||
): Promise<vscode.OnTypeRenameRanges | undefined> { | ||
const file = this.client.toOpenedFilePath(document); | ||
if (!file) { | ||
return undefined; | ||
} | ||
const args = typeConverters.Position.toFileLocationRequestArgs(file, position); | ||
// | ||
|
||
const quickInfoResponse = await this.client.interruptGetErr(() => this.client.execute('quickinfo', args, token)); | ||
if (quickInfoResponse.type !== 'response' || !quickInfoResponse.body) { | ||
return undefined; | ||
} | ||
|
||
if (!TypeScriptOnTypeRenameProvider.enabledKinds.has(quickInfoResponse.body.kind)) { | ||
return undefined; | ||
} | ||
|
||
const renameResponse = await this.client.execute('rename', args, token); | ||
if (!renameResponse || renameResponse.type !== 'response' || !renameResponse.body) { | ||
return undefined; | ||
} | ||
|
||
if (renameResponse.body.locs.length !== 1 || renameResponse.body.locs[0].file !== file) { | ||
return undefined; // not a local? | ||
} | ||
|
||
const ranges = renameResponse.body.locs[0].locs.map(typeConverters.Range.fromTextSpan); | ||
if (ranges.length <= 1) { | ||
return undefined; // not enough usages | ||
} | ||
return new vscode.OnTypeRenameRanges(ranges); | ||
} | ||
|
||
} | ||
|
||
export function register( | ||
selector: DocumentSelector, | ||
client: ITypeScriptServiceClient | ||
): vscode.Disposable { | ||
return conditionalRegistration([ | ||
requireSomeCapability(client, ClientCapability.EnhancedSyntax, ClientCapability.Semantic), | ||
], () => { | ||
return vscode.languages.registerOnTypeRenameRangeProvider(selector.syntax, | ||
new TypeScriptOnTypeRenameProvider(client)); | ||
}); | ||
} |
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