I would like to create a custom fold expression for functions of the form
x <- function(a,b,c) {
...
... body
...
}
where the lines starting from function(...) until the closing brace are to have the same fold level. Yet, I do not want code in other braces to be folded (such as if blocks).
How can I do this?
(For those who are wondering, this is r syntax)
For the start of the function, I have tried
function! FoldExpr(lnum)
let thisline = getline(a:lnum)
if thisline =~ '[^#].*function(.*)\s*{'
return 'a1'
elseif "... condition to check end of function ...
" ...
endif
endfunction
However, I am unsure how to check if a line is the last line of the function.
I am able to check if the line contains a closing }
. But how can I tell if it is the closing brace of the function?
(BTW, I am using a fold expression instead of foldmethod=syntax
as I would like to fold other objects as well, not just functions)
endfunction
. You're also missing anendif
. The Nvim-R plugin already seems to do what you want, so you may be better off using that (or lifting the relevant parts from it)?