@@ -31,6 +31,10 @@ import { CellKind, CellUri } from 'vs/workbench/contrib/notebook/common/notebook
3131import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider' ;
3232import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService' ;
3333import { 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 ] ) ;
0 commit comments