Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/dropOrPasteInto/browser/copyPasteContribution.ts
3296 views
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 { HierarchicalKind } from '../../../../base/common/hierarchicalKind.js';
7
import { IJSONSchema, SchemaToType } from '../../../../base/common/jsonSchema.js';
8
import { KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';
9
import * as nls from '../../../../nls.js';
10
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
11
import { ICodeEditor } from '../../../browser/editorBrowser.js';
12
import { EditorAction, EditorCommand, EditorContributionInstantiation, ServicesAccessor, registerEditorAction, registerEditorCommand, registerEditorContribution } from '../../../browser/editorExtensions.js';
13
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
14
import { registerEditorFeature } from '../../../common/editorFeatures.js';
15
import { CopyPasteController, PastePreference, changePasteTypeCommandId, pasteWidgetVisibleCtx } from './copyPasteController.js';
16
import { DefaultPasteProvidersFeature, DefaultTextPasteOrDropEditProvider } from './defaultProviders.js';
17
18
export const pasteAsCommandId = 'editor.action.pasteAs';
19
20
registerEditorContribution(CopyPasteController.ID, CopyPasteController, EditorContributionInstantiation.Eager); // eager because it listens to events on the container dom node of the editor
21
registerEditorFeature(DefaultPasteProvidersFeature);
22
23
registerEditorCommand(new class extends EditorCommand {
24
constructor() {
25
super({
26
id: changePasteTypeCommandId,
27
precondition: pasteWidgetVisibleCtx,
28
kbOpts: {
29
weight: KeybindingWeight.EditorContrib,
30
primary: KeyMod.CtrlCmd | KeyCode.Period,
31
}
32
});
33
}
34
35
public override runEditorCommand(_accessor: ServicesAccessor, editor: ICodeEditor) {
36
return CopyPasteController.get(editor)?.changePasteType();
37
}
38
});
39
40
registerEditorCommand(new class extends EditorCommand {
41
constructor() {
42
super({
43
id: 'editor.hidePasteWidget',
44
precondition: pasteWidgetVisibleCtx,
45
kbOpts: {
46
weight: KeybindingWeight.EditorContrib,
47
primary: KeyCode.Escape,
48
}
49
});
50
}
51
52
public override runEditorCommand(_accessor: ServicesAccessor, editor: ICodeEditor) {
53
CopyPasteController.get(editor)?.clearWidgets();
54
}
55
});
56
57
registerEditorAction(class PasteAsAction extends EditorAction {
58
private static readonly argsSchema = {
59
oneOf: [
60
{
61
type: 'object',
62
required: ['kind'],
63
properties: {
64
kind: {
65
type: 'string',
66
description: nls.localize('pasteAs.kind', "The kind of the paste edit to try pasting with.\nIf there are multiple edits for this kind, the editor will show a picker. If there are no edits of this kind, the editor will show an error message."),
67
}
68
},
69
},
70
{
71
type: 'object',
72
required: ['preferences'],
73
properties: {
74
preferences: {
75
type: 'array',
76
description: nls.localize('pasteAs.preferences', "List of preferred paste edit kind to try applying.\nThe first edit matching the preferences will be applied."),
77
items: { type: 'string' }
78
}
79
},
80
}
81
]
82
} as const satisfies IJSONSchema;
83
84
constructor() {
85
super({
86
id: pasteAsCommandId,
87
label: nls.localize2('pasteAs', "Paste As..."),
88
precondition: EditorContextKeys.writable,
89
metadata: {
90
description: 'Paste as',
91
args: [{
92
name: 'args',
93
schema: PasteAsAction.argsSchema
94
}]
95
}
96
});
97
}
98
99
public override run(_accessor: ServicesAccessor, editor: ICodeEditor, args?: SchemaToType<typeof PasteAsAction.argsSchema>) {
100
let preference: PastePreference | undefined;
101
if (args) {
102
if ('kind' in args) {
103
preference = { only: new HierarchicalKind(args.kind) };
104
} else if ('preferences' in args) {
105
preference = { preferences: args.preferences.map(kind => new HierarchicalKind(kind)) };
106
}
107
}
108
return CopyPasteController.get(editor)?.pasteAs(preference);
109
}
110
});
111
112
registerEditorAction(class extends EditorAction {
113
constructor() {
114
super({
115
id: 'editor.action.pasteAsText',
116
label: nls.localize2('pasteAsText', "Paste as Text"),
117
precondition: EditorContextKeys.writable,
118
});
119
}
120
121
public override run(_accessor: ServicesAccessor, editor: ICodeEditor) {
122
return CopyPasteController.get(editor)?.pasteAs({ providerId: DefaultTextPasteOrDropEditProvider.id });
123
}
124
});
125
126
export type PreferredPasteConfiguration = string;
127
128