Skip to content

Commit

Permalink
Add support for the pum_getpos() API (#11562)
Browse files Browse the repository at this point in the history
Add support for the pum_getpos() API
  • Loading branch information
sethfowler authored and bfredl committed Dec 16, 2019
1 parent 473aea9 commit 251b20e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions runtime/doc/autocmd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,9 @@ CompleteChanged *CompleteChanged*

It is not allowed to change the text |textlock|.

The size and position of the popup are also
available by calling |pum_getpos()|.

*CursorHold*
CursorHold When the user doesn't press a key for the time
specified with 'updatetime'. Not re-triggered
Expand Down
17 changes: 17 additions & 0 deletions runtime/doc/eval.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,7 @@ pathshorten({expr}) String shorten directory names in a path
pow({x}, {y}) Float {x} to the power of {y}
prevnonblank({lnum}) Number line nr of non-blank line <= {lnum}
printf({fmt}, {expr1}...) String format text
pum_getpos() Dict position and size of pum if visible
pumvisible() Number whether popup menu is visible
pyeval({expr}) any evaluate |Python| expression
py3eval({expr}) any evaluate |python3| expression
Expand Down Expand Up @@ -3120,6 +3121,9 @@ complete_info([{what}])
the items listed in {what} are returned. Unsupported items in
{what} are silently ignored.

To get the position of the popup menu, see |pum_getpos()|. It's
also available in |v:event| during the |CompleteChanged| event.

Examples: >
" Get all items
call complete_info()
Expand Down Expand Up @@ -6521,6 +6525,19 @@ printf({fmt}, {expr1} ...) *printf()*
arguments an error is given. Up to 18 arguments can be used.


pum_getpos() *pum_getpos()*
If the popup menu (see |ins-completion-menu|) is not visible,
returns an empty |Dictionary|, otherwise, returns a
|Dictionary| with the following keys:
height nr of items visible
width screen cells
row top screen row (0 first row)
col leftmost screen column (0 first col)
size total nr of items
scrollbar |TRUE| if visible

The values are the same as in |v:event| during |CompleteChanged|.

pumvisible() *pumvisible()*
Returns non-zero when the popup menu is visible, zero
otherwise. See |ins-completion-menu|.
Expand Down
7 changes: 7 additions & 0 deletions src/nvim/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -13972,6 +13972,13 @@ static void f_printf(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
}

// "pum_getpos()" function
static void f_pum_getpos(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
tv_dict_alloc_ret(rettv);
pum_set_event_info(rettv->vval.v_dict);
}

/*
* "pumvisible()" function
*/
Expand Down
1 change: 1 addition & 0 deletions src/nvim/eval.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ return {
pow={args=2},
prevnonblank={args=1},
printf={args=varargs(1)},
pum_getpos={},
pumvisible={},
py3eval={args=1},
pyeval={args=1},
Expand Down
42 changes: 42 additions & 0 deletions src/nvim/testdir/test_popup.vim
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,20 @@ func Test_popup_complete_info_02()
bwipe!
endfunc

func Test_popup_complete_info_no_pum()
new
call assert_false( pumvisible() )
let no_pum_info = complete_info()
let d = {
\ 'mode': '',
\ 'pum_visible': 0,
\ 'items': [],
\ 'selected': -1,
\ }
call assert_equal( d, complete_info() )
bwipe!
endfunc

func Test_CompleteChanged()
new
call setline(1, ['foo', 'bar', 'foobar', ''])
Expand Down Expand Up @@ -952,4 +966,32 @@ func Test_CompleteChanged()
bw!
endfunc

function! GetPumPosition()
call assert_true( pumvisible() )
let g:pum_pos = pum_getpos()
return ''
endfunction

func Test_pum_getpos()
new
inoremap <buffer><F5> <C-R>=GetPumPosition()<CR>
setlocal completefunc=UserDefinedComplete

let d = {
\ 'height': 5,
\ 'width': 15,
\ 'row': 1,
\ 'col': 0,
\ 'size': 5,
\ 'scrollbar': v:false,
\ }
call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
call assert_equal(d, g:pum_pos)

call assert_false( pumvisible() )
call assert_equal( {}, pum_getpos() )
bw!
unlet g:pum_pos
endfunc

" vim: shiftwidth=2 sts=2 expandtab

0 comments on commit 251b20e

Please sign in to comment.