Skip to content

Commit 59d0913

Browse files
authored
Update DropMetada and documentPaste proposed API for 1.88 compatibility (#13632)
contributed on behalf of STMicroelectronics Signed-off-by: Remi Schnekenburger <[email protected]>
1 parent de86a44 commit 59d0913

File tree

5 files changed

+183
-59
lines changed

5 files changed

+183
-59
lines changed

packages/plugin-ext/src/common/plugin-api-rpc-model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as theia from '@theia/plugin';
1818
import type * as monaco from '@theia/monaco-editor-core';
1919
import { MarkdownString as MarkdownStringDTO } from '@theia/core/lib/common/markdown-rendering';
2020
import { UriComponents } from './uri-components';
21-
import { CompletionItemTag, SnippetString } from '../plugin/types-impl';
21+
import { CompletionItemTag, DocumentPasteEditKind, SnippetString } from '../plugin/types-impl';
2222
import { Event as TheiaEvent } from '@theia/core/lib/common/event';
2323
import { URI } from '@theia/core/shared/vscode-uri';
2424
import { SerializedRegExp } from './plugin-api-rpc';
@@ -330,7 +330,7 @@ export interface DocumentDropEdit {
330330
}
331331

332332
export interface DocumentDropEditProviderMetadata {
333-
readonly id: string;
333+
readonly providedDropEditKinds?: readonly DocumentPasteEditKind[];
334334
readonly dropMimeTypes: readonly string[];
335335
}
336336

packages/plugin-ext/src/plugin/plugin-context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ import {
198198
TextMergeTabInput,
199199
WebviewEditorTabInput,
200200
DocumentPasteEdit,
201+
DocumentPasteEditKind,
202+
DocumentPasteTriggerKind,
201203
ExternalUriOpenerPriority,
202204
EditSessionIdentityMatch,
203205
TerminalOutputAnchor,
@@ -1403,6 +1405,8 @@ export function createAPIFactory(
14031405
TerminalOutputAnchor,
14041406
TerminalExitReason,
14051407
DocumentPasteEdit,
1408+
DocumentPasteEditKind,
1409+
DocumentPasteTriggerKind,
14061410
ExternalUriOpenerPriority,
14071411
TerminalQuickFixTerminalCommand,
14081412
TerminalQuickFixOpener,

packages/plugin-ext/src/plugin/types-impl.ts

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,15 +1643,11 @@ export class DocumentLink {
16431643

16441644
@es5ClassCompat
16451645
export class DocumentDropEdit {
1646-
1647-
id?: string;
1648-
1649-
priority?: number;
1650-
1651-
label?: string;
1652-
1646+
title?: string;
1647+
kind: DocumentPasteEditKind;
1648+
handledMimeType?: string;
1649+
yieldTo?: ReadonlyArray<DocumentPasteEditKind>;
16531650
insertText: string | SnippetString;
1654-
16551651
additionalEdit?: WorkspaceEdit;
16561652

16571653
constructor(insertText: string | SnippetString) {
@@ -3740,19 +3736,57 @@ export class InteractiveWindowInput {
37403736
// #endregion
37413737

37423738
// #region DocumentPaste
3739+
export class DocumentPasteEditKind {
3740+
static Empty: DocumentPasteEditKind;
3741+
3742+
constructor(public readonly value: string) { }
3743+
3744+
/** @stubbed */
3745+
append(...parts: string[]): CodeActionKind {
3746+
return CodeActionKind.Empty;
3747+
};
3748+
3749+
/** @stubbed */
3750+
intersects(other: CodeActionKind): boolean {
3751+
return false;
3752+
}
3753+
3754+
/** @stubbed */
3755+
contains(other: CodeActionKind): boolean {
3756+
return false;
3757+
}
3758+
}
3759+
DocumentPasteEditKind.Empty = new DocumentPasteEditKind('');
3760+
37433761
@es5ClassCompat
37443762
export class DocumentPasteEdit {
3745-
constructor(insertText: string | SnippetString, id: string, label: string) {
3763+
constructor(insertText: string | SnippetString, title: string, kind: DocumentPasteEditKind) {
37463764
this.insertText = insertText;
3747-
this.id = id;
3748-
this.label = label;
3765+
this.title = title;
3766+
this.kind = kind;
37493767
}
3768+
title: string;
3769+
kind: DocumentPasteEditKind;
37503770
insertText: string | SnippetString;
37513771
additionalEdit?: WorkspaceEdit;
3752-
id: string;
3753-
label: string;
3754-
priority?: number;
3772+
yieldTo?: readonly DocumentPasteEditKind[];
37553773
}
3774+
3775+
/**
3776+
* The reason why paste edits were requested.
3777+
*/
3778+
export enum DocumentPasteTriggerKind {
3779+
/**
3780+
* Pasting was requested as part of a normal paste operation.
3781+
*/
3782+
Automatic = 0,
3783+
3784+
/**
3785+
* Pasting was requested by the user with the `paste as` command.
3786+
*/
3787+
PasteAs = 1,
3788+
}
3789+
37563790
// #endregion
37573791

37583792
// #region DocumentPaste

packages/plugin/src/theia.proposed.documentPaste.d.ts

Lines changed: 117 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,107 @@
2323
export module '@theia/plugin' {
2424

2525
/**
26-
* Provider invoked when the user copies and pastes code.
26+
* The reason why paste edits were requested.
2727
*/
28-
export interface DocumentPasteEditProvider {
28+
export enum DocumentPasteTriggerKind {
29+
/**
30+
* Pasting was requested as part of a normal paste operation.
31+
*/
32+
Automatic = 0,
33+
34+
/**
35+
* Pasting was requested by the user with the `paste as` command.
36+
*/
37+
PasteAs = 1,
38+
}
39+
40+
/**
41+
* Additional information about the paste operation.
42+
*/
43+
44+
export interface DocumentPasteEditContext {
45+
/**
46+
* Requested kind of paste edits to return.
47+
*/
48+
readonly only: DocumentPasteEditKind | undefined;
49+
50+
/**
51+
* The reason why paste edits were requested.
52+
*/
53+
readonly triggerKind: DocumentPasteTriggerKind;
54+
}
55+
56+
/**
57+
* Provider invoked when the user copies or pastes in a {@linkcode TextDocument}.
58+
*/
59+
interface DocumentPasteEditProvider<T extends DocumentPasteEdit = DocumentPasteEdit> {
2960

3061
/**
3162
* Optional method invoked after the user copies text in a file.
3263
*
33-
* During {@link prepareDocumentPaste}, an extension can compute metadata that is attached to
34-
* a {@link DataTransfer} and is passed back to the provider in {@link provideDocumentPasteEdits}.
64+
* This allows the provider to attach copy metadata to the {@link DataTransfer}
65+
* which is then passed back to providers in {@linkcode provideDocumentPasteEdits}.
66+
*
67+
* Note that currently any changes to the {@linkcode DataTransfer} are isolated to the current editor session.
68+
* This means that added metadata cannot be seen by other applications.
3569
*
3670
* @param document Document where the copy took place.
37-
* @param ranges Ranges being copied in the `document`.
38-
* @param dataTransfer The data transfer associated with the copy. You can store additional values on this for later use in {@link provideDocumentPasteEdits}.
71+
* @param ranges Ranges being copied in {@linkcode document}.
72+
* @param dataTransfer The data transfer associated with the copy. You can store additional values on this for later use in {@linkcode provideDocumentPasteEdits}.
73+
* This object is only valid for the duration of this method.
3974
* @param token A cancellation token.
75+
*
76+
* @return Optional thenable that resolves when all changes to the `dataTransfer` are complete.
4077
*/
4178
prepareDocumentPaste?(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): void | Thenable<void>;
4279

4380
/**
4481
* Invoked before the user pastes into a document.
4582
*
46-
* In this method, extensions can return a workspace edit that replaces the standard pasting behavior.
83+
* Returned edits can replace the standard pasting behavior.
4784
*
4885
* @param document Document being pasted into
49-
* @param ranges Currently selected ranges in the document.
50-
* @param dataTransfer The data transfer associated with the paste.
86+
* @param ranges Range in the {@linkcode document} to paste into.
87+
* @param dataTransfer The {@link DataTransfer data transfer} associated with the paste. This object is only valid for the duration of the paste operation.
88+
* @param context Additional context for the paste.
5189
* @param token A cancellation token.
5290
*
53-
* @return Optional workspace edit that applies the paste. Return undefined to use standard pasting.
91+
* @return Set of potential {@link DocumentPasteEdit edits} that apply the paste. Return `undefined` to use standard pasting.
5492
*/
55-
provideDocumentPasteEdits?(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): ProviderResult<DocumentPasteEdit>;
93+
provideDocumentPasteEdits?(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, context: DocumentPasteEditContext,
94+
token: CancellationToken): ProviderResult<T[]>;
95+
96+
/**
97+
* Optional method which fills in the {@linkcode DocumentPasteEdit.additionalEdit} before the edit is applied.
98+
*
99+
* This is called once per edit and should be used if generating the complete edit may take a long time.
100+
* Resolve can only be used to change {@link DocumentPasteEdit.additionalEdit}.
101+
*
102+
* @param pasteEdit The {@linkcode DocumentPasteEdit} to resolve.
103+
* @param token A cancellation token.
104+
*
105+
* @returns The resolved paste edit or a thenable that resolves to such. It is OK to return the given
106+
* `pasteEdit`. If no result is returned, the given `pasteEdit` is used.
107+
*/
108+
resolveDocumentPasteEdit?(pasteEdit: T, token: CancellationToken): ProviderResult<T>;
56109
}
57110

58111
/**
59-
* An operation applied on paste
112+
* An edit applied on paste.
60113
*/
61114
class DocumentPasteEdit {
115+
62116
/**
63117
* Human readable label that describes the edit.
64118
*/
65-
label: string;
119+
title: string;
66120

67121
/**
68-
* Controls the ordering or multiple paste edits. If this provider yield to edits, it will be shown lower in the list.
122+
* {@link DocumentPasteEditKind Kind} of the edit.
123+
*
124+
* Used to identify specific types of edits.
69125
*/
70-
yieldTo?: ReadonlyArray<
71-
| { readonly extensionId: string; readonly providerId: string }
72-
| { readonly mimeType: string }
73-
>;
126+
kind: DocumentPasteEditKind;
74127

75128
/**
76129
* The text or snippet to insert at the pasted locations.
@@ -83,43 +136,77 @@ export module '@theia/plugin' {
83136
additionalEdit?: WorkspaceEdit;
84137

85138
/**
86-
* @param insertText The text or snippet to insert at the pasted locations.
139+
* Controls the ordering of paste edits provided by multiple providers.
87140
*
88-
* TODO: Reverse args, but this will break existing consumers :(
141+
* If this edit yields to another, it will be shown lower in the list of paste edit.
89142
*/
90-
constructor(insertText: string | SnippetString, id: string, label: string);
143+
yieldTo?: readonly DocumentPasteEditKind[];
144+
145+
/**
146+
* Create a new paste edit.
147+
*
148+
* @param insertText The text or snippet to insert at the pasted locations.
149+
* @param title Human readable label that describes the edit.
150+
* @param kind {@link DocumentPasteEditKind Kind} of the edit.
151+
*/
152+
constructor(insertText: string | SnippetString, title: string, kind: DocumentPasteEditKind);
153+
}
154+
155+
/**
156+
* TODO: Share with code action kind?
157+
*/
158+
class DocumentPasteEditKind {
159+
static readonly Empty: DocumentPasteEditKind;
160+
161+
// TODO: Add `Text` any others?
162+
163+
private constructor(value: string);
164+
165+
readonly value: string;
166+
167+
append(...parts: string[]): CodeActionKind;
168+
intersects(other: CodeActionKind): boolean;
169+
contains(other: CodeActionKind): boolean;
91170
}
92171

93172
interface DocumentPasteProviderMetadata {
94173
/**
95-
* Identifies the provider.
96-
*
97-
* This id is used when users configure the default provider for paste.
174+
* List of {@link DocumentPasteEditKind kinds} that the provider may return in {@linkcode DocumentPasteEditProvider.provideDocumentPasteEdits provideDocumentPasteEdits}.
98175
*
99-
* This id should be unique within the extension but does not need to be unique across extensions.
176+
* The provider will only be invoked when one of these kinds is being requested. For normal pasting, all providers will be invoked.
100177
*/
101-
readonly id: string;
178+
readonly providedPasteEditKinds: readonly DocumentPasteEditKind[];
102179

103180
/**
104-
* Mime types that {@link DocumentPasteEditProvider.prepareDocumentPaste provideDocumentPasteEdits} may add on copy.
181+
* Mime types that {@linkcode DocumentPasteEditProvider.prepareDocumentPaste prepareDocumentPaste} may add on copy.
105182
*/
106183
readonly copyMimeTypes?: readonly string[];
107184

108185
/**
109-
* Mime types that {@link DocumentPasteEditProvider.provideDocumentPasteEdits provideDocumentPasteEdits} should be invoked for.
186+
* Mime types that {@linkcode DocumentPasteEditProvider.provideDocumentPasteEdits provideDocumentPasteEdits} should be invoked for.
110187
*
111188
* This can either be an exact mime type such as `image/png`, or a wildcard pattern such as `image/*`.
112189
*
113190
* Use `text/uri-list` for resources dropped from the explorer or other tree views in the workbench.
114191
*
115-
* Use `files` to indicate that the provider should be invoked if any {@link DataTransferFile files} are present in the {@link DataTransfer}.
116-
* Note that {@link DataTransferFile} entries are only created when dropping content from outside the editor, such as
192+
* Use `files` to indicate that the provider should be invoked if any {@link DataTransferFile files} are present in the {@linkcode DataTransfer}.
193+
* Note that {@linkcode DataTransferFile} entries are only created when dropping content from outside the editor, such as
117194
* from the operating system.
118195
*/
119196
readonly pasteMimeTypes?: readonly string[];
120197
}
121198

122199
namespace languages {
200+
/**
201+
* Registers a new {@linkcode DocumentPasteEditProvider}.
202+
*
203+
* @param selector A selector that defines the documents this provider applies to.
204+
* @param provider A paste editor provider.
205+
* @param metadata Additional metadata about the provider.
206+
*
207+
* @returns A {@link Disposable} that unregisters this provider when disposed of.
208+
* @stubbed
209+
*/
123210
export function registerDocumentPasteEditProvider(selector: DocumentSelector, provider: DocumentPasteEditProvider, metadata: DocumentPasteProviderMetadata): Disposable;
124211
}
125212
}

packages/plugin/src/theia.proposed.dropMetadata.d.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ export module '@theia/plugin' {
2929
/**
3030
* Human readable label that describes the edit.
3131
*/
32-
label?: string;
32+
title?: string;
33+
34+
/**
35+
* {@link DocumentPasteEditKind Kind} of the edit.
36+
*
37+
* Used to identify specific types of edits.
38+
*
39+
* TODO: use own type?
40+
*/
41+
kind: DocumentPasteEditKind;
3342

3443
/**
3544
* The mime type from the {@link DataTransfer} that this edit applies.
@@ -39,21 +48,11 @@ export module '@theia/plugin' {
3948
/**
4049
* Controls the ordering or multiple paste edits. If this provider yield to edits, it will be shown lower in the list.
4150
*/
42-
yieldTo?: ReadonlyArray<
43-
| { readonly extensionId: string; readonly providerId: string }
44-
| { readonly mimeType: string }
45-
>;
51+
yieldTo?: ReadonlyArray<DocumentPasteEditKind>;
4652
}
4753

4854
export interface DocumentDropEditProviderMetadata {
49-
/**
50-
* Identifies the provider.
51-
*
52-
* This id is used when users configure the default provider for drop.
53-
*
54-
* This id should be unique within the extension but does not need to be unique across extensions.
55-
*/
56-
readonly id: string;
55+
readonly providedDropEditKinds?: readonly DocumentPasteEditKind[];
5756

5857
/**
5958
* List of data transfer types that the provider supports.

0 commit comments

Comments
 (0)