|
| 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 assert from 'assert'; |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import { assertNoRpc, createRandomFile, usingDisposables } from '../utils'; |
| 9 | + |
| 10 | +const textPlain = 'text/plain'; |
| 11 | + |
| 12 | +(vscode.env.uiKind === vscode.UIKind.Web ? suite.skip : suite)('vscode API - Copy Paste', () => { |
| 13 | + |
| 14 | + teardown(assertNoRpc); |
| 15 | + |
| 16 | + test('Copy should be able to overwrite text/plain', usingDisposables(async (disposables) => { |
| 17 | + const file = await createRandomFile('$abcde@'); |
| 18 | + const doc = await vscode.workspace.openTextDocument(file); |
| 19 | + |
| 20 | + const editor = await vscode.window.showTextDocument(doc); |
| 21 | + editor.selections = [new vscode.Selection(0, 1, 0, 6)]; |
| 22 | + |
| 23 | + disposables.push(vscode.languages.registerDocumentPasteEditProvider({ language: 'plaintext' }, new class implements vscode.DocumentPasteEditProvider { |
| 24 | + async prepareDocumentPaste(_document: vscode.TextDocument, _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> { |
| 25 | + const existing = dataTransfer.get(textPlain); |
| 26 | + if (existing) { |
| 27 | + const str = await existing.asString(); |
| 28 | + const reversed = reverseString(str); |
| 29 | + dataTransfer.set(textPlain, new vscode.DataTransferItem(reversed)); |
| 30 | + } |
| 31 | + } |
| 32 | + }, { copyMimeTypes: [textPlain] })); |
| 33 | + |
| 34 | + await vscode.commands.executeCommand('editor.action.clipboardCopyAction'); |
| 35 | + const newDocContent = getNextDocumentText(disposables, doc); |
| 36 | + await vscode.commands.executeCommand('editor.action.clipboardPasteAction'); |
| 37 | + assert.strictEqual(await newDocContent, '$edcba@'); |
| 38 | + })); |
| 39 | + |
| 40 | + test('Copy with empty selection should copy entire line', usingDisposables(async (disposables) => { |
| 41 | + const file = await createRandomFile('abc\ndef'); |
| 42 | + const doc = await vscode.workspace.openTextDocument(file); |
| 43 | + await vscode.window.showTextDocument(doc); |
| 44 | + |
| 45 | + disposables.push(vscode.languages.registerDocumentPasteEditProvider({ language: 'plaintext' }, new class implements vscode.DocumentPasteEditProvider { |
| 46 | + async prepareDocumentPaste(_document: vscode.TextDocument, _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> { |
| 47 | + const existing = dataTransfer.get(textPlain); |
| 48 | + if (existing) { |
| 49 | + const str = await existing.asString(); |
| 50 | + // text/plain includes the trailing new line in this case |
| 51 | + // On windows, this will always be `\r\n` even if the document uses `\n` |
| 52 | + const eol = str.match(/\r?\n$/); |
| 53 | + const reversed = reverseString(str.slice(0, -eol![0].length)); |
| 54 | + dataTransfer.set(textPlain, new vscode.DataTransferItem(reversed + '\n')); |
| 55 | + } |
| 56 | + } |
| 57 | + }, { copyMimeTypes: [textPlain] })); |
| 58 | + |
| 59 | + await vscode.commands.executeCommand('editor.action.clipboardCopyAction'); |
| 60 | + const newDocContent = getNextDocumentText(disposables, doc); |
| 61 | + await vscode.commands.executeCommand('editor.action.clipboardPasteAction'); |
| 62 | + assert.strictEqual(await newDocContent, `cba\nabc\ndef`); |
| 63 | + })); |
| 64 | + |
| 65 | + test('Copy with multiple selections should get all selections', usingDisposables(async (disposables) => { |
| 66 | + const file = await createRandomFile('111\n222\n333'); |
| 67 | + const doc = await vscode.workspace.openTextDocument(file); |
| 68 | + const editor = await vscode.window.showTextDocument(doc); |
| 69 | + |
| 70 | + editor.selections = [ |
| 71 | + new vscode.Selection(0, 0, 0, 3), |
| 72 | + new vscode.Selection(2, 0, 2, 3), |
| 73 | + ]; |
| 74 | + |
| 75 | + disposables.push(vscode.languages.registerDocumentPasteEditProvider({ language: 'plaintext' }, new class implements vscode.DocumentPasteEditProvider { |
| 76 | + async prepareDocumentPaste(document: vscode.TextDocument, ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> { |
| 77 | + const existing = dataTransfer.get(textPlain); |
| 78 | + if (existing) { |
| 79 | + const selections = ranges.map(range => document.getText(range)); |
| 80 | + dataTransfer.set(textPlain, new vscode.DataTransferItem(`(${ranges.length})${selections.join(' ')}`)); |
| 81 | + } |
| 82 | + } |
| 83 | + }, { copyMimeTypes: [textPlain] })); |
| 84 | + |
| 85 | + await vscode.commands.executeCommand('editor.action.clipboardCopyAction'); |
| 86 | + editor.selections = [new vscode.Selection(0, 0, 0, 0)]; |
| 87 | + const newDocContent = getNextDocumentText(disposables, doc); |
| 88 | + await vscode.commands.executeCommand('editor.action.clipboardPasteAction'); |
| 89 | + |
| 90 | + assert.strictEqual(await newDocContent, `(2)111 333111\n222\n333`); |
| 91 | + })); |
| 92 | + |
| 93 | + test('Earlier invoked copy providers should win when writing values', usingDisposables(async (disposables) => { |
| 94 | + const file = await createRandomFile('abc\ndef'); |
| 95 | + const doc = await vscode.workspace.openTextDocument(file); |
| 96 | + |
| 97 | + const editor = await vscode.window.showTextDocument(doc); |
| 98 | + editor.selections = [new vscode.Selection(0, 0, 0, 3)]; |
| 99 | + |
| 100 | + const callOrder: string[] = []; |
| 101 | + const a_id = 'a'; |
| 102 | + const b_id = 'b'; |
| 103 | + |
| 104 | + let providerAResolve: () => void; |
| 105 | + const providerAFinished = new Promise<void>(resolve => providerAResolve = resolve); |
| 106 | + |
| 107 | + disposables.push(vscode.languages.registerDocumentPasteEditProvider({ language: 'plaintext' }, new class implements vscode.DocumentPasteEditProvider { |
| 108 | + async prepareDocumentPaste(_document: vscode.TextDocument, _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> { |
| 109 | + callOrder.push(a_id); |
| 110 | + dataTransfer.set(textPlain, new vscode.DataTransferItem('a')); |
| 111 | + providerAResolve(); |
| 112 | + } |
| 113 | + }, { copyMimeTypes: [textPlain] })); |
| 114 | + |
| 115 | + // Later registered providers will be called first |
| 116 | + disposables.push(vscode.languages.registerDocumentPasteEditProvider({ language: 'plaintext' }, new class implements vscode.DocumentPasteEditProvider { |
| 117 | + async prepareDocumentPaste(_document: vscode.TextDocument, _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> { |
| 118 | + callOrder.push(b_id); |
| 119 | + |
| 120 | + // Wait for the first provider to finish even though we were called first. |
| 121 | + // This tests that resulting order does not depend on the order the providers |
| 122 | + // return in. |
| 123 | + await providerAFinished; |
| 124 | + |
| 125 | + dataTransfer.set(textPlain, new vscode.DataTransferItem('b')); |
| 126 | + } |
| 127 | + }, { copyMimeTypes: [textPlain] })); |
| 128 | + |
| 129 | + await vscode.commands.executeCommand('editor.action.clipboardCopyAction'); |
| 130 | + const newDocContent = getNextDocumentText(disposables, doc); |
| 131 | + await vscode.commands.executeCommand('editor.action.clipboardPasteAction'); |
| 132 | + assert.strictEqual(await newDocContent, 'b\ndef'); |
| 133 | + |
| 134 | + // Confirm provider call order is what we expected |
| 135 | + assert.deepStrictEqual(callOrder, [b_id, a_id]); |
| 136 | + })); |
| 137 | + |
| 138 | + test('Copy providers should not be able to effect the data transfer of another', usingDisposables(async (disposables) => { |
| 139 | + const file = await createRandomFile('abc\ndef'); |
| 140 | + const doc = await vscode.workspace.openTextDocument(file); |
| 141 | + |
| 142 | + const editor = await vscode.window.showTextDocument(doc); |
| 143 | + editor.selections = [new vscode.Selection(0, 0, 0, 3)]; |
| 144 | + |
| 145 | + |
| 146 | + let providerAResolve: () => void; |
| 147 | + const providerAFinished = new Promise<void>(resolve => providerAResolve = resolve); |
| 148 | + |
| 149 | + disposables.push(vscode.languages.registerDocumentPasteEditProvider({ language: 'plaintext' }, new class implements vscode.DocumentPasteEditProvider { |
| 150 | + async prepareDocumentPaste(_document: vscode.TextDocument, _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> { |
| 151 | + dataTransfer.set(textPlain, new vscode.DataTransferItem('xyz')); |
| 152 | + providerAResolve(); |
| 153 | + } |
| 154 | + }, { copyMimeTypes: [textPlain] })); |
| 155 | + |
| 156 | + disposables.push(vscode.languages.registerDocumentPasteEditProvider({ language: 'plaintext' }, new class implements vscode.DocumentPasteEditProvider { |
| 157 | + async prepareDocumentPaste(_document: vscode.TextDocument, _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> { |
| 158 | + |
| 159 | + // Wait for the first provider to finish |
| 160 | + await providerAFinished; |
| 161 | + |
| 162 | + // We we access the data transfer here, we should not see changes made by the first provider |
| 163 | + const entry = dataTransfer.get(textPlain); |
| 164 | + const str = await entry!.asString(); |
| 165 | + dataTransfer.set(textPlain, new vscode.DataTransferItem(reverseString(str))); |
| 166 | + } |
| 167 | + }, { copyMimeTypes: [textPlain] })); |
| 168 | + |
| 169 | + await vscode.commands.executeCommand('editor.action.clipboardCopyAction'); |
| 170 | + const newDocContent = getNextDocumentText(disposables, doc); |
| 171 | + await vscode.commands.executeCommand('editor.action.clipboardPasteAction'); |
| 172 | + assert.strictEqual(await newDocContent, 'cba\ndef'); |
| 173 | + |
| 174 | + })); |
| 175 | +}); |
| 176 | + |
| 177 | +function reverseString(str: string) { |
| 178 | + return str.split("").reverse().join(""); |
| 179 | +} |
| 180 | + |
| 181 | +function getNextDocumentText(disposables: vscode.Disposable[], doc: vscode.TextDocument): Promise<string> { |
| 182 | + return new Promise<string>(resolve => { |
| 183 | + disposables.push(vscode.workspace.onDidChangeTextDocument(e => { |
| 184 | + if (e.document === doc) { |
| 185 | + resolve(doc.getText()); |
| 186 | + } |
| 187 | + })); |
| 188 | + }); |
| 189 | +} |
| 190 | + |
0 commit comments