Skip to content

Commit

Permalink
Merge pull request #24 from CodinGame/fix-model-ref-of-existing-models
Browse files Browse the repository at this point in the history
Fix model ref of existing models
  • Loading branch information
CGNonofr committed Jul 12, 2022
2 parents c3b4d81 + d95ddc7 commit 4e40991
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/service-override/modelEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import { applyTextEditorOptions } from 'vs/workbench/common/editor/editorOptions
import { ScrollType } from 'vs/editor/common/editorCommon'
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'
import { ITextModel } from 'vs/editor/common/model'
import { IReference } from 'vs/base/common/lifecycle'
import { ImmortalReference, IReference } from 'vs/base/common/lifecycle'
import { URI } from 'vs/base/common/uri'
import { IModelService } from '../services'
import { IModelService } from 'vs/editor/common/services/model'
import { IFileService } from 'vs/platform/files/common/files'
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'
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 { unsupported } from '../tools'

type OpenEditor = (model: ITextModel, options: IEditorOptions | undefined, sideBySide?: boolean) => Promise<ICodeEditor | undefined>
Expand Down Expand Up @@ -101,6 +106,7 @@ class EditorService implements IEditorService {
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
Expand All @@ -119,6 +125,8 @@ class EditorService implements IEditorService {
return undefined
}

// Otherwise, let the user destroy the model, never destroy the reference

if (options != null) {
// Apply selection
applyTextEditorOptions(options, modelEditor, ScrollType.Immediate)
Expand Down Expand Up @@ -150,7 +158,24 @@ class EditorService implements IEditorService {
* This class just force dispose the refs when the model is disposed
*/
class CustomTextModelResolverService extends TextModelResolverService {
override async createModelReference (resource: URI) {
constructor (
@IInstantiationService private _instantiationService: IInstantiationService,
@IFileService fileService: IFileService,
@IUndoRedoService undoRedoService: IUndoRedoService,
@IModelService private _modelService: IModelService,
@IUriIdentityService uriIdentityService: IUriIdentityService
) {
super(_instantiationService, fileService, undoRedoService, _modelService, uriIdentityService)
}

override async createModelReference (resource: URI): Promise<IReference<IResolvedTextEditorModel>> {
// If the model already exists, return an immortal reference to it
const existingModel = this._modelService.getModel(resource)
if (existingModel != null) {
return new ImmortalReference(this._instantiationService.createInstance(TextResourceEditorModel, resource) as IResolvedTextEditorModel)
}

// Otherwise, try to create a reference to the model
const ref = await super.createModelReference(resource)
// Dispose the ref when the model is disposed or we'll get a disposed model next time
ref.object.textEditorModel.onWillDispose(() => {
Expand Down

0 comments on commit 4e40991

Please sign in to comment.