55
66import { coalesce , distinct } from 'vs/base/common/arrays' ;
77import * as glob from 'vs/base/common/glob' ;
8- import { UnownedDisposable } from 'vs/base/common/lifecycle' ;
8+ import { UnownedDisposable , Disposable } from 'vs/base/common/lifecycle' ;
99import { Schemas } from 'vs/base/common/network' ;
1010import { basename , DataUri , isEqual } from 'vs/base/common/resources' ;
1111import { withNullAsUndefined } from 'vs/base/common/types' ;
@@ -22,12 +22,13 @@ import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
2222import { EditorInput , EditorOptions , IEditor , IEditorInput } from 'vs/workbench/common/editor' ;
2323import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput' ;
2424import { webviewEditorsExtensionPoint } from 'vs/workbench/contrib/customEditor/browser/extensionPoint' ;
25- import { CustomEditorPriority , CustomEditorInfo , CustomEditorSelector , ICustomEditorService } from 'vs/workbench/contrib/customEditor/common/customEditor' ;
25+ import { CustomEditorPriority , CustomEditorInfo , CustomEditorSelector , ICustomEditorService , CONTEXT_HAS_CUSTOM_EDITORS } from 'vs/workbench/contrib/customEditor/common/customEditor' ;
2626import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput' ;
2727import { IWebviewService } from 'vs/workbench/contrib/webview/browser/webview' ;
2828import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService' ;
2929import { IEditorService , IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService' ;
3030import { CustomFileEditorInput } from './customEditorInput' ;
31+ import { IContextKeyService , IContextKey } from 'vs/platform/contextkey/common/contextkey' ;
3132
3233const defaultEditorId = 'default' ;
3334
@@ -67,18 +68,22 @@ export class CustomEditorStore {
6768 }
6869}
6970
70- export class CustomEditorService implements ICustomEditorService {
71+ export class CustomEditorService extends Disposable implements ICustomEditorService {
7172 _serviceBrand : any ;
7273
7374 private readonly editors = new CustomEditorStore ( ) ;
75+ private readonly _hasCustomEditor : IContextKey < boolean > ;
7476
7577 constructor (
78+ @IContextKeyService contextKeyService : IContextKeyService ,
7679 @IConfigurationService private readonly configurationService : IConfigurationService ,
7780 @IEditorService private readonly editorService : IEditorService ,
7881 @IInstantiationService private readonly instantiationService : IInstantiationService ,
7982 @IQuickInputService private readonly quickInputService : IQuickInputService ,
8083 @IWebviewService private readonly webviewService : IWebviewService ,
8184 ) {
85+ super ( ) ;
86+
8287 webviewEditorsExtensionPoint . setHandler ( extensions => {
8388 this . editors . clear ( ) ;
8489
@@ -92,7 +97,13 @@ export class CustomEditorService implements ICustomEditorService {
9297 } ) ;
9398 }
9499 }
100+ this . updateContext ( ) ;
95101 } ) ;
102+
103+ this . _hasCustomEditor = CONTEXT_HAS_CUSTOM_EDITORS . bindTo ( contextKeyService ) ;
104+
105+ this . _register ( this . editorService . onDidActiveEditorChange ( ( ) => this . updateContext ( ) ) ) ;
106+ this . updateContext ( ) ;
96107 }
97108
98109 public getContributedCustomEditors ( resource : URI ) : readonly CustomEditorInfo [ ] {
@@ -154,7 +165,7 @@ export class CustomEditorService implements ICustomEditorService {
154165 resource : URI ,
155166 viewType : string ,
156167 group : IEditorGroup | undefined ,
157- options ?: { readonly customClasses : string } ,
168+ options ?: { readonly customClasses : string ; } ,
158169 ) : CustomFileEditorInput {
159170 const id = generateUuid ( ) ;
160171 const webview = this . webviewService . createWebviewEditorOverlay ( id , { customClasses : options ? options . customClasses : undefined } , { } ) ;
@@ -190,11 +201,29 @@ export class CustomEditorService implements ICustomEditorService {
190201 }
191202 return this . editorService . openEditor ( input , options , group ) ;
192203 }
204+
205+ private updateContext ( ) {
206+ const activeControl = this . editorService . activeControl ;
207+ if ( ! activeControl ) {
208+ this . _hasCustomEditor . reset ( ) ;
209+ return ;
210+ }
211+ const resource = activeControl . input . getResource ( ) ;
212+ if ( ! resource ) {
213+ this . _hasCustomEditor . reset ( ) ;
214+ return ;
215+ }
216+ const possibleEditors = [
217+ ...this . getContributedCustomEditors ( resource ) ,
218+ ...this . getUserConfiguredCustomEditors ( resource ) ,
219+ ] ;
220+ this . _hasCustomEditor . set ( possibleEditors . length > 0 ) ;
221+ }
193222}
194223
195224export const customEditorsAssociationsKey = 'workbench.experimental.editorAssociations' ;
196225
197- export type CustomEditorsAssociations = readonly ( CustomEditorSelector & { readonly viewType : string } ) [ ] ;
226+ export type CustomEditorsAssociations = readonly ( CustomEditorSelector & { readonly viewType : string ; } ) [ ] ;
198227
199228export class CustomEditorContribution implements IWorkbenchContribution {
200229 constructor (
0 commit comments