Nvim-R icon indicating copy to clipboard operation
Nvim-R copied to clipboard

folding rmarkdown documents

Open gbonett opened this issue 2 years ago • 7 comments

Hi,

Thank you for providing this great tool.

Does this plug-in support folding by section header of rmarkdown documents? The documentation says that one of the main features is that it can "Fold code when foldmethod=syntax", but after setting foldmethod=syntax I am not able to see any folding when pressing "zc" (on either section headers or R code). I get the error: "E490: No fold found"

Thanks in advance for any help.

gbonett avatar May 17 '23 05:05 gbonett

Sorry, it does not. I will fix the documentation. The challenge to implementing folding for Rmarkdown or Quarto is that # is the comment string in R.

jalvesaq avatar May 17 '23 09:05 jalvesaq

I've found that this works on Neovim:

set foldmethod=expr
set foldexpr=nvim_treesitter#foldexpr()
set nofoldenable

If using init.lua:

vim.o.foldmethod = "expr"
vim.o.foldexpr = "nvim_treesitter#foldexpr()"
vim.o.foldenable = false

jalvesaq avatar May 17 '23 15:05 jalvesaq

@jalvesaq: Nice! Is there any way Nvim-R could set those automagically (for Rmd files)?

mschilli87 avatar May 17 '23 15:05 mschilli87

I tested it with both Quarto and Rmd file types, and it worked with no need for additional configuration. You should not set the option nofoldenable if you prefer all folds closed on startup. You should use auto commands if you want specific configurations for different file types.

jalvesaq avatar May 17 '23 15:05 jalvesaq

Thank you!

I set those options and now it is folding, but the folds seem to be totally random. It's not neatly folding code blocks or markdown sections - just apparently random chunks. This is probably user error on my part.

gbonett avatar May 17 '23 21:05 gbonett

Did you do the following commands?

:TSInstall r
:TSInstall markdown

Perhaps I have done something in the past that has made it work, but I don't know what it was. Maybe the above commands will help.

jalvesaq avatar May 17 '23 22:05 jalvesaq

I just need folding of the chunks:

vim.cmd([[
autocmd FileType rmd set foldmarker=```{,```
autocmd FileType rmd setlocal foldmethod=marker
]])

lf-araujo avatar Jun 11 '23 20:06 lf-araujo

Nvim-R is being superseded by R.nvim, a new project that will be inaugurated in a week at the R-nvim organization. Please, check out tmp-Nvim-R and help us to find the last remaining bugs before R.nvim inauguration. When the project is officially started, you will be able to open issues there and request new features.

Nvim-R will remain alive as a feature-frozen project for Vim users. If you want to know the reason, please, see this discussion.

Anyway, folding is not a feature under the responsibility of Nvim-R or the future R.nvim...

jalvesaq avatar Feb 18 '24 16:02 jalvesaq