Monaco-editor: How to scope the editor models / `typescriptDefaults`

Created on 11 Oct 2018  路  7Comments  路  Source: microsoft/monaco-editor

I've tried it at StackOverflow, but had no luck...

Any chance advise to scope the changes to monaco.languages.typescript or the "editor models".

Background: I want to host multiple Monaco editors with different sets of models (maybe also different typescriptDefaults).

feature-request monaco-typescript monaco-typescript-multifile

Most helpful comment

Is what you mean with the "ts2" language a "fake" language without semantic model, but just a syntax highlighting enabled editor

Yes

And when changing the focus between the editors, I also need to switch the global editor models (dispose all, and create them for the focused editor).

No, I think you just need to implement the following:

editors.forEach(function(editor) {
    editor.onDidBlurEditorWidget(() => {
        monaco.editor.setModelLanguage(editor.getModel(), 'ts2')
    });
    editor.onDidFocusEditorWidget(() => {
        monaco.editor.setModelLanguage(editor.getModel(), 'typescript')
    });
});

I'm not sure how well it's gonna work, but this is the cheapest I can think of to simulate something similar to what you describe.

All 7 comments

This is outside the scope of the monaco-typescript project. I recommend that you fork the project and implemented that on your side. Currently, that is built to have a single TypeScript language server host which exposes all the models together to typescript.

One alternative idea to cheat would be to think about user focus... i.e. an user can only work with at most 1 editor at a time. So you can use a different language id, e.g. ts2 for all editors until they gain focus. Once focus is gained, you can change the language to typescript and then the TS compiler will only see that one model. Once the editor is blurred, you can change the language id back to ts2.

Then, you can defined a monarch colorizer for ts2 which would be the same one as for TypeScript.... Diagnostics will be removed, but other than that this could simulate what you desire...

Good luck!

Okay, thanks for the info that this scenario is not supported at the moment.

About the thing with the second language id ("ts2"): Is what you mean with the "ts2" language a "fake" language without semantic model, but just a syntax highlighting enabled editor? => No error squiggles in the (non-focused) "ts2" editors, only in the focused "typescript" editor. And when changing the focus between the editors, I also need to switch the global editor models (dispose all, and create them for the focused editor).

Is what you mean with the "ts2" language a "fake" language without semantic model, but just a syntax highlighting enabled editor

Yes

And when changing the focus between the editors, I also need to switch the global editor models (dispose all, and create them for the focused editor).

No, I think you just need to implement the following:

editors.forEach(function(editor) {
    editor.onDidBlurEditorWidget(() => {
        monaco.editor.setModelLanguage(editor.getModel(), 'ts2')
    });
    editor.onDidFocusEditorWidget(() => {
        monaco.editor.setModelLanguage(editor.getModel(), 'typescript')
    });
});

I'm not sure how well it's gonna work, but this is the cheapest I can think of to simulate something similar to what you describe.

@alexandrudima Each of my editors has a different (only partially overlapping) set of editor models (the one it displays and N non-visible/dependency models). This is why I think it's necessary to dispose all current editor models and loading "the current set" when switching focus. => To completely isolate them (avoid conflicts of duplicate type definitions, model URLs, ...).

Each of my editors has a different (only partially overlapping) set of editor models (the one it displays and N non-visible/dependency models)

:+1: Ok, I didn't know that.

@alexandrudima Again thanks for your support.

Is what you mean with the "ts2" language a "fake" language without semantic model, but just a syntax highlighting enabled editor

Yes

And when changing the focus between the editors, I also need to switch the global editor models (dispose all, and create them for the focused editor).

No, I think you just need to implement the following:

editors.forEach(function(editor) {
  editor.onDidBlurEditorWidget(() => {
        monaco.editor.setModelLanguage(editor.getModel(), 'ts2')
    });
    editor.onDidFocusEditorWidget(() => {
        monaco.editor.setModelLanguage(editor.getModel(), 'typescript')
    });
});

I'm not sure how well it's gonna work, but this is the cheapest I can think of to simulate something similar to what you describe.

Anybody get this to work? Im putting in a tabbed editor, (where you write seperate non related snippets of ts that control automation tasks) and changing the models language doest seem to remove it from being used by the language server? and I still get "cannot redeclare ... etc". The approach is similar to above, set all models to plaintext on tab change, except the tab your changing too - which you set back to ts. All the relevant events fire and indicate that all models have changed - so I dont think its as much me doing it wrong as it not being supported?

Ive looked at dispose removes the model from use in the language server like I want, and I could use this - but I would like to keep the undo history on tab change, and I cant see a way of doing this when you dispose all models and create the new model for the new tab each time.

Thanks, would love any other ideas for this

Was this page helpful?
0 / 5 - 0 ratings