Skip to content

Extension API request: onCopy and onPaste #43718

Closed

Description

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.

Activity

  1. jrieken commented on Feb 27, 2018

    @jrieken
    Contributor

    However, sometimes vscode doesn't call extension commands,

    Can you specify that?

  2. stringham commented on Feb 27, 2018

    @stringham
    ContributorAuthor

    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.

  3. jrieken commented on Feb 28, 2018

    @jrieken
    Contributor

    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?

  4. stringham commented on Feb 28, 2018

    @stringham
    ContributorAuthor

    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.

  5. jrieken commented on Feb 28, 2018

    @jrieken
    Contributor

    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?

  6. stringham commented on Feb 28, 2018

    @stringham
    ContributorAuthor

    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.

  7. jrieken commented on Feb 28, 2018

    @jrieken
    Contributor

    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

  8. stringham commented on Feb 28, 2018

    @stringham
    ContributorAuthor

    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?

  9. jrieken commented on Mar 1, 2018

    @jrieken
    Contributor

    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

  10. stringham commented on Mar 2, 2018

    @stringham
    ContributorAuthor

    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?

  11. DanTup commented on Mar 12, 2018

    @DanTup
    Contributor

    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.

  12. shepelevstas commented on Apr 7, 2018

    @shepelevstas

    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.

  13. added
    feature-requestRequest for new features or functionality
    and removed
    info-neededIssue requires more information from poster
    on Jul 27, 2018
  14. jrieken commented on Jul 23, 2019

    @jrieken
    Contributor

    /duplicate of #30066

  15. added
    *duplicateIssue identified as a duplicate of another issue(s)
    on Jul 23, 2019
  16. vscodebot commented on Jul 23, 2019

    @vscodebot

    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!

  17. locked and limited conversation to collaborators on Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

*duplicateIssue identified as a duplicate of another issue(s)apifeature-requestRequest for new features or functionality

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions