在摩纳哥编辑器中显示错误的快速修复

Tim*_*m S 6 javascript monaco-editor

我正在尝试使用 Monaco 作为自定义语言的编辑器。

我使用此代码在操场上显示示例错误(省略了某些部分):

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);
});
Run Code Online (Sandbox Code Playgroud)

这会导致编辑器中出现所需的错误:

错误悬停

如何快速修复该错误?(而不是“没有可用的快速修复”)

我已经看过了,monaco.languages.registerCodeActionProvider()但我不明白这与错误检测代码有什么关系。

更一般地说,我一直在努力寻找使用 Monaco 实施 Quick Fix 的示例。

Tim*_*m S 6

我使用代码操作提供程序让它工作。

关键是使用context.markersinsideprovideCodeActions()来获取我在其他地方(通过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: () => {}
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

快速解决

仍然很想知道我是否遗漏了明显的摩纳哥文档或示例来源。我使用https://microsoft.github.io/monaco-editor/api/index.htmlmonaco.d.ts将其拼凑在一起,但需要大量的反复试验。