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
Add events for copy/paste to allow language extensions to bring using/import statements along #30066
Apologies if this already exists, I can't seem to find anything about it (I expected it'd be listed in complex commands if anywhere).
The language service I use for Dart is getting a new feature that will allow me to send a command to get data about imports/using statements that apply to a subset of code. It's designed to be run when a user copies a chunk of code to the clipboard. When pasting, this data can be exchanged for a bunch of edits that would add the equivalent imports in the new file to avoid the user ending up having to fire a bunch of "import xxx" code actions after pasting.
In order to do this I need to know when the user has copied something into their clipboard (including the file and offset/range) and also again after they have pasted (just the file) so I can add some additional edits.
Reacted by Danny Tuppeny, Andrii Dieiev, Matija Grcic, Jonas Sander, Fred Bricon, nasirjd, devkabiir, Niklas Higi, Matej Rešetár, Marcos Rodriguez Toranzo and 223 more
It would actually work out better if we could also attach additional data into the clipboard during copy (to avoid there being a mismatch between the import data we stashed and the clipboard, which might have been replaced externally since) and get it back during paste; but I don't know enough about how clipboards work to know how feasible that is!
Reacted by Marcel Garus, Lemmingh, Jacob Richman, Faheem Ahmad and David H. Silber
Having copy/paste bring along imports is a good motivating use case for designing this api. Here is some of what makes it interesting:
Multiple different extensions would likely want to offer this functionality.
Implementing it requires state management. Store state on copy. Retrieve it on paste.
An extension can not precompute the text edits for this and cannot apply them synchronously.
On paste, an extension many want to make text edits outside of the paste text (such as adding imports at the top of a file).
On paste, an extension may want to make text edits to the pasted text. For example, consider copying a block of JS that uses alias imports and then pasting that into a file that imports the same file as a namespace. We'd want to convert the alias imports in the pasted text to use namespace imports.
Some goals:
Simple VS Code setting to control paste behavior
Avoid text flickers (where you paste and then the text you just pasted is rewritten a 200ms later)
Still let users do a basic paste (without triggering any actions)
Don't let misbehaving extensions block paste (have some sort of time box on paste)
Sketches
(note that these are not API proposals, just quick ideas on how this could work)
Source Actions
Try to reuse source actions for implement an editor.codeActionsOnPaste setting. For example, the setting:
Would run organize imports source action whenever the user pastes.
I don't see this as being enough on its own, except for super simple use cases such as organize imports. We need some way for extensions to store state from a copy and use that to produce a set of text edits on paste. Source actions don't provide enough infrastructure on their own
Extend clipboard api
Let extensions be notified of clipboard copy and paste events. Let extensions store metadata on the clipboard.
vscode.env.clipboard.onCopy(asyncentry=>{constmetadata=awaitgetMetadataForSelection();entry.addMetadata('myExtension',metadata);});vscode.env.clipboard.onPaste(asyncentry=>{constmetadata=entry.getMetadata('myExtension';if(metadata){// convert metadata into text edits and apply these}});
This leaves the implementation largely up to individual extensions. There are a few big open questions here too:
Extensions are driving the behavior (not VS Code)
No unified setting to control this behavior (each extension would likely bring along its own)
Paste Action Provider
Define a contract that extensions can implement to hook into copy/paste. This basically just adds a contract on top of the clipboard api sketch above:
classAddImportsOnPasteProviderimplementsvscode.PasteActionProvider{asyncprovidePasteActions(document: vsocode.TextDocument,copiedRange: vscode.Range): Promise<vscode.PasteAction[]>{// Get metadata about copied textconstmetadata=awaitgetMetadataForSelection(document,copiedRange);// Return command that VS Code applies on pastereturn[{id: 'typescript.moveImports',command: <vscode.Command>{title: 'Move Imports',command: 'typescript.doMoveImports',args: [metadata]}}];}}
Instead of using commands, we could have have an applyPaste method on vscode.PasteActionProvider that would be invoked a paste.
So, the extended proposed is somewhat analogue to format on save which happens automatically and the onWillSave-event for less uniform cases, right?
A critical piece in this would be those clipboard events. E.g. when copying from a website and pasting into the editor, then I don't believe that we have a chance to know that copy has happened. Not even sure if we know that inside the editor, e.g when copy/pasting between two files...
If I understand correctly, in the "Paste Action Provider" example it looks like the metadata is collected during the paste operation:
// Get metadata about copied textconstmetadata=awaitgetMetadataForSelection(document,copiedRange);
I think that would fall down if a user copies text, the modifies it/deletes it/hits save (which formats). I think the metadata would have to be collected during the copy to be reliable (so the provider would probably need to have hooks for both copy and paste?).
Is this something that could also be supported for LSP? (if so, it may be worth considering how the API would translate to that).
Johannes Rieken (@jrieken) I originally thought this issue would be as simple as adding an editor.codeActionsOnPaste setting, but it turns out that that would not handle most of the interesting API use cases. For a use case such as moving imports around with copy/paste, the key part is that the operation is stateful. We can either leave managing this state up to extensions or go with a more provider based approach that allows VS Code to manage this state.
We can probably implement these concepts on desktop but I'm not sure about web. I think we should be able to use the copy and paste events, but these may trigger permissions dialogs.
Danny Tuppeny (@DanTup) The extension would need to save off the metadata in such a way that, on paste, the extension can handle any changes that happened since the copy
A provider based approach is a better fit for the LSP. The goal is to implement it in VS Code first, then add it to the LSP once it has been stabilized
Danny Tuppeny (@DanTup) The extension would need to save off the metadata in such a way that, on paste, the extension can handle any changes that happened since the copy
Yep, that's what I have in mind - but it needs the Copy event to do this too (the provider example above doesn't seem sufficient). My expectation is that when you Copy, the extension would collect a list of all imports required for that code, then when you paste, it would generate the correct code to import any that are not already imported in the new file (and account for relative paths). That shouldn't be affected by any changes happening in between.
Another use case for copy/paste is auto-escape string on paste. For example, pasting some text to a Java string literal, auto escape the characters such as \," etc.
The current PasteActionProvider seems to only consider copy/paste between editors. if copying a piece of content from outside (for example, copy a file path from File Explorer), is there a way to make the extension participate in the paste actions?
Reacted by Danny Tuppeny, Jakub Piskiewicz and Rome Li
If you run into any problems with the feature, please open a new issue with steps to reproduce and I'll take a look
1.96 also includes a number of improvements to the paste flow in general, such as the new editor.pasteAs.preferences setting for overriding how content should be pasted by default
The paste API proposal itself has been relatively stable recently but we also could use more extension author feedback on it. Please give it a try and let me know if you run into any issues/limitations. The paste extension sample is a good as a quick starting point to try it out
Tentatively planning to finalize the API in January since both the API and UX finally seem relatively complete. At this point we've also tested them using a number of our builtin languages and features too
Activity
DanTup commentedon Jul 6, 2017
It would actually work out better if we could also attach additional data into the clipboard during copy (to avoid there being a mismatch between the import data we stashed and the clipboard, which might have been replaced externally since) and get it back during paste; but I don't know enough about how clipboards work to know how feasible that is!
mjbvz commentedon Sep 18, 2020
Having copy/paste bring along imports is a good motivating use case for designing this api. Here is some of what makes it interesting:
Some goals:
Sketches
(note that these are not API proposals, just quick ideas on how this could work)
Source Actions
Try to reuse source actions for implement an
editor.codeActionsOnPastesetting. For example, the setting:Would run organize imports source action whenever the user pastes.
I don't see this as being enough on its own, except for super simple use cases such as organize imports. We need some way for extensions to store state from a copy and use that to produce a set of text edits on paste. Source actions don't provide enough infrastructure on their own
Extend clipboard api
Let extensions be notified of clipboard copy and paste events. Let extensions store metadata on the clipboard.
This leaves the implementation largely up to individual extensions. There are a few big open questions here too:
Paste Action Provider
Define a contract that extensions can implement to hook into copy/paste. This basically just adds a contract on top of the clipboard api sketch above:
Instead of using commands, we could have have an
applyPastemethod onvscode.PasteActionProviderthat would be invoked a paste.jrieken commentedon Sep 18, 2020
So, the extended proposed is somewhat analogue to format on save which happens automatically and the onWillSave-event for less uniform cases, right?
A critical piece in this would be those clipboard events. E.g. when copying from a website and pasting into the editor, then I don't believe that we have a chance to know that copy has happened. Not even sure if we know that inside the editor, e.g when copy/pasting between two files...
DanTup commentedon Sep 18, 2020
If I understand correctly, in the "Paste Action Provider" example it looks like the metadata is collected during the paste operation:
I think that would fall down if a user copies text, the modifies it/deletes it/hits save (which formats). I think the metadata would have to be collected during the copy to be reliable (so the provider would probably need to have hooks for both copy and paste?).
Is this something that could also be supported for LSP? (if so, it may be worth considering how the API would translate to that).
mjbvz commentedon Sep 18, 2020
Johannes Rieken (@jrieken) I originally thought this issue would be as simple as adding an
editor.codeActionsOnPastesetting, but it turns out that that would not handle most of the interesting API use cases. For a use case such as moving imports around with copy/paste, the key part is that the operation is stateful. We can either leave managing this state up to extensions or go with a more provider based approach that allows VS Code to manage this state.We can probably implement these concepts on desktop but I'm not sure about web. I think we should be able to use the
copyandpasteevents, but these may trigger permissions dialogs.Danny Tuppeny (@DanTup) The extension would need to save off the metadata in such a way that, on paste, the extension can handle any changes that happened since the copy
A provider based approach is a better fit for the LSP. The goal is to implement it in VS Code first, then add it to the LSP once it has been stabilized
DanTup commentedon Sep 19, 2020
Yep, that's what I have in mind - but it needs the Copy event to do this too (the provider example above doesn't seem sufficient). My expectation is that when you Copy, the extension would collect a list of all imports required for that code, then when you paste, it would generate the correct code to import any that are not already imported in the new file (and account for relative paths). That shouldn't be affected by any changes happening in between.
testforstephen commentedon Sep 21, 2020
Another use case for copy/paste is auto-escape string on paste. For example, pasting some text to a Java string literal, auto escape the characters such as
\,"etc.The current PasteActionProvider seems to only consider copy/paste between editors. if copying a piece of content from outside (for example, copy a file path from File Explorer), is there a way to make the extension participate in the paste actions?
94 remaining items
mjbvz commentedon Dec 12, 2024
Pasting with imports for JavaScript and TypeScript just shipped in 1.96: https://code.visualstudio.com/updates/v1_96#_paste-with-imports-for-javascript-and-typescript
If you run into any problems with the feature, please open a new issue with steps to reproduce and I'll take a look
1.96 also includes a number of improvements to the paste flow in general, such as the new
editor.pasteAs.preferencessetting for overriding how content should be pasted by defaultThe paste API proposal itself has been relatively stable recently but we also could use more extension author feedback on it. Please give it a try and let me know if you run into any issues/limitations. The paste extension sample is a good as a quick starting point to try it out
Tentatively planning to finalize the API in January since both the API and UX finally seem relatively complete. At this point we've also tested them using a number of our builtin languages and features too
Finalize paste api and drop api tweaks
Kaiyusa commentedon Feb 4, 2025
fisforfaheem commentedon Feb 4, 2025
RedCMD commentedon Feb 4, 2025
Faheem Ahmad (@fisforfaheem) Dart is not owned by Microsoft
you should query https://github.com/Dart-Code/Dart-Code instead
DanTup commentedon Feb 4, 2025
There's an open issue for implementing this in Dart at Dart-Code/Dart-Code#351, however it first requires adding to LSP (see microsoft/language-server-protocol#2072), which first requires it finalized and shipped in VS Code.
fisforfaheem commentedon Feb 4, 2025