vim-patch:9.1.1638: completion: not possible to delay the autcompletion

Problem:  completion: not possible to delay the autcompletion
Solution: add the 'autocompletedelay' option value (Girish Palya).

This patch introduces a new global option 'autocompletedelay'/'acl' that
specifies the delay, in milliseconds, before the autocomplete menu
appears after typing.

When set to a non-zero value, Vim waits for the specified time before
showing the completion popup, allowing users to reduce distraction from
rapid suggestion pop-ups or to fine-tune the responsiveness of
completion.

The default value is 0, which preserves the current immediate-popup
behavior.

closes: vim/vim#17960

a09b1604d4

N/A patch: vim-patch:9.1.1641: a few compiler warnings are output

Co-authored-by: Girish Palya <girishji@gmail.com>
This commit is contained in:
zeertzjq
2025-08-22 14:54:26 +08:00
parent 639f9f4cda
commit 4019d3050d
10 changed files with 200 additions and 8 deletions

View File

@@ -2206,6 +2206,13 @@ static void ins_compl_new_leader(void)
ins_compl_insert_bytes(compl_leader.data + get_compl_len(), -1);
compl_used_match = false;
if (p_acl > 0) {
pum_undisplay(true);
redraw_later(curwin, UPD_VALID);
update_screen(); // Show char (deletion) immediately
ui_flush();
}
if (compl_started) {
ins_compl_set_original_text(compl_leader.data, compl_leader.size);
if (is_cpt_func_refresh_always()) {
@@ -4811,10 +4818,10 @@ static int ins_compl_get_exp(pos_T *ini)
found_new_match = FAIL;
}
int i = -1; // total of matches, unknown
int match_count = -1; // total of matches, unknown
if (found_new_match == FAIL
|| (ctrl_x_mode_not_default() && !ctrl_x_mode_line_or_eval())) {
i = ins_compl_make_cyclic();
match_count = ins_compl_make_cyclic();
}
if (cfc_has_mode() && compl_get_longest && compl_num_bests > 0) {
@@ -4838,7 +4845,7 @@ static int ins_compl_get_exp(pos_T *ini)
sort_compl_match_list(cp_compare_nearest);
}
return i;
return match_count;
}
/// Update "compl_shown_match" to the actually shown match, it may differ when
@@ -5288,7 +5295,7 @@ static int ins_compl_next(bool allow_get_expansion, int count, bool insert_match
/// collecting, and halve the timeout.
static void check_elapsed_time(void)
{
if (cpt_sources_array == NULL) {
if (cpt_sources_array == NULL || cpt_sources_index < 0) {
return;
}
@@ -6026,6 +6033,11 @@ int ins_complete(int c, bool enable_pum)
return FAIL;
}
// Timestamp when match collection starts
uint64_t compl_start_tv = 0;
if (compl_autocomplete && p_acl > 0) {
compl_start_tv = os_hrtime();
}
compl_curr_win = curwin;
compl_curr_buf = curwin->w_buffer;
compl_shown_match = compl_curr_match;
@@ -6050,7 +6062,8 @@ int ins_complete(int c, bool enable_pum)
}
// we found no match if the list has only the "compl_orig_text"-entry
if (is_first_match(compl_first_match->cp_next)) {
bool no_matches_found = is_first_match(compl_first_match->cp_next);
if (no_matches_found) {
// remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
// because we couldn't expand anything at first place, but if we used
// ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
@@ -6074,6 +6087,22 @@ int ins_complete(int c, bool enable_pum)
ins_compl_show_statusmsg();
}
// Wait for the autocompletion delay to expire
if (compl_autocomplete && p_acl > 0 && !no_matches_found
&& (os_hrtime() - compl_start_tv) / 1000000 < (uint64_t)p_acl) {
setcursor();
ui_flush();
do {
if (char_avail()) {
ins_compl_restart();
compl_interrupted = true;
break;
} else {
os_delay(2L, true);
}
} while ((os_hrtime() - compl_start_tv) / 1000000 < (uint64_t)p_acl);
}
// Show the popup menu, unless we got interrupted.
if (enable_pum && !compl_interrupted) {
show_pum(save_w_wrow, save_w_leftcol);

View File

@@ -301,6 +301,7 @@ EXTERN unsigned cia_flags; ///< order flags of 'completeitemalign'
EXTERN char *p_cot; ///< 'completeopt'
EXTERN unsigned cot_flags; ///< flags from 'completeopt'
EXTERN int p_ac; ///< 'autocomplete'
EXTERN OptInt p_acl; ///< 'autocompletedelay'
#ifdef BACKSLASH_IN_FILENAME
EXTERN char *p_csl; ///< 'completeslash'
#endif

View File

@@ -241,6 +241,20 @@ local options = {
type = 'boolean',
varname = 'p_ac',
},
{
abbreviation = 'acl',
defaults = 0,
desc = [=[
Delay in milliseconds before the autocomplete menu appears after
typing. If you prefer it not to open too quickly, set this value
slightly above your typing speed. See |ins-autocompletion|.
]=],
full_name = 'autocompletedelay',
scope = { 'global' },
short_desc = N_('delay in msec before menu appears after typing'),
type = 'number',
varname = 'p_acl',
},
{
abbreviation = 'ai',
defaults = true,