REditorSupport / vscode-R

R Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=REditorSupport.r
MIT License
1.07k stars 128 forks source link

provide rstudio-like keymaps #551

Open maxheld83 opened 3 years ago

maxheld83 commented 3 years ago

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:


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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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.