Update paste and drop samples for latest API in VS Code insiders

This commit is contained in:
Matt Bierner
2022-06-08 16:51:33 -07:00
parent adde897872
commit cd27fb05e3
9 changed files with 66 additions and 78 deletions

View File

@ -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> {
): void | Thenable<void> {
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<vscode.DocumentPasteEdit | undefined> {
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'],
}));
}

View File

@ -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<void>;
prepareDocumentPaste?(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): void | Thenable<void>;
/**
* 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<WorkspaceEdit | SnippetTextEdit>;
provideDocumentPasteEdits(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): ProviderResult<DocumentPasteEdit>;
}
/**
* 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;
}
}

View File

@ -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<SnippetTextEdit>;
}
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;
}
}