Skip to content

Commit

Permalink
feat!: provide ref instead of model
Browse files Browse the repository at this point in the history
to be able to dispose it at the end
  • Loading branch information
Loïc Mangeonjean committed Mar 24, 2023
1 parent a20dd27 commit acd2198
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
21 changes: 10 additions & 11 deletions demo/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -57,13 +57,10 @@ Services.install({
})

let currentEditor: ({
model: monaco.editor.ITextModel
modelRef: IReference<IResolvedTextEditorModel>
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
Expand All @@ -88,7 +85,7 @@ function openNewCodeEditor (model: monaco.editor.ITextModel) {
const editor = createConfiguredEditor(
editorElem,
{
model,
model: modelRef.object.textEditorModel,
readOnly: true,
automaticLayout: true
}
Expand All @@ -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
Expand All @@ -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(),
Expand Down
32 changes: 12 additions & 20 deletions src/service-override/modelEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<ICodeEditor | undefined>
type OpenEditor = (modelRef: IReference<IResolvedTextEditorModel>, options: IEditorOptions | undefined, sideBySide?: boolean) => Promise<ICodeEditor | undefined>

class SimpleEditorPane implements IEditorPane {
constructor (private editor?: ICodeEditor) {}
Expand Down Expand Up @@ -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<IResolvedTextEditorModel> | 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
}

Expand Down Expand Up @@ -219,5 +209,7 @@ export default function getServiceOverride (openEditor: OpenEditor): IEditorOver

export {
OpenEditor,
IEditorOptions
IEditorOptions,
IResolvedTextEditorModel,
IReference
}

0 comments on commit acd2198

Please sign in to comment.