Monaco-editor: Cannot undo edit from CodeAction

Created on 12 Aug 2019  路  4Comments  路  Source: microsoft/monaco-editor

monaco-editor version: 0.17.1-0.18.1
Browser: Chrome/Firefox/Edge Dev
OS: Windows 10

If you register a CodeActionProvider that applies an edit operation, this edit operation is apparently not pushed to the undo stack.

  1. Run the code below in the playground
  2. Hover over the marker
  3. Apply the provided code action
  4. Press Ctrl+Z to undo

Nothing happens. If you edit the text before applying the code action and undo, the edit from before is undone instead of the edit from the code action.

Addition: This bug also affects the edits of the new TypeScript RenameProvider. So maybe it's edits in general.

Note that this example hard codes the creation of the marker data, which normally comes directly from a linter such as ESLint. Since the marker data is not recomputed, a small text decoration remains after applying the code action. This does not happen when the marker data is recomputed.

Playground:

const markerRange = new monaco.Range(2, 8, 2, 22);

monaco.languages.registerCodeActionProvider("javascript", {
    provideCodeActions(model, range, context, token) {
        /** @type {monaco.languages.CodeAction[]} */
        const codeActions = [];
        for (const marker of context.markers) {
            /** @type {monaco.languages.TextEdit} */
            const fix = {
                range: markerRange,
                text: '"Hello world!"'
            };
            codeActions.push({
                title: `Fix: ${marker.message}`,
                diagnostics: [marker],
                edit: {
                    edits: [{
                        edits: [fix],
                        resource: model.uri,
                    }],
                },
                kind: "quickfix" // not sure if necessary / what value should be used
            });
        }
        return { actions: codeActions, dispose() {} };
    }
});

const editor = monaco.editor.create(document.getElementById("container"), {
    value: "function hello() {\n\talert('Hello world!');\n}",
    language: "javascript",
    lightbulb: { enabled: true },
});

monaco.editor.setModelMarkers(editor.getModel(), "ESLint", [{
    message: "Strings must use doublequote.",
    startLineNumber: markerRange.startLineNumber,
    startColumn: markerRange.startColumn,
    endLineNumber: markerRange.endLineNumber,
    endColumn: markerRange.endColumn,
    source: "ESLint",
    severity: monaco.MarkerSeverity.Warning
}]);

Most helpful comment

I took a stab at taking your example @gins3000 and getting it upstreamed after confirming it locally 馃憤

All 4 comments

Any known workaround for this issue?

@rkistner, if you're still looking for a workaround:

In https://github.com/microsoft/vscode/blob/6ee4811eed9a20487b28996522cdbe664c852dfa/src/vs/editor/standalone/browser/simpleServices.ts#L651 if I replace

applyEdits(edits.map(edit => EditOperation.replaceMove(Range.lift(edit.range), edit.text)));

with

model.pushEditOperations([], edit.map((e) => EditOperation.replaceMove(Range.lift(e.range), e.text)), () => []);
model.pushStackElement();

the undo/redo functionality seems to be working again (tested with monaco-editor-core 0.17.0). I have not thoroughly investigated if this quick fix breaks anything else, but maybe it works for your purposes.

Since this issue is already open since August, I expect it can't be this simple. If @alexandrudima has any insight on this, that would be helpful.

I took a stab at taking your example @gins3000 and getting it upstreamed after confirming it locally 馃憤

Thanks a bunch @orta !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robclive picture robclive  路  3Comments

poloten4uk picture poloten4uk  路  3Comments

brandalorian picture brandalorian  路  3Comments

Panhaiwei picture Panhaiwei  路  3Comments

Spongman picture Spongman  路  3Comments