Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show quick fix for an error in Monaco Editor

I'm experimenting with Monaco as an editor for a custom language.

I use this code to show an example error in the playground (some parts omitted):

const editor = monaco.editor.create(<omitted>);
const model = editor.getModel();
model.onDidChangeContent(event => {
   const value = model.getValue();
   const errors = GetErrors(value); // Implementation of GetErrors() not shown here

    monaco.editor.setModelMarkers(model, "Example", errors);
});

Which results in the desired error in the editor:

Error hover

How do I make a quick fix appear for that error? (Instead of "No quick fixes available")

I've looked at monaco.languages.registerCodeActionProvider() but I don't see how that ties in to the error detection code.

More generally, I've struggled to find examples for implementing Quick Fix with Monaco.

like image 279
Tim S Avatar asked Sep 18 '19 13:09

Tim S


People also ask

What is the use of Monaco editor?

The Monaco Editor is the code editor that powers VS Code. A good page describing the code editor's features is here. It is licensed under the MIT License and supports Edge, Chrome, Firefox, Safari and Opera. The Monaco editor is not supported in mobile browsers or mobile web frameworks.

Is Monaco editor free?

monaco-editor - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers.


1 Answers

I got it working using a Code Action Provider.

The key was to use context.markers inside provideCodeActions() to get the errors I raised elsewhere (via setModelMarkers()).

monaco.languages.registerCodeActionProvider("myLanguage", {
    provideCodeActions: (
        model /**ITextModel*/,
        range /**Range*/,
        context /**CodeActionContext*/,
        token /**CancellationToken*/
    ) => {

        const actions = context.markers.map(error => {
            return {
                title: `Example quick fix`,
                diagnostics: [error],
                kind: "quickfix",
                edit: {
                    edits: [
                        {
                            resource: model.uri,
                            edits: [
                                {
                                    range: error,
                                    text: "This text replaces the text with the error"
                                }
                            ]
                        }
                    ]
                },
                isPreferred: true
            };
        });
        return {
            actions: actions,
            dispose: () => {}
        }
    }
});

Quick fix

Would still love to know if I'm missing an obvious source of documentation or examples for Monaco. I pieced this together using https://microsoft.github.io/monaco-editor/api/index.html and monaco.d.ts but it took a lot of trial and error.

like image 126
Tim S Avatar answered Sep 22 '22 18:09

Tim S