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.
Ctrl+Z to undoNothing 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
}]);
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 !
Most helpful comment
I took a stab at taking your example @gins3000 and getting it upstreamed after confirming it locally 馃憤