From acd219862627bd02a0c2e327e9b9288cafb6bb89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Fri, 24 Mar 2023 09:51:39 +0100 Subject: [PATCH] feat!: provide ref instead of model to be able to dispose it at the end --- demo/src/setup.ts | 21 +++++++++---------- src/service-override/modelEditor.ts | 32 +++++++++++------------------ 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/demo/src/setup.ts b/demo/src/setup.ts index 0f3dc51b..1e23ed10 100644 --- a/demo/src/setup.ts +++ b/demo/src/setup.ts @@ -7,7 +7,7 @@ import 'monaco-editor/esm/vs/editor/standalone/browser/quickAccess/standaloneGot import 'monaco-editor/esm/vs/editor/standalone/browser/quickAccess/standaloneCommandsQuickAccess' import 'monaco-editor/esm/vs/editor/standalone/browser/referenceSearch/standaloneReferenceSearch' import { Services, StandaloneServices } from 'vscode/services' -import getModelEditorServiceOverride from 'vscode/service-override/modelEditor' +import getModelEditorServiceOverride, { IReference, IResolvedTextEditorModel, OpenEditor } from 'vscode/service-override/modelEditor' import getNotificationServiceOverride from 'vscode/service-override/notifications' import getDialogsServiceOverride from 'vscode/service-override/dialogs' import getConfigurationServiceOverride from 'vscode/service-override/configuration' @@ -57,13 +57,10 @@ Services.install({ }) let currentEditor: ({ - model: monaco.editor.ITextModel + modelRef: IReference editor: monaco.editor.IStandaloneCodeEditor } & monaco.IDisposable) | null = null -function openNewCodeEditor (model: monaco.editor.ITextModel) { - if (currentEditor != null && model === currentEditor.model) { - return currentEditor.editor - } +const openNewCodeEditor: OpenEditor = async (modelRef) => { if (currentEditor != null) { currentEditor.dispose() currentEditor = null @@ -88,7 +85,7 @@ function openNewCodeEditor (model: monaco.editor.ITextModel) { const editor = createConfiguredEditor( editorElem, { - model, + model: modelRef.object.textEditorModel, readOnly: true, automaticLayout: true } @@ -97,13 +94,17 @@ function openNewCodeEditor (model: monaco.editor.ITextModel) { currentEditor = { dispose: () => { editor.dispose() + modelRef.dispose() document.body.removeChild(container) currentEditor = null }, - model, + modelRef, editor } + editor.onDidBlurEditorText(() => { + currentEditor?.dispose() + }) container.addEventListener('mousedown', (event) => { if (event.target !== container) { return @@ -122,9 +123,7 @@ function openNewCodeEditor (model: monaco.editor.ITextModel) { // Override services StandaloneServices.initialize({ - ...getModelEditorServiceOverride(async (model) => { - return openNewCodeEditor(model) - }), + ...getModelEditorServiceOverride(openNewCodeEditor), ...getNotificationServiceOverride(), ...getDialogsServiceOverride(), ...getConfigurationServiceOverride(), diff --git a/src/service-override/modelEditor.ts b/src/service-override/modelEditor.ts index 25e92547..0b5356ce 100644 --- a/src/service-override/modelEditor.ts +++ b/src/service-override/modelEditor.ts @@ -14,7 +14,6 @@ import { DEFAULT_EDITOR_MAX_DIMENSIONS, DEFAULT_EDITOR_MIN_DIMENSIONS } from 'vs import { applyTextEditorOptions } from 'vs/workbench/common/editor/editorOptions' import { IEditor, ScrollType } from 'vs/editor/common/editorCommon' import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors' -import { ITextModel } from 'vs/editor/common/model' import { Disposable, ImmortalReference, IReference } from 'vs/base/common/lifecycle' import { URI } from 'vs/base/common/uri' import { IModelService } from 'vs/editor/common/services/model' @@ -26,7 +25,7 @@ import { TextResourceEditorModel } from 'vs/workbench/common/editor/textResource import { ICodeEditor } from 'vs/editor/browser/editorBrowser' import { unsupported } from '../tools' -type OpenEditor = (model: ITextModel, options: IEditorOptions | undefined, sideBySide?: boolean) => Promise +type OpenEditor = (modelRef: IReference, options: IEditorOptions | undefined, sideBySide?: boolean) => Promise class SimpleEditorPane implements IEditorPane { constructor (private editor?: ICodeEditor) {} @@ -120,30 +119,21 @@ class EditorService extends Disposable implements IEditorService { let modelEditor: ICodeEditor | undefined - // Try to get the existing model - const modelService = StandaloneServices.get(IModelService) - let model = modelService.getModel(resource) - - let newModelRef: IReference | undefined - if (model == null) { - // The model doesn't exist, resolve it - const modelRef = await this.textModelService.createModelReference(resource) - newModelRef = modelRef - model = modelRef.object.textEditorModel - } else { - // If the model was already existing, try to find an associated editor - const codeEditors = StandaloneServices.get(ICodeEditorService).listCodeEditors() - modelEditor = codeEditors.find(editor => editor.getModel() === model) - } + // The model doesn't exist, resolve it + const modelRef = await this.textModelService.createModelReference(resource) + + // If the model was already existing, try to find an associated editor + const codeEditors = StandaloneServices.get(ICodeEditorService).listCodeEditors() + modelEditor = codeEditors.find(editor => editor.getModel() === modelRef.object.textEditorModel) // If there is no editor associated to the model, try to open a new one if (modelEditor == null) { - modelEditor = await this._openEditor(model, options, preferredGroup === SIDE_GROUP) + modelEditor = await this._openEditor(modelRef, options, preferredGroup === SIDE_GROUP) } if (modelEditor == null) { // Dispose the newly created model if `openEditor` wasn't able to open it - newModelRef?.dispose() + modelRef.dispose() return undefined } @@ -219,5 +209,7 @@ export default function getServiceOverride (openEditor: OpenEditor): IEditorOver export { OpenEditor, - IEditorOptions + IEditorOptions, + IResolvedTextEditorModel, + IReference }