Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sending code to R from Rnoweb documents #299

Merged
merged 3 commits into from
Dec 29, 2024
Merged

Fix sending code to R from Rnoweb documents #299

merged 3 commits into from
Dec 29, 2024

Conversation

jalvesaq
Copy link
Member

Close #298

@jalvesaq
Copy link
Member Author

This pull request fixes an issue by calling a function that does nothing:

    if node then vim.schedule(function() end) end

Clearly, this shouldn't be necessary and we are circumventing what seems to be a Neovim or tree-sitter bug. I don't know whether the fix works or introduces new bugs on macOs or Windows. So, the pull request needs review.

@jalvesaq
Copy link
Member Author

Example of Rnoweb document that can be used to test the changes:

Example.Rnw:

\documentclass{article}

\begin{document}

Here is a code chunk.

<<example>>=
x <- 1
print(x)
x |>
    print()
@

And another one:

<<example2>>=
#| echo: false
#| fig-cap: "Example of Figure"

# Cursor on `a`. Send the line and jump to `b`.
a <- 1
b <- 2

# Cursor on `d`. Send the line and jump to `if`.
d <- data.frame(var1 = c(1, 2, 3, 4, 5), var2 = c(1, 2, 3, 3, 2))

# Cursor on `if`. Send the next two lines.
if (TRUE)
    library("ggplot2")

# Cursor on `if`. Send the bracket block.
if (TRUE) {
    # Cursor on `print`. Send the line.
    print(1)
    print(2)
}

# Cursor on `plus_one`. Send the bracket block.
plus_one <- function(a) {
    # Cursor on `b`. Send the line.
    b <- a + 1
    # <LocalLeader>fc here. Send the function `plus_one`.
    nested_fun <- function(x) {
    # <LocalLeader>fc here. Send the function `plus_one`.
        y <- x + 1
        return(invisible(y))
    }
    b
}

# <LocalLeader>fa. Send the functions `plus_one` and `plus_two`.

# Cursor anywhere on the line below. Send the whole line and jump to `d`.
a <- 1 ; b <- 2
d <- "a;b"

library("dplyr")
library("ggplot2")

# Statements chained by a pipe operator
# Cursor anywhere in the three lines below. Send the three lines.
mtcars |>
    select(mpg, qsec) |>
    cor()

# Statements chained by arithmetic operator
# Cursor anywhere in the three lines below. Send the three lines.
ggplot(mtcars, aes(mpg, qsec)) +
    geom_point() +
    geom_smooth(span = 3.0)
@

End of document.


\end{document}

@jalvesaq jalvesaq force-pushed the strange_fix branch 3 times, most recently from 0a5ba06 to ffa636c Compare December 29, 2024 18:58
@PMassicotte
Copy link
Collaborator

Testing it now...

@PMassicotte
Copy link
Collaborator

In your example.Rnw, at the

# <LocalLeader>fa. Send the functions `plus_one` and `plus_two`.

I am getting Not in an R file when I try to execute it.

doc/R.nvim.txt Outdated Show resolved Hide resolved
@jalvesaq
Copy link
Member Author

In your example.Rnw, at the

# <LocalLeader>fa. Send the functions `plus_one` and `plus_two`.

I am getting Not in an R file when I try to execute it.

It was my mistake to have included the <LocalLeader>fa command in the example. But I will improve the message, making it more generic.

Copy link
Collaborator

@PMassicotte PMassicotte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a few comments.

@PMassicotte
Copy link
Collaborator

In your example

plus_one <- function(a) {
    # Cursor on `b`. Send the line.
    b <- a + 1
    # <LocalLeader>fc here. Send the function `plus_one`.
    nested_fun <- function(x) {
    # <LocalLeader>fc here. Send the function `plus_one`.
        y <- x + 1
        return(invisible(y))
    }
    b
}

If I place my cursor on plus_one or nested_fun and press enter, it sends the function correctly. Why the fc is not working? I might have missed something.

@R-nvim R-nvim deleted a comment from jalvesaq Dec 29, 2024
@R-nvim R-nvim deleted a comment from jalvesaq Dec 29, 2024
@jalvesaq
Copy link
Member Author

If I place my cursor on plus_one or nested_fun and press enter, it sends the function correctly. Why the fc is not working? I might have missed something.

It's because this chunk of R code is actually an R script that I use to check if the use of tree-sitter to send code to R is still working since tree-sitter is frequently changing and breaking our code in R.nvim. I should have cleaned up the code and deleted what is supposed to work only in R scripts. From my understanding, the algorithm to send functions is: (1) parse all tree-sitter nodes of the R buffer and get all functions; (2) send the current function according to the cursor position or all functions (depending on the command being \fc or \fa). To make the code work on Rmd, Quarto, and Rnoweb, the algorithm would have to search for functions in all chunks of R code, but this is not implemented yet.

