]> granicus.if.org Git - vim/commitdiff
patch 8.0.1493: completion items cannot be annotated v8.0.1493
authorBram Moolenaar <Bram@vim.org>
Sat, 10 Feb 2018 15:19:32 +0000 (16:19 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 10 Feb 2018 15:19:32 +0000 (16:19 +0100)
Problem:    Completion items cannot be annotated.
Solution:   Add a "user_data" entry to the completion item. (Ben Jackson,
            coses #2608, closes #2508)

runtime/doc/insert.txt
src/edit.c
src/structs.h
src/testdir/test_ins_complete.vim
src/version.c

index 73719113ff3bf2a011fb9ebf4b875c461baf5d3e..2f485102bb061838398211923a5d6c4c9869f00a 100644 (file)
@@ -1,4 +1,4 @@
-*insert.txt*    For Vim version 8.0.  Last change: 2018 Jan 26
+*insert.txt*    For Vim version 8.0.  Last change: 2018 Feb 10
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -1103,6 +1103,8 @@ items:
                        item with the same word is already present.
        empty           when non-zero this match will be added even when it is
                        an empty string
+       user_data       custom data which is associated with the item and
+                       available in |v:completed_item|
 
 All of these except "icase", "dup" and "empty" must be a string.  If an item
 does not meet these requirements then an error message is given and further
@@ -1196,6 +1198,8 @@ The menu is used when:
 
 The 'pumheight' option can be used to set a maximum height.  The default is to
 use all space available.
+The 'pumwidth' option can be used to set a minimum width.  The default is 15
+characters.
 
 There are three states:
 1. A complete match has been inserted, e.g., after using CTRL-N or CTRL-P.
index fa1d84bbc7a685f42c26e90f2631cfcf8dfb5a2f..a9e6343a806c3c932f5459db8c780d7b1c9ecd93 100644 (file)
@@ -4236,6 +4236,8 @@ ins_compl_add_tv(typval_T *tv, int dir)
                                                     (char_u *)"kind", FALSE);
        cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
                                                     (char_u *)"info", FALSE);
+       cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
+                                                (char_u *)"user_data", FALSE);
        if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
            icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
        if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
