vscode-R: Run Selection/Line fails when some lines end in commas
Describe the bug
Lines ending in commas seem to cause R: Run Selection/Line to send the wrong lines to the console.
To Reproduce
- Create a file
temp.Rwith the following content:
list(
1,
2
)
- Place cursor on second or third line
- Use command
R: Run Selection/Line
Expected: All four lines are sent to console (if cursor was on second line), or only ‘2’ is sent to console (if cursor was on third line)
Actual: Middle two lines are sent to console:
> 1,
Error: unexpected ',' in "1,"
> 2
[1] 2
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 17 (4 by maintainers)
Would anyone like to volunteer to try making the one-line change I suggested above? I think that would probably fix the comma issue and also change the behaviour so that parenthesised expressions split over several lines are sent to the console in full. If that works, a few unit tests would also need to be updated and then a PR could be made.
If no volunteers I will probably make the change myself at some point but hard to say when.
I don’t quite understand why this is the expected behavior. Everything inside a parenthesis, are there because their working together makes it work… in addition to the parenthesis itself. It’s a similar issue to whether or not line execution should involve pipe. It doesn’t make sense to execute without pipe, and it doesn’t make sense to execute an individual line inside a parenthesis without the function that calls it.
In the example of:
Yes, all 5 lines should be executed.
Something to consider if changing the existing behaviour is how to handle nesting. Consider the following case:
If the cursor is on line 3, should it send
b(1)ora(b(1))? How about if the cursor is on line 2?I don’t use R much these days and haven’t made any changes to the vscode-R code base in some time, so it’s unlikely I’ll make a PR relating to this any time soon. But anyone else is of course welcome to take it on if they’d like. If changing intended behaviour, I recommend getting agreement from one or two other active contributors before starting on the code modifications.
I think the best way to see the current intended behaviour for selecting code to be sent to the terminal is the unit tests in this file: https://github.com/REditorSupport/vscode-R/blob/master/src/test/suite/extension.test.ts
The general concept was to send the ‘minimum bit of runnable code’ to the console. That’s why
1is sent to the console in this example with the cursor on the second line:The main exception to that concept is that it also looks for operators at the end of the previous line, as you’ve found. I think I chose that behaviour mainly to handle chains of lines ending in
%>%, and possibly for compatibility with RStudio.So those cases are intended behaviour. The comma examples aren’t intended behaviour, because the comma code isn’t runnable without the surrounding parentheses/brackets and so what gets sent to the console produces an error.
The intended behaviour could be changed, of course - I’m just noting what the intended behaviour was when this was implemented.