vim-patch:8.1.1138: add CompleteChanged #10644

(This was originally a Neovim patch, but this commit merges some changes
from the Vim patch.)

d7f246c68c
This commit is contained in:
Justin M. Keyes
2019-07-29 02:36:46 +02:00
committed by GitHub
parent 505f47403b
commit caa8c06bae
5 changed files with 79 additions and 31 deletions

View File

@@ -590,18 +590,19 @@ CompleteDone After Insert mode completion is done. Either
completed item. completed item.
CompleteChanged *CompleteChanged* CompleteChanged *CompleteChanged*
After each time popup menu changed, not fired After each time the Insert mode completion
on popup menu hide, use |CompleteDone| for popup menu changed. Not fired on popup menu hide,
menu hide. use |CompleteDone| for that. Never triggered
recursively.
Sets these |v:event| keys: Sets these |v:event| keys:
completed_item completed_item
height height nr of items visible
width width screen cells
row row top screen row
col col leftmost screen column
size size total nr of items
scrollbar scrollbar TRUE if visible
It is not allowed to change the text |textlock|. It is not allowed to change the text |textlock|.

View File

@@ -2580,10 +2580,35 @@ static bool pum_enough_matches(void)
return i >= 2; return i >= 2;
} }
/* static void trigger_complete_changed_event(int cur)
* Show the popup menu for the list of matches. {
* Also adjusts "compl_shown_match" to an entry that is actually displayed. static bool recursive = false;
*/
if (recursive) {
return;
}
dict_T *v_event = get_vim_var_dict(VV_EVENT);
if (cur < 0) {
tv_dict_add_dict(v_event, S_LEN("completed_item"), tv_dict_alloc());
} else {
dict_T *item = ins_compl_dict_alloc(compl_curr_match);
tv_dict_add_dict(v_event, S_LEN("completed_item"), item);
}
pum_set_event_info(v_event);
tv_dict_set_keys_readonly(v_event);
recursive = true;
textlock++;
apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, false, curbuf);
textlock--;
recursive = false;
tv_dict_clear(v_event);
}
/// Show the popup menu for the list of matches.
/// Also adjusts "compl_shown_match" to an entry that is actually displayed.
void ins_compl_show_pum(void) void ins_compl_show_pum(void)
{ {
compl_T *compl; compl_T *compl;
@@ -2715,22 +2740,9 @@ void ins_compl_show_pum(void)
pum_display(compl_match_array, compl_match_arraysize, cur, array_changed, 0); pum_display(compl_match_array, compl_match_arraysize, cur, array_changed, 0);
curwin->w_cursor.col = col; curwin->w_cursor.col = col;
if (!has_event(EVENT_COMPLETECHANGED)) { if (has_event(EVENT_COMPLETECHANGED)) {
return; trigger_complete_changed_event(cur);
} }
dict_T *dict = get_vim_var_dict(VV_EVENT);
if (cur < 0) {
tv_dict_add_dict(dict, S_LEN("completed_item"), tv_dict_alloc());
} else {
dict_T *item = ins_compl_dict_alloc(compl_curr_match);
tv_dict_add_dict(dict, S_LEN("completed_item"), item);
}
pum_set_boundings(dict);
tv_dict_set_keys_readonly(dict);
textlock++;
apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, false, curbuf);
textlock--;
tv_dict_clear(dict);
} }
#define DICT_FIRST (1) /* use just first element in "dict" */ #define DICT_FIRST (1) /* use just first element in "dict" */

View File

@@ -1402,7 +1402,7 @@ int op_delete(oparg_T *oap)
yankreg_T *reg = NULL; yankreg_T *reg = NULL;
int did_yank = false; int did_yank = false;
if (oap->regname != 0) { if (oap->regname != 0) {
//yank without message // yank without message
did_yank = op_yank(oap, false, true); did_yank = op_yank(oap, false, true);
if (!did_yank) { if (!did_yank) {
// op_yank failed, don't do anything // op_yank failed, don't do anything
@@ -3447,7 +3447,7 @@ void ex_display(exarg_T *eap)
MSG_PUTS_ATTR("^J", attr); MSG_PUTS_ATTR("^J", attr);
n -= 2; n -= 2;
} }
for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; p++) { // -V1019 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; p++) { // -V1019 NOLINT(whitespace/line_length)
clen = (*mb_ptr2len)(p); clen = (*mb_ptr2len)(p);
msg_outtrans_len(p, clen); msg_outtrans_len(p, clen);
p += clen - 1; p += clen - 1;

View File

@@ -855,7 +855,8 @@ int pum_get_height(void)
return pum_height; return pum_height;
} }
void pum_set_boundings(dict_T *dict) /// Add size information about the pum to "dict".
void pum_set_event_info(dict_T *dict)
{ {
if (!pum_visible()) { if (!pum_visible()) {
return; return;

View File

@@ -841,4 +841,38 @@ func Test_popup_complete_info_02()
bwipe! bwipe!
endfunc endfunc
func Test_CompleteChanged()
new
call setline(1, ['foo', 'bar', 'foobar', ''])
set complete=. completeopt=noinsert,noselect,menuone
function! OnPumChange()
let g:event = copy(v:event)
let g:item = get(v:event, 'completed_item', {})
let g:word = get(g:item, 'word', v:null)
endfunction
augroup AAAAA_Group
au!
autocmd CompleteChanged * :call OnPumChange()
augroup END
call cursor(4, 1)
call feedkeys("Sf\<C-N>", 'tx')
call assert_equal({'completed_item': {}, 'width': 15,
\ 'height': 2, 'size': 2,
\ 'col': 0, 'row': 4, 'scrollbar': v:false}, g:event)
call feedkeys("a\<C-N>\<C-N>\<C-E>", 'tx')
call assert_equal('foo', g:word)
call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
call assert_equal('foobar', g:word)
call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
call assert_equal(v:null, g:word)
call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 'tx')
call assert_equal('foobar', g:word)
autocmd! AAAAA_Group
set complete& completeopt&
delfunc! OnPumchange
bw!
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab