vscode-R
vscode-R copied to clipboard
Add ability to run code from @examples in Roxygen lines
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.
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:
- In
src/selection.ts: Before or at the start ofextendSelection(), check whether the line starts with#' - In
src/lineCache.ts: ModifycleanLine()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!
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 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!
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.
@nathaneastwood , did you ever put any code together for this? If not I was thinking of trying to pick it up
No I didn't.
I am not a TypeScript player still this looks to be only a one regex to add ...
We want to update this 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);
}
Just switched to VS code from RStudio and really missing this feature
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 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 ...
Nothing from me either, I'm too busy these days.
For me the work around is to use sth like:
- move the cursor at the upper left corner of the example, start of the first line of the example code
- 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.
- Now simply copy and paste the code to the R console.
optionally 1. could be done with Selection tab and Column selection option
Would love to see this feature too. That's one thing I really miss in RStudio when developing packages.
This issue is stale because it has been open for 365 days with no activity.
not stale