c++vimomnicppcomplete

How to let omnicppcomplete automatically close empty argument lists?


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?


Solution

  • 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.