@@ -4758,6 +4760,8 @@ ins_compl_insert(int in_compl_func)
                    EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
        dict_add_nr_str(dict, "info", 0L,
                    EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
+       dict_add_nr_str(dict, "user_data", 0L,
+                   EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
     }
     set_vim_var_dict(VV_COMPLETED_ITEM, dict);
     if (!in_compl_func)
index 6aaccc51191504e16267f4b672249a3580083bca..abfb73176fc27cab205de2bb2229804fe9a71f3c 100644 (file)
@@ -3240,11 +3240,12 @@ typedef struct
 /*
  * Array indexes used for cptext argument of ins_compl_add().
  */
-#define CPT_ABBR    0  /* "abbr" */
-#define CPT_MENU    1  /* "menu" */
-#define CPT_KIND    2  /* "kind" */
-#define CPT_INFO    3  /* "info" */
-#define CPT_COUNT   4  /* Number of entries */
+#define CPT_ABBR       0       /* "abbr" */
+#define CPT_MENU       1       /* "menu" */
+#define CPT_KIND       2       /* "kind" */
+#define CPT_INFO       3       /* "info" */
+#define CPT_USER_DATA  4       /* "user data" */
+#define CPT_COUNT      5       /* Number of entries */
 
 typedef struct {
   UINT32_T total[2];
index 652b1d9e4213055d2dd1ddeaf542cb15b89a4223..020e2288201b1f62ee68f2e36a727cf81d1b688a 100644 (file)
@@ -117,6 +117,126 @@ func Test_omni_dash()
   set omnifunc=
 endfunc
 
+function! s:CompleteDone_CompleteFuncDict( findstart, base )
+  if a:findstart
+    return 0
+  endif
+
+  return {
+          \ 'words': [
+            \ {
+              \ 'word': 'aword',
+              \ 'abbr': 'wrd',
+              \ 'menu': 'extra text',
+              \ 'info': 'words are cool',
+              \ 'kind': 'W',
+              \ 'user_data': 'test'
+            \ }
+          \ ]
+        \ }
+endfunction
+
+function! s:CompleteDone_CheckCompletedItemDict()
+  call assert_equal( 'aword',          v:completed_item[ 'word' ] )
+  call assert_equal( 'wrd',            v:completed_item[ 'abbr' ] )
+  call assert_equal( 'extra text',     v:completed_item[ 'menu' ] )
+  call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
+  call assert_equal( 'W',              v:completed_item[ 'kind' ] )
+  call assert_equal( 'test',           v:completed_item[ 'user_data' ] )
+
+  let s:called_completedone = 1
+endfunction
+
+function Test_CompleteDoneDict()
+  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict()
+
+  set completefunc=<SID>CompleteDone_CompleteFuncDict
+  execute "normal a\<C-X>\<C-U>\<C-Y>"
+  set completefunc&
+
+  call assert_equal( 'test', v:completed_item[ 'user_data' ] )
+  call assert_true( s:called_completedone )
+
+  let s:called_completedone = 0
+  au! CompleteDone
+endfunc
+
+function! s:CompleteDone_CompleteFuncDictNoUserData( findstart, base )
+  if a:findstart
+    return 0
+  endif
+
+  return {
+          \ 'words': [
+            \ {
+              \ 'word': 'aword',
+              \ 'abbr': 'wrd',
+              \ 'menu': 'extra text',
+              \ 'info': 'words are cool',
+              \ 'kind': 'W'
+            \ }
+          \ ]
+        \ }
+endfunction
+
+function! s:CompleteDone_CheckCompletedItemDictNoUserData()
+  call assert_equal( 'aword',          v:completed_item[ 'word' ] )
+  call assert_equal( 'wrd',            v:completed_item[ 'abbr' ] )
+  call assert_equal( 'extra text',     v:completed_item[ 'menu' ] )
+  call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
+  call assert_equal( 'W',              v:completed_item[ 'kind' ] )
+  call assert_equal( '',               v:completed_item[ 'user_data' ] )
+
+  let s:called_completedone = 1
+endfunction
+
+function Test_CompleteDoneDictNoUserData()
+  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
+
+  set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
+  execute "normal a\<C-X>\<C-U>\<C-Y>"
+  set completefunc&
+
+  call assert_equal( '', v:completed_item[ 'user_data' ] )
+  call assert_true( s:called_completedone )
+
+  let s:called_completedone = 0
+  au! CompleteDone
+endfunc
+
+function! s:CompleteDone_CompleteFuncList( findstart, base )
+  if a:findstart
+    return 0
+  endif
+
+  return [ 'aword' ]
+endfunction
+
+function! s:CompleteDone_CheckCompletedItemList()
+  call assert_equal( 'aword', v:completed_item[ 'word' ] )
+  call assert_equal( '',      v:completed_item[ 'abbr' ] )
+  call assert_equal( '',      v:completed_item[ 'menu' ] )
+  call assert_equal( '',      v:completed_item[ 'info' ] )
+  call assert_equal( '',      v:completed_item[ 'kind' ] )
+  call assert_equal( '',      v:completed_item[ 'user_data' ] )
+
+  let s:called_completedone = 1
+endfunction
+
+function Test_CompleteDoneList()
+  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
+
+  set completefunc=<SID>CompleteDone_CompleteFuncList
+  execute "normal a\<C-X>\<C-U>\<C-Y>"
+  set completefunc&
+
+  call assert_equal( '', v:completed_item[ 'user_data' ] )
+  call assert_true( s:called_completedone )
+
+  let s:called_completedone = 0
+  au! CompleteDone
+endfunc
+
 " Check that when using feedkeys() typeahead does not interrupt searching for
 " completions.
 func Test_compl_feedkeys()
index be5f864ecfe31e2ebcc4c0fd24ed980ac1391504..261e3d7c699e81eaae46e92d38641e8c13010f5c 100644 (file)
@@ -771,6 +771,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1493,
 /**/
     1492,
 /**/