Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/dropOrPasteInto/browser/configurationSchema.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 { Emitter, Event } from '../../../../base/common/event.js';
7
import { HierarchicalKind } from '../../../../base/common/hierarchicalKind.js';
8
import { IJSONSchema } from '../../../../base/common/jsonSchema.js';
9
import { Disposable } from '../../../../base/common/lifecycle.js';
10
import { editorConfigurationBaseNode } from '../../../../editor/common/config/editorConfigurationSchema.js';
11
import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.js';
12
import { pasteAsCommandId } from '../../../../editor/contrib/dropOrPasteInto/browser/copyPasteContribution.js';
13
import { pasteAsPreferenceConfig } from '../../../../editor/contrib/dropOrPasteInto/browser/copyPasteController.js';
14
import { dropAsPreferenceConfig } from '../../../../editor/contrib/dropOrPasteInto/browser/dropIntoEditorController.js';
15
import * as nls from '../../../../nls.js';
16
import { ConfigurationScope, Extensions, IConfigurationNode, IConfigurationPropertySchema, IConfigurationRegistry } from '../../../../platform/configuration/common/configurationRegistry.js';
17
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
18
import { Registry } from '../../../../platform/registry/common/platform.js';
19
import { IWorkbenchContribution } from '../../../common/contributions.js';
20
21
const dropEnumValues: string[] = [];
22
23
const dropAsPreferenceSchema: IConfigurationPropertySchema = {
24
type: 'array',
25
scope: ConfigurationScope.LANGUAGE_OVERRIDABLE,
26
description: nls.localize('dropPreferredDescription', "Configures the preferred type of edit to use when dropping content.\n\nThis is an ordered list of edit kinds. The first available edit of a preferred kind will be used."),
27
default: [],
28
items: {
29
description: nls.localize('dropKind', "The kind identifier of the drop edit."),
30
anyOf: [
31
{ type: 'string' },
32
{ enum: dropEnumValues }
33
],
34
}
35
};
36
37
const pasteEnumValues: string[] = [];
38
39
const pasteAsPreferenceSchema: IConfigurationPropertySchema = {
40
type: 'array',
41
scope: ConfigurationScope.LANGUAGE_OVERRIDABLE,
42
description: nls.localize('pastePreferredDescription', "Configures the preferred type of edit to use when pasting content.\n\nThis is an ordered list of edit kinds. The first available edit of a preferred kind will be used."),
43
default: [],
44
items: {
45
description: nls.localize('pasteKind', "The kind identifier of the paste edit."),
46
anyOf: [
47
{ type: 'string' },
48
{ enum: pasteEnumValues }
49
]
50
}
51
};
52
53
export const editorConfiguration = Object.freeze<IConfigurationNode>({
54
...editorConfigurationBaseNode,
55
properties: {
56
[pasteAsPreferenceConfig]: pasteAsPreferenceSchema,
57
[dropAsPreferenceConfig]: dropAsPreferenceSchema,
58
}
59
});
60
61
export class DropOrPasteSchemaContribution extends Disposable implements IWorkbenchContribution {
62
63
public static ID = 'workbench.contrib.dropOrPasteIntoSchema';
64
65
private readonly _onDidChangeSchemaContributions = this._register(new Emitter<void>());
66
67
private _allProvidedDropKinds: HierarchicalKind[] = [];
68
private _allProvidedPasteKinds: HierarchicalKind[] = [];
69
70
constructor(
71
@IKeybindingService keybindingService: IKeybindingService,
72
@ILanguageFeaturesService private readonly languageFeatures: ILanguageFeaturesService
73
) {
74
super();
75
76
this._register(
77
Event.runAndSubscribe(
78
Event.debounce(
79
Event.any(languageFeatures.documentPasteEditProvider.onDidChange, languageFeatures.documentPasteEditProvider.onDidChange),
80
() => { },
81
1000,
82
), () => {
83
this.updateProvidedKinds();
84
this.updateConfigurationSchema();
85
86
this._onDidChangeSchemaContributions.fire();
87
}));
88
89
this._register(keybindingService.registerSchemaContribution({
90
getSchemaAdditions: () => this.getKeybindingSchemaAdditions(),
91
onDidChange: this._onDidChangeSchemaContributions.event,
92
}));
93
}
94
95
private updateProvidedKinds(): void {
96
// Drop
97
const dropKinds = new Map<string, HierarchicalKind>();
98
for (const provider of this.languageFeatures.documentDropEditProvider.allNoModel()) {
99
for (const kind of provider.providedDropEditKinds ?? []) {
100
dropKinds.set(kind.value, kind);
101
}
102
}
103
this._allProvidedDropKinds = Array.from(dropKinds.values());
104
105
// Paste
106
const pasteKinds = new Map<string, HierarchicalKind>();
107
for (const provider of this.languageFeatures.documentPasteEditProvider.allNoModel()) {
108
for (const kind of provider.providedPasteEditKinds ?? []) {
109
pasteKinds.set(kind.value, kind);
110
}
111
}
112
this._allProvidedPasteKinds = Array.from(pasteKinds.values());
113
}
114
115
private updateConfigurationSchema(): void {
116
pasteEnumValues.length = 0;
117
for (const codeActionKind of this._allProvidedPasteKinds) {
118
pasteEnumValues.push(codeActionKind.value);
119
}
120
121
dropEnumValues.length = 0;
122
for (const codeActionKind of this._allProvidedDropKinds) {
123
dropEnumValues.push(codeActionKind.value);
124
}
125
126
Registry.as<IConfigurationRegistry>(Extensions.Configuration)
127
.notifyConfigurationSchemaUpdated(editorConfiguration);
128
}
129
130
private getKeybindingSchemaAdditions(): IJSONSchema[] {
131
return [
132
{
133
if: {
134
required: ['command'],
135
properties: {
136
'command': { const: pasteAsCommandId }
137
}
138
},
139
then: {
140
properties: {
141
'args': {
142
oneOf: [
143
{
144
required: ['kind'],
145
properties: {
146
'kind': {
147
anyOf: [
148
{ enum: Array.from(this._allProvidedPasteKinds.map(x => x.value)) },
149
{ type: 'string' },
150
]
151
}
152
}
153
},
154
{
155
required: ['preferences'],
156
properties: {
157
'preferences': {
158
type: 'array',
159
items: {
160
anyOf: [
161
{ enum: Array.from(this._allProvidedPasteKinds.map(x => x.value)) },
162
{ type: 'string' },
163
]
164
}
165
}
166
}
167
}
168
]
169
}
170
}
171
}
172
},
173
];
174
}
175
}
176
177