R-nvim / R.nvim

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

Handle correcly multiple exptessions that are on the same line seperated by ; #180

Closed PMassicotte closed 2 months ago

PMassicotte commented 2 months ago

Need to be fully tested, but works for simple cases I tested.

https://github.com/R-nvim/R.nvim/issues/161#issuecomment-2212464974

PMassicotte commented 2 months ago

One should use it a bit to see if I did not break anything.

PMassicotte commented 2 months ago

Working on a better solution for things line print("a;b").

jalvesaq commented 2 months ago

Addressing the problem with a different approach on the experimental branch https://github.com/R-nvim/R.nvim/tree/next_sibling

Instead of moving to the next line, move to the next line and column where the next sibling node begins. I didn't test it carefully...

jalvesaq commented 2 months ago

The next_sibling branch fails with this code:

mtcars |>
    select(mpg, qsec) |>
    cor()

ggplot(mtcars, aes(mpg, qsec)) +
    geom_point() +
    geom_smooth()
jalvesaq commented 2 months ago

It's better to keep this pull request. The only problem that I could see is that is skips one line after sending a line with multiple statements, and the fix for this is:

diff --git a/lua/r/send.lua b/lua/r/send.lua
index ea17bf1..4b863e8 100644
--- a/lua/r/send.lua
+++ b/lua/r/send.lua
@@ -66,7 +66,7 @@ local function get_code_to_send(txt, row)
             local line_txt = vim.fn.getline(i + 1)
             table.insert(lines, line_txt)
         end
-        row = end_row + 1
+        row = end_row
     end

     return lines, row
PMassicotte commented 2 months ago

Good catch! Fixed.

PMassicotte commented 2 months ago

Do you think we should merge it?

jalvesaq commented 2 months ago

We also have to replace brace_list with braced_expression:

diff --git a/lua/r/send.lua b/lua/r/send.lua
index 3459dfd..60b63b5 100644
--- a/lua/r/send.lua
+++ b/lua/r/send.lua
@@ -115,7 +115,7 @@ local function get_code_to_send(txt, row)

     while node do
         local parent = node:parent()
-        if parent and (parent:type() == "program" or parent:type() == "brace_list") then
+        if parent and (parent:type() == "program" or parent:type() == "braced_expression") then
             break
         end
         node = parent
@@ -127,7 +127,7 @@ local function get_code_to_send(txt, row)
             local line_txt = vim.fn.getline(i + 1)
             table.insert(lines, line_txt)
         end
-        row = end_row + 1
+        row = end_row
     end

     return lines, row
jalvesaq commented 2 months ago

After changing brace_list with braced_expression, everything seems to work.

PMassicotte commented 2 months ago

Just pushed. Works on my side too.

jalvesaq commented 2 months ago

We can merge it.

jalvesaq commented 2 months ago

Great job! Thank you!