Adopt preparePasteEdits for js/ts update imports on paste (#230007)

Client side of https://github.com/microsoft/TypeScript/pull/60053
This commit is contained in:
Matt Bierner
2024-09-27 12:50:23 -07:00
committed by GitHub
parent acc4f88d1b
commit f74d65ef2c
5 changed files with 40 additions and 6 deletions

View File

@@ -50,7 +50,24 @@ class DocumentPasteProvider implements vscode.DocumentPasteEditProvider {
private readonly _client: ITypeScriptServiceClient,
) { }
prepareDocumentPaste(document: vscode.TextDocument, ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken) {
async prepareDocumentPaste(document: vscode.TextDocument, ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken) {
if (!this.isEnabled(document)) {
return;
}
const file = this._client.toOpenTsFilePath(document);
if (!file) {
return;
}
const response = await this._client.execute('preparePasteEdits', {
file,
copiedTextSpan: ranges.map(typeConverters.Range.toTextSpan),
}, token);
if (token.isCancellationRequested || response.type !== 'response' || !response.body) {
return;
}
dataTransfer.set(DocumentPasteProvider.metadataMimeType,
new vscode.DataTransferItem(new CopyMetadata(document.uri, ranges).toJSON()));
}
@@ -62,8 +79,7 @@ class DocumentPasteProvider implements vscode.DocumentPasteEditProvider {
_context: vscode.DocumentPasteEditContext,
token: vscode.CancellationToken,
): Promise<vscode.DocumentPasteEdit[] | undefined> {
const config = vscode.workspace.getConfiguration(this._modeId, document.uri);
if (!config.get(settingId, false)) {
if (!this.isEnabled(document)) {
return;
}
@@ -127,12 +143,17 @@ class DocumentPasteProvider implements vscode.DocumentPasteEditProvider {
return metadata ? CopyMetadata.fromJSON(metadata) : undefined;
}
private isEnabled(document: vscode.TextDocument) {
const config = vscode.workspace.getConfiguration(this._modeId, document.uri);
return config.get(settingId, false);
}
}
export function register(selector: DocumentSelector, language: LanguageDescription, client: ITypeScriptServiceClient) {
return conditionalRegistration([
requireSomeCapability(client, ClientCapability.Semantic),
requireMinVersion(client, API.v560),
requireMinVersion(client, API.v570),
requireGlobalConfiguration(language.id, settingId),
], () => {
return vscode.languages.registerDocumentPasteEditProvider(selector.semantic, new DocumentPasteProvider(language.id, client), {

View File

@@ -28,7 +28,6 @@ export class API {
public static readonly v520 = API.fromSimpleString('5.2.0');
public static readonly v544 = API.fromSimpleString('5.4.4');
public static readonly v540 = API.fromSimpleString('5.4.0');
public static readonly v550 = API.fromSimpleString('5.5.0');
public static readonly v560 = API.fromSimpleString('5.6.0');
public static readonly v570 = API.fromSimpleString('5.7.0');

View File

@@ -19,5 +19,18 @@ declare module '../../../../node_modules/typescript/lib/typescript' {
interface Response {
readonly _serverType?: ServerType;
}
//#region PreparePasteEdits
interface PreparePasteEditsRequest extends FileRequest {
command: 'preparePasteEdits';
arguments: PreparePasteEditsRequestArgs;
}
interface PreparePasteEditsRequestArgs extends FileRequestArgs {
copiedTextSpan: TextSpan[];
}
interface PreparePasteEditsResponse extends Response {
body: boolean;
}
//#endregion
}
}

View File

@@ -78,6 +78,7 @@ interface StandardTsServerRequests {
'linkedEditingRange': [Proto.FileLocationRequestArgs, Proto.LinkedEditingRangeResponse];
'mapCode': [Proto.MapCodeRequestArgs, Proto.MapCodeResponse];
'getPasteEdits': [Proto.GetPasteEditsRequestArgs, Proto.GetPasteEditsResponse];
'preparePasteEdits': [Proto.PreparePasteEditsRequestArgs, Proto.PreparePasteEditsResponse];
}
interface NoResponseTsServerRequests {