Search code examples
monaco-editor

Preferred way of linking models


Is there a preferred way of linking models in Monaco (ctrl + click)? I'm implementing a code editor with external tabs management + file tree, one editor, multiple models being swapped between. It would be ideal for users to be able to ctrl + click includes (references to other models, e.g: @include 'abc') and make them "followable".

What I'm currently doing is registering codelenses to open said models. In the lenses definition I do a model.findMatches(regexp), match the include statement, get the include filename from the match groups and register the open command. This is currently working, but I don't know if there's a better, more standard, less intensive way of having this functionality (preferably via ctrl + click) than scanning the model viewport for matches everytime the lenses are generated (keyup).


Solution

  • Answering my own question here after four months. Yes, you don't want lenses to register links but rather register them through your language registerLinkProvider so users can CTRL + click them as usual.

    To do so use the monaco.languages.registerLinkProvider method which returns an object with two methods provideLinks and resolveLink.

    provideLinks is where you'll want to do your parsing and capture your links, whatever you return in this method it will get passed to resolveLink.

    resolveLink will take whatever you returned from provideLinks so you can handle your linking logic.

    Hope this helps others and remember, this is your best Monaco friend.