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

provide rstudio-like keymaps #551

Open
maxheld83 opened this issue Feb 9, 2021 · 8 comments
Open

provide rstudio-like keymaps #551

maxheld83 opened this issue Feb 9, 2021 · 8 comments

Comments

@maxheld83
Copy link

Describe the solution you'd like

I'd love it if there was a way to quickly get the key bindings that I (and I suspect many others) have muscle-memoried from RStudio.

Caveats:

  • This should only cover R-specific keybindings, such as for devtools::load_all() or R CMD check and not the general-purpose IDE/Editor bindings (i.e. CTRL+1 for "move to source editor".
    VSCode is a different beast and has a completely different design, so it doesn't make sense for it to behave like RStudio all around.
    As a heuristic, only those keybindings should be enabled which only make sense inside R projects.
    If there's already a native, cross-language VSCode shortcut, that should take precedence.
  • Consequently, this should only trigger when "when": "editorLangId == r || editorLangId == rmd && editorTextFocus".
  • Some default VSCode keybindings would have to be overwritten (see below).
  • The whole thing could maybe be enabled as an option in the setting.

Here's what I have so far (using tasks as per #59):

    {
        "description": "Insert <-",
        "key": "alt+-",
        "command": "type",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "args": {
            "text": "<- "
        }
    },
    {
        "description": "Insert %>%",
        "key": "ctrl+shift+m",
        "command": "type",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "args": {
            "text": "%>% "
        }
    },
    {
        // "description": "Disable (overloaded with above)",
        "key": "ctrl+shift+m",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "command": "-workbench.actions.view.problems"
    },
    {
        "description": "Load all", 
        "key": "cmd+shift+l",
        "command": "r.runCommand",
        "when": "editorLangId == r || editorLangId == rmd",
        "args": "devtools::load_all()"
    },
    {
        "description": "Test",
        "key": "cmd+shift+t",
        "command": "workbench.action.tasks.runTask",
        "when": "editorLangId == r || editorLangId == rmd",
        "args": "Test"
    },
    {
        "description": "Document",
        "key": "cmd+shift+d",
        "command": "workbench.action.tasks.runTask",
        "when": "editorLangId == r || editorLangId == rmd",
        "args": "Document"
    },
    {
        "description": "Check",
        "key": "cmd+shift+e",
        "command": "workbench.action.tasks.runTask",
        "when": "editorLangId == r || editorLangId == rmd",
        "args": "Check"
    },
@maxheld83
Copy link
Author

maxheld83 commented Feb 11, 2021

Is there a better way to identify whether you're currently "in an R project", other than
"when": "editorLangId == r || editorLangId == rmd"?
Maybe when there's a DESCRIPTION at project root?
Is there already such a facility inside vscode-R?

editorLangId == r || editorLangId == rmd && editorTextFocus" doesn't quite create the RStudio experience. Whenever you're in a terminal (no way of knowing whether it's running radian at any point), or one of the non-R files (say, DESCRIPTION, README.md, ...) these keybindings won't fire.

@ManuelHentschel
Copy link
Member

Is there a better way to identify whether you're currently "in an R project", other than
"when": "editorLangId == r || editorLangId == rmd"?

It's possible to set arbitrary context values from within the extension. This is done e.g. to only show help panel related navigation buttons when the active panel is actually an R help panel. See e.g.
https://github.com/Ikuyadeu/vscode-R/blob/39f5914d691a3befb04768e4df33b7f83978fd44/package.json#L709
https://github.com/Ikuyadeu/vscode-R/blob/39f5914d691a3befb04768e4df33b7f83978fd44/src/rHelpPanel.ts#L113
https://code.visualstudio.com/api/references/when-clause-contexts#add-a-custom-when-clause-context

This could be used to e.g. set a context value r.isRProject that is true whenever we identify the current project as an R project, or r.terminalIsR to indicate that the currently active terminal window is one opened by the extension.

@bersbersbers
Copy link

bersbersbers commented Mar 9, 2021

I found this issue because I fell for an unexpected key mapping collision: in RStudio, Ctrl+Shift+S runs the source, while that combination in VS Code (and many other tools, compare https://defkey.com/what-means/ctrl-shift-s) is usually used for "File - Save As". So instead of re-saving my file, I accidentally ran some R code in VS Code due to
https://github.com/Ikuyadeu/vscode-R/blob/abd2392d7ef02ea5151b26004368fdedfcc979f4/package.json#L651-L656

In general, I am not sure it is beneficial to emulate RStudio behavior at all. RStudio is only for people working (mostly) with R. VS Code is not only for people who mostly use R, but people also use JavaScript, Python, etc. - often several languages across the same project. So I think I would put more focus on being consistent within the VS Code environment rather than compatibility with RStudio. Please consider that if and when revising any keyboard shortcuts (including Ctrl+Shift+S). It's easy for me to unmap*, but new users will always fall for that.

*How? Put this in your keybindings.json:

    {
        "key": "ctrl+shift+s",
        "command": "-r.runSource",
        "when": "editorTextFocus && editorLangId == 'r'"
    },

@malcook
Copy link

malcook commented Mar 9, 2021

@bersbersbers cogently make a point I too arrived at after at first being delighted to find emacs keybinding emulation and then throwing up my hands in frustration as I found they introduced conflicts with other things I wanted, including the ability to follow along others' VS Code tutorials & recipes that were given in terms of standard bindings.

Of course, TIMTOWTDI.

Or, À chacun son goût.

Or YMMV.

For me, I'm hoping if/when notebooks allow mixing cells between different languages/kernels, someone will think through how to be consistent in bindings between them. This will be a stickier wicket. Living in emacs org-mode now with poor consistency in this regard is a bit of a pain.

(mostly lurking, but thanks for vscode-R)

@andycraig
Copy link
Collaborator

@maxheld83 Thank you for the suggestion! I'm inclined to agree with @bersbersbers and @malcook that these shouldn't be defaults. A setting to enable RStudio-like keymaps (disabled by default) is an option although someone would need to volunteer to implement it.

We have a keybindings page in the Wiki: https://github.com/Ikuyadeu/vscode-R/wiki/Keyboard-shortcuts We could add the suggested keybindings to an RStudio-like keymaps section there? Then users who want them can do a quick copy-paste into their keybindings.json.

@bersbersbers
Copy link

bersbersbers commented Mar 13, 2021

We could add the suggested keybindings to an RStudio-like keymaps section there? Then users who want them can do a quick copy-paste into their keybindings.json.

You could also add a default-false setting such as config.latex-workshop.bind.altKeymap.enabled in LaTeX Workshop and scope two sets of key bindings to that. That's how they implement it:
https://github.com/James-Yu/LaTeX-Workshop/blob/f6016f6dd15992894935545e25ef1a6a2b6ef411/package.json#L481-L577

I think this is a great idea in general, and it would allow maintaining one (default) set of key bindings with maximum compatibility to VS Code, and another for RStudio users that can be enabled using a single setting.

@krlmlr
Copy link
Contributor

krlmlr commented Mar 17, 2021

The flymaps extension provides a keymap for Alt+- and Ctrl+Shift+M, a good start I think. Continuing the keymap development there would allow this extension to be installed and perhaps configured separately.

https://github.com/ShixiangWang/flymaps

@andycraig
Copy link
Collaborator

Doing this via the flymaps extension sounds fine, and adding a setting to switch on RStudio-like keymaps sounds fine too. I'll mark this as help wanted - volunteers welcome.

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

No branches or pull requests

6 participants