mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 10:38:59 +01:00
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:
@@ -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), {
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user