patch 9.1.1590: cannot perform autocompletion
Problem: cannot perform autocompletion
Solution: Add the 'autocomplete' option value
(Girish Palya)
This change introduces the 'autocomplete' ('ac') boolean option to
enable automatic popup menu completion during insert mode. When enabled,
Vim shows a completion menu as you type, similar to pressing |i\_CTRL-N|
manually. The items are collected from sources defined in the
'complete' option.
To ensure responsiveness, this feature uses a time-sliced strategy:
- Sources earlier in the 'complete' list are given more time.
- If a source exceeds its allocated timeout, it is interrupted.
- The next source is then started with a reduced timeout (exponentially
decayed).
- A small minimum ensures every source still gets a brief chance to
contribute.
The feature is fully compatible with other |i_CTRL-X| completion modes,
which can temporarily suspend automatic completion when triggered.
See :help 'autocomplete' and :help ins-autocompletion for more details.
To try it out, use :set ac
You should see a popup menu appear automatically with suggestions. This
works seamlessly across:
- Large files (multi-gigabyte size)
- Massive codebases (:argadd thousands of .c or .h files)
- Large dictionaries via the `k` option
- Slow or blocking LSP servers or user-defined 'completefunc'
Despite potential slowness in sources, the menu remains fast,
responsive, and useful.
Compatibility: This mode is fully compatible with existing completion
methods. You can still invoke any CTRL-X based completion (e.g.,
CTRL-X CTRL-F for filenames) at any time (CTRL-X temporarily
suspends 'autocomplete'). To specifically use i_CTRL-N, dismiss the
current popup by pressing CTRL-E first.
---
How it works
To keep completion snappy under all conditions, autocompletion uses a
decaying time-sliced algorithm:
- Starts with an initial timeout (80ms).
- If a source does not complete within the timeout, it's interrupted and
the timeout is halved for the next source.
- This continues recursively until a minimum timeout (5ms) is reached.
- All sources are given a chance, but slower ones are de-prioritized
quickly.
Most of the time, matches are computed well within the initial window.
---
Implementation details
- Completion logic is mostly triggered in `edit.c` and handled in
insexpand.c.
- Uses existing inc_compl_check_keys() mechanism, so no new polling
hooks are needed.
- The completion system already checks for user input periodically; it
now also checks for timer expiry.
---
Design notes
- The menu doesn't continuously update after it's shown to prevent
visual distraction (due to resizing) and ensure the internal list
stays synchronized with the displayed menu.
- The 'complete' option determines priority—sources listed earlier get
more time.
- The exponential time-decay mechanism prevents indefinite collection,
contributing to low CPU usage and a minimal memory footprint.
- Timeout values are intentionally not configurable—this system is
optimized to "just work" out of the box. If autocompletion feels slow,
it typically indicates a deeper performance bottleneck (e.g., a slow
custom function not using `complete_check()`) rather than a
configuration issue.
---
Performance
Based on testing, the total roundtrip time for completion is generally
under 200ms. For common usage, it often responds in under 50ms on an
average laptop, which falls within the "feels instantaneous" category
(sub-100ms) for perceived user experience.
| Upper Bound (ms) | Perceived UX
|----------------- |-------------
| <100 ms | Excellent; instantaneous
| <200 ms | Good; snappy
| >300 ms | Noticeable lag
| >500 ms | Sluggish/Broken
---
Why this belongs in core:
- Minimal and focused implementation, tightly integrated with existing
Insert-mode completion logic.
- Zero reliance on autocommands and external scripting.
- Makes full use of Vim’s highly composable 'complete' infrastructure
while avoiding the complexity of plugin-based solutions.
- Gives users C native autocompletion with excellent responsiveness and
no configuration overhead.
- Adds a key UX functionality in a simple, performant, and Vim-like way.
closes: #17812
Signed-off-by: Girish Palya <girishji@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
44309b9d08
commit
af9a7a04f1
23
src/edit.c
23
src/edit.c
@ -983,6 +983,17 @@ doESCkey:
|
||||
case Ctrl_H:
|
||||
did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
|
||||
auto_format(FALSE, TRUE);
|
||||
if (did_backspace && p_ac && !char_avail()
|
||||
&& curwin->w_cursor.col > 0)
|
||||
{
|
||||
c = char_before_cursor();
|
||||
if (ins_compl_setup_autocompl(c))
|
||||
{
|
||||
update_screen(UPD_VALID); // Show char deletion immediately
|
||||
out_flush();
|
||||
goto docomplete; // Trigger autocompletion
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Ctrl_W: // delete word before the cursor
|
||||
@ -1401,6 +1412,14 @@ normalchar:
|
||||
// closed fold.
|
||||
foldOpenCursor();
|
||||
#endif
|
||||
// Trigger autocompletion
|
||||
if (p_ac && !char_avail() && ins_compl_setup_autocompl(c))
|
||||
{
|
||||
update_screen(UPD_VALID); // Show character immediately
|
||||
out_flush();
|
||||
goto docomplete;
|
||||
}
|
||||
|
||||
break;
|
||||
} // end of switch (c)
|
||||
|
||||
@ -2233,6 +2252,10 @@ insertchar(
|
||||
if ( !ISSPECIAL(c)
|
||||
&& (!has_mbyte || (*mb_char2len)(c) == 1)
|
||||
&& !has_insertcharpre()
|
||||
#ifdef FEAT_EVAL
|
||||
// Skip typeahead if test_override("char_avail", 1) was called.
|
||||
&& !disable_char_avail_for_testing
|
||||
#endif
|
||||
&& vpeekc() != NUL
|
||||
&& !(State & REPLACE_FLAG)
|
||||
&& !cindent_on()
|
||||
|
||||
278
src/insexpand.c
278
src/insexpand.c
@ -198,6 +198,24 @@ static expand_T compl_xp;
|
||||
static win_T *compl_curr_win = NULL; // win where completion is active
|
||||
static buf_T *compl_curr_buf = NULL; // buf where completion is active
|
||||
|
||||
#define COMPL_INITIAL_TIMEOUT_MS 80
|
||||
// Autocomplete uses a decaying timeout: starting from COMPL_INITIAL_TIMEOUT_MS,
|
||||
// if the current source exceeds its timeout, it is interrupted and the next
|
||||
// begins with half the time. A small minimum timeout ensures every source
|
||||
// gets at least a brief chance.
|
||||
static int compl_autocomplete = FALSE; // whether autocompletion is active
|
||||
static int compl_timeout_ms = COMPL_INITIAL_TIMEOUT_MS;
|
||||
static int compl_time_slice_expired = FALSE; // time budget exceeded for current source
|
||||
static int compl_from_nonkeyword = FALSE; // completion started from non-keyword
|
||||
|
||||
// Halve the current completion timeout, simulating exponential decay.
|
||||
#define COMPL_MIN_TIMEOUT_MS 5
|
||||
#define DECAY_COMPL_TIMEOUT() \
|
||||
do { \
|
||||
if (compl_timeout_ms > COMPL_MIN_TIMEOUT_MS) \
|
||||
compl_timeout_ms /= 2; \
|
||||
} while (0)
|
||||
|
||||
// List of flags for method of completion.
|
||||
static int compl_cont_status = 0;
|
||||
# define CONT_ADDING 1 // "normal" or "adding" expansion
|
||||
@ -224,6 +242,9 @@ typedef struct cpt_source_T
|
||||
int cs_refresh_always; // Whether 'refresh:always' is set for func
|
||||
int cs_startcol; // Start column returned by func
|
||||
int cs_max_matches; // Max items to display from this source
|
||||
#ifdef ELAPSED_FUNC
|
||||
elapsed_T compl_start_tv; // Timestamp when match collection starts
|
||||
#endif
|
||||
} cpt_source_T;
|
||||
|
||||
#define STARTCOL_NONE -9
|
||||
@ -283,7 +304,7 @@ ins_ctrl_x(void)
|
||||
if (!ctrl_x_mode_cmdline())
|
||||
{
|
||||
// if the next ^X<> won't ADD nothing, then reset compl_cont_status
|
||||
if (compl_cont_status & CONT_N_ADDS)
|
||||
if ((compl_cont_status & CONT_N_ADDS) && !p_ac)
|
||||
compl_cont_status |= CONT_INTRPT;
|
||||
else
|
||||
compl_cont_status = 0;
|
||||
@ -553,6 +574,9 @@ is_first_match(compl_T *match)
|
||||
int
|
||||
ins_compl_accept_char(int c)
|
||||
{
|
||||
if (compl_autocomplete && compl_from_nonkeyword)
|
||||
return FALSE;
|
||||
|
||||
if (ctrl_x_mode & CTRL_X_WANT_IDENT)
|
||||
// When expanding an identifier only accept identifier chars.
|
||||
return vim_isIDc(c);
|
||||
@ -816,7 +840,9 @@ cfc_has_mode(void)
|
||||
static int
|
||||
is_nearest_active(void)
|
||||
{
|
||||
return (get_cot_flags() & (COT_NEAREST | COT_FUZZY)) == COT_NEAREST;
|
||||
int flags = get_cot_flags();
|
||||
return (compl_autocomplete || (flags & COT_NEAREST))
|
||||
&& !(flags & COT_FUZZY);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -1268,7 +1294,7 @@ ins_compl_del_pum(void)
|
||||
pum_wanted(void)
|
||||
{
|
||||
// 'completeopt' must contain "menu" or "menuone"
|
||||
if ((get_cot_flags() & COT_ANY_MENU) == 0)
|
||||
if ((get_cot_flags() & COT_ANY_MENU) == 0 && !compl_autocomplete)
|
||||
return FALSE;
|
||||
|
||||
// The display looks bad on a B&W display.
|
||||
@ -1301,7 +1327,7 @@ pum_enough_matches(void)
|
||||
compl = compl->cp_next;
|
||||
} while (!is_first_match(compl));
|
||||
|
||||
if (get_cot_flags() & COT_MENUONE)
|
||||
if ((get_cot_flags() & COT_MENUONE) || compl_autocomplete)
|
||||
return (i >= 1);
|
||||
return (i >= 2);
|
||||
}
|
||||
@ -1552,7 +1578,8 @@ ins_compl_build_pum(void)
|
||||
int i = 0;
|
||||
int cur = -1;
|
||||
unsigned int cur_cot_flags = get_cot_flags();
|
||||
int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
|
||||
int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0
|
||||
|| compl_autocomplete;
|
||||
int fuzzy_filter = (cur_cot_flags & COT_FUZZY) != 0;
|
||||
compl_T *match_head = NULL;
|
||||
compl_T *match_tail = NULL;
|
||||
@ -2016,10 +2043,12 @@ ins_compl_files(
|
||||
leader_len = (int)ins_compl_leader_len();
|
||||
}
|
||||
|
||||
for (i = 0; i < count && !got_int && !compl_interrupted; i++)
|
||||
for (i = 0; i < count && !got_int && !compl_interrupted
|
||||
&& !compl_time_slice_expired; i++)
|
||||
{
|
||||
fp = mch_fopen((char *)files[i], "r"); // open dictionary file
|
||||
if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN))
|
||||
if (flags != DICT_EXACT && !shortmess(SHM_COMPLETIONSCAN)
|
||||
&& !compl_autocomplete)
|
||||
{
|
||||
msg_hist_off = TRUE; // reset in msg_trunc_attr()
|
||||
vim_snprintf((char *)IObuff, IOSIZE,
|
||||
@ -2032,7 +2061,8 @@ ins_compl_files(
|
||||
|
||||
// Read dictionary file line by line.
|
||||
// Check each line for a match.
|
||||
while (!got_int && !compl_interrupted && !vim_fgets(buf, LSIZE, fp))
|
||||
while (!got_int && !compl_interrupted && !compl_time_slice_expired
|
||||
&& !vim_fgets(buf, LSIZE, fp))
|
||||
{
|
||||
ptr = buf;
|
||||
if (regmatch != NULL)
|
||||
@ -2215,6 +2245,9 @@ ins_compl_clear(void)
|
||||
VIM_CLEAR_STRING(compl_orig_text);
|
||||
compl_enter_selects = FALSE;
|
||||
cpt_sources_clear();
|
||||
compl_autocomplete = FALSE;
|
||||
compl_from_nonkeyword = FALSE;
|
||||
compl_num_bests = 0;
|
||||
#ifdef FEAT_EVAL
|
||||
// clear v:completed_item
|
||||
set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
|
||||
@ -2305,7 +2338,7 @@ ins_compl_has_preinsert(void)
|
||||
{
|
||||
int cur_cot_flags = get_cot_flags();
|
||||
return (cur_cot_flags & (COT_PREINSERT | COT_FUZZY | COT_MENUONE))
|
||||
== (COT_PREINSERT | COT_MENUONE);
|
||||
== (COT_PREINSERT | COT_MENUONE) && !compl_autocomplete;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -2429,6 +2462,8 @@ ins_compl_new_leader(void)
|
||||
save_w_wrow = curwin->w_wrow;
|
||||
save_w_leftcol = curwin->w_leftcol;
|
||||
compl_restarting = TRUE;
|
||||
if (p_ac)
|
||||
compl_autocomplete = TRUE;
|
||||
if (ins_complete(Ctrl_N, FALSE) == FAIL)
|
||||
compl_cont_status = 0;
|
||||
compl_restarting = FALSE;
|
||||
@ -2543,6 +2578,9 @@ ins_compl_restart(void)
|
||||
compl_cont_status = 0;
|
||||
compl_cont_mode = 0;
|
||||
cpt_sources_clear();
|
||||
compl_autocomplete = FALSE;
|
||||
compl_from_nonkeyword = FALSE;
|
||||
compl_num_bests = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -2903,6 +2941,9 @@ ins_compl_stop(int c, int prev_mode, int retval)
|
||||
edit_submode = NULL;
|
||||
showmode();
|
||||
}
|
||||
compl_autocomplete = FALSE;
|
||||
compl_from_nonkeyword = FALSE;
|
||||
compl_best_matches = 0;
|
||||
|
||||
if (c == Ctrl_C && cmdwin_type != 0)
|
||||
// Avoid the popup menu remains displayed when leaving the
|
||||
@ -3637,7 +3678,10 @@ f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
|
||||
RedrawingDisabled = 0;
|
||||
|
||||
ins_compl_check_keys(0, TRUE);
|
||||
rettv->vval.v_number = ins_compl_interrupted();
|
||||
if (compl_autocomplete && compl_time_slice_expired)
|
||||
rettv->vval.v_number = TRUE;
|
||||
else
|
||||
rettv->vval.v_number = ins_compl_interrupted();
|
||||
|
||||
RedrawingDisabled = save_RedrawingDisabled;
|
||||
}
|
||||
@ -4111,6 +4155,7 @@ process_next_cpt_value(
|
||||
{
|
||||
int compl_type = -1;
|
||||
int status = INS_COMPL_CPT_OK;
|
||||
int skip_source = compl_autocomplete && compl_from_nonkeyword;
|
||||
|
||||
st->found_all = FALSE;
|
||||
*advance_cpt_idx = FALSE;
|
||||
@ -4118,7 +4163,8 @@ process_next_cpt_value(
|
||||
while (*st->e_cpt == ',' || *st->e_cpt == ' ')
|
||||
st->e_cpt++;
|
||||
|
||||
if (*st->e_cpt == '.' && !curbuf->b_scanned)
|
||||
if (*st->e_cpt == '.' && !curbuf->b_scanned && !skip_source
|
||||
&& !compl_time_slice_expired)
|
||||
{
|
||||
st->ins_buf = curbuf;
|
||||
st->first_match_pos = *start_match_pos;
|
||||
@ -4139,7 +4185,8 @@ process_next_cpt_value(
|
||||
// wrap and come back there a second time.
|
||||
st->set_match_pos = TRUE;
|
||||
}
|
||||
else if (vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
|
||||
else if (!skip_source && !compl_time_slice_expired
|
||||
&& vim_strchr((char_u *)"buwU", *st->e_cpt) != NULL
|
||||
&& (st->ins_buf = ins_compl_next_buf(
|
||||
st->ins_buf, *st->e_cpt)) != curbuf)
|
||||
{
|
||||
@ -4164,7 +4211,7 @@ process_next_cpt_value(
|
||||
st->dict = st->ins_buf->b_fname;
|
||||
st->dict_f = DICT_EXACT;
|
||||
}
|
||||
if (!shortmess(SHM_COMPLETIONSCAN))
|
||||
if (!shortmess(SHM_COMPLETIONSCAN) && !compl_autocomplete)
|
||||
{
|
||||
msg_hist_off = TRUE; // reset in msg_trunc_attr()
|
||||
vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
|
||||
@ -4182,18 +4229,6 @@ process_next_cpt_value(
|
||||
{
|
||||
if (ctrl_x_mode_line_or_eval())
|
||||
compl_type = -1;
|
||||
else if (*st->e_cpt == 'k' || *st->e_cpt == 's')
|
||||
{
|
||||
if (*st->e_cpt == 'k')
|
||||
compl_type = CTRL_X_DICTIONARY;
|
||||
else
|
||||
compl_type = CTRL_X_THESAURUS;
|
||||
if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
|
||||
{
|
||||
st->dict = st->e_cpt;
|
||||
st->dict_f = DICT_FIRST;
|
||||
}
|
||||
}
|
||||
#ifdef FEAT_COMPL_FUNC
|
||||
else if (*st->e_cpt == 'F' || *st->e_cpt == 'o')
|
||||
{
|
||||
@ -4203,24 +4238,39 @@ process_next_cpt_value(
|
||||
compl_type = -1;
|
||||
}
|
||||
#endif
|
||||
#ifdef FEAT_FIND_ID
|
||||
else if (*st->e_cpt == 'i')
|
||||
compl_type = CTRL_X_PATH_PATTERNS;
|
||||
else if (*st->e_cpt == 'd')
|
||||
compl_type = CTRL_X_PATH_DEFINES;
|
||||
#endif
|
||||
else if (*st->e_cpt == ']' || *st->e_cpt == 't')
|
||||
else if (!skip_source)
|
||||
{
|
||||
compl_type = CTRL_X_TAGS;
|
||||
if (!shortmess(SHM_COMPLETIONSCAN))
|
||||
if (*st->e_cpt == 'k' || *st->e_cpt == 's')
|
||||
{
|
||||
msg_hist_off = TRUE; // reset in msg_trunc_attr()
|
||||
vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
|
||||
(void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
|
||||
if (*st->e_cpt == 'k')
|
||||
compl_type = CTRL_X_DICTIONARY;
|
||||
else
|
||||
compl_type = CTRL_X_THESAURUS;
|
||||
if (*++st->e_cpt != ',' && *st->e_cpt != NUL)
|
||||
{
|
||||
st->dict = st->e_cpt;
|
||||
st->dict_f = DICT_FIRST;
|
||||
}
|
||||
}
|
||||
#ifdef FEAT_FIND_ID
|
||||
else if (*st->e_cpt == 'i')
|
||||
compl_type = CTRL_X_PATH_PATTERNS;
|
||||
else if (*st->e_cpt == 'd')
|
||||
compl_type = CTRL_X_PATH_DEFINES;
|
||||
#endif
|
||||
else if (*st->e_cpt == ']' || *st->e_cpt == 't')
|
||||
{
|
||||
compl_type = CTRL_X_TAGS;
|
||||
if (!shortmess(SHM_COMPLETIONSCAN) && !compl_autocomplete)
|
||||
{
|
||||
msg_hist_off = TRUE; // reset in msg_trunc_attr()
|
||||
vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
|
||||
(void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
|
||||
}
|
||||
}
|
||||
else
|
||||
compl_type = -1;
|
||||
}
|
||||
else
|
||||
compl_type = -1;
|
||||
|
||||
// in any case e_cpt is advanced to the next entry
|
||||
(void)copy_option_part(&st->e_cpt, IObuff, IOSIZE, ",");
|
||||
@ -4747,7 +4797,8 @@ get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
|
||||
int looped_around = FALSE;
|
||||
char_u *ptr = NULL;
|
||||
int len = 0;
|
||||
int in_collect = (cfc_has_mode() && compl_length > 0);
|
||||
int in_fuzzy_collect = (cfc_has_mode() && compl_length > 0)
|
||||
|| ((get_cot_flags() & COT_FUZZY) && compl_autocomplete);
|
||||
char_u *leader = ins_compl_leader();
|
||||
int score = 0;
|
||||
int in_curbuf = st->ins_buf == curbuf;
|
||||
@ -4773,7 +4824,7 @@ get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
|
||||
|
||||
++msg_silent; // Don't want messages for wrapscan.
|
||||
|
||||
if (in_collect)
|
||||
if (in_fuzzy_collect)
|
||||
{
|
||||
found_new_match = search_for_fuzzy_match(st->ins_buf,
|
||||
st->cur_match_pos, leader, compl_direction,
|
||||
@ -4832,7 +4883,7 @@ get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
|
||||
&& start_pos->col == st->cur_match_pos->col)
|
||||
continue;
|
||||
|
||||
if (!in_collect)
|
||||
if (!in_fuzzy_collect)
|
||||
ptr = ins_compl_get_next_word_or_line(st->ins_buf, st->cur_match_pos,
|
||||
&len, &cont_s_ipos);
|
||||
if (ptr == NULL || (ins_compl_has_preinsert() && STRCMP(ptr, compl_pattern.string) == 0))
|
||||
@ -4850,7 +4901,7 @@ get_next_default_completion(ins_compl_next_state_T *st, pos_T *start_pos)
|
||||
in_curbuf ? NULL : st->ins_buf->b_sfname,
|
||||
0, cont_s_ipos, score) != NOTDONE)
|
||||
{
|
||||
if (in_collect && score == compl_first_match->cp_next->cp_score)
|
||||
if (in_fuzzy_collect && score == compl_first_match->cp_next->cp_score)
|
||||
compl_num_bests++;
|
||||
found_new_match = OK;
|
||||
break;
|
||||
@ -5171,6 +5222,21 @@ prepare_cpt_compl_funcs(void)
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Start the timer for the current completion source.
|
||||
*/
|
||||
static void
|
||||
compl_source_start_timer(int source_idx UNUSED)
|
||||
{
|
||||
#ifdef ELAPSED_FUNC
|
||||
if (compl_autocomplete && cpt_sources_array != NULL)
|
||||
{
|
||||
ELAPSED_INIT(cpt_sources_array[source_idx].compl_start_tv);
|
||||
compl_time_slice_expired = FALSE;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Safely advance the cpt_sources_index by one.
|
||||
*/
|
||||
@ -5188,6 +5254,8 @@ advance_cpt_sources_index_safe(void)
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
#define COMPL_FUNC_TIMEOUT_MS 300
|
||||
#define COMPL_FUNC_TIMEOUT_NON_KW_MS 1000
|
||||
/*
|
||||
* Get the next expansion(s), using "compl_pattern".
|
||||
* The search starts at position "ini" in curbuf and in the direction
|
||||
@ -5206,6 +5274,7 @@ ins_compl_get_exp(pos_T *ini)
|
||||
int found_new_match;
|
||||
int type = ctrl_x_mode;
|
||||
int may_advance_cpt_idx = FALSE;
|
||||
pos_T start_pos = *ini;
|
||||
|
||||
if (!compl_started)
|
||||
{
|
||||
@ -5226,7 +5295,15 @@ ins_compl_get_exp(pos_T *ini)
|
||||
? (char_u *)"." : curbuf->b_p_cpt);
|
||||
strip_caret_numbers_in_place(st.e_cpt_copy);
|
||||
st.e_cpt = st.e_cpt_copy == NULL ? (char_u *)"" : st.e_cpt_copy;
|
||||
st.last_match_pos = st.first_match_pos = *ini;
|
||||
|
||||
// In large buffers, timeout may miss nearby matches — search above cursor
|
||||
#define LOOKBACK_LINE_COUNT 1000
|
||||
if (compl_autocomplete && is_nearest_active())
|
||||
{
|
||||
start_pos.lnum = MAX(1, start_pos.lnum - LOOKBACK_LINE_COUNT);
|
||||
start_pos.col = 0;
|
||||
}
|
||||
st.last_match_pos = st.first_match_pos = start_pos;
|
||||
}
|
||||
else if (st.ins_buf != curbuf && !buf_valid(st.ins_buf))
|
||||
st.ins_buf = curbuf; // In case the buffer was wiped out.
|
||||
@ -5238,7 +5315,14 @@ ins_compl_get_exp(pos_T *ini)
|
||||
if (cpt_sources_array != NULL && ctrl_x_mode_normal()
|
||||
&& !ctrl_x_mode_line_or_eval()
|
||||
&& !(compl_cont_status & CONT_LOCAL))
|
||||
{
|
||||
cpt_sources_index = 0;
|
||||
if (compl_autocomplete)
|
||||
{
|
||||
compl_source_start_timer(0);
|
||||
compl_timeout_ms = COMPL_INITIAL_TIMEOUT_MS;
|
||||
}
|
||||
}
|
||||
|
||||
// For ^N/^P loop over all the flags/windows/buffers in 'complete'.
|
||||
for (;;)
|
||||
@ -5252,15 +5336,19 @@ ins_compl_get_exp(pos_T *ini)
|
||||
if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
|
||||
&& (!compl_started || st.found_all))
|
||||
{
|
||||
int status = process_next_cpt_value(&st, &type, ini,
|
||||
int status = process_next_cpt_value(&st, &type, &start_pos,
|
||||
cfc_has_mode(), &may_advance_cpt_idx);
|
||||
|
||||
if (status == INS_COMPL_CPT_END)
|
||||
break;
|
||||
if (status == INS_COMPL_CPT_CONT)
|
||||
{
|
||||
if (may_advance_cpt_idx && !advance_cpt_sources_index_safe())
|
||||
break;
|
||||
if (may_advance_cpt_idx)
|
||||
{
|
||||
if (!advance_cpt_sources_index_safe())
|
||||
break;
|
||||
compl_source_start_timer(cpt_sources_index);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -5270,11 +5358,24 @@ ins_compl_get_exp(pos_T *ini)
|
||||
if (compl_pattern.string == NULL)
|
||||
break;
|
||||
|
||||
// get the next set of completion matches
|
||||
found_new_match = get_next_completion_match(type, &st, ini);
|
||||
if (compl_autocomplete && type == CTRL_X_FUNCTION)
|
||||
// LSP servers may sporadically take >1s to respond (e.g., while
|
||||
// loading modules), but other sources might already have matches.
|
||||
// To show results quickly use a short timeout for keyword
|
||||
// completion. Allow longer timeout for non-keyword completion
|
||||
// where only function based sources (e.g. LSP) are active.
|
||||
compl_timeout_ms = compl_from_nonkeyword
|
||||
? COMPL_FUNC_TIMEOUT_NON_KW_MS : COMPL_FUNC_TIMEOUT_MS;
|
||||
|
||||
if (may_advance_cpt_idx && !advance_cpt_sources_index_safe())
|
||||
break;
|
||||
// get the next set of completion matches
|
||||
found_new_match = get_next_completion_match(type, &st, &start_pos);
|
||||
|
||||
if (may_advance_cpt_idx)
|
||||
{
|
||||
if (!advance_cpt_sources_index_safe())
|
||||
break;
|
||||
compl_source_start_timer(cpt_sources_index);
|
||||
}
|
||||
|
||||
// break the loop for specialized modes (use 'complete' just for the
|
||||
// generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
|
||||
@ -5291,7 +5392,7 @@ ins_compl_get_exp(pos_T *ini)
|
||||
if ((ctrl_x_mode_not_default()
|
||||
&& !ctrl_x_mode_line_or_eval()) || compl_interrupted)
|
||||
break;
|
||||
compl_started = TRUE;
|
||||
compl_started = compl_time_slice_expired ? FALSE : TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -5302,6 +5403,10 @@ ins_compl_get_exp(pos_T *ini)
|
||||
compl_started = FALSE;
|
||||
}
|
||||
|
||||
// Reset the timeout after collecting matches from function source
|
||||
if (compl_autocomplete && type == CTRL_X_FUNCTION)
|
||||
compl_timeout_ms = COMPL_INITIAL_TIMEOUT_MS;
|
||||
|
||||
// For `^P` completion, reset `compl_curr_match` to the head to avoid
|
||||
// mixing matches from different sources.
|
||||
if (!compl_dir_forward())
|
||||
@ -5317,7 +5422,7 @@ ins_compl_get_exp(pos_T *ini)
|
||||
|
||||
i = -1; // total of matches, unknown
|
||||
if (found_new_match == FAIL || (ctrl_x_mode_not_default()
|
||||
&& !ctrl_x_mode_line_or_eval()))
|
||||
&& !ctrl_x_mode_line_or_eval()))
|
||||
i = ins_compl_make_cyclic();
|
||||
|
||||
if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0)
|
||||
@ -5624,7 +5729,8 @@ find_next_completion_match(
|
||||
int found_end = FALSE;
|
||||
compl_T *found_compl = NULL;
|
||||
unsigned int cur_cot_flags = get_cot_flags();
|
||||
int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
|
||||
int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0
|
||||
|| compl_autocomplete;
|
||||
int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
|
||||
string_T *leader;
|
||||
|
||||
@ -5752,7 +5858,8 @@ ins_compl_next(
|
||||
int started = compl_started;
|
||||
buf_T *orig_curbuf = curbuf;
|
||||
unsigned int cur_cot_flags = get_cot_flags();
|
||||
int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0;
|
||||
int compl_no_insert = (cur_cot_flags & COT_NOINSERT) != 0
|
||||
|| compl_autocomplete;
|
||||
int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
|
||||
int compl_preinsert = ins_compl_has_preinsert();
|
||||
|
||||
@ -5843,7 +5950,7 @@ ins_compl_next(
|
||||
|
||||
// Enter will select a match when the match wasn't inserted and the popup
|
||||
// menu is visible.
|
||||
if (compl_no_insert && !started)
|
||||
if (compl_no_insert && !started && compl_selected_item != -1)
|
||||
compl_enter_selects = TRUE;
|
||||
else
|
||||
compl_enter_selects = !insert_match && compl_match_array != NULL;
|
||||
@ -5855,6 +5962,29 @@ ins_compl_next(
|
||||
return num_matches;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if the current completion source exceeded its timeout. If so, stop
|
||||
* collecting, and halve the timeout.
|
||||
*/
|
||||
static void
|
||||
check_elapsed_time(void)
|
||||
{
|
||||
#ifdef ELAPSED_FUNC
|
||||
if (cpt_sources_array == NULL)
|
||||
return;
|
||||
|
||||
elapsed_T *start_tv
|
||||
= &cpt_sources_array[cpt_sources_index].compl_start_tv;
|
||||
long elapsed_ms = ELAPSED_FUNC(*start_tv);
|
||||
|
||||
if (elapsed_ms > compl_timeout_ms)
|
||||
{
|
||||
compl_time_slice_expired = TRUE;
|
||||
DECAY_COMPL_TIMEOUT();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Call this while finding completions, to check whether the user has hit a key
|
||||
* that should change the currently displayed completion, or exit completion
|
||||
@ -5915,8 +6045,14 @@ ins_compl_check_keys(int frequency, int in_compl_func)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT))
|
||||
else if (compl_autocomplete)
|
||||
check_elapsed_time();
|
||||
|
||||
if (compl_pending != 0 && !got_int && !(cot_flags & COT_NOINSERT)
|
||||
&& !compl_autocomplete)
|
||||
{
|
||||
// Insert the first match immediately and advance compl_shown_match,
|
||||
// before finding other matches.
|
||||
int todo = compl_pending > 0 ? compl_pending : -compl_pending;
|
||||
|
||||
compl_pending = 0;
|
||||
@ -6070,6 +6206,7 @@ get_normal_compl_info(char_u *line, int startcol, colnr_T curs_col)
|
||||
compl_pattern.length = len;
|
||||
compl_col += curs_col;
|
||||
compl_length = 0;
|
||||
compl_from_nonkeyword = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -6631,7 +6768,7 @@ ins_compl_start(void)
|
||||
compl_startpos.col = compl_col;
|
||||
}
|
||||
|
||||
if (!shortmess(SHM_COMPLETIONMENU))
|
||||
if (!shortmess(SHM_COMPLETIONMENU) && !compl_autocomplete)
|
||||
{
|
||||
if (compl_cont_status & CONT_LOCAL)
|
||||
edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
|
||||
@ -6662,7 +6799,7 @@ ins_compl_start(void)
|
||||
// showmode might reset the internal line pointers, so it must
|
||||
// be called before line = ml_get(), or when this address is no
|
||||
// longer needed. -- Acevedo.
|
||||
if (!shortmess(SHM_COMPLETIONMENU))
|
||||
if (!shortmess(SHM_COMPLETIONMENU) && !compl_autocomplete)
|
||||
{
|
||||
edit_submode_extra = (char_u *)_("-- Searching...");
|
||||
edit_submode_highl = HLF_COUNT;
|
||||
@ -6831,7 +6968,7 @@ ins_complete(int c, int enable_pum)
|
||||
else
|
||||
compl_cont_status &= ~CONT_S_IPOS;
|
||||
|
||||
if (!shortmess(SHM_COMPLETIONMENU))
|
||||
if (!shortmess(SHM_COMPLETIONMENU) && !compl_autocomplete)
|
||||
ins_compl_show_statusmsg();
|
||||
|
||||
// Show the popup menu, unless we got interrupted.
|
||||
@ -6844,6 +6981,23 @@ ins_complete(int c, int enable_pum)
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns TRUE if the given character 'c' can be used to trigger
|
||||
* autocompletion.
|
||||
*/
|
||||
int
|
||||
ins_compl_setup_autocompl(int c)
|
||||
{
|
||||
#ifdef ELAPSED_FUNC
|
||||
if (vim_isprintc(c))
|
||||
{
|
||||
compl_autocomplete = TRUE;
|
||||
return TRUE;
|
||||
}
|
||||
#endif
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove (if needed) and show the popup menu
|
||||
*/
|
||||
@ -7144,6 +7298,7 @@ get_cpt_func_completion_matches(callback_T *cb UNUSED)
|
||||
if (set_compl_globals(startcol, curwin->w_cursor.col, TRUE) == OK)
|
||||
{
|
||||
expand_by_function(0, cpt_compl_pattern.string, cb);
|
||||
|
||||
cpt_sources_array[cpt_sources_index].cs_refresh_always =
|
||||
compl_opt_refresh_always;
|
||||
compl_opt_refresh_always = FALSE;
|
||||
@ -7196,7 +7351,10 @@ cpt_compl_refresh(void)
|
||||
}
|
||||
cpt_sources_array[cpt_sources_index].cs_startcol = startcol;
|
||||
if (ret == OK)
|
||||
{
|
||||
compl_source_start_timer(cpt_sources_index);
|
||||
get_cpt_func_completion_matches(cb);
|
||||
}
|
||||
}
|
||||
else
|
||||
cpt_sources_array[cpt_sources_index].cs_startcol
|
||||
|
||||
20
src/misc1.c
20
src/misc1.c
@ -565,6 +565,26 @@ gchar_cursor(void)
|
||||
return (int)*ml_get_cursor();
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the character immediately before the cursor.
|
||||
*/
|
||||
int
|
||||
char_before_cursor(void)
|
||||
{
|
||||
if (curwin->w_cursor.col == 0)
|
||||
return -1;
|
||||
|
||||
char_u *line = ml_get_curline();
|
||||
|
||||
if (has_mbyte)
|
||||
{
|
||||
char_u *p = line + curwin->w_cursor.col;
|
||||
int prev_len = (*mb_head_off)(line, p - 1) + 1;
|
||||
return mb_ptr2char(p - prev_len);
|
||||
}
|
||||
return line[curwin->w_cursor.col - 1];
|
||||
}
|
||||
|
||||
/*
|
||||
* Write a character at the current cursor position.
|
||||
* It is directly written into the block.
|
||||
|
||||
@ -523,6 +523,7 @@ EXTERN char_u *p_cia; // 'completeitemalign'
|
||||
EXTERN unsigned cia_flags; // order flags of 'completeitemalign'
|
||||
EXTERN char_u *p_cot; // 'completeopt'
|
||||
EXTERN unsigned cot_flags; // flags from 'completeopt'
|
||||
EXTERN int p_ac; // 'autocomplete'
|
||||
// Keep in sync with p_cot_values in optionstr.c
|
||||
#define COT_MENU 0x001
|
||||
#define COT_MENUONE 0x002
|
||||
|
||||
@ -388,6 +388,11 @@ static struct vimoption options[] =
|
||||
{(char_u *)0L, (char_u *)0L}
|
||||
#endif
|
||||
SCTX_INIT},
|
||||
#ifdef ELAPSED_FUNC
|
||||
{"autocomplete", "ac", P_BOOL|P_VI_DEF,
|
||||
(char_u *)&p_ac, PV_NONE, NULL,
|
||||
NULL, {(char_u *)FALSE, (char_u *)0L} SCTX_INIT},
|
||||
#endif
|
||||
{"autoindent", "ai", P_BOOL|P_VI_DEF,
|
||||
(char_u *)&p_ai, PV_AI, NULL, NULL,
|
||||
{(char_u *)FALSE, (char_u *)0L} SCTX_INIT},
|
||||
|
||||
7
src/po/vim.pot
generated
7
src/po/vim.pot
generated
@ -8,7 +8,7 @@ msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2025-07-24 19:13+0200\n"
|
||||
"POT-Creation-Date: 2025-07-25 18:40+0200\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
@ -9914,7 +9914,7 @@ msgstr ""
|
||||
msgid "specifies how Insert mode completion works for CTRL-N and CTRL-P"
|
||||
msgstr ""
|
||||
|
||||
msgid "use fuzzy collection for specific completion modes"
|
||||
msgid "automatic completion in insert mode"
|
||||
msgstr ""
|
||||
|
||||
msgid "whether to use a popup menu for Insert mode completion"
|
||||
@ -9923,6 +9923,9 @@ msgstr ""
|
||||
msgid "popup menu item align order"
|
||||
msgstr ""
|
||||
|
||||
msgid "use fuzzy collection for specific completion modes"
|
||||
msgstr ""
|
||||
|
||||
msgid "options for the Insert mode completion info popup"
|
||||
msgstr ""
|
||||
|
||||
|
||||
@ -69,4 +69,6 @@ int ins_complete(int c, int enable_pum);
|
||||
void free_insexpand_stuff(void);
|
||||
int ins_compl_cancel(void);
|
||||
void f_complete_match(typval_T *argvars, typval_T *rettv);
|
||||
int ins_compl_setup_autocompl(int c);
|
||||
// void ins_compl_disable_autocompl(void);
|
||||
/* vim: set ft=c : */
|
||||
|
||||
@ -10,6 +10,7 @@ int plines_win_col(win_T *wp, linenr_T lnum, long column);
|
||||
int plines_m_win(win_T *wp, linenr_T first, linenr_T last, int max);
|
||||
int gchar_pos(pos_T *pos);
|
||||
int gchar_cursor(void);
|
||||
int char_before_cursor(void);
|
||||
void pchar_cursor(int c);
|
||||
char_u *skip_to_option_part(char_u *p);
|
||||
void check_status(buf_T *buf);
|
||||
|
||||
@ -586,30 +586,40 @@ endfunc
|
||||
func Test_cpt_func_cursorcol()
|
||||
func CptColTest(findstart, query)
|
||||
if a:findstart
|
||||
call assert_equal("foo bar", getline(1))
|
||||
call assert_equal(8, col('.'))
|
||||
call assert_equal(b:info_compl_line, getline(1))
|
||||
call assert_equal(b:info_cursor_col, col('.'))
|
||||
return col('.')
|
||||
endif
|
||||
call assert_equal("foo ", getline(1))
|
||||
call assert_equal(5, col('.'))
|
||||
call assert_equal(b:expn_compl_line, getline(1))
|
||||
call assert_equal(b:expn_cursor_col, col('.'))
|
||||
return v:none
|
||||
endfunc
|
||||
|
||||
set complete=FCptColTest
|
||||
new
|
||||
call feedkeys("ifoo bar\<C-N>", "tx")
|
||||
bwipe!
|
||||
new
|
||||
|
||||
" Replace mode
|
||||
let b:info_compl_line = "foo barxyz"
|
||||
let b:expn_compl_line = "foo barbaz"
|
||||
let b:info_cursor_col = 10
|
||||
let b:expn_cursor_col = 5
|
||||
call feedkeys("ifoo barbaz\<Esc>2hRxy\<C-N>", "tx")
|
||||
|
||||
" Insert mode
|
||||
let b:info_compl_line = "foo bar"
|
||||
let b:expn_compl_line = "foo "
|
||||
let b:info_cursor_col = 8
|
||||
let b:expn_cursor_col = 5
|
||||
call feedkeys("Sfoo bar\<C-N>", "tx")
|
||||
|
||||
set completeopt=longest
|
||||
call feedkeys("ifoo bar\<C-N>", "tx")
|
||||
bwipe!
|
||||
new
|
||||
call feedkeys("Sfoo bar\<C-N>", "tx")
|
||||
|
||||
set completeopt=menuone
|
||||
call feedkeys("ifoo bar\<C-N>", "tx")
|
||||
bwipe!
|
||||
new
|
||||
call feedkeys("Sfoo bar\<C-N>", "tx")
|
||||
|
||||
set completeopt=menuone,preinsert
|
||||
call feedkeys("ifoo bar\<C-N>", "tx")
|
||||
call feedkeys("Sfoo bar\<C-N>", "tx")
|
||||
bwipe!
|
||||
set complete& completeopt&
|
||||
delfunc CptColTest
|
||||
@ -3643,7 +3653,7 @@ func Test_cfc_with_longest()
|
||||
exe "normal ggdGShello helio heo\<C-X>\<C-N>\<ESC>"
|
||||
call assert_equal("hello helio heo", getline('.'))
|
||||
|
||||
" kdcit
|
||||
" dict
|
||||
call writefile(['help'], 'test_keyword.txt', 'D')
|
||||
set complete=ktest_keyword.txt
|
||||
exe "normal ggdGSh\<C-N>\<ESC>"
|
||||
@ -4860,6 +4870,27 @@ func Test_complete_fuzzy_omnifunc_backspace()
|
||||
unlet g:do_complete
|
||||
endfunc
|
||||
|
||||
" Test that option shortmess=c turns off completion messages
|
||||
func Test_shortmess()
|
||||
CheckScreendump
|
||||
|
||||
let lines =<< trim END
|
||||
call setline(1, ['hello', 'hullo', 'heee'])
|
||||
END
|
||||
|
||||
call writefile(lines, 'Xpumscript', 'D')
|
||||
let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
|
||||
call term_sendkeys(buf, "Goh\<C-N>")
|
||||
call TermWait(buf, 200)
|
||||
call VerifyScreenDump(buf, 'Test_shortmess_complmsg_1', {})
|
||||
call term_sendkeys(buf, "\<ESC>:set shm+=c\<CR>")
|
||||
call term_sendkeys(buf, "Sh\<C-N>")
|
||||
call TermWait(buf, 200)
|
||||
call VerifyScreenDump(buf, 'Test_shortmess_complmsg_2', {})
|
||||
|
||||
call StopVimInTerminal(buf)
|
||||
endfunc
|
||||
|
||||
" Test 'complete' containing F{func} that complete from nonkeyword
|
||||
func Test_nonkeyword_trigger()
|
||||
|
||||
@ -4976,25 +5007,320 @@ func Test_nonkeyword_trigger()
|
||||
unlet g:CallCount
|
||||
endfunc
|
||||
|
||||
" Test that option shortmess=c turns off completion messages
|
||||
func Test_shortmess()
|
||||
CheckScreendump
|
||||
func Test_autocomplete_trigger()
|
||||
" Trigger expansion even when another char is waiting in the typehead
|
||||
call test_override("char_avail", 1)
|
||||
|
||||
let lines =<< trim END
|
||||
call setline(1, ['hello', 'hullo', 'heee'])
|
||||
END
|
||||
let g:CallCount = 0
|
||||
func! NonKeywordComplete(findstart, base)
|
||||
let line = getline('.')->strpart(0, col('.') - 1)
|
||||
let nonkeyword2 = len(line) > 1 && match(line[-2:-2], '\k') != 0
|
||||
if a:findstart
|
||||
return nonkeyword2 ? col('.') - 3 : (col('.') - 2)
|
||||
else
|
||||
let g:CallCount += 1
|
||||
return [$"{a:base}foo", $"{a:base}bar"]
|
||||
endif
|
||||
endfunc
|
||||
|
||||
call writefile(lines, 'Xpumscript', 'D')
|
||||
let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
|
||||
call term_sendkeys(buf, "Goh\<C-N>")
|
||||
call TermWait(buf, 200)
|
||||
call VerifyScreenDump(buf, 'Test_shortmess_complmsg_1', {})
|
||||
call term_sendkeys(buf, "\<ESC>:set shm+=c\<CR>")
|
||||
call term_sendkeys(buf, "Sh\<C-N>")
|
||||
call TermWait(buf, 200)
|
||||
call VerifyScreenDump(buf, 'Test_shortmess_complmsg_2', {})
|
||||
new
|
||||
inoremap <buffer> <F2> <Cmd>let b:matches = complete_info(["matches"]).matches<CR>
|
||||
inoremap <buffer> <F3> <Cmd>let b:selected = complete_info(["selected"]).selected<CR>
|
||||
|
||||
call StopVimInTerminal(buf)
|
||||
call setline(1, ['abc', 'abcd', 'fo', 'b', ''])
|
||||
set autocomplete
|
||||
|
||||
" Test 1a: Nonkeyword doesn't open menu without F{func} when autocomplete
|
||||
call feedkeys("GS=\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal([], b:matches)
|
||||
call assert_equal('=', getline('.'))
|
||||
" ^N opens menu of keywords (of len > 1)
|
||||
call feedkeys("S=\<C-E>\<C-N>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'abcd', 'fo'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal('=abc', getline('.'))
|
||||
|
||||
" Test 1b: With F{func} nonkeyword collects matches
|
||||
set complete=.,FNonKeywordComplete
|
||||
let g:CallCount = 0
|
||||
call feedkeys("S=\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['=foo', '=bar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
call assert_equal('=', getline('.'))
|
||||
let g:CallCount = 0
|
||||
call feedkeys("S->\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['->foo', '->bar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(2, g:CallCount)
|
||||
call assert_equal('->', getline('.'))
|
||||
|
||||
" Test 1c: Keyword after nonkeyword can collect both types of items
|
||||
let g:CallCount = 0
|
||||
call feedkeys("S#a\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abcd', 'abc', '#afoo', '#abar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(2, g:CallCount)
|
||||
call assert_equal('#a', getline('.'))
|
||||
let g:CallCount = 0
|
||||
call feedkeys("S#a.\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['.foo', '.bar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(3, g:CallCount)
|
||||
call assert_equal('#a.', getline('.'))
|
||||
let g:CallCount = 0
|
||||
call feedkeys("S#a.a\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abcd', 'abc', '.afoo', '.abar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(4, g:CallCount)
|
||||
call assert_equal('#a.a', getline('.'))
|
||||
|
||||
" Test 1d: Nonkeyword after keyword collects items again
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sa\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abcd', 'abc', 'afoo', 'abar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
call assert_equal('a', getline('.'))
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sa#\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['#foo', '#bar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(2, g:CallCount)
|
||||
call assert_equal('a#', getline('.'))
|
||||
|
||||
" Test 2: Filter nonkeyword and keyword matches with differet startpos
|
||||
for fuzzy in range(2)
|
||||
if fuzzy
|
||||
set completeopt+=fuzzy
|
||||
endif
|
||||
call feedkeys("S#ab\<F2>\<F3>\<Esc>0", 'tx!')
|
||||
if fuzzy
|
||||
call assert_equal(['#abar', 'abc', 'abcd'], b:matches->mapnew('v:val.word'))
|
||||
else " Ordering of items is by 'nearest' to cursor by default
|
||||
call assert_equal(['abcd', 'abc', '#abar'], b:matches->mapnew('v:val.word'))
|
||||
endif
|
||||
call assert_equal(-1, b:selected)
|
||||
call assert_equal('#ab', getline('.'))
|
||||
call feedkeys("S#ab" . repeat("\<C-N>", 3) . "\<F3>\<Esc>0", 'tx!')
|
||||
call assert_equal(fuzzy ? '#abcd' : '#abar', getline('.'))
|
||||
call assert_equal(2, b:selected)
|
||||
|
||||
let g:CallCount = 0
|
||||
call feedkeys("GS#aba\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['#abar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(2, g:CallCount)
|
||||
call assert_equal('#aba', getline('.'))
|
||||
|
||||
let g:CallCount = 0
|
||||
call feedkeys("S#abc\<F2>\<Esc>0", 'tx!')
|
||||
if fuzzy
|
||||
call assert_equal(['abc', 'abcd'], b:matches->mapnew('v:val.word'))
|
||||
else
|
||||
call assert_equal(['abcd', 'abc'], b:matches->mapnew('v:val.word'))
|
||||
endif
|
||||
call assert_equal(2, g:CallCount)
|
||||
set completeopt&
|
||||
endfor
|
||||
|
||||
" Test 3: Navigate menu containing nonkeyword and keyword items
|
||||
call feedkeys("S#a\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abcd', 'abc', '#afoo', '#abar'], b:matches->mapnew('v:val.word'))
|
||||
call feedkeys("S#a" . repeat("\<C-N>", 3) . "\<Esc>0", 'tx!')
|
||||
call assert_equal('#afoo', getline('.'))
|
||||
call feedkeys("S#a" . repeat("\<C-N>", 3) . "\<C-P>\<Esc>0", 'tx!')
|
||||
call assert_equal('#abc', getline('.'))
|
||||
|
||||
call feedkeys("S#a.a\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abcd', 'abc', '.afoo', '.abar'], b:matches->mapnew('v:val.word'))
|
||||
call feedkeys("S#a.a" . repeat("\<C-N>", 2) . "\<Esc>0", 'tx!')
|
||||
call assert_equal('#a.abc', getline('.'))
|
||||
call feedkeys("S#a.a" . repeat("\<C-N>", 3) . "\<Esc>0", 'tx!')
|
||||
call assert_equal('#a.afoo', getline('.'))
|
||||
call feedkeys("S#a.a" . repeat("\<C-N>", 3) . "\<C-P>\<Esc>0", 'tx!')
|
||||
call assert_equal('#a.abc', getline('.'))
|
||||
call feedkeys("S#a.a" . repeat("\<C-P>", 6) . "\<Esc>0", 'tx!')
|
||||
call assert_equal('#a.abar', getline('.'))
|
||||
|
||||
" Test 4a: When autocomplete menu is active, ^X^N completes buffer keywords
|
||||
let g:CallCount = 0
|
||||
call feedkeys("S#a\<C-X>\<C-N>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'abcd'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(2, g:CallCount)
|
||||
|
||||
" Test 4b: When autocomplete menu is active, ^X^O completes omnifunc
|
||||
let g:CallCount = 0
|
||||
set omnifunc=NonKeywordComplete
|
||||
call feedkeys("S#a\<C-X>\<C-O>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['#afoo', '#abar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(3, g:CallCount)
|
||||
|
||||
" Test 4c: When autocomplete menu is active, ^E^N completes keyword
|
||||
call feedkeys("Sa\<C-E>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal([], b:matches->mapnew('v:val.word'))
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sa\<C-E>\<C-N>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'abcd', 'afoo', 'abar'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(2, g:CallCount)
|
||||
|
||||
" Test 4d: When autocomplete menu is active, ^X^L completes lines
|
||||
%d
|
||||
let g:CallCount = 0
|
||||
call setline(1, ["afoo bar", "barbar foo", "foo bar", "and"])
|
||||
call feedkeys("Goa\<C-X>\<C-L>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['afoo bar', 'and'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
|
||||
" Test 5: When invalid prefix stops completion, backspace should restart it
|
||||
%d
|
||||
set complete&
|
||||
call setline(1, ["afoo bar", "barbar foo", "foo bar", "and"])
|
||||
call feedkeys("Goabc\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal([], b:matches->mapnew('v:val.word'))
|
||||
call feedkeys("Sabc\<BS>\<BS>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['and', 'afoo'], b:matches->mapnew('v:val.word'))
|
||||
call feedkeys("Szx\<BS>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal([], b:matches->mapnew('v:val.word'))
|
||||
call feedkeys("Sazx\<Left>\<BS>\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['and', 'afoo'], b:matches->mapnew('v:val.word'))
|
||||
|
||||
bw!
|
||||
call test_override("char_avail", 0)
|
||||
delfunc NonKeywordComplete
|
||||
set autocomplete&
|
||||
unlet g:CallCount
|
||||
endfunc
|
||||
|
||||
" Test autocomplete timing
|
||||
func Test_autocomplete_timer()
|
||||
|
||||
let g:CallCount = 0
|
||||
func! TestComplete(delay, check, refresh, findstart, base)
|
||||
if a:findstart
|
||||
return col('.') - 1
|
||||
else
|
||||
let g:CallCount += 1
|
||||
if a:delay
|
||||
sleep 310m " Exceed timeout
|
||||
endif
|
||||
if a:check
|
||||
while !complete_check()
|
||||
sleep 2m
|
||||
endwhile
|
||||
return v:none " This should trigger after interrupted by timeout
|
||||
endif
|
||||
let res = [["ab", "ac", "ad"], ["abb", "abc", "abd"], ["acb", "cc", "cd"]]
|
||||
if a:refresh
|
||||
return #{words: res[g:CallCount - 1], refresh: 'always'}
|
||||
endif
|
||||
return res[g:CallCount - 1]
|
||||
endif
|
||||
endfunc
|
||||
|
||||
" Trigger expansion even when another char is waiting in the typehead
|
||||
call test_override("char_avail", 1)
|
||||
|
||||
new
|
||||
inoremap <buffer> <F2> <Cmd>let b:matches = complete_info(["matches"]).matches<CR>
|
||||
inoremap <buffer> <F3> <Cmd>let b:selected = complete_info(["selected"]).selected<CR>
|
||||
set autocomplete
|
||||
|
||||
call setline(1, ['abc', 'bcd', 'cde'])
|
||||
|
||||
" Test 1: When matches are found before timeout expires, it exits
|
||||
" 'collection' mode and transitions to 'filter' mode.
|
||||
set complete=.,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 0])
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Goa\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'ab', 'ac', 'ad'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sab\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'ab'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
|
||||
" Test 2: When timeout expires before all matches are found, it returns
|
||||
" with partial list but still transitions to 'filter' mode.
|
||||
set complete=.,Ffunction('TestComplete'\\,\ [1\\,\ 0\\,\ 0])
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sab\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'ab'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
|
||||
" Test 3: When interrupted by ^N before timeout expires, it remains in
|
||||
" 'collection' mode without transitioning.
|
||||
set complete=.,Ffunction('TestComplete'\\,\ [0\\,\ 1\\,\ 0])
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sa\<C-N>b\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(2, g:CallCount)
|
||||
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sa\<C-N>b\<C-N>c\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(3, g:CallCount)
|
||||
|
||||
" Test 4: Simulate long running func that is stuck in complete_check()
|
||||
let g:CallCount = 0
|
||||
set complete=.,Ffunction('TestComplete'\\,\ [0\\,\ 1\\,\ 0])
|
||||
call feedkeys("Sa\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sab\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
|
||||
" Test 5: refresh:always stays in 'collection' mode
|
||||
set complete=.,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 1])
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sa\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'ab', 'ac', 'ad'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sab\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'abb', 'abd'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(2, g:CallCount)
|
||||
|
||||
" Test 6: <c-n> and <c-p> navigate menu
|
||||
set complete=.,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 0])
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sab\<c-n>\<F2>\<F3>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'ab'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(0, b:selected)
|
||||
call assert_equal(1, g:CallCount)
|
||||
call feedkeys("Sab\<c-n>\<c-n>\<F2>\<F3>\<Esc>0", 'tx!')
|
||||
call assert_equal(1, b:selected)
|
||||
call feedkeys("Sab\<c-n>\<c-p>\<F2>\<F3>\<Esc>0", 'tx!')
|
||||
call assert_equal(-1, b:selected)
|
||||
|
||||
" Test 7: Following 'cot' option values have no effect
|
||||
set completeopt=menu,menuone,noselect,noinsert,longest,preinsert
|
||||
set complete=.,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 0])
|
||||
let g:CallCount = 0
|
||||
call feedkeys("Sab\<c-n>\<F2>\<F3>\<Esc>0", 'tx!')
|
||||
call assert_equal(['abc', 'ab'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(0, b:selected)
|
||||
call assert_equal(1, g:CallCount)
|
||||
call assert_equal('abc', getline(4))
|
||||
set completeopt&
|
||||
|
||||
" Test 8: {func} completes after space, but not '.'
|
||||
set complete=.,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 0])
|
||||
let g:CallCount = 0
|
||||
call feedkeys("S \<F2>\<F3>\<Esc>0", 'tx!')
|
||||
call assert_equal(['ab', 'ac', 'ad'], b:matches->mapnew('v:val.word'))
|
||||
call assert_equal(1, g:CallCount)
|
||||
set complete=.
|
||||
call feedkeys("S \<F2>\<F3>\<Esc>0", 'tx!')
|
||||
call assert_equal([], b:matches->mapnew('v:val.word'))
|
||||
|
||||
" Test 9: Matches nearest to the cursor are prioritized (by default)
|
||||
%d
|
||||
let g:CallCount = 0
|
||||
set complete=.
|
||||
call setline(1, ["fo", "foo", "foobar", "foobarbaz"])
|
||||
call feedkeys("jof\<F2>\<Esc>0", 'tx!')
|
||||
call assert_equal(['foo', 'foobar', 'fo', 'foobarbaz'], b:matches->mapnew('v:val.word'))
|
||||
|
||||
bw!
|
||||
call test_override("char_avail", 0)
|
||||
delfunc TestComplete
|
||||
set autocomplete& complete&
|
||||
unlet g:CallCount
|
||||
endfunc
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab nofoldenable
|
||||
|
||||
@ -719,6 +719,8 @@ static char *(features[]) =
|
||||
|
||||
static int included_patches[] =
|
||||
{ /* Add new patch number below this line */
|
||||
/**/
|
||||
1590,
|
||||
/**/
|
||||
1589,
|
||||
/**/
|
||||
|
||||
Reference in New Issue
Block a user