Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/customEditor/common/extensionPoint.ts
5267 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 { coalesce } from '../../../../base/common/arrays.js';
7
import { TypeFromJsonSchema, IJSONSchema } from '../../../../base/common/jsonSchema.js';
8
import { Disposable } from '../../../../base/common/lifecycle.js';
9
import * as nls from '../../../../nls.js';
10
import { IExtensionManifest } from '../../../../platform/extensions/common/extensions.js';
11
import { SyncDescriptor } from '../../../../platform/instantiation/common/descriptors.js';
12
import { Registry } from '../../../../platform/registry/common/platform.js';
13
import { CustomEditorPriority } from './customEditor.js';
14
import { Extensions, IExtensionFeatureTableRenderer, IExtensionFeaturesRegistry, IRenderedData, IRowData, ITableData } from '../../../services/extensionManagement/common/extensionFeatures.js';
15
import { ExtensionsRegistry } from '../../../services/extensions/common/extensionsRegistry.js';
16
import { languagesExtPoint } from '../../../services/language/common/languageService.js';
17
18
const Fields = Object.freeze({
19
viewType: 'viewType',
20
displayName: 'displayName',
21
selector: 'selector',
22
priority: 'priority',
23
});
24
25
const customEditorsContributionSchema = {
26
type: 'object',
27
required: [
28
Fields.viewType,
29
Fields.displayName,
30
Fields.selector,
31
],
32
additionalProperties: false,
33
properties: {
34
[Fields.viewType]: {
35
type: 'string',
36
markdownDescription: nls.localize('contributes.viewType', 'Identifier for the custom editor. This must be unique across all custom editors, so we recommend including your extension id as part of `viewType`. The `viewType` is used when registering custom editors with `vscode.registerCustomEditorProvider` and in the `onCustomEditor:${id}` [activation event](https://code.visualstudio.com/api/references/activation-events).'),
37
},
38
[Fields.displayName]: {
39
type: 'string',
40
description: nls.localize('contributes.displayName', 'Human readable name of the custom editor. This is displayed to users when selecting which editor to use.'),
41
},
42
[Fields.selector]: {
43
type: 'array',
44
description: nls.localize('contributes.selector', 'Set of globs that the custom editor is enabled for.'),
45
items: {
46
type: 'object',
47
defaultSnippets: [{
48
body: {
49
filenamePattern: '$1',
50
}
51
}],
52
additionalProperties: false,
53
properties: {
54
filenamePattern: {
55
type: 'string',
56
description: nls.localize('contributes.selector.filenamePattern', 'Glob that the custom editor is enabled for.'),
57
},
58
}
59
}
60
},
61
[Fields.priority]: {
62
type: 'string',
63
markdownDeprecationMessage: nls.localize('contributes.priority', 'Controls if the custom editor is enabled automatically when the user opens a file. This may be overridden by users using the `workbench.editorAssociations` setting.'),
64
enum: [
65
CustomEditorPriority.default,
66
CustomEditorPriority.option,
67
],
68
markdownEnumDescriptions: [
69
nls.localize('contributes.priority.default', 'The editor is automatically used when the user opens a resource, provided that no other default custom editors are registered for that resource.'),
70
nls.localize('contributes.priority.option', 'The editor is not automatically used when the user opens a resource, but a user can switch to the editor using the `Reopen With` command.'),
71
],
72
default: CustomEditorPriority.default
73
}
74
}
75
} as const satisfies IJSONSchema;
76
77
export type ICustomEditorsExtensionPoint = TypeFromJsonSchema<typeof customEditorsContributionSchema>;
78
79
export const customEditorsExtensionPoint = ExtensionsRegistry.registerExtensionPoint<ICustomEditorsExtensionPoint[]>({
80
extensionPoint: 'customEditors',
81
deps: [languagesExtPoint],
82
jsonSchema: {
83
description: nls.localize('contributes.customEditors', 'Contributed custom editors.'),
84
type: 'array',
85
defaultSnippets: [{
86
body: [{
87
[Fields.viewType]: '$1',
88
[Fields.displayName]: '$2',
89
[Fields.selector]: [{
90
filenamePattern: '$3'
91
}],
92
}]
93
}],
94
items: customEditorsContributionSchema
95
},
96
activationEventsGenerator: function* (contribs: readonly ICustomEditorsExtensionPoint[]) {
97
for (const contrib of contribs) {
98
const viewType = contrib[Fields.viewType];
99
if (viewType) {
100
yield `onCustomEditor:${viewType}`;
101
}
102
}
103
},
104
});
105
106
class CustomEditorsDataRenderer extends Disposable implements IExtensionFeatureTableRenderer {
107
108
readonly type = 'table';
109
110
shouldRender(manifest: IExtensionManifest): boolean {
111
return !!manifest.contributes?.customEditors;
112
}
113
114
render(manifest: IExtensionManifest): IRenderedData<ITableData> {
115
const customEditors = manifest.contributes?.customEditors || [];
116
if (!customEditors.length) {
117
return { data: { headers: [], rows: [] }, dispose: () => { } };
118
}
119
120
const headers = [
121
nls.localize('customEditors view type', "View Type"),
122
nls.localize('customEditors priority', "Priority"),
123
nls.localize('customEditors filenamePattern', "Filename Pattern"),
124
];
125
126
const rows: IRowData[][] = customEditors
127
.map(customEditor => {
128
return [
129
customEditor.viewType,
130
customEditor.priority ?? '',
131
coalesce(customEditor.selector.map(x => x.filenamePattern)).join(', ')
132
];
133
});
134
135
return {
136
data: {
137
headers,
138
rows
139
},
140
dispose: () => { }
141
};
142
}
143
}
144
145
Registry.as<IExtensionFeaturesRegistry>(Extensions.ExtensionFeaturesRegistry).registerExtensionFeature({
146
id: 'customEditors',
147
label: nls.localize('customEditors', "Custom Editors"),
148
access: {
149
canToggle: false
150
},
151
renderer: new SyncDescriptor(CustomEditorsDataRenderer),
152
});
153
154