Skip to content

Add ability to specify the provided code action kinds from a CodeActionProvider #2625

@spahnke

Description

@spahnke

I'm using a CodeActionProvider to provide code actions of type "quickfix" (some of which are preferred) and "source" and was wondering why editor.action.autoFix (Shift+Alt+.) was not working, and why the source action context menu entry was not showing.
grafik

Looking into the when clause of editor.action.autoFix there was a reference to the context key supportedCodeAction which was always empty in the Monaco editor. Additionally the "Refactor..." and "Source Action..." actions are contributed to the context menu only when respective supportedCodeActions are set ("refactor", "source"). Digging a bit further on how VS Code does it with extensions I saw that the VS Code API has an additional parameter metatadata on registerCodeActionProvider which is not handled in the Monaco case.

I will submit a PR for this: microsoft/vscode#131254

Tested with the following code by running the playground from sources (the third parameter is new):

monaco.languages.registerCodeActionProvider('javascript', {
    provideCodeActions(model, range, context, token) {
        return Promise.resolve({
            actions: [{
                title: "Testing",
                edit: {
                    edits: [{
                        edit: { range: new monaco.Range(1, 1, 1, 1), text: `test${model.getEOL()}` },
                        resource: model.uri,
                    }],
                },
                isPreferred: true,
                kind: "quickfix",
            },
            {
                title: "Testing2",
                edit: {
                    edits: [{
                        edit: { range: new monaco.Range(1, 1, 1, 1), text: `test2${model.getEOL()}` },
                        resource: model.uri,
                    }],
                },
                kind: "source.fixAll",
            },
            {
                title: "Testing3",
                edit: {
                    edits: [{
                        edit: { range: new monaco.Range(1, 1, 1, 1), text: `test3${model.getEOL()}` },
                        resource: model.uri,
                    }],
                },
                kind: "refactor.extract",
            }],
            dispose: () => { },
        })
    }
}, { providedCodeActionKinds: ["quickfix", "refactor", "source"] });

monaco.editor.create(document.getElementById("container"), {
    value: "const foo = 1;",
    language: "javascript"
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions