From 0fddeff12852af78c0cd4c7c8e585047e4a4f848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Wed, 15 Mar 2023 18:56:02 +0100 Subject: [PATCH] feat: implement EditorService.onDidActiveEditorChange --- src/service-override/modelEditor.ts | 38 +++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/service-override/modelEditor.ts b/src/service-override/modelEditor.ts index 61a8afdc..054d5422 100644 --- a/src/service-override/modelEditor.ts +++ b/src/service-override/modelEditor.ts @@ -7,16 +7,15 @@ import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService import { CodeEditorService } from 'vs/workbench/services/editor/browser/codeEditorService' import { IEditorService, isPreferredGroup, PreferredGroup, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService' import { IEditorControl, IEditorPane, IResourceDiffEditorInput, isEditorInput, isResourceEditorInput, ITextDiffEditorPane, IUntitledTextResourceEditorInput, IUntypedEditorInput } from 'vs/workbench/common/editor' -import { Event } from 'vs/base/common/event' +import { Emitter, Event } from 'vs/base/common/event' import { EditorInput } from 'vs/workbench/common/editor/editorInput' import { IEditorOptions, IResourceEditorInput, ITextResourceEditorInput } from 'vs/platform/editor/common/editor' import { DEFAULT_EDITOR_MAX_DIMENSIONS, DEFAULT_EDITOR_MIN_DIMENSIONS } from 'vs/workbench/browser/parts/editor/editor' -import { ICodeEditor } from 'vs/editor/browser/editorBrowser' import { applyTextEditorOptions } from 'vs/workbench/common/editor/editorOptions' -import { ScrollType } from 'vs/editor/common/editorCommon' +import { IEditor, ScrollType } from 'vs/editor/common/editorCommon' import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors' import { ITextModel } from 'vs/editor/common/model' -import { ImmortalReference, IReference } from 'vs/base/common/lifecycle' +import { Disposable, ImmortalReference, IReference } from 'vs/base/common/lifecycle' import { URI } from 'vs/base/common/uri' import { IModelService } from 'vs/editor/common/services/model' import { IFileService } from 'vs/platform/files/common/files' @@ -24,6 +23,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity' import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo' import { TextResourceEditorModel } from 'vs/workbench/common/editor/textResourceEditorModel' +import { ICodeEditor } from 'vs/editor/browser/editorBrowser' import { unsupported } from '../tools' type OpenEditor = (model: ITextModel, options: IEditorOptions | undefined, sideBySide?: boolean) => Promise @@ -55,20 +55,42 @@ class SimpleEditorPane implements IEditorPane { } } -class EditorService implements IEditorService { +class EditorService extends Disposable implements IEditorService { + public activeTextEditorControl: IEditor | undefined + private _onDidActiveEditorChange = this._register(new Emitter()) + constructor ( private _openEditor: OpenEditor, @ITextModelService private textModelService: ITextModelService - ) {} + ) { + super() + + setTimeout(() => { + const codeEditorService = StandaloneServices.get(ICodeEditorService) + this.activeTextEditorControl = codeEditorService.getFocusedCodeEditor() ?? undefined + const handleCodeEditor = (editor: ICodeEditor) => { + const onEditorFocused = () => { + const newFocusedEditor = codeEditorService.getFocusedCodeEditor() + if (newFocusedEditor !== this.activeTextEditorControl) { + this.activeTextEditorControl = newFocusedEditor ?? undefined + this._onDidActiveEditorChange.fire() + } + } + editor.onDidFocusEditorText(onEditorFocused) + editor.onDidFocusEditorWidget(onEditorFocused) + } + this._register(codeEditorService.onCodeEditorAdd(handleCodeEditor)) + codeEditorService.listCodeEditors().forEach(handleCodeEditor) + }) + } readonly _serviceBrand: undefined - onDidActiveEditorChange = Event.None + onDidActiveEditorChange = this._onDidActiveEditorChange.event onDidVisibleEditorsChange = Event.None onDidEditorsChange = Event.None onDidCloseEditor = Event.None activeEditorPane: undefined activeEditor: undefined - get activeTextEditorControl () { return StandaloneServices.get(ICodeEditorService).getFocusedCodeEditor() ?? undefined } activeTextEditorLanguageId = undefined visibleEditorPanes = [] visibleEditors = []