R-nvim / R.nvim

Neovim plugin to edit R files
GNU General Public License v3.0
129 stars 15 forks source link

Send some preceding lines to the console #99

Closed wurli closed 3 months ago

wurli commented 3 months ago

For example, hitting <CR> with the cursor on the second line here, all 3 lines will now be sent to the console:

iris |>
  subset(Species == "setosa") |>
  transform(Sepal.Area = Sepal.Length * Sepal.Width)

The previous behaviour in this case was that only lines 2 and 3 would be sent, so you'd either have to go back to line 1 and re-send, or select all 3 lines before sending in visual mode.

This also works in the case of mismatched parentheses. For example, hitting <CR> with the cursor on the second line here will now cause both lines to be sent:

(
123)

The new behaviour mimics RStudio, which feels more seamless to me.

I've tested in both R scripts and Quarto.

I haven't tested with rnoweb. I understand R chunks in noweb are delimited at the start by lines like <<my-chunk>>, so when looking for the start of a noweb chunk I've just tested for lines beginning with <<.

jalvesaq commented 3 months ago

Thank you for your contribution! In a quick test, it seems to work and not introduce any bugs, including in an Rnoweb file. And I agree that it's more useful to send the whole chain of piped lines than only from the current line to the end. @PMassicotte has implemented a new way of sending part of a piped chain and might want to review this pull request too.

wurli commented 3 months ago

Thank you for your review! I've just snuck in one more change to include commented lines. Now, all the lines in the following code would be sent if the cursor was placed on any of them:

# Bla
# Bla
iris |>
  subset(Species == "setosa") |>
  # Bla
  # Bla
  transform(Sepal.Area = Sepal.Length * Sepal.Width)

Again, this is the same behaviour as RStudio. If you prefer I'd be more than happy to move this commit to its own pull request.

jalvesaq commented 3 months ago

I think it's better to keep the new commits here. Thanks!

wurli commented 3 months ago

Great, thanks! A couple more changes in line with RStudio's behaviour:

Now the following bits of code will all be sent in their entirety, regardless of where the cursor is:

2 %%

  4
1:

  10
!

  TRUE
a?

  b
list(a = 1)$

  a
list(a = 1)[

  "a"

]
# setClass("foo", representation(bar = "character"))
new("foo", bar = "baz")@

  bar

Think this will be it for now 😁

wurli commented 3 months ago

I've temporarily closed this - I've noticed a couple more bugs I'd like to fix, then will reopen.

PMassicotte commented 3 months ago

Let me know when it is ready to review/test!

wurli commented 3 months ago

Hi both, thanks for bearing with. After some head scratching looking for an algorithm to reliably detect code to be sent, I decided to change tack and try using treesitter. This turned out to work really well!

Now:

The only slightly tricky bit was getting this to work with Quarto/R Markdown/Rnoweb, which I was able to get working by manually telling treesitter to parse the current chunk as R code. Based on my testing it all seems to work okay.

PMassicotte commented 3 months ago

Thank you!

Rapidly browsed the code and it looks good. I will take a deeper look when I am back to home in 1-2 hr!

wurli commented 3 months ago

Excellent, many thanks 🙂

PMassicotte commented 3 months ago

Tx for your contribution.

jalvesaq commented 3 months ago

I'm seeing some warnings coming from the Lua server. The problems highlighted are two:

  1. vim.treesitter.get_parser():parse() receives only one argument (a table, according to treesitter's documentation). So, line 55 of lua/r/send.lua should be:
    vim.treesitter.get_parser(0, "r"):parse({ chunk_start_row, chunk_end_row })
  1. node can be nil in lines 98, 103, and 105.
PMassicotte commented 3 months ago

I will make a PR

wurli commented 3 months ago

Thanks both! For future reference - how does one check the Lua server for warnings?

jalvesaq commented 3 months ago

As far as I remember, when you run the Lua Language Server (lua_ls), the diagnostics are displayed by default, but diagnostics and completions of Neovim functions are available only because I have installed neodev. I also get additional diagnostics from selene enabled by none-ls.nvim. My complete configuration is in this comment.

wurli commented 3 months ago

Many thanks! I’ll make sure to put any additional pull requests through a bit more testing using these.