You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow custom editors to hook into Edit menu actions such as copy, undo, redo, ... #90110
Problem
For custom editors, we currently support undo/redo using keyboard shortcuts. However you cannot undo and redo using the Edit menu in VS Code.
Other commands like copy and paste only work in webviews because we do a execCommand('copy') call. This ends up copying the correct text but bypasses the custom copy command we have for webviews:
I would think that Undo/Redo should use some global (workbench level?) thing that keeps track of all the undos/redos that are possible and then run them. This would be the beginning of workspace-level undo/redo. We can start simple and only let the text and custom editors add edits (e.g. not the file explorer which we could do later).
I briefly talked about this with Alex when we discussed custom editors a while ago, maybe he can chime in with his point of view for how such a system would work. There are some tricky bits around this as soon as we add non-editor related undo/redo because once you move focus from one editor to another, you need to basically move all edits in the history for that editor to the front. Alternatively we could track edits per editor and simply navigate in the edit stack of the editor that has focus.
Currently, undo and redo are completely owned by the editor, with a small workaround where they check if the current focused element is a <textarea> or <input> and then they invoke document.executeCommand('undo') and document.executeCommand('redo').
To get a more collaborative story, I believe the best way would be to have a workbench service where various places can contribute undo/redo stack elements. This needs to be thought through such as to not disrupt the current undo/redo behaviour and IMHO this needs to be adopted somehow by the editor.
Here is the current undo/redo implementation for the editor (this is stored per text buffer): vs/editor/common/model/editStack.ts. By looking at the way that class is used already some requirements for such a workbench service can be devised:
the ability to remove everything. Currently, when someone calls model.setValue(), the undo/redo stack for that text buffer is removed entirely. The same happens when disposing a text buffer, the undo/redo stack is cleared. The reason is that a subsequent load of the file from disk could end up loading different content and the undo/redo stack could be invalid.
the ability to push a stack element and have it be "opened" such that subsequent edits can go in the same stack element.
the ability to undo/redo based on a resource, because as the focus moves in the workbench, undo/redo will work on the focused resource, and not on the chronological operations. i.e. typing in a.ts, then in b.ts and then moving focus to a.ts and running undo will undo the edits in a.ts and leave the edits in b.ts untouched.
To prevent disruption, I suggest that any work done here is done via a PR which we can test and merge in only when things work smoothly.
Alexandru Dima (@alexdima) I'm going to look into adopting the UndoRedoService within the model layer of custom editors, similarly to how you've adopted it in TextModel. Will copy you on the PR
Matt Bierner (@mjbvz) Sorry, I meant for the custom editor that I'm working on, which deals with diagram components, not text.
These operations are working fine (on Fedora 32) for a textarea inside a custom editor.
I just wonder how can we leverage from these menus on other types of custom editors.
Currently, Ctrl+C and Ctrl+V are handled by us but Edit->Copy doesn't do anything because we don't have such a handler.
Guilherme Caponetto (@caponetto) Can you please file a new feature request for that. It sounds like a good use case that will likely require some new APIs for custom editors
For copy and paste, you could try using the copy event inside the webview
Matt Bierner (@mjbvz) Trying to verify this with the hex editor extension which listens to the undo and redo events which get fired for custom editors. I can execute undo and redo via keyboard shortcut, but not via the command palette. When you mean edit menu do you mean the right click menu?
I was able to test the binary pawdraw editor but undo/redo didn't have an effect in the cat scratch editor. I'm not sure if I'm testing this correctly...
Logan Ramos (@lramos15) Thanks for testing. This is for the undo/redo commands in the edit menu from the title bar. I've opened #101553 to track the undo/redo command palette not working
I can't repo this not working on windows, even if trying to trigger these actions quickly. Connor Peet (@connor4312) Can you make sure you have the latest extension sample version? Any specific repo steps?
Activity
mjbvz commentedon Feb 6, 2020
Also adding Kai Maetzel (@kieferrm) and Peng Lyu (@rebornix) for notebooks
bpasero commentedon Feb 6, 2020
I would think that Undo/Redo should use some global (workbench level?) thing that keeps track of all the undos/redos that are possible and then run them. This would be the beginning of workspace-level undo/redo. We can start simple and only let the text and custom editors add edits (e.g. not the file explorer which we could do later).
I briefly talked about this with Alex when we discussed custom editors a while ago, maybe he can chime in with his point of view for how such a system would work. There are some tricky bits around this as soon as we add non-editor related undo/redo because once you move focus from one editor to another, you need to basically move all edits in the history for that editor to the front. Alternatively we could track edits per editor and simply navigate in the edit stack of the editor that has focus.
alexdima commentedon Feb 6, 2020
Currently,
undoandredoare completely owned by the editor, with a small workaround where they check if the current focused element is a<textarea>or<input>and then they invokedocument.executeCommand('undo')anddocument.executeCommand('redo').To get a more collaborative story, I believe the best way would be to have a workbench service where various places can contribute undo/redo stack elements. This needs to be thought through such as to not disrupt the current undo/redo behaviour and IMHO this needs to be adopted somehow by the editor.
Here is the current undo/redo implementation for the editor (this is stored per text buffer):
vs/editor/common/model/editStack.ts. By looking at the way that class is used already some requirements for such a workbench service can be devised:model.setValue(), the undo/redo stack for that text buffer is removed entirely. The same happens when disposing a text buffer, the undo/redo stack is cleared. The reason is that a subsequent load of the file from disk could end up loading different content and the undo/redo stack could be invalid.a.ts, then inb.tsand then moving focus toa.tsand runningundowill undo the edits ina.tsand leave the edits inb.tsuntouched.To prevent disruption, I suggest that any work done here is done via a PR which we can test and merge in only when things work smoothly.
mjbvz commentedon Mar 10, 2020
Alexandru Dima (@alexdima) I'm going to look into adopting the UndoRedoService within the model layer of custom editors, similarly to how you've adopted it in
TextModel. Will copy you on the PRmjbvz commentedon Mar 13, 2020
Custom editors are now be using the
UndoRedoService. I've also exposed a way for extensions to contribute thelabelused for changes17 remaining items
mjbvz commentedon Jun 24, 2020
Guilherme Caponetto (@caponetto) Which operating system are you on? I see this working as expected on MacOS using a
<textarea>inside a custom editorcaponetto commentedon Jun 24, 2020
Matt Bierner (@mjbvz) Sorry, I meant for the custom editor that I'm working on, which deals with diagram components, not text.
These operations are working fine (on Fedora 32) for a
textareainside a custom editor.I just wonder how can we leverage from these menus on other types of custom editors.
Currently, Ctrl+C and Ctrl+V are handled by us but Edit->Copy doesn't do anything because we don't have such a handler.
mjbvz commentedon Jun 24, 2020
Guilherme Caponetto (@caponetto) Can you please file a new feature request for that. It sounds like a good use case that will likely require some new APIs for custom editors
For copy and paste, you could try using the
copyevent inside the webviewlramos15 commentedon Jul 1, 2020
Matt Bierner (@mjbvz) Trying to verify this with the hex editor extension which listens to the undo and redo events which get fired for custom editors. I can execute undo and redo via keyboard shortcut, but not via the command palette. When you mean edit menu do you mean the right click menu?
connor4312 commentedon Jul 1, 2020
I was able to test the binary pawdraw editor but undo/redo didn't have an effect in the cat scratch editor. I'm not sure if I'm testing this correctly...
mjbvz commentedon Jul 2, 2020
Logan Ramos (@lramos15) Thanks for testing. This is for the undo/redo commands in the edit menu from the title bar. I've opened #101553 to track the undo/redo command palette not working
mjbvz commentedon Jul 2, 2020
Connor Peet (@connor4312) Were you following #90110 (comment)?
I just tried on MacOS but see it working properly:
connor4312 commentedon Jul 2, 2020
Yea. Actually testing it some more it looks like can undo, but it's inconsistent, video: https://memes.peet.io/img/20-07-4a124599-3ddf-4c7a-aba0-8313019828bd.webm
Might an issue with the sample rather than vscode
mjbvz commentedon Jul 2, 2020
I can't repo this not working on windows, even if trying to trigger these actions quickly. Connor Peet (@connor4312) Can you make sure you have the latest extension sample version? Any specific repo steps?