1

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)

  • Note that your VimScript isn't valid at all. There are no curly braces in VimScript; the end of a function is denoted with endfunction. You're also missing an endif. 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)? – Martin Tournoij Apr 15 at 23:58
  • @MartinTournoij Thanks, I've corrected the mistakes in the VimScript. – husB Apr 16 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.