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

Send code to terminal by `Ctrl + ENTER` but don't run #820

Open Yunuuuu opened 2 years ago

Yunuuuu commented 2 years ago

Describe the bug When I send code to terminal by Ctrl + ENTER, it just send code but don't run code, and we should move focus to terminal and input ENTER manually to run the code.

To Reproduce Steps to reproduce the behavior:

  1. create a terminal
  2. run this code below:
    
    library(IlluminaHumanMethylation450kanno.ilmn12.hg19)  
    ## sorry for this, I have tried other code which don't rely on any package data to reproduce this problem but it failed.
    ## this package is from bioconductor
    ## so we should install it by  BiocManager::install("IlluminaHumanMethylation450kanno.ilmn12.hg19")

library(tidyverse)

After loading packages, try this code below `several times`, the frequency of problem is almost  1/2, so it won't be hard to reproduce this problems

probe_with_one_more_gene <- Other$UCSC_RefGene_Name %>% purrr::map(~ unique(str_split(., pattern = ";")[[1]])) %>% purrr::map_lgl( ~ length(.) >= 2 ) %>% { # nolint rownames(Other)[.] }

3.  See error (it seems vscode-r just run code before line `rownames(Other)[.]` and after code wasn't run, as we could see a character `+` after the line `rownames(Other)[.]` which indicates the line before should have been run.)
![image](https://user-images.githubusercontent.com/19575010/136897839-352e41ed-2dbb-4bde-a442-30b67604ba19.png)

if we don't run it manully, we can even delete code like this.
![image](https://user-images.githubusercontent.com/19575010/136897869-f525b99d-3ebd-42b4-b13c-488cc0cdc0de.png)

The problems only occured in `radian` terminal no matter I turn on or off `r.bracketedPaste` setting. the code run by `raw r terminal` is like this which looks like raw r terminal run code line by line as indicated by the character `+`.
![image](https://user-images.githubusercontent.com/19575010/136897267-bf42bc31-8c2b-4a5b-8764-0295fc925c0e.png)

## Do you want to fix by self? (We hope your help!)

Yes, I want to be helpful.

## (If yes,) what kind of help do you want? (e.g. Which file should I fix, Survey (related documents)

(If related)setting.json

```json
    "[r]": {
        "editor.rulers": [
            80 
        ],
        "editor.wordWrap": "bounded",
        "editor.wordSeparators": "`~!@#%$^&*()-=+[{]}\\|;:'\",<>/?",
        "rewrap.autoWrap.enabled": true,
        "rewrap.wrappingColumn": 80,
        "rewrap.doubleSentenceSpacing": true
    },
    "r.alwaysUseActiveTerminal": true,
    // "r.bracketedPaste": true,
    "r.rpath.windows": "C:\\Yun\\R\\R-4.1.1\\bin\\x64\\R.exe",
    "r.rterm.windows": "C:\\Users\\****\\AppData\\Local\\Programs\\Python\\Python39\\Scripts\\radian.exe",
    // "r.rterm.windows": "C:\\Yun\\R\\R-4.1.1\\bin\\x64\\Rterm.exe",
    "r.rterm.option": [
        "--no-save",
        "--no-restore",
        "--local-history",
        "--r-binary=C:\\Yun\\R\\R-4.1.1\\bin\\x64\\R.exe"
    ],
    "r.plot.useHttpgd": true,
    "r.session.levelOfObjectDetail": "Detailed",
    "r.session.objectLengthLimit": 100,
    "r.session.viewers.viewColumn": {
        "plot": "Active",
        "viewer": "Active",
        "view": "Active",
        "helpPanel": "Active"
    }

Expected behavior Run code directly when sending code by typing Ctrl + ENTER

Environment (please complete the following information):

Additional context If we could run code line by line like raw r terminal when sending code?

renkun-ken commented 2 years ago

I tried sending your code to R without bracket paste mode and sending it to radian with bracket paste mode. Both work as expected.

Yunuuuu commented 2 years ago

Thanks for your quickly reply, I came across it one time in every two times run of this code:

probe_with_one_more_gene <- Other$UCSC_RefGene_Name %>%
  purrr::map(~ unique(str_split(., pattern = ";")[[1]])) %>%
  purrr::map_lgl(
    ~ length(.) >= 2
  ) %>%
  { # nolint
    rownames(Other)[.]
  }

I don't know what's the matter, it agains

image

renkun-ken commented 2 years ago

Do you select the code and send it or put the cursor in the beginning of the code and send it?

Yunuuuu commented 2 years ago

I just put the cursor in the beginning of the code sometimes in the median of the first line of the code block and type Ctrl + ENTER

renkun-ken commented 2 years ago

What if you remove # nolint and try again?

Yunuuuu commented 2 years ago

Thanks for your help, it remains here

image

and I can delete code after character +. image

Yunuuuu commented 2 years ago

I move the curly braces to the last line or remove it, it remains here:

image

image

renkun-ken commented 2 years ago

Looks like it is a Windows-specific issue where radian vscode terminal does not support bracket paste mode (is it still true today?). It does not occur on macOS and Linux.

@randy3k Maybe it is because radian does not handle such expressions well on Windows?

renkun-ken commented 2 years ago

What if you select the code and send it?

Yunuuuu commented 2 years ago

It remains here and I can delete code after character +

image

image

image

Yunuuuu commented 2 years ago

here is my .radian_profile:

# Do not copy the whole configuration, just specify what you need!
# see https://help.farbox.com/pygments.html
# for a list of supported color schemes, default scheme is "native"
options(radian.color_scheme = "monokai")

# either  `"emacs"` (default) or `"vi"`.
# options(radian.editing_mode = "emacs")

# indent continuation lines
# turn this off if you want to copy code without the extra indentation;
# but it leads to less elegent layout
options(radian.indent_lines = FALSE)

# auto match brackets and quotes
options(radian.auto_match = FALSE)

# highlight matching bracket
options(radian.highlight_matching_bracket = FALSE)

# auto indentation for new line and curly braces
options(radian.auto_indentation = TRUE)
options(radian.tab_size = 4)

# pop up completion while typing
options(radian.complete_while_typing = TRUE)
# the minimum length of prefix to trigger auto completions
options(radian.completion_prefix_length = 1)
# timeout in seconds to cancel completion if it takes too long
# set it to 0 to disable it
options(radian.completion_timeout = 0.05)
# add spaces around equals in function argument completion
options(radian.completion_adding_spaces_around_equals = TRUE)

# automatically adjust R buffer size based on terminal width
options(radian.auto_width = TRUE)

# insert new line between prompts
options(radian.insert_new_line = TRUE)

# the filename that local history is stored, this file would be used instead of
# `radian.global_history_file` if it exists in the current working directory
options(radian.local_history_file = ".radian_history")

# allows user defined shortcuts, these keys should be escaped when send through the terminal.
# In the following example, `esc` + `-` sends `<-` and `esc` + `m` sends `%>%`.
# Note that in some terminals, you could mark `alt` as `escape` so you could use `alt` + `-` instead.
options(radian.escape_key_map = list(
    list(key = "-", value = " <- "),
    list(key = "m", value = " %>% ")
))
Yunuuuu commented 2 years ago

I have tried to use windows terminal:wt which support bracket paste mode, although it need open another window, it may remain convenient with windows terminal:wt quake mode. However, vscode can only send code to the vscode integrated terminal instead of a outer terminal . I can config windows terminal:wt by this setting:

    "r.alwaysUseActiveTerminal": true,
     "r.bracketedPaste": true,
    "r.rpath.windows": "C:\\Yun\\R\\R-4.1.1\\bin\\x64\\R.exe",
    "r.rterm.windows": "C:\\Users\\yunyu\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe",
    "r.rterm.option": [
         "-w",
        "_quake",
        "new-tab",
        "C:\\Users\\yunyu\\AppData\\Local\\Programs\\Python\\Python39\\Scripts\\radian.exe",
        "--no-save",
        "--no-restore",
        "--local-history",
        "--r-binary=C:\\Yun\\R\\R-4.1.1\\bin\\x64\\R.exe"
    ],
    "r.plot.useHttpgd": true,
    "r.session.levelOfObjectDetail": "Detailed",
    "r.session.objectLengthLimit": 100,
    "r.session.viewers.viewColumn": {
        "plot": "Active",
        "viewer": "Active",
        "view": "Active",
        "helpPanel": "Active"
    }
randy3k commented 2 years ago

Normally, hitting enter on radian would execute the input unless there is a syntax error. If the input is incomplete, radian will insert a new line after the code instead.

However, something unexpected occurred to @Yunuuuu. The + prompt means that at some point radian thinks that the code is executable and passes the input to R, but it really is not. Hence, the + prompt is shown.

I imagine it may be a synchronization issue when a lot of text is "streamed" to R at the same time.

randy3k commented 2 years ago

@Yunuuuu What happens if you just copy and paste the code to the radian console on vscode terminal?

Yunuuuu commented 2 years ago

Hi randy3k, thanks for your help, but it should run code when I using Ctrl + ENTER to send code to terminal, shouldn't it?

Yunuuuu commented 2 years ago

When I paste the code to it, like this: image

randy3k commented 2 years ago

Both are the results of copy and paste the code? Or just the bottom?

I was asking that to determine if it was an issue introduced by vscode or an issue of radian itself.

Yunuuuu commented 2 years ago

Both are pasted into the terminal.

Yunuuuu commented 2 years ago

I have tested by coping this code and pasting it multiple times.

if the first pasting comes with a character +, the code after that is always no problem. image

if the first pasting comes without a character +, the code (two or more) after that will enter a error eventually. image

Yunuuuu commented 2 years ago

this seems like these issues?

Awalys has a ")" when submit a function to terminal Extra bracket, }, when sending multiple line for-loop code (from ST to Terminus) Erroneous doubling of closing items and failure to parse input properly Send to terminal doubles closing parentheses

randy3k commented 2 years ago

I have tested by coping this code and pasting it multiple times.

Thanks. But it's expected to fail if you just copy and paste multiple times without executing the code.

Though the + sign is concerning, I don't expect to see it when copy and paste.

Yunuuuu commented 2 years ago

randy3k, Could it be resolved in windows? I have switched to rterm.exe, but it’s really defective without some elementary features.

randy3k commented 2 years ago

Could you reproduce the copy and paste issue in an external terminal outside of vscode? Say powershell or Microsoft Terminal?

Yunuuuu commented 2 years ago

they all just correct without any error when implementing the pasting in powershell, cmd.exe, or Microsoft Terminal

image

Yunuuuu commented 2 years ago

I have tried to create radian in vscode using powershell shell, which gave a same problem

image

So this is the problem of vscode?

Fred-Wu commented 2 years ago

I, sometimes, encounter this as well. It's pretty random. Have to type Enter again in Radian to run the codes that have been sent to run.

toscm commented 2 years ago

Hi everyone. I think I encountered the same problem a while ago and I believe adding options(radian.auto_match = FALSE) to my .Rprofile fixed it for me.

Yunuuuu commented 2 years ago

Thanks for telling me that. I have turned radian.auto_match option to FALSE, but it didn't help.

randy3k commented 2 years ago

I just realize that it could be a trailing new line issue: see this for discussion.

Yunuuuu commented 2 years ago

Could it be fixed? I have tried code with trailing space or without, all of them behave alike. Or can we always send a ENTER operation when sending code in windows? A ENTER operation won't break any code, will it?

Yunuuuu commented 2 years ago

I don't know whether vscode-r or radian could provide such operation ? the pause after sending code is annoying, which we must run code manually.

Yunuuuu commented 2 years ago

Hi, I have found a way to do this. We should install another extension(multi-command) to run multiply command with just a keybinding at once. Just create a new keybinding:

    // send code for R
    {
        "key": "ctrl+enter",
        "command": "extension.multiCommand.execute",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "args": {
            "sequence": [
                "r.runSelection",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }

            ]
        }
    },
    {
        "key": "ctrl+enter",
        "command": "-r.runSelection",
        "when": "editorTextFocus && editorLangId == 'r'"
    },
    {
        "key": "ctrl+enter",
        "command": "-r.runSelection",
        "when": "editorTextFocus && editorLangId == 'rmd'"
    }
Yunuuuu commented 2 years ago

It remains here, the code sometimes don't run.

Yunuuuu commented 2 years ago

radian sometimes cannot explain workbench.action.terminal.sendSequence \u000D command like this. image

The code didn't run but inserted a new line character.

Yunuuuu commented 2 years ago

Hi, @randy3k and @renkun-ken , can radian or vscode-r extension provide a command for running code and we can define it in cooperation with vscode-r's command r.runSelection in windows since the ENTER code (like \u000D or \u000A) send from vscode is just explained by radian as next line? If these, we can just define a specific keyboard instead moving the focus to the terminal and running the code manually.

Several problems concerning these have been raised, this problem is common in windows.

Yunuuuu commented 2 years ago

I'm not sure whether the vscode command workbench.action.terminal.sendSequence with a argument "\u000D" is just like we type ENTER in terminal directly? If this is true, it indicatesradian cannot deal with ENTER type correctly in windows? I don't know how to test these.

randy3k commented 2 years ago

As far as I know, from radian's perspective, sending \u000D is the correct way to send "Enter" to radian. I just realized I have never asked, what version of radian are you using?

Yunuuuu commented 2 years ago
radian version: 0.5.12
python version: 3.9.7
Yunuuuu commented 2 years ago

I have updated to the latest version (0.5.13), the problem remains here.

Yunuuuu commented 2 years ago

It seems following operation would just be explained as Next Line when we send this command with r.runSelection simultaneously and meanwhile the code implemented by r.runSelection don't run effectively:

               {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }
Yunuuuu commented 2 years ago

I finally found a nice way to use vscode-r with radian in windows. I Just post it here in case some windows users may have been confused of the conjunction of the both (vscode-r and radian).

Problems and solutions are as follows:

The first one:

Problem 1: there will be exceeding brackets occasionally when we sending code to the terminal. Solution 1: turn off the option radian.auto_match of radian in the .radian_profile

The second one:

Problem 2: The code sending to radian terminal from the editor by inputting ENTER + CTRL will not be run, which we need implement it manually in the terminal panel by inputting ENTER. Solution 2: Firstly, we should install another extension (multi-command), then, override the ENTER + CTRL keyboard with following keybindings setting in vscode:

    // send code for R
    {
        "key": "ctrl+enter",
        "command": "extension.multiCommand.execute",
        "when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
        "args": {
            "interval": 20, // this is needed to execute following both command asynchronousely, otherwise, the problem would remain here
            "sequence": [
                "r.runSelection", 
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }
            ]
        }
    },
    {
        "key": "ctrl+enter",
        "command": "-r.runSelection",
        "when": "editorTextFocus && editorLangId == 'r'"
    },
    {
        "key": "ctrl+enter",
        "command": "-r.runSelection",
        "when": "editorTextFocus && editorLangId == 'rmd'"
    }

Finally, we need turn off the option r.bracketedPaste of vscode-r, which is opposite to what is suggested in the vscode-r manual. I have tried several times which confirmed that above setting would only work by turning off this option.

D3SL commented 2 years ago

Finally, we need turn off the option r.bracketedPaste of vscode-r, which is opposite to what is suggested in the vscode-r manual. I have tried several times which confirmed that above setting would only work by turning off this option.

I've been giving this a shot for a while and at least for me it doesn't seem to be a working fix. I don't seem to get the extraneous characters at the end of things anymore but large ctrl+enter blocks will still "break" part way through and turn into colorless unformatted input, and I still have to switch focus to the console and manually hit enter quite often.

JosephineRoper commented 1 year ago

I was also still having this problem (as per @D3SL - no extra characters after that fix, but often the code won't be ENTER-ed). This is a bit of a kludgy fix, but I modified the keybinding above to include an extra enter. This means about half the time, a superfluous enter is sent and I get a blank line in the terminal after my output, but for me that's ok, much better than the previous situation.

// send code for R from https://github.com/REditorSupport/vscode-R/issues/820
{
"key": "ctrl+enter",
"command": "extension.multiCommand.execute",
"when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
"args": {
    "interval": 20, // this is needed to execute following both command asynchronousely, otherwise, the problem would remain here
    "sequence": [
        "r.runSelection", 
        {
            "command": "workbench.action.terminal.sendSequence",
            "args": { "text": "\u000D" }
        }, 
        {
            "command": "workbench.action.terminal.sendSequence",
            "args": { "text": "\u000D" }
        }
    ]
}
},
{

"key": "ctrl+enter",
"command": "-r.runSelection",
"when": "editorTextFocus && editorLangId == 'r'"
},
{

"key": "ctrl+enter",
"command": "-r.runSelection",
"when": "editorTextFocus && editorLangId == 'rmd'"
}
JGBSilva commented 1 year ago

I'm still having this issue, even after trying the radian.auto_match fix. My issue at the moment is not even it adding it in the end of the block of code in the terminal. Now it seems to add random } in the middle of the code, causing it to break and spitting errrs like: "Error: unexpected '}' in "}"", but at least it seems to auto-run the code when using Ctrl+Enter.

Looking at the code inputed in the terminal, it seems the code block is being split at some random point and that causes the error. As you can see in this picture, the code was running and then it stopped, gave the error, and continued in a new command input.

image

kendonB commented 1 year ago

I have a similar problem, after attempting the solution here

My command is long - could be why

urban_rural <- read_sf("data/boundaries/statsnzstatistical-area-1-higher-geographies-2018-generalised-SHP/statistical-area-1-higher-geographies-2018-generalised.shp")
MEpiVet commented 3 months ago

Just adding the quarto keybindings to the above solutions; I personally didnt need the double enter. But did need the .radian_profile fix and no bracketed paste in settings

//FIX FOR RADIAN ISSUE from https://github.com/REditorSupport/vscode-R/issues/820
    {
        "key": "ctrl+enter",
        "command": "extension.multiCommand.execute",
        "when": "editorTextFocus && editorLangId =~ r|rmd/",
        "args": {
            "interval": 20, // this is needed to execute following both command asynchronousely, otherwise, the problem would remain here
            "sequence": [
                "r.runSelection", 
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }//, 
                // {
                //     "command": "workbench.action.terminal.sendSequence",
                //     "args": { "text": "\u000D" }
                // }
            ]
        }
    },
    {
        "key": "ctrl+enter",
        "command": "extension.multiCommand.execute",
        "when": "(editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'quarto')||activeCustomEditorId == 'quarto.visualEditor'",
        "args": {
            "interval": 20, // this is needed to execute following both command asynchronousely, otherwise, the problem would remain here
            "sequence": [
                "quarto.runCurrent", 
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }
            ]
        }
    },
    {
        "key": "ctrl+shift+enter",
        "command": "extension.multiCommand.execute",
        "when": "(editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'quarto')||activeCustomEditorId == 'quarto.visualEditor'",
        "args": {
            "interval": 20,
            "sequence": [
                "quarto.runCurrentCell",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }
            ]
        }
    },
    {
        "key": "alt+enter",
        "command": "extension.multiCommand.execute",
        "when": "(editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'quarto')||activeCustomEditorId == 'quarto.visualEditor'",
        "args": {
            "interval": 20,
            "sequence": [
                "quarto.runSelection",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }
            ]
        }
    },
    {
        "key": "ctrl+alt+p",
        "command": "extension.multiCommand.execute",
        "when": "(editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'quarto')||activeCustomEditorId == 'quarto.visualEditor'",
        "args": {
            "interval": 20,
            "sequence": [
                "quarto.runPreviousCell",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }
            ]
        }
    },
    {
        "key": "ctrl+alt+n",
        "command": "extension.multiCommand.execute",
        "when": "(editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'quarto')||activeCustomEditorId == 'quarto.visualEditor'",
        "args": {
            "interval": 20,
            "sequence": [
                "quarto.runNextCell",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }
            ]
        }
    },
    {
        "key": "ctrl+alt+r",
        "command": "extension.multiCommand.execute",
        "when": "(editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'quarto')||activeCustomEditorId == 'quarto.visualEditor'",
        "args": {
            "interval": 20,
            "sequence": [
                "quarto.runAllCells",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }
                }
            ]
        }
    },
    {
        "key": "ctrl+enter",
        "command": "-r.runSelection",
        "when": "editorTextFocus && editorLangId =~ r|rmd/"
    }