|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { DocumentSelector } from '../configuration/documentSelector'; |
| 8 | +import * as typeConverters from '../typeConverters'; |
| 9 | +import { ClientCapability, ITypeScriptServiceClient } from '../typescriptService'; |
| 10 | +import { conditionalRegistration, requireMinVersion, requireSomeCapability } from './util/dependentRegistration'; |
| 11 | +import protocol from '../tsServer/protocol/protocol'; |
| 12 | +import { API } from '../tsServer/api'; |
| 13 | +import { LanguageDescription } from '../configuration/languageDescription'; |
| 14 | + |
| 15 | +class CopyMetadata { |
| 16 | + constructor( |
| 17 | + readonly resource: vscode.Uri, |
| 18 | + readonly ranges: readonly vscode.Range[], |
| 19 | + ) { } |
| 20 | + |
| 21 | + toJSON() { |
| 22 | + return JSON.stringify({ |
| 23 | + resource: this.resource.toJSON(), |
| 24 | + ranges: this.ranges, |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + static fromJSON(str: string): CopyMetadata | undefined { |
| 29 | + try { |
| 30 | + const parsed = JSON.parse(str); |
| 31 | + return new CopyMetadata( |
| 32 | + vscode.Uri.from(parsed.resource), |
| 33 | + parsed.ranges.map((r: any) => new vscode.Range(r[0].line, r[0].character, r[1].line, r[1].character))); |
| 34 | + } catch { |
| 35 | + // ignore |
| 36 | + } |
| 37 | + return undefined; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class DocumentPasteProvider implements vscode.DocumentPasteEditProvider { |
| 42 | + |
| 43 | + static readonly kind = vscode.DocumentDropOrPasteEditKind.Empty.append('text', 'jsts', 'pasteWithImports'); |
| 44 | + static readonly metadataMimeType = 'application/vnd.code.jsts.metadata'; |
| 45 | + |
| 46 | + constructor( |
| 47 | + private readonly _modeId: string, |
| 48 | + private readonly _client: ITypeScriptServiceClient, |
| 49 | + ) { } |
| 50 | + |
| 51 | + prepareDocumentPaste(document: vscode.TextDocument, ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken) { |
| 52 | + dataTransfer.set(DocumentPasteProvider.metadataMimeType, |
| 53 | + new vscode.DataTransferItem(new CopyMetadata(document.uri, ranges).toJSON())); |
| 54 | + } |
| 55 | + |
| 56 | + async provideDocumentPasteEdits( |
| 57 | + document: vscode.TextDocument, |
| 58 | + ranges: readonly vscode.Range[], |
| 59 | + dataTransfer: vscode.DataTransfer, |
| 60 | + _context: vscode.DocumentPasteEditContext, |
| 61 | + token: vscode.CancellationToken, |
| 62 | + ): Promise<vscode.DocumentPasteEdit[] | undefined> { |
| 63 | + const config = vscode.workspace.getConfiguration(this._modeId, document.uri); |
| 64 | + if (!config.get('experimental.updateImportsOnPaste')) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const file = this._client.toOpenTsFilePath(document); |
| 69 | + if (!file) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const text = await dataTransfer.get('text/plain')?.asString(); |
| 74 | + if (!text || token.isCancellationRequested) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Get optional metadata |
| 79 | + const metadata = await this.extractMetadata(dataTransfer, token); |
| 80 | + if (token.isCancellationRequested) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + let copiedFrom: { |
| 85 | + file: string; |
| 86 | + spans: protocol.TextSpan[]; |
| 87 | + } | undefined; |
| 88 | + if (metadata) { |
| 89 | + const spans = metadata.ranges.map(typeConverters.Range.toTextSpan); |
| 90 | + const copyFile = this._client.toTsFilePath(metadata.resource); |
| 91 | + if (copyFile) { |
| 92 | + copiedFrom = { file: copyFile, spans }; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const response = await this._client.execute('getPasteEdits', { |
| 97 | + file, |
| 98 | + // TODO: only supports a single paste for now |
| 99 | + pastedText: [text], |
| 100 | + pasteLocations: ranges.map(typeConverters.Range.toTextSpan), |
| 101 | + copiedFrom |
| 102 | + }, token); |
| 103 | + if (response.type !== 'response' || !response.body || token.isCancellationRequested) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const edit = new vscode.DocumentPasteEdit('', vscode.l10n.t("Paste with imports"), DocumentPasteProvider.kind); |
| 108 | + const additionalEdit = new vscode.WorkspaceEdit(); |
| 109 | + for (const edit of response.body.edits) { |
| 110 | + additionalEdit.set(this._client.toResource(edit.fileName), edit.textChanges.map(typeConverters.TextEdit.fromCodeEdit)); |
| 111 | + } |
| 112 | + edit.additionalEdit = additionalEdit; |
| 113 | + return [edit]; |
| 114 | + } |
| 115 | + |
| 116 | + private async extractMetadata(dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Promise<CopyMetadata | undefined> { |
| 117 | + const metadata = await dataTransfer.get(DocumentPasteProvider.metadataMimeType)?.asString(); |
| 118 | + if (token.isCancellationRequested) { |
| 119 | + return undefined; |
| 120 | + } |
| 121 | + |
| 122 | + return metadata ? CopyMetadata.fromJSON(metadata) : undefined; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export function register(selector: DocumentSelector, language: LanguageDescription, client: ITypeScriptServiceClient) { |
| 127 | + return conditionalRegistration([ |
| 128 | + requireSomeCapability(client, ClientCapability.Semantic), |
| 129 | + requireMinVersion(client, API.v550), |
| 130 | + ], () => { |
| 131 | + return vscode.languages.registerDocumentPasteEditProvider(selector.semantic, new DocumentPasteProvider(language.id, client), { |
| 132 | + providedPasteEditKinds: [DocumentPasteProvider.kind], |
| 133 | + copyMimeTypes: [DocumentPasteProvider.metadataMimeType], |
| 134 | + pasteMimeTypes: ['text/plain'], |
| 135 | + }); |
| 136 | + }); |
| 137 | +} |
0 commit comments