nowox
nowox

Reputation: 29166

How to use folds in vim with c like programs

I recently started using folding with vim. Unfortunately it is not very useful in the case I declare my functions like this:

/**
 * A function that do something.
 * @param a   A
 * @param b   B
 * @return    Something
 * 
 **/
int i_do_something(int a, int b) {
   something();
}

In my dreams, I would like my folds to be like this:

+-- 10 lines: A function that do something.
+-- 42 lines: Another function.

And not something like this:

+-- 7 lines: *
int i_do_something(int a, int b) {
   something();
}

I tried to use the marker method but it doesn't work how I expected.

How can I properly use folds with vim in a C like program ?

Upvotes: 0

Views: 108

Answers (2)

anon
anon

Reputation:

Here's a custom fold function to fold as you asked. Copy to .vimrc and enjoy! :)

Using this function, fold text on comments beginning with /** will display the text on the second line of the comment with the indent and * removed. You'll end up with something like: (5 lines) Description. Regular C style /* comments have a similar display but with the first line text. All other folds will still display the all the first line as usual.

set foldtext=FoldText()

function FoldText()
    let text=""
    let region = synIDattr(synID(a:line, a:column, 1),"name")

    let text = "(" . (v:foldend - v:foldstart) . ' lines) '

    if region == 'cCommentStart' && 
    \ match(getline(v:foldstart), "/*\\*\\*") != -1
        let text = text . substitute(getline(v:foldstart + 1), '^\s* \*', '', '')
    elseif region == 'cCommentStart' && 
    \ match(getline(v:foldstart), "/*\\*\\*") == -1
        let text = text . substitute(getline(v:foldstart), '/\*', '', '')
    else
        let text = text . getline(v:foldstart)
    endif

    return text
endfunction

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172748

You can influence what gets displayed in the closed fold via the 'foldtext' option. Maybe someone already has written such; else, you have to write a Vim function that also considers the next (folded) lines, and extracts the text you want to see.

Upvotes: 1

Related Questions