2

I have these two functions

  void function_one(){
    return;
  }

  void function_two()
  {
    return;
  }

when I use zM to fold the functions I get

  +--  3 lines: void function_one(){--

  void function_two()
  +--  3 lines: {-

the function_two doesn't get a nice folding, How I can get the same folding as for function_one in function_two?

PD: I use two code styles, curly brackets on same line as function name and curly braces under the function name, because some projects use one code style and other projects use the other code style.

2
  • I assume this is C or C++? Nov 10, 2016 at 18:15
  • @Carpetsmoker yes, both c and c++
    – juanpablo
    Nov 10, 2016 at 19:03

2 Answers 2

1

I did define VimFold4C that tries its best to fold C & C++ codes. The caveat is when we are modifying the code: I'm using caches in order to speed up the fold processing, but alas it has quirks.

0

You can add the following into ~/.vim/after/syntax/c.vim:

let s:contains = ''
if exists("c_curly_error")
  let s:contains = ' contains=ALLBUT,cBadBlock,cCurlyError,@cParenGroup,cErrInParen,cErrInBracket,@cStringGroup,@Spell'
endif

let s:pattern = '%('

if &ft ==# "cpp"
  " struct/class inheriting
  let s:pattern .= ''
        \ . '%(<struct|<class)@<='
        \ . '\s\ze\s*\S+[^:]:[^:]\s*\S+.*'
  let s:pattern .= '|'

  " Constructors
  let s:pattern .= ''
        \ . '%('
        \ .    '%([^,:]|\n|^|<%(public|private|protected)>\s*:)'
        \ .    '\n\s*'
        \ . ')@<='
        \ . '%(<%(while|for|if|switch|catch)>)@!'
        \ . '\S\ze\S*%(::\S+)*\s*\(.*\)\s*%(:.*)?'
  let s:pattern .= '|'
endif

let s:pattern .= '%(<%(while|for|if|switch|catch)\(.*)@<=\)\ze\s*' . '|'

let s:pattern .= ''
      \ . '%('
      \ .    '^\s*%(//.*|.*\*/|\{|<%(public|private|protected)>\s*:|.*\>)?'
      \ .    '\s*\n\s*\S+'
      \ . ')@<='
      \ . '\s\ze\s*\S+\s*'
      \ . '%(.*[^:]:[^:].*)@!'
      \ . '%(\s+\S+)*'

let s:pattern .= ')%(;\s*)@<!%(//.*|/\*.*\*/)?\n\s*'

syn clear cBlock
exec 'syn region cBlock_ end="}" fold' . s:contains
      \ . ' start = "\%#=1\C\v' . s:pattern . '\{"'
      \ . ' start = "\%#=1\C\v%(' . s:pattern . ')@<!\{"'

unlet s:contains s:pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.