Skip to content

Commit d2911af

Browse files
committed
Move handler into CustomEditorInfoStore
1 parent 7879818 commit d2911af

1 file changed

Lines changed: 37 additions & 31 deletions

File tree

src/vs/workbench/contrib/customEditor/browser/customEditors.ts

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor
3030
import { IEditorService, IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService';
3131
import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService';
3232
import { CustomFileEditorInput } from './customEditorInput';
33+
import { Emitter } from 'vs/base/common/event';
3334
export const defaultEditorId = 'default';
3435

3536
const defaultEditorInfo = new CustomEditorInfo({
@@ -41,37 +42,57 @@ const defaultEditorInfo = new CustomEditorInfo({
4142
priority: CustomEditorPriority.default,
4243
});
4344

44-
export class CustomEditorInfoStore {
45-
private readonly contributedEditors = new Map<string, CustomEditorInfo>();
45+
export class CustomEditorInfoStore extends Disposable {
4646

47-
public clear() {
48-
this.contributedEditors.clear();
47+
private readonly _contributedEditors = new Map<string, CustomEditorInfo>();
48+
49+
constructor() {
50+
super();
51+
52+
webviewEditorsExtensionPoint.setHandler(extensions => {
53+
this._contributedEditors.clear();
54+
55+
for (const extension of extensions) {
56+
for (const webviewEditorContribution of extension.value) {
57+
this.add(new CustomEditorInfo({
58+
id: webviewEditorContribution.viewType,
59+
displayName: webviewEditorContribution.displayName,
60+
selector: webviewEditorContribution.selector || [],
61+
priority: webviewEditorContribution.priority || CustomEditorPriority.default,
62+
}));
63+
}
64+
}
65+
this._onChange.fire();
66+
});
4967
}
5068

69+
private readonly _onChange = this._register(new Emitter<void>());
70+
public readonly onChange = this._onChange.event;
71+
5172
public get(viewType: string): CustomEditorInfo | undefined {
5273
return viewType === defaultEditorId
5374
? defaultEditorInfo
54-
: this.contributedEditors.get(viewType);
75+
: this._contributedEditors.get(viewType);
5576
}
5677

57-
public add(info: CustomEditorInfo): void {
58-
if (info.id === defaultEditorId || this.contributedEditors.has(info.id)) {
59-
console.log(`Custom editor with id '${info.id}' already registered`);
60-
return;
61-
}
62-
this.contributedEditors.set(info.id, info);
78+
public getContributedEditors(resource: URI): readonly CustomEditorInfo[] {
79+
return Array.from(this._contributedEditors.values())
80+
.filter(customEditor => customEditor.matches(resource));
6381
}
6482

65-
public getContributedEditors(resource: URI): readonly CustomEditorInfo[] {
66-
return Array.from(this.contributedEditors.values()).filter(customEditor =>
67-
customEditor.matches(resource));
83+
private add(info: CustomEditorInfo): void {
84+
if (info.id === defaultEditorId || this._contributedEditors.has(info.id)) {
85+
console.error(`Custom editor with id '${info.id}' already registered`);
86+
return;
87+
}
88+
this._contributedEditors.set(info.id, info);
6889
}
6990
}
7091

7192
export class CustomEditorService extends Disposable implements ICustomEditorService {
7293
_serviceBrand: any;
7394

74-
private readonly _editorInfoStore = new CustomEditorInfoStore();
95+
private readonly _editorInfoStore = this._register(new CustomEditorInfoStore());
7596

7697
private readonly _models: CustomEditorModelManager;
7798

@@ -94,26 +115,11 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ
94115

95116
this._models = new CustomEditorModelManager(workingCopyService);
96117

97-
webviewEditorsExtensionPoint.setHandler(extensions => {
98-
this._editorInfoStore.clear();
99-
100-
for (const extension of extensions) {
101-
for (const webviewEditorContribution of extension.value) {
102-
this._editorInfoStore.add(new CustomEditorInfo({
103-
id: webviewEditorContribution.viewType,
104-
displayName: webviewEditorContribution.displayName,
105-
selector: webviewEditorContribution.selector || [],
106-
priority: webviewEditorContribution.priority || CustomEditorPriority.default,
107-
}));
108-
}
109-
}
110-
this.updateContexts();
111-
});
112-
113118
this._hasCustomEditor = CONTEXT_HAS_CUSTOM_EDITORS.bindTo(contextKeyService);
114119
this._focusedCustomEditorIsEditable = CONTEXT_FOCUSED_CUSTOM_EDITOR_IS_EDITABLE.bindTo(contextKeyService);
115120
this._webviewHasOwnEditFunctions = webviewHasOwnEditFunctionsContext.bindTo(contextKeyService);
116121

122+
this._register(this._editorInfoStore.onChange(() => this.updateContexts()));
117123
this._register(this.editorService.onDidActiveEditorChange(() => this.updateContexts()));
118124

119125
this._register(fileService.onAfterOperation(e => {

0 commit comments

Comments
 (0)