]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1020: no way to get current selected item in a async context v9.1.1020
authorglepnir <glephunter@gmail.com>
Thu, 16 Jan 2025 13:37:44 +0000 (14:37 +0100)
committerChristian Brabandt <cb@256bit.org>
Thu, 16 Jan 2025 13:37:44 +0000 (14:37 +0100)
Problem:  no way to get current selected item in a async context
Solution: add completed flag to show the entries of currently selected
          index item (@glepnir)

closes: #16451

Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/builtin.txt
runtime/doc/todo.txt
src/insexpand.c
src/testdir/test_ins_complete.vim
src/version.c

index 9ba22dc742d35380d54f9c4564d6b05101080cc1..9d56f5082231519b0da95e95dc26f6dce6418bed 100644 (file)
@@ -1,4 +1,4 @@
-*builtin.txt*  For Vim version 9.1.  Last change: 2025 Jan 15
+*builtin.txt*  For Vim version 9.1.  Last change: 2025 Jan 16
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -1946,7 +1946,8 @@ complete_info([{what}])                           *complete_info()*
                                typed text only, or the last completion after
                                no item is selected when using the <Up> or
                                <Down> keys)
-                  inserted     Inserted string. [NOT IMPLEMENTED YET]
+                  completed    Return a dictionary containing the entries of
+                               the currently selected index item.
 
                                                        *complete_info_mode*
                mode values are:
index 48937b104143123a438f8eb51d02f61dab308dcc..2d48cc974d5481d996d63ca08ae3480713f2a095 100644 (file)
@@ -1,4 +1,4 @@
-*todo.txt*      For Vim version 9.1.  Last change: 2024 Dec 30
+*todo.txt*      For Vim version 9.1.  Last change: 2025 Jan 16
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -309,9 +309,6 @@ Problem with Visual highlight when 'linebreak' and 'showbreak' are set.
 GUI Scroll test fails on FreeBSD when using Motif.  See FIXME in
 Test_scrollbars in src/test_gui.vim
 
-Selected index returned by complete_info() does not match the index in the
-list of items.  #12230
-
 Support dark mode for MS-Windows: #12282
 
 Remote command escapes single quote with backslash, should be doubling the
index 4a02f0ec8eb8ccb0acd71a5d8c16f6ec72555745..e33e15d43bca00268b839a2069f17470fa19af17 100644 (file)
@@ -3252,6 +3252,26 @@ ins_compl_update_sequence_numbers(void)
     }
 }
 
+/*
+ * Fill the dict of complete_info
+ */
+    static void
+fill_complete_info_dict(dict_T *di, compl_T *match, int add_match)
+{
+    dict_add_string(di, "word", match->cp_str.string);
+    dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
+    dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
+    dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
+    dict_add_string(di, "info", match->cp_text[CPT_INFO]);
+    if (add_match)
+        dict_add_bool(di, "match", match->cp_in_match_array);
+    if (match->cp_user_data.v_type == VAR_UNKNOWN)
+        // Add an empty string for backwards compatibility
+        dict_add_string(di, "user_data", (char_u *)"");
+    else
+        dict_add_tv(di, "user_data", &match->cp_user_data);
+}
+
 /*
  * Get complete information
  */
@@ -3264,13 +3284,13 @@ get_complete_info(list_T *what_list, dict_T *retdict)
 #define CI_WHAT_PUM_VISIBLE    0x02
 #define CI_WHAT_ITEMS          0x04
 #define CI_WHAT_SELECTED       0x08
-#define CI_WHAT_INSERTED       0x10
+#define CI_WHAT_COMPLETED      0x10
 #define CI_WHAT_MATCHES                0x20
 #define CI_WHAT_ALL            0xff
     int                what_flag;
 
     if (what_list == NULL)
-       what_flag = CI_WHAT_ALL & ~CI_WHAT_MATCHES;
+       what_flag = CI_WHAT_ALL & ~(CI_WHAT_MATCHES | CI_WHAT_COMPLETED);
     else
     {
        what_flag = 0;
@@ -3287,8 +3307,8 @@ get_complete_info(list_T *what_list, dict_T *retdict)
                what_flag |= CI_WHAT_ITEMS;
            else if (STRCMP(what, "selected") == 0)
                what_flag |= CI_WHAT_SELECTED;
-           else if (STRCMP(what, "inserted") == 0)
-               what_flag |= CI_WHAT_INSERTED;
+           else if (STRCMP(what, "completed") == 0)
+               what_flag |= CI_WHAT_COMPLETED;
            else if (STRCMP(what, "matches") == 0)
                what_flag |= CI_WHAT_MATCHES;
        }
