vscode-R icon indicating copy to clipboard operation
vscode-R copied to clipboard

Suggestion to add r.str function

Open ipstone opened this issue 1 year ago • 3 comments

It's really great/encouraging to see vscode-R grows and progressed so much and fast!

Coming from nvim-R extension in the vim world, I really hope to remap vim style keybindings to R function str, to inspect R object.

However, I found that for r.xyzfunctions configured in the https://github.com/REditorSupport/vscode-R/blob/9c7f4a217b8cfddd87d9f93489bae09b0923bc8b/src/extension.ts#L71

  • It's quite easy to remap these functions to key combinations, such as '; r; h ' (which is press leader key such as comma, then r, then h) to show the header of the object
 "vim.normalModeKeyBindingsNonRecursive": [
{
            "before": [
                ",",
                "r",
                "h"
            ],
            "commands": [
                "r.head",
            ],
            "when": "editorTextFocus && editorLangId == 'r'",
        },

but when I try to do so for str function, it would not work. (On the other hand, for str function, when using a traditional keybinding such as the following, it works fine:

    {
        "description": "show R object structure",
        "key": "ctrl+shift+t",
        "command": "r.runCommandWithSelectionOrWord",
        "when": "editorTextFocus",
        "args": "str($$)"
    },

I am wondering whether we can add str function directly to the extension, at the line: https://github.com/REditorSupport/vscode-R/blob/9c7f4a217b8cfddd87d9f93489bae09b0923bc8b/src/extension.ts#L71 such as

'r.str': () => rTerminal.runSelectionOrWord(['str']),

This is really a very detailed small point, but I think it might provide value for the light weight way of inspecting an object, which allows a vim style key binding to work. Thanks

ipstone avatar Sep 12 '23 03:09 ipstone

No maintainer, and this does not directly answer your question. But I use Vspacecode, which has lots of great keybinding predefined https://github.com/VSpaceCode/VSpaceCode Unfortunately, it does not have a R major mode yet, so I created some key bindings in the lasts days https://github.com/VSpaceCode/VSpaceCode/pull/332

This included str and some others, which are quite easy to implement

                                            {
                                                "key": "v",
                                                "name": "+View R",
                                                "icon": "preview",
                                                "type": "bindings",
                                                "bindings": [
                                                    {
                                                        "key": "c",
                                                        "name": "Column numbers",
                                                        "type": "command",
                                                        "icon": "list-ordered",
                                                        "command": "r.runCommandWithSelectionOrWord",
                                                        "args": "ncol($$)"
                                                    },
                                                    {
                                                        "key": "h",
                                                        "name": "head",
                                                        "type": "command",
                                                        "icon": "list-filter",
                                                        "command": "r.head"
                                                    },
                                                    {
                                                        "key": "l",
                                                        "name": "length",
                                                        "type": "command",
                                                        "icon": "list-ordered",
                                                        "command": "r.length"
                                                    },
                                                    {
                                                        "key": "n",
                                                        "name": "Names",
                                                        "type": "command",
                                                        "icon": "symbol-parameter",
                                                        "command": "r.names"
                                                    },
                                                    {
                                                        "key": "p",
                                                        "name": "print",
                                                        "icon": "symbol-field",
                                                        "type": "command",
                                                        "command": "r.runCommandWithSelectionOrWord",
                                                        "args": "$$"
                                                    },
                                                    {
                                                        "key": "r",
                                                        "name": "Row numbers",
                                                        "type": "command",
                                                        "icon": "list-ordered",
                                                        "command": "r.nrow"
                                                    },
                                                    {
                                                        "key": "s",
                                                        "name": "str",
                                                        "type": "command",
                                                        "icon": "list-flat",
                                                        "command": "r.runCommandWithSelectionOrWord",
                                                        "args": "str($$)"
                                                    },
                                                    {
                                                        "key": "v",
                                                        "name": "View",
                                                        "type": "command",
                                                        "icon": "preview",
                                                        "command": "r.view"
                                                    },

image

Until now the pull request is not merged, so you have to use this guide: https://github.com/VSpaceCode/VSpaceCode/blob/master/CONTRIBUTING.md

mihem avatar Sep 14 '23 11:09 mihem

thanks @mihem

Do you mind briefly describing how your function/keybinding was implemented in VSpaceCode?

 "command": "r.runCommandWithSelectionOrWord",
                                                        "args": "str($$)"

Is the above all needed to make it working? I am just wondering if I can do the same in vscode-R , with similar configurations. When I use the following, it doesn't send the selected object to the terminal as 'str' function call:

 "vim.normalModeKeyBindingsNonRecursive": [
{
            "before": [
                ",",
                "r",
                "t"
            ],
            "commands": [
                "r.runSelectionOnWord"
            ],
            "when": "editorTextFocus && editorLangId == 'r'",
	    "args": "str($$)"
        },

ipstone avatar Nov 29 '23 22:11 ipstone

Looking at vscode-R source code, I think we can add some r functions here: https://github.com/REditorSupport/vscode-R/blob/54f924cd2efa053cb6615903aaa8952f28405f11/src/extension.ts#L68

such that

'r.str': () => rTerminal.runSelectionOrWord(['str']),

I haven't worked on vscode plugin, will see how to do this, and then perhaps open a pull request. Any suggestions/tips would be appreciated.

ipstone avatar Nov 29 '23 23:11 ipstone

I found solutions for this:

  • instead of tweak the keybindings (add keybindings) through vim extension, I can directly add the keybinding as the following in the keybindings.json setup for vscode:
    {
        "key": ", r t",
        "description": "show R object structure",
        "command": "r.runCommandWithSelectionOrWord",
        "args": "str($$)",
        "when": "editorTextFocus && editorLangId == 'r' && vim.mode != 'Insert'"
    },

This works well as intended, when in vim.mode (not in insert mode under vim), and language is R, then typing in the fast sequence of ',' 'r', 't' : it will send the object to R terminal, and run the str() command.

I am closing this issue now.

ipstone avatar May 23 '24 21:05 ipstone