44'use strict' ;
55
66import { inject , injectable } from 'inversify' ;
7+ import { TextEditor } from 'vscode' ;
78import { IExtensionSingleActivationService } from '../../activation/types' ;
8- import { ICommandManager } from '../../common/application/types' ;
9+ import { ICommandManager , IDocumentManager } from '../../common/application/types' ;
10+ import { PYTHON_LANGUAGE } from '../../common/constants' ;
911import { ContextKey } from '../../common/contextKey' ;
1012import { IDisposable , IDisposableRegistry } from '../../common/types' ;
1113import { EditorContexts } from '../constants' ;
@@ -14,28 +16,55 @@ import { IInteractiveWindow, IInteractiveWindowProvider, INotebookEditor, INoteb
1416@injectable ( )
1517export class ActiveEditorContextService implements IExtensionSingleActivationService , IDisposable {
1618 private readonly disposables : IDisposable [ ] = [ ] ;
19+ private nativeContext : ContextKey ;
20+ private interactiveContext : ContextKey ;
21+ private interactiveOrNativeContext : ContextKey ;
22+ private pythonOrInteractiveContext : ContextKey ;
23+ private pythonOrNativeContext : ContextKey ;
24+ private pythonOrInteractiveOrNativeContext : ContextKey ;
25+ private isPythonFileActive : boolean = false ;
1726 constructor (
1827 @inject ( IInteractiveWindowProvider ) private readonly interactiveProvider : IInteractiveWindowProvider ,
1928 @inject ( INotebookEditorProvider ) private readonly notebookProvider : INotebookEditorProvider ,
29+ @inject ( IDocumentManager ) private readonly docManager : IDocumentManager ,
2030 @inject ( ICommandManager ) private readonly commandManager : ICommandManager ,
2131 @inject ( IDisposableRegistry ) disposables : IDisposableRegistry
2232 ) {
2333 disposables . push ( this ) ;
34+ this . nativeContext = new ContextKey ( EditorContexts . IsNativeActive , this . commandManager ) ;
35+ this . interactiveContext = new ContextKey ( EditorContexts . IsInteractiveActive , this . commandManager ) ;
36+ this . interactiveOrNativeContext = new ContextKey ( EditorContexts . IsInteractiveOrNativeActive , this . commandManager ) ;
37+ this . pythonOrNativeContext = new ContextKey ( EditorContexts . IsPythonOrNativeActive , this . commandManager ) ;
38+ this . pythonOrInteractiveContext = new ContextKey ( EditorContexts . IsPythonOrInteractiveActive , this . commandManager ) ;
39+ this . pythonOrInteractiveOrNativeContext = new ContextKey ( EditorContexts . IsPythonOrInteractiveOrNativeActive , this . commandManager ) ;
2440 }
2541 public dispose ( ) {
2642 this . disposables . forEach ( item => item . dispose ( ) ) ;
2743 }
2844 public async activate ( ) : Promise < void > {
45+ this . docManager . onDidChangeActiveTextEditor ( this . onDidChangeActiveTextEditor , this , this . disposables ) ;
2946 this . interactiveProvider . onDidChangeActiveInteractiveWindow ( this . onDidChangeActiveInteractiveWindow , this , this . disposables ) ;
3047 this . notebookProvider . onDidChangeActiveNotebookEditor ( this . onDidChangeActiveNotebookEditor , this , this . disposables ) ;
3148 }
3249
3350 private onDidChangeActiveInteractiveWindow ( e ?: IInteractiveWindow ) {
34- const interactiveContext = new ContextKey ( EditorContexts . IsInteractive , this . commandManager ) ;
35- interactiveContext . set ( ! ! e ) . ignoreErrors ( ) ;
51+ this . interactiveContext . set ( ! ! e ) . ignoreErrors ( ) ;
52+ this . updateMergedContexts ( ) ;
3653 }
3754 private onDidChangeActiveNotebookEditor ( e ?: INotebookEditor ) {
38- const interactiveContext = new ContextKey ( EditorContexts . IsNative , this . commandManager ) ;
39- interactiveContext . set ( ! ! e ) . ignoreErrors ( ) ;
55+ this . nativeContext . set ( ! ! e ) . ignoreErrors ( ) ;
56+ this . updateMergedContexts ( ) ;
57+ }
58+ private onDidChangeActiveTextEditor ( e ?: TextEditor ) {
59+ this . isPythonFileActive = e ?. document . languageId === PYTHON_LANGUAGE ;
60+ this . updateMergedContexts ( ) ;
61+ }
62+ private updateMergedContexts ( ) {
63+ this . interactiveOrNativeContext . set ( this . nativeContext . value === true && this . interactiveContext . value === true ) . ignoreErrors ( ) ;
64+ this . pythonOrNativeContext . set ( this . nativeContext . value === true || this . isPythonFileActive === true ) . ignoreErrors ( ) ;
65+ this . pythonOrInteractiveContext . set ( this . interactiveContext . value === true || this . isPythonFileActive === true ) . ignoreErrors ( ) ;
66+ this . pythonOrInteractiveOrNativeContext
67+ . set ( this . nativeContext . value === true || ( this . interactiveContext . value === true && this . isPythonFileActive === true ) )
68+ . ignoreErrors ( ) ;
4069 }
4170}
0 commit comments