Skip to content

Commit b64d298

Browse files
committed
notebook respect editor association
1 parent 7f5bada commit b64d298

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import { CellKind, CellUri } from 'vs/workbench/contrib/notebook/common/notebook
3131
import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider';
3232
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
3333
import { IEditorService, IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService';
34+
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
35+
import { CustomEditorsAssociations, customEditorsAssociationsSettingId } from 'vs/workbench/services/editor/common/editorAssociationsSetting';
36+
import { coalesce, distinct } from 'vs/base/common/arrays';
37+
import { CustomEditorInfo } from 'vs/workbench/contrib/customEditor/common/customEditor';
3438

3539
// Editor Contribution
3640

@@ -96,7 +100,8 @@ export class NotebookContribution implements IWorkbenchContribution {
96100
constructor(
97101
@IEditorService private readonly editorService: IEditorService,
98102
@INotebookService private readonly notebookService: INotebookService,
99-
@IInstantiationService private readonly instantiationService: IInstantiationService
103+
@IInstantiationService private readonly instantiationService: IInstantiationService,
104+
@IConfigurationService private readonly configurationService: IConfigurationService
100105

101106
) {
102107
this.editorService.overrideOpenEditor({
@@ -106,9 +111,12 @@ export class NotebookContribution implements IWorkbenchContribution {
106111
return [];
107112
}
108113

109-
const infos = notebookService.getContributedNotebookProviders(resource);
114+
const associatedEditors = distinct([
115+
...this.getUserAssociatedNotebookEditors(resource),
116+
...this.getContributedEditors(resource)
117+
], editor => editor.id);
110118

111-
return infos.map(info => {
119+
return associatedEditors.map(info => {
112120
return {
113121
label: info.displayName,
114122
id: info.id,
@@ -128,12 +136,41 @@ export class NotebookContribution implements IWorkbenchContribution {
128136
});
129137
}
130138

139+
getUserAssociatedEditors(resource: URI) {
140+
const rawAssociations = this.configurationService.getValue<CustomEditorsAssociations>(customEditorsAssociationsSettingId) || [];
141+
142+
return coalesce(rawAssociations
143+
.filter(association => CustomEditorInfo.selectorMatches(association, resource)));
144+
}
145+
146+
getUserAssociatedNotebookEditors(resource: URI) {
147+
const rawAssociations = this.configurationService.getValue<CustomEditorsAssociations>(customEditorsAssociationsSettingId) || [];
148+
149+
return coalesce(rawAssociations
150+
.filter(association => CustomEditorInfo.selectorMatches(association, resource))
151+
.map(association => this.notebookService.getContributedNotebookProvider(association.viewType)));
152+
}
153+
154+
getContributedEditors(resource: URI) {
155+
return this.notebookService.getContributedNotebookProviders(resource);
156+
}
157+
131158
private onEditorOpening(originalInput: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup, id: string | undefined): IOpenEditorOverride | undefined {
132159
let resource = originalInput.resource;
133160
if (!resource) {
134161
return undefined;
135162
}
136163

164+
if (id === undefined) {
165+
const userAssociatedEditors = this.getUserAssociatedEditors(resource);
166+
const notebookEditor = userAssociatedEditors.filter(association => this.notebookService.getContributedNotebookProvider(association.viewType));
167+
168+
if (userAssociatedEditors.length && !notebookEditor.length) {
169+
// user pick a non-notebook editor for this resource
170+
return undefined;
171+
}
172+
}
173+
137174
if (this._resourceMapping.has(resource)) {
138175
const input = this._resourceMapping.get(resource);
139176

@@ -145,7 +182,7 @@ export class NotebookContribution implements IWorkbenchContribution {
145182
let info: NotebookProviderInfo | undefined;
146183
const data = CellUri.parse(resource);
147184
if (data) {
148-
const infos = this.notebookService.getContributedNotebookProviders(data.notebook);
185+
const infos = this.getContributedEditors(data.notebook);
149186

150187
if (infos.length) {
151188
const info = id === undefined ? infos[0] : (infos.find(info => info.id === id) || infos[0]);

src/vs/workbench/contrib/notebook/browser/notebookService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface INotebookService {
4949
executeNotebookCell(viewType: string, uri: URI, handle: number, token: CancellationToken): Promise<void>;
5050

5151
getContributedNotebookProviders(resource: URI): readonly NotebookProviderInfo[];
52+
getContributedNotebookProvider(viewType: string): NotebookProviderInfo | undefined;
5253
getNotebookProviderResourceRoots(): URI[];
5354
destoryNotebookDocument(viewType: string, notebook: INotebookTextModel): void;
5455
updateActiveNotebookDocument(viewType: string, resource: URI): void;
@@ -273,6 +274,10 @@ export class NotebookService extends Disposable implements INotebookService, ICu
273274
return this.notebookProviderInfoStore.getContributedNotebook(resource);
274275
}
275276

277+
getContributedNotebookProvider(viewType: string): NotebookProviderInfo | undefined {
278+
return this.notebookProviderInfoStore.get(viewType);
279+
}
280+
276281
getContributedNotebookOutputRenderers(mimeType: string): readonly NotebookOutputRendererInfo[] {
277282
return this.notebookRenderersInfoStore.getContributedRenderer(mimeType);
278283
}

0 commit comments

Comments
 (0)