Close #298
Co-authored-by: Philippe Massicotte <[email protected]>
@PMassicotte
Copy link
Collaborator

PMassicotte commented Dec 29, 2024

If I place my cursor on plus_one or nested_fun and press enter, it sends the function correctly. Why the fc is not working? I might have missed something.

It's because this chunk of R code is actually an R script that I use to check if the use of tree-sitter to send code to R is still working since tree-sitter is frequently changing and breaking our code in R.nvim. I should have cleaned up the code and deleted what is supposed to work only in R scripts. From my understanding, the algorithm to send functions is: (1) parse all tree-sitter nodes of the R buffer and get all functions; (2) send the current function according to the cursor position or all functions (depending on the command being \fc or \fa). To make the code work on Rmd, Quarto, and Rnoweb, the algorithm would have to search for functions in all chunks of R code, but this is not implemented yet.

If I comment out

    -- if vim.bo[bufnr].filetype ~= "r" then
    --     inform("Not yet supported in '" .. vim.bo[bufnr].filetype .. "' files.")
    --     return
    -- end

The fa command works in the example.Rnw document.

Peek 2024-12-29 15-52

@jalvesaq
Copy link
Member Author

If I comment out [...] The fa command works in the example.Rnw document

I confirm that it works on my side too! This is great!

@jalvesaq
Copy link
Member Author

It doesn't work in Rmd or Quarto, though. So, we have to bring back the previous informational message.

@PMassicotte
Copy link
Collaborator

It doesn't work in Rmd or Quarto, though. So, we have to bring back the previous informational message.

It works, but strangely. Let me try to explain below.

@jalvesaq
Copy link
Member Author

It doesn't seem to work on rhelp (extension .Rd) either.

@PMassicotte
Copy link
Collaborator

PMassicotte commented Dec 29, 2024

  1. Create a file.qmd (same apply to your example.Rwn file)
#| echo: false
f1 <- function() {
  1 + 1
}

  1. Close nvim.
  2. Open this file in nvim
  3. Place cursor on f1, press enter. It will send the current function and move down.
  4. Place cursor on f1 again and it works also.
  5. Run localleader fc, nothing happens.
  6. Replace cursor on f1 and enter again, it is no longer working.

It looks like that the buffer type (filetype) is beeing changed somewhere when we try to run fX commands.

@jalvesaq
Copy link
Member Author

Replace cursor on f1 and enter again, it is no longer working.
It looks like that the buffer type (filetype) is beeing changed somewhere when we try to run fX commands.

I can replicate the same buggy behavior. If we move the cursor, R.nvim recovers the ability to send lines. Maybe, we could try the vim.schedule() here too...

@PMassicotte
Copy link
Collaborator

PMassicotte commented Dec 29, 2024

Replace cursor on f1 and enter again, it is no longer working.
It looks like that the buffer type (filetype) is beeing changed somewhere when we try to run fX commands.

I can replicate the same buggy behavior. If we move the cursor, R.nvim recovers the ability to send lines. Maybe, we could try the vim.schedule() here too...

If I move the cursor anywhere and try to press enter, I still get Not inside R or Python code chunk [within markdown].

EDIT: Ah it works if I move it outside the chunk and come back in.

@jalvesaq
Copy link
Member Author

I just have to move one character above or ahead to bring it to normal behavior.

The vim.schedule(function() end) doesn't change anything in this case.

@PMassicotte
Copy link
Collaborator

Maybe we can merge this PR and open another for the sending behaviour in rmd and qmd files.

@jalvesaq
Copy link
Member Author

Maybe we can merge this PR and open another for the sending behaviour in rmd and qmd files.

I agree, but let's restore the previous informational message before merging it...

@jalvesaq
Copy link
Member Author

I suggest:

    if vim.bo[bufnr].filetype ~= "r" and vim.bo[bufnr].filetype ~= "rnoweb" then
        inform("Not yet supported in '" .. vim.bo[bufnr].filetype .. "' files.")
        return
    end

@PMassicotte
Copy link
Collaborator

Sounds good, Then another issue to fix the fX commands.

@PMassicotte PMassicotte merged commit 7679e80 into main Dec 29, 2024
2 checks passed
@PMassicotte PMassicotte deleted the strange_fix branch December 29, 2024 21:29
@PMassicotte
Copy link
Collaborator

Good job, thanks for the changes.

jalvesaq added a commit that referenced this pull request Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rnoweb: Error: unexpected input in "<<"
2 participants