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

Add actions for executing code #2684

Merged
merged 4 commits into from
Apr 16, 2024
Merged

Add actions for executing code #2684

merged 4 commits into from
Apr 16, 2024

Conversation

jmcphers
Copy link
Collaborator

@jmcphers jmcphers commented Apr 5, 2024

Intent

Make it possible to create keybindings that run code in Positron. This feature has been requested frequently by both private beta and internal beta users.

Addresses #2375
Addresses #2661

Approach

There are two new commands here; they have arguments and semantics that are different enough to warrant the bifurcation.

workbench.action.executeCode.console - executes code as if the user typed it. This is useful for keybindings that are used more like macros.

workbench.action.executeCode.silently - executes code silently. This is useful for code that is run for its side effects (e.g. making API calls).

Both commands accept an argument object that can specify langId and code fields. They can also accept just a plain string as an argument, in which case the code will be run in whatever console happens to be active.

QA Notes

Here is an example keybinding to start playing with this:

    {
        "key": "Alt+Shift+E",
        "command": "workbench.action.executeCode.console",
        "description": "Print Elbereth",
        "when": "editorTextFocus",
        "args": {
            "langId": "python", 
            "code": "print('Elbereth')",
            "focus": true
        }
    }

Try:

  • executing silently and observing side effects (note that many side effects are not visible until you run something else in the console since that's when most kernels check for changes)
  • specifying the args as a plain string

@jmcphers jmcphers requested a review from jennybc April 5, 2024 22:22
@jmcphers jmcphers force-pushed the feature/execute-code-actions branch from bfd8857 to 9169c36 Compare April 5, 2024 22:31
@jmcphers jmcphers force-pushed the feature/execute-code-actions branch from 9169c36 to 55445a6 Compare April 5, 2024 22:32
Copy link
Contributor

@lionel- lionel- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's going to be so useful!

});

/**
* Execute Code Silently: executes code, but doesn't show it to the user.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc: This action executes code immediately after the currently running command (if any) has finished. It has priority over the queue of pending console inputs from the user but still needs to wait until the current command is finished, which might take a long time.

Any output (messages, warnings, or errors) generated by this command is discarded silently instead of being shown in the console.

@jennybc
Copy link
Member

jennybc commented Apr 9, 2024

I'm playing with this now and want to leave a few more notes for test driving:

  1. Open the Command Palette.
  2. Type "Open Keyboard Shortcuts" and select "Open Keyboard Shortcuts (JSON)". Make sure to not accidentally open the JSON for the default keyboard shortcuts (ask me how I know).
  3. Paste the keybinding snippet above at the end of the list (for me it's the only entry at this point), making sure it's comma-separated from the previous entries.

I am also in some unhappy state w.r.t. Python right now and the above snippet works simply by substituting 'r' for 'python' in langId. Important to note the when clause that it only fires when an editor is focused.

@jennybc
Copy link
Member

jennybc commented Apr 9, 2024

I just had a lovely time with this:

{
        "key": "Cmd+Shift+R",
        "command": "workbench.action.executeCode.console",
        "when": "editorTextFocus",
        "args": {
            "langId": "r",
            "code": "reprex::reprex_selection()",
            "focus": true
        }
    }

This gives me an experience similar to what I enjoy in RStudio, where (1) reprex::reprex_selection() has to be registered as an addin, so that I can (2) customize a keybinding for it.

@jennybc
Copy link
Member

jennybc commented Apr 9, 2024

Possibly silly example, but this works for an active R console (didn't try Python due to unhappy situation mentioned above):

{
        "key": "Cmd+Shift+H",
        "command": "workbench.action.executeCode.console",
        "when": "editorTextFocus",
        "args": "1 + 1"
}

@jennybc
Copy link
Member

jennybc commented Apr 9, 2024

Here's a good example for experiencing workbench.action.executeCode.silently (but also probably a terrible example of code that you would want to use this for):

{
        "key": "Cmd+Shift+J",
        "command": "workbench.action.executeCode.silently",
        "when": "editorTextFocus",
        "args": {
            "langId": "r",
            "code": "options(digits = 2)"
        }
}

Here's me printing pi before and after invoking the keyboard shortcut:

> pi
[1] 3.141593
> pi
[1] 3.1

Copy link
Member

@jennybc jennybc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few straightforward experiments worked and I think this is great!

Because of the example I chose (messing with significant digits), I used the "Restart console" button in the R console, which dutifully restarted the console. But now when I invoke the keyboard shortcut (Cmd + Shift + J, in the case of that example), I see this

Screenshot 2024-04-09 at 11 22 18 AM

But there is definitely an active R console. The other keybindings are still working, which suggests the problem is specific to workbench.action.executeCode.silently.

@jmcphers
Copy link
Collaborator Author

But there is definitely an active R console.

I spent some time trying to reproduce this and found that this is a bug in the code that tracks console sessions; fixed in 7b08b7b.

@jmcphers jmcphers merged commit 1ce5697 into main Apr 16, 2024
1 check passed
@jmcphers jmcphers deleted the feature/execute-code-actions branch April 16, 2024 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants