From cd27fb05e3838a427d37050aaa6a8420e26cc4ae Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 8 Jun 2022 16:51:33 -0700 Subject: [PATCH] Update paste and drop samples for latest API in VS Code insiders --- document-paste/README.md | 2 +- document-paste/package.json | 2 +- document-paste/src/extension.ts | 20 ++++---- .../src/vscode.proposed.documentPaste.d.ts | 34 ++++++++++++-- .../src/vscode.proposed.textEditorDrop.d.ts | 47 ------------------- drop-on-document/README.md | 2 +- drop-on-document/package.json | 2 +- drop-on-document/src/extension.ts | 10 ++-- .../src/vscode.proposed.textEditorDrop.d.ts | 25 ++++++---- 9 files changed, 66 insertions(+), 78 deletions(-) delete mode 100644 document-paste/src/vscode.proposed.textEditorDrop.d.ts diff --git a/document-paste/README.md b/document-paste/README.md index 4e0111ce..5591007d 100644 --- a/document-paste/README.md +++ b/document-paste/README.md @@ -2,4 +2,4 @@ This sample shows usage of the [document paste edit proposal](https://github.com/microsoft/vscode/issues/30066). -Requires enabling `editor.experimental.pasteActions.enabled` \ No newline at end of file +Requires enabling `editor.experimental.pasteActions.enabled` and VS Code 1.69. \ No newline at end of file diff --git a/document-paste/package.json b/document-paste/package.json index f187fa56..97346d62 100644 --- a/document-paste/package.json +++ b/document-paste/package.json @@ -15,7 +15,7 @@ "documentPaste" ], "engines": { - "vscode": "^1.67.0" + "vscode": "^1.69.0" }, "categories": [ "Other" diff --git a/document-paste/src/extension.ts b/document-paste/src/extension.ts index 58c49cb2..e6751c94 100644 --- a/document-paste/src/extension.ts +++ b/document-paste/src/extension.ts @@ -11,19 +11,20 @@ class CopyCountPasteEditProvider implements vscode.DocumentPasteEditProvider { prepareDocumentPaste?( _document: vscode.TextDocument, - _range: vscode.Range, + _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken - ): void | Thenable { + ): void | Thenable { dataTransfer.set(this.countMimeTypes, new vscode.DataTransferItem(this.count++)); + dataTransfer.set('text/plain', new vscode.DataTransferItem(this.count++)); } - + async provideDocumentPasteEdits( _document: vscode.TextDocument, - range: vscode.Range, + _ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, - token: vscode.CancellationToken - ) { + _token: vscode.CancellationToken + ): Promise { const countDataTransferItem = dataTransfer.get(this.countMimeTypes) if (!countDataTransferItem) { return undefined; @@ -36,13 +37,12 @@ class CopyCountPasteEditProvider implements vscode.DocumentPasteEditProvider { const count = await countDataTransferItem.asString(); const text = await textDataTransferItem.asString(); - // Build a snippet to insert const snippet = new vscode.SnippetString(); snippet.appendText(`(copy #${count}) ${text}`); - return new vscode.SnippetTextEdit(range, snippet); + return { insertText: snippet } } } @@ -51,5 +51,7 @@ export function activate(context: vscode.ExtensionContext) { const selector: vscode.DocumentSelector = { language: 'plaintext' }; // Register our provider - context.subscriptions.push(vscode.languages.registerDocumentPasteEditProvider(selector, new CopyCountPasteEditProvider())); + context.subscriptions.push(vscode.languages.registerDocumentPasteEditProvider(selector, new CopyCountPasteEditProvider(), { + pasteMimeTypes: ['text/plain'], + })); } diff --git a/document-paste/src/vscode.proposed.documentPaste.d.ts b/document-paste/src/vscode.proposed.documentPaste.d.ts index 1777c8d4..29335e1d 100644 --- a/document-paste/src/vscode.proposed.documentPaste.d.ts +++ b/document-paste/src/vscode.proposed.documentPaste.d.ts @@ -19,11 +19,11 @@ declare module 'vscode' { * a {@link DataTransfer} and is passed back to the provider in {@link provideDocumentPasteEdits}. * * @param document Document where the copy took place. - * @param range Range being copied in the `document`. + * @param ranges Ranges being copied in the `document`. * @param dataTransfer The data transfer associated with the copy. You can store additional values on this for later use in {@link provideDocumentPasteEdits}. * @param token A cancellation token. */ - prepareDocumentPaste?(document: TextDocument, range: Range, dataTransfer: DataTransfer, token: CancellationToken): void | Thenable; + prepareDocumentPaste?(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): void | Thenable; /** * Invoked before the user pastes into a document. @@ -31,16 +31,40 @@ declare module 'vscode' { * In this method, extensions can return a workspace edit that replaces the standard pasting behavior. * * @param document Document being pasted into - * @param range Currently selected range in the document. + * @param ranges Currently selected ranges in the document. * @param dataTransfer The data transfer associated with the paste. * @param token A cancellation token. * * @return Optional workspace edit that applies the paste. Return undefined to use standard pasting. */ - provideDocumentPasteEdits(document: TextDocument, range: Range, dataTransfer: DataTransfer, token: CancellationToken): ProviderResult; + provideDocumentPasteEdits(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): ProviderResult; + } + + /** + * An operation applied on paste + */ + interface DocumentPasteEdit { + /** + * The text or snippet to insert at the pasted locations. + */ + readonly insertText: string | SnippetString; + + /** + * An optional additional edit to apply on paste. + */ + readonly additionalEdit?: WorkspaceEdit; + } + + interface DocumentPasteProviderMetadata { + /** + * Mime types that `provideDocumentPasteEdits` should be invoked for. + * + * Use the special `files` mimetype to indicate the provider should be invoked if any files are present in the `DataTransfer`. + */ + readonly pasteMimeTypes: readonly string[]; } namespace languages { - export function registerDocumentPasteEditProvider(selector: DocumentSelector, provider: DocumentPasteEditProvider): Disposable; + export function registerDocumentPasteEditProvider(selector: DocumentSelector, provider: DocumentPasteEditProvider, metadata: DocumentPasteProviderMetadata): Disposable; } } diff --git a/document-paste/src/vscode.proposed.textEditorDrop.d.ts b/document-paste/src/vscode.proposed.textEditorDrop.d.ts deleted file mode 100644 index 5468bc21..00000000 --- a/document-paste/src/vscode.proposed.textEditorDrop.d.ts +++ /dev/null @@ -1,47 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -declare module 'vscode' { - - // https://github.com/microsoft/vscode/issues/142990 - - export class SnippetTextEdit { - snippet: SnippetString; - range: Range; - constructor(range: Range, snippet: SnippetString); - } - - /** - * Provider which handles dropping of resources into a text editor. - * - * The user can drop into a text editor by holding down `shift` while dragging. Requires `workbench.experimental.editor.dropIntoEditor.enabled` to be on. - */ - export interface DocumentOnDropEditProvider { - /** - * Provide edits which inserts the content being dragged and dropped into the document. - * - * @param document The document in which the drop occurred. - * @param position The position in the document where the drop occurred. - * @param dataTransfer A {@link DataTransfer} object that holds data about what is being dragged and dropped. - * @param token A cancellation token. - * - * @return A {@link SnippetTextEdit} or a thenable that resolves to such. The lack of a result can be - * signaled by returning `undefined` or `null`. - */ - provideDocumentOnDropEdits(document: TextDocument, position: Position, dataTransfer: DataTransfer, token: CancellationToken): ProviderResult; - } - - export namespace languages { - /** - * Registers a new {@link DocumentOnDropEditProvider}. - * - * @param selector A selector that defines the documents this provider applies to. - * @param provider A drop provider. - * - * @return A {@link Disposable} that unregisters this provider when disposed of. - */ - export function registerDocumentOnDropEditProvider(selector: DocumentSelector, provider: DocumentOnDropEditProvider): Disposable; - } -} diff --git a/drop-on-document/README.md b/drop-on-document/README.md index ba0c5b00..d31e10fc 100644 --- a/drop-on-document/README.md +++ b/drop-on-document/README.md @@ -1,6 +1,6 @@ # Document on Drop Sample -This sample shows usage of the [document on drop API proposal](https://github.com/microsoft/vscode/issues/142990). +This sample shows usage of the [document on drop API proposal](https://github.com/microsoft/vscode/issues/142990). It requires VS Code 1.69. This sample adds two sample drop providers for plain text files: diff --git a/drop-on-document/package.json b/drop-on-document/package.json index 464f83d6..5fe88230 100644 --- a/drop-on-document/package.json +++ b/drop-on-document/package.json @@ -14,7 +14,7 @@ "textEditorDrop" ], "engines": { - "vscode": "^1.67.0" + "vscode": "^1.69.0" }, "categories": [ "Other" diff --git a/drop-on-document/src/extension.ts b/drop-on-document/src/extension.ts index 1cd97993..87b1b6f5 100644 --- a/drop-on-document/src/extension.ts +++ b/drop-on-document/src/extension.ts @@ -9,13 +9,13 @@ const uriListMime = 'text/uri-list'; * Note this does not apply to text that is drag and dropped with-in the current editor, * only for text dropped from external apps. */ - class ReverseTextOnDropProvider implements vscode.DocumentOnDropEditProvider { +class ReverseTextOnDropProvider implements vscode.DocumentOnDropEditProvider { async provideDocumentOnDropEdits( _document: vscode.TextDocument, position: vscode.Position, dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken - ): Promise { + ): Promise { // Check the data transfer to see if we have some kind of text data const dataTransferItem = dataTransfer.get('text') ?? dataTransfer.get('text/plain'); if (!dataTransferItem) { @@ -32,7 +32,7 @@ const uriListMime = 'text/uri-list'; // Adding the reversed text snippet.appendText([...text].reverse().join('')); - return new vscode.SnippetTextEdit(new vscode.Range(position, position), snippet); + return { insertText: snippet }; } } @@ -51,7 +51,7 @@ class FileNameListOnDropProvider implements vscode.DocumentOnDropEditProvider { position: vscode.Position, dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken - ): Promise { + ): Promise { // Check the data transfer to see if we have dropped a list of uris const dataTransferItem = dataTransfer.get(uriListMime); if (!dataTransferItem) { @@ -90,7 +90,7 @@ class FileNameListOnDropProvider implements vscode.DocumentOnDropEditProvider { } }); - return new vscode.SnippetTextEdit(new vscode.Range(position, position), snippet); + return { insertText: snippet }; } } diff --git a/drop-on-document/src/vscode.proposed.textEditorDrop.d.ts b/drop-on-document/src/vscode.proposed.textEditorDrop.d.ts index 5468bc21..1b0d76db 100644 --- a/drop-on-document/src/vscode.proposed.textEditorDrop.d.ts +++ b/drop-on-document/src/vscode.proposed.textEditorDrop.d.ts @@ -7,12 +7,6 @@ declare module 'vscode' { // https://github.com/microsoft/vscode/issues/142990 - export class SnippetTextEdit { - snippet: SnippetString; - range: Range; - constructor(range: Range, snippet: SnippetString); - } - /** * Provider which handles dropping of resources into a text editor. * @@ -27,10 +21,25 @@ declare module 'vscode' { * @param dataTransfer A {@link DataTransfer} object that holds data about what is being dragged and dropped. * @param token A cancellation token. * - * @return A {@link SnippetTextEdit} or a thenable that resolves to such. The lack of a result can be + * @return A {@link DocumentDropEdit} or a thenable that resolves to such. The lack of a result can be * signaled by returning `undefined` or `null`. */ - provideDocumentOnDropEdits(document: TextDocument, position: Position, dataTransfer: DataTransfer, token: CancellationToken): ProviderResult; + provideDocumentOnDropEdits(document: TextDocument, position: Position, dataTransfer: DataTransfer, token: CancellationToken): ProviderResult; + } + + /** + * An edit operation applied on drop. + */ + export interface DocumentDropEdit { + /** + * The text or snippet to insert at the drop location. + */ + readonly insertText: string | SnippetString; + + /** + * An optional additional edit to apply on drop. + */ + readonly additionalEdit?: WorkspaceEdit; } export namespace languages {