@@ -3300,8 +3320,8 @@ get_complete_info(list_T *what_list, dict_T *retdict)
     if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
        ret = dict_add_number(retdict, "pum_visible", pum_visible());
 
-    if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED
-               || what_flag & CI_WHAT_MATCHES))
+    if (ret == OK && (what_flag & (CI_WHAT_ITEMS | CI_WHAT_SELECTED
+                                   | CI_WHAT_MATCHES | CI_WHAT_COMPLETED)))
     {
        list_T      *li = NULL;
        dict_T      *di;
@@ -3309,6 +3329,7 @@ get_complete_info(list_T *what_list, dict_T *retdict)
        int         selected_idx = -1;
        int         has_items = what_flag & CI_WHAT_ITEMS;
        int         has_matches = what_flag & CI_WHAT_MATCHES;
+       int         has_completed = what_flag & CI_WHAT_COMPLETED;
 
        if (has_items || has_matches)
        {
@@ -3338,18 +3359,7 @@ get_complete_info(list_T *what_list, dict_T *retdict)
                        ret = list_append_dict(li, di);
                        if (ret != OK)
                            return;
-                       dict_add_string(di, "word", match->cp_str.string);
-                       dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
-                       dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
-                       dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
-                       dict_add_string(di, "info", match->cp_text[CPT_INFO]);
-                       if (has_matches && has_items)
-                           dict_add_bool(di, "match", match->cp_in_match_array);
-                       if (match->cp_user_data.v_type == VAR_UNKNOWN)
-                           // Add an empty string for backwards compatibility
-                           dict_add_string(di, "user_data", (char_u *)"");
-                       else
-                           dict_add_tv(di, "user_data", &match->cp_user_data);
+                       fill_complete_info_dict(di, match, has_matches && has_items);
                    }
                    if (compl_curr_match != NULL
                            && compl_curr_match->cp_number == match->cp_number)
@@ -3362,11 +3372,15 @@ get_complete_info(list_T *what_list, dict_T *retdict)
        }
        if (ret == OK && (what_flag & CI_WHAT_SELECTED))
            ret = dict_add_number(retdict, "selected", selected_idx);
-    }
 
-    if (ret == OK && (what_flag & CI_WHAT_INSERTED))
-    {
-       // TODO
+       if (ret == OK && selected_idx != -1 && has_completed)
+       {
+           di = dict_alloc();
+           if (di == NULL)
+               return;
+           fill_complete_info_dict(di, compl_curr_match, FALSE);
+           ret = dict_add_dict(retdict, "completed", di);
+       }
     }
 }
 
index 8b266667da5f5b9a563e55c7c86b4f0bf1ec6a0d..289a281885b54896d9ca42522c01b26d4ce1b0e2 100644 (file)
@@ -2975,4 +2975,34 @@ func Test_complete_info_matches()
   set cot&
 endfunc
 
+func Test_complete_info_completed()
+  func ShownInfo()
+    let g:compl_info = complete_info(['completed'])
+    return ''
+  endfunc
+  set completeopt+=noinsert
+
+  new
+  call setline(1, ['aaa', 'aab', 'aba', 'abb'])
+  inoremap <buffer><F5> <C-R>=ShownInfo()<CR>
+
+  call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
+  call assert_equal({'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},  g:compl_info['completed'])
+
+  call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx')
+  call assert_equal({'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},  g:compl_info['completed'])
+
+  call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx')
+  call assert_equal({'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},  g:compl_info['completed'])
+
+  set completeopt+=noselect
+  call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
+  call assert_equal({}, g:compl_info)
+
+  bw!
+  bw!
+  delfunc ShownInfo
+  set cot&
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab nofoldenable
index 7ce1a98ef5325146229901549ff1fada8c8058b7..5a47806a5229c153c86f812e80f2974c89dd7e82 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1020,
 /**/
     1019,
 /**/