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
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 { coalesce } from '../../../../base/common/arrays.js';
7
import { 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, CustomEditorSelector } 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
export interface ICustomEditorsExtensionPoint {
26
readonly [Fields.viewType]: string;
27
readonly [Fields.displayName]: string;
28
readonly [Fields.selector]?: readonly CustomEditorSelector[];
29
readonly [Fields.priority]?: string;
30
}
31
32
const CustomEditorsContribution: IJSONSchema = {
33
description: nls.localize('contributes.customEditors', 'Contributed custom editors.'),
34
type: 'array',
35
defaultSnippets: [{
36
body: [{
37
[Fields.viewType]: '$1',
38
[Fields.displayName]: '$2',
39
[Fields.selector]: [{
40
filenamePattern: '$3'
41
}],
42
}]
43
}],
44
items: {
45
type: 'object',
46
required: [
47
Fields.viewType,
48
Fields.displayName,
49
Fields.selector,
50
],
51
properties: {
52
[Fields.viewType]: {
53
type: 'string',
54
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).'),
55
},
56
[Fields.displayName]: {
57
type: 'string',
58
description: nls.localize('contributes.displayName', 'Human readable name of the custom editor. This is displayed to users when selecting which editor to use.'),
59
},
60
[Fields.selector]: {
61
type: 'array',
62
description: nls.localize('contributes.selector', 'Set of globs that the custom editor is enabled for.'),
63
items: {
64
type: 'object',
65
defaultSnippets: [{
66
body: {
67
filenamePattern: '$1',
68
}
69
}],
70
properties: {
71
filenamePattern: {
72
type: 'string',
73
description: nls.localize('contributes.selector.filenamePattern', 'Glob that the custom editor is enabled for.'),
74
},
75
}
76
}
77
},
78
[Fields.priority]: {
79
type: 'string',
80
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.'),
81
enum: [
82
CustomEditorPriority.default,
83
CustomEditorPriority.option,
84
],
85
markdownEnumDescriptions: [
86
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.'),
87
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.'),
88
],
89
default: 'default'
90
}
91
}
92
}
93
};
94
95
export const customEditorsExtensionPoint = ExtensionsRegistry.registerExtensionPoint<ICustomEditorsExtensionPoint[]>({
96
extensionPoint: 'customEditors',
97
deps: [languagesExtPoint],
98
jsonSchema: CustomEditorsContribution,
99
activationEventsGenerator: (contribs: ICustomEditorsExtensionPoint[], result: { push(item: string): void }) => {
100
for (const contrib of contribs) {
101
const viewType = contrib[Fields.viewType];
102
if (viewType) {
103
result.push(`onCustomEditor:${viewType}`);
104
}
105
}
106
},
107
});
108
109
class CustomEditorsDataRenderer extends Disposable implements IExtensionFeatureTableRenderer {
110
111
readonly type = 'table';
112
113
shouldRender(manifest: IExtensionManifest): boolean {
114
return !!manifest.contributes?.customEditors;
115
}
116
117
render(manifest: IExtensionManifest): IRenderedData<ITableData> {
118
const customEditors = manifest.contributes?.customEditors || [];
119
if (!customEditors.length) {
120
return { data: { headers: [], rows: [] }, dispose: () => { } };
121
}
122
123
const headers = [
124
nls.localize('customEditors view type', "View Type"),
125
nls.localize('customEditors priority', "Priority"),
126
nls.localize('customEditors filenamePattern', "Filename Pattern"),
127
];
128
129
const rows: IRowData[][] = customEditors
130
.map(customEditor => {
131
return [
132
customEditor.viewType,
133
customEditor.priority ?? '',
134
coalesce(customEditor.selector.map(x => x.filenamePattern)).join(', ')
135
];
136
});
137
138
return {
139
data: {
140
headers,
141
rows
142
},
143
dispose: () => { }
144
};
145
}
146
}
147
148
Registry.as<IExtensionFeaturesRegistry>(Extensions.ExtensionFeaturesRegistry).registerExtensionFeature({
149
id: 'customEditors',
150
label: nls.localize('customEditors', "Custom Editors"),
151
access: {
152
canToggle: false
153
},
154
renderer: new SyncDescriptor(CustomEditorsDataRenderer),
155
});
156
157