views:

220

answers:

1

Is it possible to let Vim's omnicppcomplete automatically close argument lists for functions or methods that do not take any arguments?

For example, assuming v is an STL vector, when auto completing v.clear(), we end up with:

v.clear(

It would be nice if the closing parenthesis would be automatically added. Is this possible?

+3  A: 

It looks like it should be possible: I'm not sure whether I have the latest version of the omnicppcomplete script, but in my autoload/omni/cpp/complete.vim, there is a function called s:ExtendTagItemToPopupItem. In this function, there is:

" Formating information for the preview window
if index(['f', 'p'], tagItem.kind[0])>=0
    let szItemWord .= '('
    if g:OmniCpp_ShowPrototypeInAbbr && has_key(tagItem, 'signature')
        let szAbbr .= tagItem.signature
    else
        let szAbbr .= '('
    endif
endif

After the line (#165 in my version) let szItemWord .= '(', add:

    if (has_key(tagItem, 'signature') == 0) || (tagItem['signature'] =~ '()')
        let szItemWord .= ')'
    endif

That should do the trick (although I don't use C++ much, so I haven't tested it extensively). It basically checks whether the "signature" of the function contains "()" as opposed to (for example) "(int *major, int *minor)". If the brackets are empty, it adds a closing brace.

It could probably be improved by changing '()' to '(\s*\(void\)\?\s*)' for completeness: this would check for "()", "( )", "(void)", "( void )" etc.

Al
Thanks! Works like a charm. I think this functionality is a good candidate for a new omnicppcomplete patch. I'll start working on it when I have the time with your permission.There is room for some more improvement. I would like the popup to remain visible when filling in method arguments. Escpecially for long argument lists this can come in handy.
Ton van den Heuvel
I agree that this would make the completion much better. It's possible that if you suggest this to the author of omnicppcomplete, they can offer some suggestions on how to do (I worked the above out by just digging around).
Al