When writing C code, I use a mixture of GNU and K&R style. This means that the return value, each parameter and the opening body curly brace of a function is on its own line. I would also like to use Vim's folding feature but with foldmethod=syntax
, the folding looks like this:
Is it possible to see the function name in the fold summary without any special fold markers or foldexpr
s?
Something which might be a good compromise - if you use the indent
fold - is to set the foldminlines
parameter to a higher number.
:set foldmethod=indent
:set foldminlines=5
If most of your functions are long, it will affect only your list of parameters. The downside obviously is, that it will automatically unfold also small functions which are smaller then 5 lines long.
Try this as a starting point (I have it in my vimrc but I found it online):
" Folding {
function! CssFoldText()
let line = getline(v:foldstart)
let nnum = nextnonblank(v:foldstart + 1)
while nnum < v:foldend+1
let line = line . " " . substitute(getline(nnum), "^ *", "", "g")
let nnum = nnum + 1
endwhile
return line
endfunction
setlocal foldtext=CssFoldText()
setlocal foldmethod=marker
setlocal foldmarker={,}
setlocal fillchars=fold:/
setlocal foldlevel=-1
" highlight Folded term=underline cterm=bold gui=bold guifg=Blue guibg=Black
" highlight FoldColumn term=underline cterm=bold gui=bold guifg=Blue guibg=Black
"}
It's possible to do it also with syntax folding. You need to add the following to ~/.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
But beware, if file is big, recalculating folds may be quite heavy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.