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

Extension API request: onCopy and onPaste #43718

Closed
stringham opened this issue Feb 15, 2018 · 14 comments
Closed

Extension API request: onCopy and onPaste #43718

stringham opened this issue Feb 15, 2018 · 14 comments
Assignees
Labels
api *duplicate Issue identified as a duplicate of another issue(s) feature-request Request for new features or functionality

Comments

@stringham
Copy link
Contributor

stringham commented Feb 15, 2018

I've created an extension which automatically brings imports when you copy and paste between files. However, in order to do this I had to override the default cut/copy/paste hotkeys.

In each of my commands I call the default editor.action.clipboard*Action and then do the things specific to my plugin. Here's the relevant snippet:

    context.subscriptions
        .push(vscode.commands.registerTextEditorCommand('copy-with-imports.copy', (editor: vscode.TextEditor, edit) => {
            vscode.commands.executeCommand('editor.action.clipboardCopyAction');
            saveLastCopy(vscode.window.activeTextEditor);
        }));

    context.subscriptions
        .push(vscode.commands.registerTextEditorCommand('copy-with-imports.cut', (editor: vscode.TextEditor, edit) => {
            saveLastCopy(vscode.window.activeTextEditor);
            vscode.commands.executeCommand('editor.action.clipboardCutAction');
        }));

    context.subscriptions.push(vscode.commands.registerTextEditorCommand('copy-with-imports.paste', (editor, edit) => {
        ...
        vscode.commands.executeCommand('editor.action.clipboardPasteAction').then(() => {

However, sometimes vscode doesn't call extension commands, which makes my extension break copy/paste.

I would like to either be able respond to the built in commands rather than overriding the hotkeys, or allow the default hotkey to run if vscode decides to skip my extension defined command.

@jrieken jrieken added info-needed Issue requires more information from poster api labels Feb 27, 2018
@jrieken
Copy link
Member

jrieken commented Feb 27, 2018

However, sometimes vscode doesn't call extension commands,

Can you specify that?

@stringham
Copy link
Contributor Author

I don't know under which circumstances vscode doesn't run commands defined in extensions, sometimes it seems to be random and if I reload the window it starts to work again.

One place I've found it to be very reproducible is when you open very large documents. Even if you do ctrl+a and then delete, the hotkeys that should be running commands defined in extensions don't work.

For example, download and uncompress this file:
large-file.json.gz

open it in vscode and none of the hotkeys that I have hooked up to extensions work. With copy with imports installed I cannot copy/paste with this file, and with Cursor move installed, ctrl+left, ctrl+right do not move the cursor.

@jrieken
Copy link
Member

jrieken commented Feb 28, 2018

One place I've found it to be very reproducible is when you open very large documents.

Yeah, that's the designed behaviour because for performance reasons we don't let extensions know about very large documents.

hotkeys that I have hooked up to extensions work.

When you say that do you mean nothing happens or that the extension behaviour doesn't happen?

@stringham
Copy link
Contributor Author

I've defined a hotkey to call my extension's version of copy/paste when you do ctrl+c/ctrl+v but since my extension commands aren't called, and I've overridden the default hotkey, nothing happens when those hotkeys are pressed.

So that makes my extension sometimes completely break copy/paste. This is a very poor experience and I'd like some kind of work around.

The ideas I can think of are either:

  1. let me subscribe to when copy/cut/paste happen so I can enhance those features with my extension but not override the default hotkeys.

  2. Have some way to conditionally override hotkeys. If vscode decides not to give my extension access to the file then fallback to the default hotkey. This would probably need to be opt-in when registering the command, give the fallback built in command to call when the extension is disabled by vscode.

@jrieken
Copy link
Member

jrieken commented Feb 28, 2018

Ok, when dealing with large documents (>50MB) that's a known limitation. Are there other scenarios you know when your command should run but doesn't?

@stringham
Copy link
Contributor Author

It happens periodically, it seems to be when the vscode process is using high CPU. However, I haven't been able to consistently reproduce it.

Copy/paste will break, I'll reload vscode and it will work again.

@jrieken
Copy link
Member

jrieken commented Feb 28, 2018

it seems to be when the vscode process is using high CPU.

Likely when an extension goes crazy and taking away all compute from other extensions. When that happens, please profile running extensions. Do so by pressing F1 > Running Extensions > Hit the Red button: https://github.com/Microsoft/vscode/wiki/Performance-Issues#profile-the-running-extensions

@stringham
Copy link
Contributor Author

Right, so I can do that to figure out which my the extensions I am running may be using the compute power. But this isn't a reasonable workaround for my extension to perform properly for other people.

There ought to be a way for my extension to have a degraded condition where it still allows basic editing abilities (copy/paste) even when other extensions are going crazy.

Thoughts?

@jrieken
Copy link
Member

jrieken commented Mar 1, 2018

There ought to be a way for my extension to have a degraded condition where it still allows basic editing

Yeah, the unfortunate reality of JavaScript. What we can do is detect that a command wanted to run but that the request never made it over the wire. We can then show a message and our assumption is that such a case is rare. What we cannot do is run another command because it is hard for us to figure-out what other command is appropriate

@stringham
Copy link
Contributor Author

So I'm proposing a couple of options that would make my use case work:

  1. add an API like vscode.workspace.onBeforeCommand(commandName, handler) and vscode.workspace.onAfterCommand(commandName, handler) where I could respond to the built in command running. Then I don't have to override the default hotkeys. This is probably the ideal solution.

  2. when I register a command, I could supply a fallback built in command to run in the case that vscode fails to call my extension (whether because the file is too big or another extension is using too much cpu.)

Do you think either of these would work?

@DanTup
Copy link
Contributor

DanTup commented Mar 12, 2018

I requested the same thing in #30066. My analysis server can bring imports with copy/pastes but currently I can't participate in the actions :(

Overriding hotkeys doesn't seem like a good solution since users can change them.

@shepelevstas
Copy link

shepelevstas commented Apr 7, 2018

Option №1 makes more sense, I would want it too! I also made an extension that overrides the native copy and cut commands. That means I can not use any other extension that is intended to work on copy and cut commands execution. Attaching a handler to native command instead of overriding it would solve the problem.

@jrieken jrieken added feature-request Request for new features or functionality and removed info-needed Issue requires more information from poster labels Jul 27, 2018
@jrieken
Copy link
Member

jrieken commented Jul 23, 2019

/duplicate of #30066

@vscodebot vscodebot bot added the *duplicate Issue identified as a duplicate of another issue(s) label Jul 23, 2019
@vscodebot
Copy link

vscodebot bot commented Jul 23, 2019

Thanks for creating this issue! We figured it's covering the same as another one we already have. Thus, we closed this one as a duplicate. You can search for existing issues here. See also our issue reporting guidelines.

Happy Coding!

@vscodebot vscodebot bot closed this as completed Jul 23, 2019
@vscodebot vscodebot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api *duplicate Issue identified as a duplicate of another issue(s) feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

4 participants