簡體   English   中英

在摩納哥編輯器中顯示錯誤的快速修復

[英]Show quick fix for an error in Monaco Editor

我正在嘗試將摩納哥作為自定義語言的編輯器。

我使用以下代碼來顯示操場上的示例錯誤(省略了某些部分):

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);
});

在編輯器中導致所需的錯誤:

錯誤懸停

如何針對該錯誤顯示快速修復? (而不是“沒有快速修復可用”)

我看過monaco.languages.registerCodeActionProvider()但是看不到它與錯誤檢測代碼的聯系。

更籠統地說,我一直在努力尋找與摩納哥一起實施Quick Fix的示例。

我使用代碼操作提供程序來工作。

關鍵是在provideCodeActions()使用context.markers來獲取我在其他地方(通過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: () => {}
        }
    }
});

快速解決

我仍然想知道我是否缺少摩納哥的明顯文獻或示例資源。 我使用https://microsoft.github.io/monaco-editor/api/index.htmlmonaco.d.ts將它們拼湊在一起,但是這花費了大量的試驗和錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM