Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot undo edit from CodeAction #1548

Closed
spahnke opened this issue Aug 12, 2019 · 4 comments · Fixed by microsoft/vscode#83938
Closed

Cannot undo edit from CodeAction #1548

spahnke opened this issue Aug 12, 2019 · 4 comments · Fixed by microsoft/vscode#83938
Assignees
Milestone

Comments

@spahnke
Copy link
Contributor

spahnke commented Aug 12, 2019

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
}]);
@rkistner
Copy link

Any known workaround for this issue?

@gins3000
Copy link

@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.

@orta
Copy link
Contributor

orta commented Nov 4, 2019

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

@gins3000
Copy link

gins3000 commented Nov 4, 2019

Thanks a bunch @orta !

@vscodebot vscodebot bot locked and limited conversation to collaborators Dec 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants