Skip to content

configurable run command in R terminal? #199

Closed

Description

@MilesMcBain

Is your feature request related to a problem? Please describe.

The R extension has keybindings for some key R functions like nrow, devtools::test() etc. It would be nice to be able to configure our own keybindings that run code in the active R terminal.

Describe the solution you'd like

A command added to the extension like this:

    async function runCommandInRTerm({ text }: { text: string }) {
        const callableTerminal = await chooseTerminal();
        if (callableTerminal === undefined) {
            return;
        }
        callableTerminal.sendText(text);
    }

Allows keybinding config like this to work:

[
  {
    "description": "run drake::r_make()",
    "key": "ctrl+;",
    "command": "r.runCommandInRTerm",
    "when": "editorTextFocus",
    "args": {
      "text": "drake::r_make()\n"
    }
  }
]

Describe alternatives you've considered

Adding commands directly to the extension via PRs - seems unlikely to scale well and create unnecessary maintenance overhead for niche use cases.

Activity

  1. andycraig commented on Jan 22, 2020

    @andycraig
    Collaborator

    Hi @MilesMcBain, there’s a function chooseTerminalAndSendText that does this, but at the moment it’s not exposed in the "commands" section of package.json so it can’t be directly accessed by the user.

    I think the necessary change should be only a few lines, so I’ll try to do a PR tonight. (Or feel free to do one earlier if you’d like!)

  2. MilesMcBain commented on Jan 22, 2020

    @MilesMcBain
    CollaboratorAuthor

    I tried writing a function that was just a rename of chooseTerminalAndSendText but I ran into issues passing args to commands in keybindings.

    See how my version takes { text }: { text: string }?. This was the only way I could get it to work. Otherwise it would silently fail before executing the command. I'm a total VSCode/Typescript noob though so I could easily believe I was missing something.

  3. andycraig commented on Jan 22, 2020

    @andycraig
    Collaborator

    Ah, you were ahead of me :) Let me have a look and see if I can figure out the args issue.

  4. self-assigned this
    on Jan 22, 2020
  5. andycraig commented on Jan 23, 2020

    @andycraig
    Collaborator

    It seems to work if chooseTerminalAndSendText is exposed in package.json provided only the value of args is specified:

    [
      {
        "description": "run drake::r_make()",
        "key": "ctrl+;",
        "command": "r.chooseTerminalAndSendText",
        "when": "editorTextFocus && editorLangId == 'r'",
        "args": "drake::r_make()"
      }
    ]
    

    I should be able to do a PR for this on Sunday, but feel free to do one in the meantime if you want.

  6. MilesMcBain commented on Jan 24, 2020

    @MilesMcBain
    CollaboratorAuthor

    I'll have a go.

    Another idea I had is a variant of this that take the current word or selection and sends it as an arg.

    Would enable stuff like:

    [
      {
        "description": "loadd target under cursor",
        "key": "ctrl+l",
        "command": "r.somethingSomethingWithSelectionOrWord",
        "when": "editorTextFocus && editorLangId == 'r'",
        "args": "drake::loadd"
      }
    

    Thoughts on that one?

  7. andycraig commented on Jan 24, 2020

    @andycraig
    Collaborator

    @MilesMcBain Great, thank you!

    Another idea I had is a variant of this that take the current word or selection and sends it as an arg.

    Definitely. I've split this off into its own issue.

    You might also be interested in #183

  8. andycraig commented on Jan 26, 2020

    @andycraig
    Collaborator

    @MilesMcBain At the risk of bikeshedding, I think I prefer the idea of the parameter being named, as in your original version.

    I have a PR #203 for #183 that I’ll try to change to use named arguments too. I’ll sort that out and then let’s do this one.

  9. renkun-ken commented on Feb 22, 2020

    @renkun-ken
    Member

    Closed by #237.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions