R-nvim / R.nvim

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

R code lines incorrectly identified #190

Closed carschandler closed 2 months ago

carschandler commented 2 months ago

This might be related to the recent change to treesitter identification, but recently, I began having issues where commands that relied on being inside an R code block in a .Rmd document stopped working. For example, if I am typing into an empty code block like this:

```{r}
*cursor here*
```

and I use the standard <M--> bind to enter a <-, it doesn't work, but if I am typing above a line of code, i.e.

```{r}
*cursor here*
a <- 42
```

then it works.

I'm experiencing a similar issue where sometimes it will tell me that I'm not over an R-chunk when using \cc or RDSendLine.

carschandler commented 2 months ago

Seems to be any time I'm on the last line of the chunk

PMassicotte commented 2 months ago

Probably something off by 1 somewhere.

jalvesaq commented 2 months ago

I confirm the bug. It works in Quarto, but not in RMarkdown.

jalvesaq commented 2 months ago

Trying to debug this issue, I changed lualine_x in my lualine config to show the output of get_lang().

                sections = {
                    lualine_a = { "mode" },
                    lualine_b = { {'branch', icon = ''}, "diagnostics" },
                    lualine_c = { "filename", "searchcount",  },
                    -- lualine_x = { selectionCount },
                    lualine_x = { require("r.utils").get_lang },
                    lualine_y = { {rstatus, color = rsttcolor }},
                    lualine_z = { "progress", "location" },
                },

After that, the bug disappeared, even if I removed get_lang from my lualine config. However, based on the error message that I got in my first attempt to display the output of get_lang in the Neovim status line, I created the branch https://github.com/R-nvim/R.nvim/tree/fix_get_lang

Could you try it and see if it makes any difference (since I could no longer replicate the bug even before the changes)?

jalvesaq commented 2 months ago

I updated the branch fix_get_lang...

lokesh-krishna commented 2 months ago

On the fix_get_lang branch but still face both issues. Alt-- results in <M--> instead of the usual <-. Get notified that I'm not in inside a R or Python code chunk. This even happens when I'm not on the last line in a chunk.

jalvesaq commented 2 months ago

It works on my side. So, we might have different tree-sitter parsers. You can try:

jalvesaq commented 2 months ago

On the main branch, if I have a chunk of R code in an Rmd file with just two lines, x and y, and press <M--> three times after x and after y I get:

x <-  <-  <- 
y <- <M--><M-->

On the fix_get_lang branch, I get:

x <-  <-  <- 
y <-  <-  <- 

So, I believe that now I know how to replicate the bug reliably and that it is fixed on the fix_get_lang branch.

lokesh-krishna commented 2 months ago

I followed the instructions regarding tree-sitter and am on the latest update on the fix_get_lang branch. With my test file. I still face the issue of R.nvim saying Not inside R or Python code chunk. I'm not sure if this is the same issue as what has been discussed and resolved here though. Should this conversation continue here or should I open a new issue?

jalvesaq commented 2 months ago

It's the same issue. It's fixed here, but not there. I tried nvim 0.9-5 (from the Debian package) and nvim 0.11.0-dev (installed from source some days ago). Because I can't replicate the issue, the only thing that I can do is merge the branch anyway since it at least fixes the bug on my side.

lokesh-krishna commented 2 months ago

Is there any recommendation you have for troubleshooting this? Sorry for the trouble 🙈 EDIT: Facing this issue in Quarto. EDIT: I can get it to reliably work as long as my cursor is on the firstl line of the code block. Anything else and it won't work.

lokesh-krishna commented 2 months ago

Okay, I'm facing this issue with everything other than RSendParagraph and RDSendParagraph. These 2 work and the rest don't.

jalvesaq commented 2 months ago

Next attempt:

Create the simplest Quarto document that replicates the bug and run this command:

:InspectTree

Post here the quarto document and the contents of the left window (the tree of nodes of treesitter).

It will also help if you enable treesitter highlighting instead of Vim's syntax highlight because you will see if treesitter is recognizing the code as R.

lokesh-krishna commented 2 months ago

image

I do have tree-sitter syntax highlighting enabled. Everything else works fine in the code block (LSP diagnostics, auto-complete, etc). I'm even able to send lines to R, but only if I'm using RSendParagraph or RDSendParagraph. The line and selection based ones are the ones that throw the Not inside R or Python code chunk.

However, the line based ones do work after I've run :InspectTree. They continue to work even after I've closed the InspectTree window. What might be up here?

Trying to debug this issue, I changed lualine_x in my lualine config to show the output of get_lang().

It sounds kinda similar to this situation, doesn't it?

jalvesaq commented 2 months ago

With exactly the same 7 lines in a .Rmd I get exactly the same output.

However, the line based ones do work after I've run :InspectTree. They continue to work even after I've closed the InspectTree window. What might be up here?

Perhaps lazy=false in nvim-treesitter could make a difference...

lokesh-krishna commented 2 months ago

It didn't :(

Really appreciate y'all being so patient with me. I'll just continue trying to figure this one out and share any updates.