Skip to content

Question about createModelReference? #482

Closed

Description

@buglessbuild

Hello Team,

I was going through the README and found this new method of creating a model using createModelReference. I'm using a websocket LSP server to provide LSP support in the Monaco editor. So, do I still need to create fileSystemProvider and other things before using createModelReference or can I simply replace createModel with createModelReference without making any other changes?

import * as monaco from 'monaco-editor'
import { RegisteredFileSystemProvider, RegisteredMemoryFile, registerFileSystemOverlay } from '@codingame/monaco-vscode-files-service-override'

const fileUri = monaco.Uri.file(<file uri>);

// Do I always need these three lines before creating a reference of the model
const fileSystemProvider = new RegisteredFileSystemProvider(false)
fileSystemProvider.registerFile(new RegisteredMemoryFile(fileUri, <file content>))
const overlayDisposable = registerFileSystemOverlay(1, fileSystemProvider)

const modelRef = await monaco.editor.createModelReference(fileUri)

const editor = monaco.editor.create({ model: modelRef.object.textEditorModel })

One more thing: It seems like all the methods we used to access, like model.updateOptions, have been moved inside modelRef.object.textEditorModel. Am I right?

Thank you for your help!

Activity

  1. buglessbuild commented on Aug 20, 2024

    @buglessbuild
    Author

    @CGNonofr do you have any thoughts on this?

  2. CGNonofr commented on Aug 20, 2024

    @CGNonofr
    Contributor

    So, do I still need to create fileSystemProvider and other things before using createModelReference or can I simply replace createModel with createModelReference without making any other changes?

    createModelReference create a model reference bound to a file, it needs the file to exist

    One more thing: It seems like all the methods we used to access, like model.updateOptions, have been moved inside modelRef.object.textEditorModel. Am I right?

    createModelReference returns a wrapper to the monaco model indeed, modelRef.object.textEditorModel is the exact same object as the one returned by createModel. modelRef is the ref itself you have to dispose when you don't need it anymore, modelRef.object is a wrapper for the model making the link between the model and the filesystem/workingcopy, allowing you to save/revert changes

  3. locked and limited conversation to collaborators on Aug 21, 2024
  4. converted this issue into a discussion #490 on Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions