jalvesaq / Nvim-R

Vim plugin to work with R
GNU General Public License v2.0
968 stars 126 forks source link

Support of native pipe (`|>`) vs. magrittr pipe (`%>%`) #717

Closed jarodmeng closed 1 year ago

jarodmeng commented 1 year ago

First of all, huge thanks for developing and (more crucially) maintaining this plug-in! It has been a critical part of my terminal workflow for years now. Let me know if there's a venue through which we can donate to support the effort.

I recently changed my workflow to use the native pipe (|>) instead of the magrittr pipe (%>%) and discovered some discrepancy in the user experience. I'm not sure if they are caused by Nvim-R, but thought I can report them first. 1) When I use the space key to send the code to R, previously (with %>% pipe) the whole block of code is sent in one shot. Sending the same code (with |> pipe) behaves differently. It stops at every line and I need to press space key once per line to send the whole block.

# space key sends the whole block of code in one go
mtcars %>%
  group_by(mpg) %>%
  summarize(n = n())

# space key needs to be pressed twice (once per line) to send the block
mtcars |>
  group_by(mpg) |>
  summarize(n = n())

Not sure if there's a setting I can use to correct this behavior for the native pipe.

2) Indentation of new line after pipe is different

When using the %>% pipe and pressing the return key to start a new line, the cursor is auto indented to the beginning of the new line. When I changed to |>, the new line somehow always starts in the middle of the new line and I have to press backspace a few times to get it to the beginning of the line.

I'm not sure if this is a Nvim-R problem, but don't know how I can tell which plugin or nvim feature is controlling the indentation here.

jalvesaq commented 1 year ago

I will send to Bram Moolenaar the updated version of Vim runtime files supporting R. While I don't do so, please add R-Vim-runtime to your plugins. The issue should be fixed.

jarodmeng commented 1 year ago

I installed R-vim-runtime using my plugin manager. However, the space bar execution still doesn't recognize the native pipe. Do I need to configure some settings in Nvim-R or R-vim-runtime for it to work?

jalvesaq commented 1 year ago

I'm now using <Enter> to send lines to R, and it works with the native pipe. For example, if I press <Enter> when the cursor is on the rnorm() line from the code below, Nvim-R sends three lines to R and the cursor jumps to the ls() line:

rnorm(100) |>
    round() |>
    summary()
ls()
jarodmeng commented 1 year ago

I found that I had these two lines in my .vimrc.

vmap <Space> <Plug>RDSendSelection
nmap <Space> <Plug>RDSendLine

I think I added them per Nvim-R documentation long time ago.

How can one turn on the setting to use Enter to send lines?

jalvesaq commented 1 year ago

For vimrc or init.vim:

vmap <Enter> <Plug>RDSendSelection
nmap <Enter> <Plug>RDSendLine

For init.lua:

vim.keymap.set('v', '<Enter>', '<Plug>RDSendSelection')
vim.keymap.set('n', '<Enter>', '<Plug>RDSendLine')
jalvesaq commented 1 year ago

Note: it makes no difference to use Enter or Space to send lines. Both will either work or not.

jalvesaq commented 1 year ago

I'm closing the issue because I can't replicate it...