matt
matt

Reputation: 83

Make vim keyword completion menu show function parameters

Problem

I want to make the vim keyword completion menu show the parameters of the functions it proposes to me. I would like it to look something like this :

screenshot of vim omni completion using ctags

This is omni-completion using ctags, it is great but it is slow, so i can't use it with a large tags file.

Issues

Here's what my current keyword completion using ctags looks like (set complete=t) :

screenshot of vim keyword completion using ctags

My research

I tried to look at vim's doc (ins-completion, 'complete', 'completeopt', 'completefunc', 'omnifunc', 'ft-c-omni') but the only way to achieve my goal seems to write my own 'completefunc' what i don't feel like to do.

However i found that i can get a popup menu showing the informations i want, but with my current configuration (set completeopt=menuone,noinsert,popup) it shows up only in omni completion which, again, is too slow.

Upvotes: 3

Views: 1068

Answers (1)

matt
matt

Reputation: 83

I didn't manage to get vim keyword completion to behave as i want so i browsed the vim documentation (:h tag, popup, popup_atcursor) and i came to the conclusion that creating my own simple popup should do the trick.

Code

" get the parameters of a function and put it in a popup using ctags
func GetFuncParamsFromTag()
    silent write
    " jump to tag under cursor
    silent execute "normal \<c-]>"
    " if there is '(' on the same line, it may be a function
    if search('(', "n") == winsaveview()["lnum"]
        " yank the function's name and parameters
        silent execute "normal v/)\<cr>y\<c-t>"
        " remove any previously present popup
        call popup_clear()
        " make the popup spawn above/below the cursor
        call popup_atcursor(getreg('0'), #{moved: [0, 80], highlight: 'WildMenu'})
    endif
endfunc

nnoremap <silent> <leader>? :call GetFuncParamsFromTag()<cr>

Preview

It looks like this :

screenshot

You just have to press <leader>? on a function's name in Normal mode and you get a nice little popup showing only the prototype of the function.

EDIT:

I found a workaround to get this to work when i use vim completion.

autocmd CompleteDone * execute "normal ^,?" | call feedkeys("\<esc>:autocmd! InsertLeave * ++once call popup_clear()\<cr>A")

This autocmd brings a popup whenever you complete a word, so if you type myImcompleteFunctionTag<c-]><c-y>
it will complete your tag, then open the popup, and leave you in insert mode at the end of the line. The rest of the autocmd closes the popup the next time you leave insert mode.

Upvotes: 1

Related Questions