From c44309d73492d9caf1f21432ba1beabb46ea0b56 Mon Sep 17 00:00:00 2001 From: Marc Dumais Date: Wed, 3 May 2023 16:14:59 -0400 Subject: [PATCH] Stub for proposed API: DocumentPaste The immediate goal is for vscode built-in extension `vscode.markdown-language-features` v1.72.2 to work again (minus the "DocumentPaste" benefits. Fixes #12430 Signed-off-by: Marc Dumais --- packages/plugin-ext/src/plugin/languages.ts | 10 +++ .../plugin-ext/src/plugin/plugin-context.ts | 11 ++- packages/plugin-ext/src/plugin/types-impl.ts | 11 +++ packages/plugin/src/theia-proposed.d.ts | 69 +++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) diff --git a/packages/plugin-ext/src/plugin/languages.ts b/packages/plugin-ext/src/plugin/languages.ts index d60a7ee0b2c29..7a60825bf5e32 100644 --- a/packages/plugin-ext/src/plugin/languages.ts +++ b/packages/plugin-ext/src/plugin/languages.ts @@ -999,6 +999,16 @@ export class LanguagesExtImpl implements LanguagesExt { return result; } // #endregion + + // region DocumentPaste + + // Stub + registerDocumentPasteEditProvider( + extension: Plugin, selector: theia.DocumentSelector, provider: theia.DocumentPasteEditProvider, metadata: theia.DocumentPasteProviderMetadata + ): theia.Disposable { + return Disposable.NULL; + } + // #endregion } function getPluginLabel(pluginInfo: PluginInfo): string { diff --git a/packages/plugin-ext/src/plugin/plugin-context.ts b/packages/plugin-ext/src/plugin/plugin-context.ts index c171e4f54c065..88c94202cdecd 100644 --- a/packages/plugin-ext/src/plugin/plugin-context.ts +++ b/packages/plugin-ext/src/plugin/plugin-context.ts @@ -191,7 +191,8 @@ import { TerminalEditorTabInput, TextDiffTabInput, TextMergeTabInput, - WebviewEditorTabInput + WebviewEditorTabInput, + DocumentPasteEdit } from './types-impl'; import { AuthenticationExtImpl } from './authentication-ext'; import { SymbolKind } from '../common/plugin-api-rpc-model'; @@ -918,6 +919,11 @@ export function createAPIFactory( }, createLanguageStatusItem(id: string, selector: theia.DocumentSelector): theia.LanguageStatusItem { return languagesExt.createLanguageStatusItem(plugin, id, selector); + }, + registerDocumentPasteEditProvider( + selector: theia.DocumentSelector, provider: theia.DocumentPasteEditProvider, metadata: theia.DocumentPasteProviderMetadata + ): theia.Disposable { + return languagesExt.registerDocumentPasteEditProvider(plugin, selector, provider, metadata); } }; @@ -1336,7 +1342,8 @@ export function createAPIFactory( TabInputWebview: WebviewEditorTabInput, TabInputTerminal: TerminalEditorTabInput, TerminalLocation, - TerminalExitReason + TerminalExitReason, + DocumentPasteEdit }; }; } diff --git a/packages/plugin-ext/src/plugin/types-impl.ts b/packages/plugin-ext/src/plugin/types-impl.ts index 13371907138bb..468365f74ffcd 100644 --- a/packages/plugin-ext/src/plugin/types-impl.ts +++ b/packages/plugin-ext/src/plugin/types-impl.ts @@ -3429,3 +3429,14 @@ export class InteractiveWindowInput { } // #endregion + +// #region DocumentPaste +@es5ClassCompat +export class DocumentPasteEdit { + constructor(insertText: string | SnippetString) { + this.insertText = insertText; + } + insertText: string | SnippetString; + additionalEdit?: WorkspaceEdit; +} +// #endregion diff --git a/packages/plugin/src/theia-proposed.d.ts b/packages/plugin/src/theia-proposed.d.ts index d97447cff6a10..f2573ebc27ad9 100644 --- a/packages/plugin/src/theia-proposed.d.ts +++ b/packages/plugin/src/theia-proposed.d.ts @@ -613,6 +613,75 @@ export module '@theia/plugin' { } + // #region DocumentPaste + + // https://github.com/microsoft/vscode/issues/30066/ + + /** + * Provider invoked when the user copies and pastes code. + */ + export interface DocumentPasteEditProvider { + + /** + * Optional method invoked after the user copies text in a file. + * + * During {@link prepareDocumentPaste}, an extension can compute metadata that is attached to + * a {@link DataTransfer} and is passed back to the provider in {@link provideDocumentPasteEdits}. + * + * @param document Document where the copy took place. + * @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, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): void | Thenable; + + /** + * Invoked before the user pastes into a document. + * + * In this method, extensions can return a workspace edit that replaces the standard pasting behavior. + * + * @param document Document being pasted into + * @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, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): ProviderResult; + } + + /** + * An operation applied on paste + */ + class DocumentPasteEdit { + /** + * The text or snippet to insert at the pasted locations. + */ + insertText: string | SnippetString; + + /** + * An optional additional edit to apply on paste. + */ + additionalEdit?: WorkspaceEdit; + + /** + * @param insertText The text or snippet to insert at the pasted locations. + */ + constructor(insertText: string | SnippetString); + } + + 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, metadata: DocumentPasteProviderMetadata): Disposable; + } // #endregion }