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

Add ability to run code from @examples in Roxygen lines

Open nathaneastwood opened this issue 6 years ago • 15 comments

In RStudio, a really neat feature is the ability to run code written next to Roxygen tage (#') by using Ctrl (Cmd) + Enter. The Roxygen tags do not get sent to the console. In VSCode I have to highlight the code and hit Ctrl (Cmd) + Enter. Not a particularly huge concern, but a nice to have.

An example:

#' print("hello world")
my_fun <- function() ...

Having the cursor on the first line and hitting Ctrl (Cmd) + Enter would produce [1] "hello world" in RStudio but in VSCode it would send my_fun to the console.

nathaneastwood avatar Nov 27 '19 11:11 nathaneastwood

Hi, I really like this feature from RStudio too.

Off the top of my head, I think this could be done with a couple of modifications:

  1. In src/selection.ts: Before or at the start of extendSelection(), check whether the line starts with #'
  2. In src/lineCache.ts: Modify cleanLine() to take a parameter that allows it to remove #' from the start of a line if identified as being in 'Roxygen mode' from 1.

In src/selection.ts, checkForBlankOrComment() might need a modification too.

I can probably do this some time in 2020, but very happy to provide assistance to anyone who'd like to have a go at implementing it a bit earlier!

andycraig avatar Nov 27 '19 12:11 andycraig

I've never developed in TypeScript but I'm happy to give it a go - it can't be too different from R or Python, right? :D

nathaneastwood avatar Nov 27 '19 13:11 nathaneastwood

@nathaneastwood Fantastic! I’ll remove the ‘help wanted’ tag. The very first TypeScript I wrote was for sending code to the terminal too, so it seems like an appropriate place to start :)

There are unit tests for extendSelection() in test/extension.test.ts. I suggest adding some for code written next to Roxygen comments. Then you can develop by running them, which is much quicker than repeatedly firing up R consoles and hitting Ctrl-Enter.

There are some brief instructions on how to run a development version of vscode-R here: https://github.com/Ikuyadeu/vscode-R/blob/master/CONTRIBUTING.md

I also recommend installing the ‘tslint’ VSCode extension.

I thought of one more modification that will probably be necessary, which is removing the #' again in getSelection() after the call to currentDocument.getText().

Thank you again for volunteering, and let me know if you have any trouble!

andycraig avatar Nov 27 '19 22:11 andycraig

I'm wondering which is the desired behavior: if the selection contains both #'-started comments and normal code, it seems we should treat the comments as comments.

For example, if selection contains the whole block of the following code:

#' Test code
#' @examples
#' test(1)
test <- function(x) {
  x + 1
}

Sending the selection should not convert #' test(1) to test(1) and should send only the function code to terminal.

It seems that the correct behavior should be if the selection only contains #' lines, then #' should be removed and sent to terminal. Otherwise, the comment should be left as it is.

renkun-ken avatar Nov 27 '19 22:11 renkun-ken

@nathaneastwood , did you ever put any code together for this? If not I was thinking of trying to pick it up

gowerc avatar Feb 04 '22 14:02 gowerc

No I didn't.

nathaneastwood avatar Feb 05 '22 19:02 nathaneastwood

I am not a TypeScript player still this looks to be only a one regex to add ...

We want to update this function

The core source function

Now for each line (this is a string) we want to replace ^\s*#'\s* with nothing. it will be great to have some validation that each line in selection start with ^\s*#'.

from

    if(useRepl && vscode.debug.activeDebugSession?.type === 'R-Debugger'){
        await sendRangeToRepl(selection.range);
    } else{
        await runTextInTerm(selection.selectedText);
    }

to sth like, pseudo code.

    if(useRepl && vscode.debug.activeDebugSession?.type === 'R-Debugger'){
        await sendRangeToRepl(selection.range);
    } else{
        var txt = selection.selectedText;
        // this if for performance
        if (txt.includes("#'")) {
          var txt_lines = txt.split("\n")
          var txt_lines_clean = txt_lines.forEach(function (value) {
             value.replace(/^\s*#'\s*/, "")
           })
          txt = txt_lines_clean.join("\n")
        }
        await runTextInTerm(txt);
    }

Polkas avatar Mar 08 '22 21:03 Polkas

Just switched to VS code from RStudio and really missing this feature

amarquard avatar May 16 '22 07:05 amarquard

Using vscode more often (again) lately, I was also heavily missing this one - @andycraig @nathaneastwood any plans/motivation to tackle this in the near future? 🤓

pat-s avatar Jun 13 '22 11:06 pat-s

@pat-s No plans from me I'm afraid - I miss this feature when I use R but these days I don't use R very much ...

andycraig avatar Jun 13 '22 11:06 andycraig

Nothing from me either, I'm too busy these days.

nathaneastwood avatar Jun 13 '22 19:06 nathaneastwood

For me the work around is to use sth like:

  1. move the cursor at the upper left corner of the example, start of the first line of the example code
  2. click with the mouse holding down shift + alt (on mac shift + option), on the down right corner of the code block. It is important that the mouse click is on the right (column height) of the most extreme line of code.
  3. Now simply copy and paste the code to the R console.

optionally 1. could be done with Selection tab and Column selection option

Polkas avatar Jul 03 '22 12:07 Polkas

Would love to see this feature too. That's one thing I really miss in RStudio when developing packages.

marklhc avatar Jan 15 '23 19:01 marklhc

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] avatar Jan 16 '24 01:01 github-actions[bot]

not stale

gowerc avatar Jan 16 '24 08:01 gowerc