Skip to content

Commit 9b56a57

Browse files
committed
patch 8.0.1493: completion items cannot be annotated
Problem: Completion items cannot be annotated. Solution: Add a "user_data" entry to the completion item. (Ben Jackson, coses #2608, closes #2508)
1 parent b301f6b commit 9b56a57

5 files changed

Lines changed: 137 additions & 6 deletions

File tree

runtime/doc/insert.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*insert.txt* For Vim version 8.0. Last change: 2018 Jan 26
1+
*insert.txt* For Vim version 8.0. Last change: 2018 Feb 10
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1103,6 +1103,8 @@ items:
11031103
item with the same word is already present.
11041104
empty when non-zero this match will be added even when it is
11051105
an empty string
1106+
user_data custom data which is associated with the item and
1107+
available in |v:completed_item|
11061108

11071109
All of these except "icase", "dup" and "empty" must be a string. If an item
11081110
does not meet these requirements then an error message is given and further
@@ -1196,6 +1198,8 @@ The menu is used when:
11961198

11971199
The 'pumheight' option can be used to set a maximum height. The default is to
11981200
use all space available.
1201+
The 'pumwidth' option can be used to set a minimum width. The default is 15
1202+
characters.
11991203

12001204
There are three states:
12011205
1. A complete match has been inserted, e.g., after using CTRL-N or CTRL-P.

src/edit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,8 @@ ins_compl_add_tv(typval_T *tv, int dir)
42364236
(char_u *)"kind", FALSE);
42374237
cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
42384238
(char_u *)"info", FALSE);
4239+
cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4240+
(char_u *)"user_data", FALSE);
42394241
if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
42404242
icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
42414243
if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
@@ -4758,6 +4760,8 @@ ins_compl_insert(int in_compl_func)
47584760
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
47594761
dict_add_nr_str(dict, "info", 0L,
47604762
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4763+
dict_add_nr_str(dict, "user_data", 0L,
4764+
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
47614765
}
47624766
set_vim_var_dict(VV_COMPLETED_ITEM, dict);
47634767
if (!in_compl_func)

src/structs.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3240,11 +3240,12 @@ typedef struct
32403240
/*
32413241
* Array indexes used for cptext argument of ins_compl_add().
32423242
*/
3243-
#define CPT_ABBR 0 /* "abbr" */
3244-
#define CPT_MENU 1 /* "menu" */
3245-
#define CPT_KIND 2 /* "kind" */
3246-
#define CPT_INFO 3 /* "info" */
3247-
#define CPT_COUNT 4 /* Number of entries */
3243+
#define CPT_ABBR 0 /* "abbr" */
3244+
#define CPT_MENU 1 /* "menu" */
3245+
#define CPT_KIND 2 /* "kind" */
3246+
#define CPT_INFO 3 /* "info" */
3247+
#define CPT_USER_DATA 4 /* "user data" */
3248+
#define CPT_COUNT 5 /* Number of entries */
32483249

32493250
typedef struct {
32503251
UINT32_T total[2];

src/testdir/test_ins_complete.vim

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,126 @@ func Test_omni_dash()
117117
set omnifunc=
118118
endfunc
119119

120+
function! s:CompleteDone_CompleteFuncDict( findstart, base )
121+
if a:findstart
122+
return 0
123+
endif
124+
125+
return {
126+
\ 'words': [
127+
\ {
128+
\ 'word': 'aword',
129+
\ 'abbr': 'wrd',
130+
\ 'menu': 'extra text',
131+
\ 'info': 'words are cool',
132+
\ 'kind': 'W',
133+
\ 'user_data': 'test'
134+
\ }
135+
\ ]
136+
\ }
137+
endfunction
138+
139+
function! s:CompleteDone_CheckCompletedItemDict()
140+
call assert_equal( 'aword', v:completed_item[ 'word' ] )
141+
call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
142+
call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
143+
call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
144+
call assert_equal( 'W', v:completed_item[ 'kind' ] )
145+
call assert_equal( 'test', v:completed_item[ 'user_data' ] )
146+
147+
let s:called_completedone = 1
148+
endfunction
149+
150+
function Test_CompleteDoneDict()
151+
au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict()
152+
153+
set completefunc=<SID>CompleteDone_CompleteFuncDict
154+
execute "normal a\<C-X>\<C-U>\<C-Y>"
155+
set completefunc&
156+
157+
call assert_equal( 'test', v:completed_item[ 'user_data' ] )
158+
call assert_true( s:called_completedone )
159+
160+
let s:called_completedone = 0
161+
au! CompleteDone
162+
endfunc
163+
164+
function! s:CompleteDone_CompleteFuncDictNoUserData( findstart, base )
165+
if a:findstart
166+
return 0
167+
endif
168+
169+
return {
170+
\ 'words': [
171+
\ {
172+
\ 'word': 'aword',
173+
\ 'abbr': 'wrd',
174+
\ 'menu': 'extra text',
175+
\ 'info': 'words are cool',
176+
\ 'kind': 'W'
177+
\ }
178+
\ ]
179+
\ }
180+
endfunction
181+
182+
function! s:CompleteDone_CheckCompletedItemDictNoUserData()
183+
call assert_equal( 'aword', v:completed_item[ 'word' ] )
184+
call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
185+
call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
186+
call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
187+
call assert_equal( 'W', v:completed_item[ 'kind' ] )
188+
call assert_equal( '', v:completed_item[ 'user_data' ] )
189+
190+
let s:called_completedone = 1
191+
endfunction
192+
193+
function Test_CompleteDoneDictNoUserData()
194+
au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
195+
196+
set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
197+
execute "normal a\<C-X>\<C-U>\<C-Y>"
198+
set completefunc&
199+
200+
call assert_equal( '', v:completed_item[ 'user_data' ] )
201+
call assert_true( s:called_completedone )
202+
203+
let s:called_completedone = 0
204+
au! CompleteDone
205+
endfunc
206+
207+
function! s:CompleteDone_CompleteFuncList( findstart, base )
208+
if a:findstart
209+
return 0
210+
endif
211+
212+
return [ 'aword' ]
213+
endfunction
214+
215+
function! s:CompleteDone_CheckCompletedItemList()
216+
call assert_equal( 'aword', v:completed_item[ 'word' ] )
217+
call assert_equal( '', v:completed_item[ 'abbr' ] )
218+
call assert_equal( '', v:completed_item[ 'menu' ] )
219+
call assert_equal( '', v:completed_item[ 'info' ] )
220+
call assert_equal( '', v:completed_item[ 'kind' ] )
221+
call assert_equal( '', v:completed_item[ 'user_data' ] )
222+
223+
let s:called_completedone = 1
224+
endfunction
225+
226+
function Test_CompleteDoneList()
227+
au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
228+
229+
set completefunc=<SID>CompleteDone_CompleteFuncList
230+
execute "normal a\<C-X>\<C-U>\<C-Y>"
231+
set completefunc&
232+
233+
call assert_equal( '', v:completed_item[ 'user_data' ] )
234+
call assert_true( s:called_completedone )
235+
236+
let s:called_completedone = 0
237+
au! CompleteDone
238+
endfunc
239+
120240
" Check that when using feedkeys() typeahead does not interrupt searching for
121241
" completions.
122242
func Test_compl_feedkeys()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static char *(features[]) =
771771

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1493,
774776
/**/
775777
1492,
776778
/**/

0 commit comments

Comments
 (0)