summaryrefslogtreecommitdiff
path: root/src/insexpand.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-10-26 19:22:42 +0100
committerBram Moolenaar <Bram@vim.org>2020-10-26 19:22:42 +0100
commitf9d51354de069dddc049b9e109b1932c92e5aee6 (patch)
tree83a613101e8d34be4e88e1eb6bf0e404c330fb92 /src/insexpand.c
parenta360dbe3b63bdca93bbf8cc431578a446e8ce14c (diff)
downloadvim-git-f9d51354de069dddc049b9e109b1932c92e5aee6.tar.gz
patch 8.2.1907: complete_info().selected may be wrongv8.2.1907
Problem: Complete_info().selected may be wrong. Solution: Update cp_number if it was never set. (issue #6945)
Diffstat (limited to 'src/insexpand.c')
-rw-r--r--src/insexpand.c108
1 files changed, 58 insertions, 50 deletions
diff --git a/src/insexpand.c b/src/insexpand.c
index 23ab0afc0..a0aa1024a 100644
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -2498,6 +2498,56 @@ ins_compl_mode(void)
return (char_u *)"";
}
+ static void
+ins_compl_update_sequence_numbers()
+{
+ int number = 0;
+ compl_T *match;
+
+ if (compl_direction == FORWARD)
+ {
+ // search backwards for the first valid (!= -1) number.
+ // This should normally succeed already at the first loop
+ // cycle, so it's fast!
+ for (match = compl_curr_match->cp_prev; match != NULL
+ && match != compl_first_match;
+ match = match->cp_prev)
+ if (match->cp_number != -1)
+ {
+ number = match->cp_number;
+ break;
+ }
+ if (match != NULL)
+ // go up and assign all numbers which are not assigned
+ // yet
+ for (match = match->cp_next;
+ match != NULL && match->cp_number == -1;
+ match = match->cp_next)
+ match->cp_number = ++number;
+ }
+ else // BACKWARD
+ {
+ // search forwards (upwards) for the first valid (!= -1)
+ // number. This should normally succeed already at the
+ // first loop cycle, so it's fast!
+ for (match = compl_curr_match->cp_next; match != NULL
+ && match != compl_first_match;
+ match = match->cp_next)
+ if (match->cp_number != -1)
+ {
+ number = match->cp_number;
+ break;
+ }
+ if (match != NULL)
+ // go down and assign all numbers which are not
+ // assigned yet
+ for (match = match->cp_prev; match
+ && match->cp_number == -1;
+ match = match->cp_prev)
+ match->cp_number = ++number;
+ }
+}
+
/*
* Get complete information
*/
@@ -2584,8 +2634,12 @@ get_complete_info(list_T *what_list, dict_T *retdict)
}
if (ret == OK && (what_flag & CI_WHAT_SELECTED))
- ret = dict_add_number(retdict, "selected", (compl_curr_match != NULL) ?
- compl_curr_match->cp_number - 1 : -1);
+ {
+ if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
+ ins_compl_update_sequence_numbers();
+ ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
+ ? compl_curr_match->cp_number - 1 : -1);
+ }
// TODO
// if (ret == OK && (what_flag & CI_WHAT_INSERTED))
@@ -4009,59 +4063,13 @@ ins_complete(int c, int enable_pum)
{
edit_submode_extra = (char_u *)_("The only match");
edit_submode_highl = HLF_COUNT;
- compl_curr_match->cp_number = 0;
+ compl_curr_match->cp_number = 1;
}
else
{
// Update completion sequence number when needed.
if (compl_curr_match->cp_number == -1)
- {
- int number = 0;
- compl_T *match;
-
- if (compl_direction == FORWARD)
- {
- // search backwards for the first valid (!= -1) number.
- // This should normally succeed already at the first loop
- // cycle, so it's fast!
- for (match = compl_curr_match->cp_prev; match != NULL
- && match != compl_first_match;
- match = match->cp_prev)
- if (match->cp_number != -1)
- {
- number = match->cp_number;
- break;
- }
- if (match != NULL)
- // go up and assign all numbers which are not assigned
- // yet
- for (match = match->cp_next;
- match != NULL && match->cp_number == -1;
- match = match->cp_next)
- match->cp_number = ++number;
- }
- else // BACKWARD
- {
- // search forwards (upwards) for the first valid (!= -1)
- // number. This should normally succeed already at the
- // first loop cycle, so it's fast!
- for (match = compl_curr_match->cp_next; match != NULL
- && match != compl_first_match;
- match = match->cp_next)
- if (match->cp_number != -1)
- {
- number = match->cp_number;
- break;
- }
- if (match != NULL)
- // go down and assign all numbers which are not
- // assigned yet
- for (match = match->cp_prev; match
- && match->cp_number == -1;
- match = match->cp_prev)
- match->cp_number = ++number;
- }
- }
+ ins_compl_update_sequence_numbers();
// The match should always have a sequence number now, this is
// just a safety check.