mirror of
https://github.com/neovim/neovim.git
synced 2025-10-13 13:26:06 +00:00
vim-patch:8.2.3937: Insert mode completion function is too long
Problem: Insert mode completion function is too long.
Solution: Refactor into multiple functions. (Yegappan Lakshmanan,
closes vim/vim#9423)
edc6f10390
Cherry-pick a typo fix from patch 8.2.3637.
This commit is contained in:
@@ -2464,196 +2464,181 @@ static bool thesaurus_func_complete(int type)
|
||||
&& (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
|
||||
}
|
||||
|
||||
/// Get the next expansion(s), using "compl_pattern".
|
||||
/// The search starts at position "ini" in curbuf and in the direction
|
||||
/// compl_direction.
|
||||
/// When "compl_started" is false start at that position, otherwise continue
|
||||
/// where we stopped searching before.
|
||||
/// This may return before finding all the matches.
|
||||
/// Return the total number of matches or -1 if still unknown -- Acevedo
|
||||
static int ins_compl_get_exp(pos_T *ini)
|
||||
{
|
||||
static pos_T first_match_pos;
|
||||
static pos_T last_match_pos;
|
||||
static char *e_cpt = ""; // curr. entry in 'complete'
|
||||
static bool found_all = false; // Found all matches of a
|
||||
// certain type.
|
||||
static buf_T *ins_buf = NULL; // buffer being scanned
|
||||
/// Return value of process_next_cpt_value()
|
||||
enum {
|
||||
INS_COMPL_CPT_OK = 1,
|
||||
INS_COMPL_CPT_CONT,
|
||||
INS_COMPL_CPT_END,
|
||||
};
|
||||
|
||||
pos_T *pos;
|
||||
char **matches;
|
||||
int save_p_scs;
|
||||
bool save_p_ws;
|
||||
int save_p_ic;
|
||||
int i;
|
||||
int num_matches;
|
||||
int len;
|
||||
int found_new_match;
|
||||
int type = ctrl_x_mode;
|
||||
char_u *ptr;
|
||||
/// Process the next 'complete' option value in "e_cpt_arg".
|
||||
///
|
||||
/// If successful, the arguments are set as below:
|
||||
/// e_cpt_arg - pointer to the next option value in 'e_cpt_arg'
|
||||
/// compl_type_arg - type of insert mode completion to use
|
||||
/// found_all_arg - all matches of this type are found
|
||||
/// buf_arg - search for completions in this buffer
|
||||
/// first_match_pos - position of the first completion match
|
||||
/// last_match_pos - position of the last completion match
|
||||
/// set_match_pos - true if the first match position should be saved to avoid
|
||||
/// loops after the search wraps around.
|
||||
/// dict - name of the dictionary or thesaurus file to search
|
||||
/// dict_f - flag specifying whether "dict" is an exact file name or not
|
||||
///
|
||||
/// @return INS_COMPL_CPT_OK if the next value is processed successfully.
|
||||
/// INS_COMPL_CPT_CONT to skip the current value and process the next
|
||||
/// option value.
|
||||
/// INS_COMPL_CPT_END if all the values in "e_cpt" are processed.
|
||||
static int process_next_cpt_value(char **e_cpt_arg, int *compl_type_arg, bool *found_all_arg,
|
||||
buf_T **buf_arg, pos_T *start_match_pos, pos_T *first_match_pos,
|
||||
pos_T *last_match_pos, bool *set_match_pos, char_u **dict_arg,
|
||||
int *dict_flag)
|
||||
{
|
||||
char *e_cpt = *e_cpt_arg;
|
||||
int compl_type = -1;
|
||||
int status = INS_COMPL_CPT_OK;
|
||||
buf_T *buf = *buf_arg;
|
||||
bool found_all = false;
|
||||
char_u *dict = NULL;
|
||||
int dict_f = 0;
|
||||
bool set_match_pos;
|
||||
pos_T prev_pos = { 0, 0, 0 };
|
||||
|
||||
assert(curbuf != NULL);
|
||||
|
||||
if (!compl_started) {
|
||||
FOR_ALL_BUFFERS(buf) {
|
||||
buf->b_scanned = false;
|
||||
}
|
||||
found_all = false;
|
||||
ins_buf = curbuf;
|
||||
e_cpt = (compl_cont_status & CONT_LOCAL) ? "." : (char *)curbuf->b_p_cpt;
|
||||
last_match_pos = first_match_pos = *ini;
|
||||
} else if (ins_buf != curbuf && !buf_valid(ins_buf)) {
|
||||
ins_buf = curbuf; // In case the buffer was wiped out.
|
||||
}
|
||||
assert(ins_buf != NULL);
|
||||
|
||||
compl_old_match = compl_curr_match; // remember the last current match
|
||||
pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
|
||||
|
||||
// For ^N/^P loop over all the flags/windows/buffers in 'complete'
|
||||
for (;;) {
|
||||
found_new_match = FAIL;
|
||||
set_match_pos = false;
|
||||
|
||||
// For ^N/^P pick a new entry from e_cpt if compl_started is off,
|
||||
// or if found_all says this entry is done. For ^X^L only use the
|
||||
// entries from 'complete' that look in loaded buffers.
|
||||
if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
|
||||
&& (!compl_started || found_all)) {
|
||||
found_all = false;
|
||||
while (*e_cpt == ',' || *e_cpt == ' ') {
|
||||
e_cpt++;
|
||||
}
|
||||
if (*e_cpt == '.' && !curbuf->b_scanned) {
|
||||
ins_buf = curbuf;
|
||||
first_match_pos = *ini;
|
||||
buf = curbuf;
|
||||
*first_match_pos = *start_match_pos;
|
||||
// Move the cursor back one character so that ^N can match the
|
||||
// word immediately after the cursor.
|
||||
if (ctrl_x_mode_normal() && dec(&first_match_pos) < 0) {
|
||||
if (ctrl_x_mode_normal() && dec(first_match_pos) < 0) {
|
||||
// Move the cursor to after the last character in the
|
||||
// buffer, so that word at start of buffer is found
|
||||
// correctly.
|
||||
first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
|
||||
first_match_pos.col = (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
|
||||
first_match_pos->lnum = buf->b_ml.ml_line_count;
|
||||
first_match_pos->col = (colnr_T)STRLEN(ml_get(first_match_pos->lnum));
|
||||
}
|
||||
last_match_pos = first_match_pos;
|
||||
type = 0;
|
||||
*last_match_pos = *first_match_pos;
|
||||
compl_type = 0;
|
||||
|
||||
// Remember the first match so that the loop stops when we
|
||||
// wrap and come back there a second time.
|
||||
set_match_pos = true;
|
||||
*set_match_pos = true;
|
||||
} else if (vim_strchr("buwU", *e_cpt) != NULL
|
||||
&& (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) {
|
||||
&& (buf = ins_compl_next_buf(buf, *e_cpt)) != curbuf) {
|
||||
// Scan a buffer, but not the current one.
|
||||
if (ins_buf->b_ml.ml_mfp != NULL) { // loaded buffer
|
||||
if (buf->b_ml.ml_mfp != NULL) { // loaded buffer
|
||||
compl_started = true;
|
||||
first_match_pos.col = last_match_pos.col = 0;
|
||||
first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
|
||||
last_match_pos.lnum = 0;
|
||||
type = 0;
|
||||
first_match_pos->col = last_match_pos->col = 0;
|
||||
first_match_pos->lnum = buf->b_ml.ml_line_count + 1;
|
||||
last_match_pos->lnum = 0;
|
||||
compl_type = 0;
|
||||
} else { // unloaded buffer, scan like dictionary
|
||||
found_all = true;
|
||||
if (ins_buf->b_fname == NULL) {
|
||||
continue;
|
||||
if (buf->b_fname == NULL) {
|
||||
status = INS_COMPL_CPT_CONT;
|
||||
goto done;
|
||||
}
|
||||
type = CTRL_X_DICTIONARY;
|
||||
dict = (char_u *)ins_buf->b_fname;
|
||||
compl_type = CTRL_X_DICTIONARY;
|
||||
dict = (char_u *)buf->b_fname;
|
||||
dict_f = DICT_EXACT;
|
||||
}
|
||||
msg_hist_off = true; // reset in msg_trunc_attr()
|
||||
vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
|
||||
ins_buf->b_fname == NULL
|
||||
? buf_spname(ins_buf)
|
||||
: ins_buf->b_sfname == NULL
|
||||
? ins_buf->b_fname
|
||||
: ins_buf->b_sfname);
|
||||
buf->b_fname == NULL
|
||||
? buf_spname(buf)
|
||||
: buf->b_sfname == NULL
|
||||
? buf->b_fname
|
||||
: buf->b_sfname);
|
||||
(void)msg_trunc_attr((char *)IObuff, true, HL_ATTR(HLF_R));
|
||||
} else if (*e_cpt == NUL) {
|
||||
break;
|
||||
status = INS_COMPL_CPT_END;
|
||||
} else {
|
||||
if (ctrl_x_mode_line_or_eval()) {
|
||||
type = -1;
|
||||
compl_type = -1;
|
||||
} else if (*e_cpt == 'k' || *e_cpt == 's') {
|
||||
if (*e_cpt == 'k') {
|
||||
type = CTRL_X_DICTIONARY;
|
||||
compl_type = CTRL_X_DICTIONARY;
|
||||
} else {
|
||||
type = CTRL_X_THESAURUS;
|
||||
compl_type = CTRL_X_THESAURUS;
|
||||
}
|
||||
if (*++e_cpt != ',' && *e_cpt != NUL) {
|
||||
dict = (char_u *)e_cpt;
|
||||
dict_f = DICT_FIRST;
|
||||
}
|
||||
} else if (*e_cpt == 'i') {
|
||||
type = CTRL_X_PATH_PATTERNS;
|
||||
compl_type = CTRL_X_PATH_PATTERNS;
|
||||
} else if (*e_cpt == 'd') {
|
||||
type = CTRL_X_PATH_DEFINES;
|
||||
compl_type = CTRL_X_PATH_DEFINES;
|
||||
} else if (*e_cpt == ']' || *e_cpt == 't') {
|
||||
msg_hist_off = true; // reset in msg_trunc_attr()
|
||||
type = CTRL_X_TAGS;
|
||||
compl_type = CTRL_X_TAGS;
|
||||
vim_snprintf((char *)IObuff, IOSIZE, "%s", _("Scanning tags."));
|
||||
(void)msg_trunc_attr((char *)IObuff, true, HL_ATTR(HLF_R));
|
||||
} else {
|
||||
type = -1;
|
||||
compl_type = -1;
|
||||
}
|
||||
|
||||
// in any case e_cpt is advanced to the next entry
|
||||
(void)copy_option_part(&e_cpt, (char *)IObuff, IOSIZE, ",");
|
||||
|
||||
found_all = true;
|
||||
if (type == -1) {
|
||||
continue;
|
||||
}
|
||||
if (compl_type == -1) {
|
||||
status = INS_COMPL_CPT_CONT;
|
||||
}
|
||||
}
|
||||
|
||||
// If complete() was called then compl_pattern has been reset.
|
||||
// The following won't work then, bail out.
|
||||
if (compl_pattern == NULL) {
|
||||
break;
|
||||
done:
|
||||
*e_cpt_arg = e_cpt;
|
||||
*compl_type_arg = compl_type;
|
||||
*found_all_arg = found_all;
|
||||
*buf_arg = buf;
|
||||
*dict_arg = dict;
|
||||
*dict_flag = dict_f;
|
||||
return status;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case -1:
|
||||
break;
|
||||
case CTRL_X_PATH_PATTERNS:
|
||||
case CTRL_X_PATH_DEFINES:
|
||||
/// Get the next set of identifiers or defines matching "compl_pattern" in
|
||||
/// included files.
|
||||
static void get_next_include_file_completion(int compl_type)
|
||||
{
|
||||
find_pattern_in_path((char_u *)compl_pattern, compl_direction,
|
||||
STRLEN(compl_pattern), false, false,
|
||||
((type == CTRL_X_PATH_DEFINES
|
||||
((compl_type == CTRL_X_PATH_DEFINES
|
||||
&& !(compl_cont_status & CONT_SOL))
|
||||
? FIND_DEFINE
|
||||
: FIND_ANY),
|
||||
? FIND_DEFINE : FIND_ANY),
|
||||
1L, ACTION_EXPAND, 1, MAXLNUM);
|
||||
break;
|
||||
|
||||
case CTRL_X_DICTIONARY:
|
||||
case CTRL_X_THESAURUS:
|
||||
if (thesaurus_func_complete(type)) {
|
||||
expand_by_function(type, (char_u *)compl_pattern);
|
||||
} else {
|
||||
ins_compl_dictionaries(dict != NULL ? dict
|
||||
: (type == CTRL_X_THESAURUS
|
||||
? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
|
||||
: (*curbuf->b_p_dict ==
|
||||
NUL ? p_dict : curbuf->b_p_dict)),
|
||||
(char_u *)compl_pattern,
|
||||
dict != NULL ? dict_f : 0, type == CTRL_X_THESAURUS);
|
||||
}
|
||||
dict = NULL;
|
||||
break;
|
||||
|
||||
case CTRL_X_TAGS:
|
||||
/// Get the next set of words matching "compl_pattern" in dictionary or
|
||||
/// thesaurus files.
|
||||
static void get_next_dict_tsr_completion(int compl_type, char_u **dict, int dict_f)
|
||||
{
|
||||
if (thesaurus_func_complete(compl_type)) {
|
||||
expand_by_function(compl_type, (char_u *)compl_pattern);
|
||||
} else {
|
||||
ins_compl_dictionaries(*dict != NULL ? *dict
|
||||
: (compl_type == CTRL_X_THESAURUS
|
||||
? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr)
|
||||
: (*curbuf->b_p_dict == NUL ? p_dict : curbuf->b_p_dict)),
|
||||
(char_u *)compl_pattern,
|
||||
*dict != NULL ? dict_f : 0,
|
||||
compl_type == CTRL_X_THESAURUS);
|
||||
}
|
||||
*dict = NULL;
|
||||
}
|
||||
|
||||
/// Get the next set of tag names matching "compl_pattern".
|
||||
static void get_next_tag_completion(void)
|
||||
{
|
||||
// set p_ic according to p_ic, p_scs and pat for find_tags().
|
||||
save_p_ic = p_ic;
|
||||
const int save_p_ic = p_ic;
|
||||
p_ic = ignorecase((char_u *)compl_pattern);
|
||||
|
||||
// Find up to TAG_MANY matches. Avoids that an enormous number
|
||||
// of matches is found when compl_pattern is empty
|
||||
g_tag_at_cursor = true;
|
||||
char **matches;
|
||||
int num_matches;
|
||||
if (find_tags((char_u *)compl_pattern, &num_matches, &matches,
|
||||
TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
|
||||
| (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0),
|
||||
@@ -2662,11 +2647,18 @@ static int ins_compl_get_exp(pos_T *ini)
|
||||
}
|
||||
g_tag_at_cursor = false;
|
||||
p_ic = save_p_ic;
|
||||
break;
|
||||
}
|
||||
|
||||
case CTRL_X_FILES:
|
||||
/// Get the next set of filename matching "compl_pattern".
|
||||
static void get_next_filename_completion(void)
|
||||
{
|
||||
char **matches;
|
||||
int num_matches;
|
||||
if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
|
||||
EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) {
|
||||
EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) != OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
// May change home directory back to "~".
|
||||
tilde_replace((char_u *)compl_pattern, num_matches, matches);
|
||||
#ifdef BACKSLASH_IN_FILENAME
|
||||
@@ -2686,33 +2678,52 @@ static int ins_compl_get_exp(pos_T *ini)
|
||||
#endif
|
||||
ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
|
||||
}
|
||||
break;
|
||||
|
||||
case CTRL_X_CMDLINE:
|
||||
case CTRL_X_CMDLINE_CTRL_X:
|
||||
/// Get the next set of command-line completions matching "compl_pattern".
|
||||
static void get_next_cmdline_completion(void)
|
||||
{
|
||||
char **matches;
|
||||
int num_matches;
|
||||
if (expand_cmdline(&compl_xp, (char_u *)compl_pattern,
|
||||
(int)STRLEN(compl_pattern),
|
||||
&num_matches, &matches) == EXPAND_OK) {
|
||||
ins_compl_add_matches(num_matches, matches, false);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CTRL_X_FUNCTION:
|
||||
case CTRL_X_OMNI:
|
||||
expand_by_function(type, (char_u *)compl_pattern);
|
||||
break;
|
||||
|
||||
case CTRL_X_SPELL:
|
||||
num_matches = expand_spelling(first_match_pos.lnum,
|
||||
(char_u *)compl_pattern, &matches);
|
||||
/// Get the next set of spell suggestions matching "compl_pattern".
|
||||
static void get_next_spell_completion(linenr_T lnum)
|
||||
{
|
||||
char **matches;
|
||||
int num_matches = expand_spelling(lnum, (char_u *)compl_pattern, &matches);
|
||||
if (num_matches > 0) {
|
||||
ins_compl_add_matches(num_matches, matches, p_ic);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: // normal ^P/^N and ^X^L
|
||||
/// Get the next set of words matching "compl_pattern" for default completion(s)
|
||||
/// (normal ^P/^N and ^X^L).
|
||||
/// Search for "compl_pattern" in the buffer "ins_buf" starting from the
|
||||
/// position "start_pos" in the "compl_direction" direction. If "save_match_pos"
|
||||
/// is true, then set the "first_match_pos" and "last_match_pos".
|
||||
///
|
||||
/// @param ins_buf buffer being scanned
|
||||
/// @param start_pos search start position
|
||||
/// @param cur_match_pos current match position
|
||||
/// @param prev_match_pos previous match position
|
||||
/// @param save_match_pos set first_match_pos/last_match_pos
|
||||
/// @param first_match_pos first match position
|
||||
/// @param last_match_pos last match position
|
||||
/// @param scan_curbuf scan current buffer for completion
|
||||
///
|
||||
/// @return OK if a new next match is found, otherwise FAIL.
|
||||
static int get_next_default_completion(buf_T *ins_buf, pos_T *start_pos, pos_T *cur_match_pos,
|
||||
pos_T *prev_match_pos, bool *save_match_pos,
|
||||
pos_T *first_match_pos, pos_T *last_match_pos,
|
||||
bool scan_curbuf)
|
||||
{
|
||||
// If 'infercase' is set, don't use 'smartcase' here
|
||||
save_p_scs = p_scs;
|
||||
const int save_p_scs = p_scs;
|
||||
assert(ins_buf);
|
||||
if (ins_buf->b_p_inf) {
|
||||
p_scs = false;
|
||||
@@ -2722,13 +2733,14 @@ static int ins_compl_get_exp(pos_T *ini)
|
||||
// end but never from the middle, thus setting nowrapscan in this
|
||||
// buffers is a good idea, on the other hand, we always set
|
||||
// wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
|
||||
save_p_ws = p_ws;
|
||||
const int save_p_ws = p_ws;
|
||||
if (ins_buf != curbuf) {
|
||||
p_ws = false;
|
||||
} else if (*e_cpt == '.') {
|
||||
} else if (scan_curbuf) {
|
||||
p_ws = true;
|
||||
}
|
||||
bool looped_around = false;
|
||||
int found_new_match = FAIL;
|
||||
for (;;) {
|
||||
bool cont_s_ipos = false;
|
||||
|
||||
@@ -2737,66 +2749,62 @@ static int ins_compl_get_exp(pos_T *ini)
|
||||
// has added a word that was at the beginning of the line.
|
||||
if (ctrl_x_mode_line_or_eval()
|
||||
|| (compl_cont_status & CONT_SOL)) {
|
||||
found_new_match = search_for_exact_line(ins_buf, pos,
|
||||
compl_direction,
|
||||
(char_u *)compl_pattern);
|
||||
found_new_match = search_for_exact_line(ins_buf, cur_match_pos,
|
||||
compl_direction, (char_u *)compl_pattern);
|
||||
} else {
|
||||
found_new_match = searchit(NULL, ins_buf, pos, NULL,
|
||||
found_new_match = searchit(NULL, ins_buf, cur_match_pos, NULL,
|
||||
compl_direction,
|
||||
(char_u *)compl_pattern, 1L,
|
||||
SEARCH_KEEP + SEARCH_NFMSG,
|
||||
(char_u *)compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
|
||||
RE_LAST, NULL);
|
||||
}
|
||||
msg_silent--;
|
||||
if (!compl_started || set_match_pos) {
|
||||
if (!compl_started || *save_match_pos) {
|
||||
// set "compl_started" even on fail
|
||||
compl_started = true;
|
||||
first_match_pos = *pos;
|
||||
last_match_pos = *pos;
|
||||
set_match_pos = false;
|
||||
} else if (first_match_pos.lnum == last_match_pos.lnum
|
||||
&& first_match_pos.col == last_match_pos.col) {
|
||||
*first_match_pos = *cur_match_pos;
|
||||
*last_match_pos = *cur_match_pos;
|
||||
*save_match_pos = false;
|
||||
} else if (first_match_pos->lnum == last_match_pos->lnum
|
||||
&& first_match_pos->col == last_match_pos->col) {
|
||||
found_new_match = FAIL;
|
||||
} else if ((compl_direction == FORWARD)
|
||||
&& (prev_pos.lnum > pos->lnum
|
||||
|| (prev_pos.lnum == pos->lnum
|
||||
&& prev_pos.col >= pos->col))) {
|
||||
&& (prev_match_pos->lnum > cur_match_pos->lnum
|
||||
|| (prev_match_pos->lnum == cur_match_pos->lnum
|
||||
&& prev_match_pos->col >= cur_match_pos->col))) {
|
||||
if (looped_around) {
|
||||
found_new_match = FAIL;
|
||||
} else {
|
||||
looped_around = true;
|
||||
}
|
||||
} else if ((compl_direction != FORWARD)
|
||||
&& (prev_pos.lnum < pos->lnum
|
||||
|| (prev_pos.lnum == pos->lnum
|
||||
&& prev_pos.col <= pos->col))) {
|
||||
&& (prev_match_pos->lnum < cur_match_pos->lnum
|
||||
|| (prev_match_pos->lnum == cur_match_pos->lnum
|
||||
&& prev_match_pos->col <= cur_match_pos->col))) {
|
||||
if (looped_around) {
|
||||
found_new_match = FAIL;
|
||||
} else {
|
||||
looped_around = true;
|
||||
}
|
||||
}
|
||||
prev_pos = *pos;
|
||||
*prev_match_pos = *cur_match_pos;
|
||||
if (found_new_match == FAIL) {
|
||||
if (ins_buf == curbuf) {
|
||||
found_all = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// when ADDING, the text before the cursor matches, skip it
|
||||
if ((compl_cont_status & CONT_ADDING) && ins_buf == curbuf
|
||||
&& ini->lnum == pos->lnum
|
||||
&& ini->col == pos->col) {
|
||||
&& start_pos->lnum == cur_match_pos->lnum
|
||||
&& start_pos->col == cur_match_pos->col) {
|
||||
continue;
|
||||
}
|
||||
ptr = ml_get_buf(ins_buf, pos->lnum, false) + pos->col;
|
||||
char_u *ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, false) + cur_match_pos->col;
|
||||
int len;
|
||||
if (ctrl_x_mode_line_or_eval()) {
|
||||
if (compl_cont_status & CONT_ADDING) {
|
||||
if (pos->lnum >= ins_buf->b_ml.ml_line_count) {
|
||||
if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count) {
|
||||
continue;
|
||||
}
|
||||
ptr = ml_get_buf(ins_buf, pos->lnum + 1, false);
|
||||
ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, false);
|
||||
if (!p_paste) {
|
||||
ptr = (char_u *)skipwhite((char *)ptr);
|
||||
}
|
||||
@@ -2818,14 +2826,13 @@ static int ins_compl_get_exp(pos_T *ini)
|
||||
tmp_ptr = find_word_end(tmp_ptr);
|
||||
len = (int)(tmp_ptr - ptr);
|
||||
|
||||
if ((compl_cont_status & CONT_ADDING)
|
||||
&& len == compl_length) {
|
||||
if (pos->lnum < ins_buf->b_ml.ml_line_count) {
|
||||
if ((compl_cont_status & CONT_ADDING) && len == compl_length) {
|
||||
if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count) {
|
||||
// Try next line, if any. the new word will be "join" as if the
|
||||
// normal command "J" was used. IOSIZE is always greater than
|
||||
// compl_length, so the next STRNCPY always works -- Acevedo
|
||||
STRNCPY(IObuff, ptr, len); // NOLINT(runtime/printf)
|
||||
ptr = ml_get_buf(ins_buf, pos->lnum + 1, false);
|
||||
ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, false);
|
||||
tmp_ptr = ptr = (char_u *)skipwhite((char *)ptr);
|
||||
// Find start of next word.
|
||||
tmp_ptr = find_word_start(tmp_ptr);
|
||||
@@ -2869,6 +2876,122 @@ static int ins_compl_get_exp(pos_T *ini)
|
||||
}
|
||||
p_scs = save_p_scs;
|
||||
p_ws = save_p_ws;
|
||||
|
||||
return found_new_match;
|
||||
}
|
||||
|
||||
/// Get the next expansion(s), using "compl_pattern".
|
||||
/// The search starts at position "ini" in curbuf and in the direction
|
||||
/// compl_direction.
|
||||
/// When "compl_started" is false start at that position, otherwise continue
|
||||
/// where we stopped searching before.
|
||||
/// This may return before finding all the matches.
|
||||
/// Return the total number of matches or -1 if still unknown -- Acevedo
|
||||
static int ins_compl_get_exp(pos_T *ini)
|
||||
{
|
||||
static pos_T first_match_pos;
|
||||
static pos_T last_match_pos;
|
||||
static char *e_cpt = ""; // curr. entry in 'complete'
|
||||
static bool found_all = false; // Found all matches of a
|
||||
// certain type.
|
||||
static buf_T *ins_buf = NULL; // buffer being scanned
|
||||
|
||||
pos_T *pos;
|
||||
int i;
|
||||
int found_new_match;
|
||||
int type = ctrl_x_mode;
|
||||
char_u *dict = NULL;
|
||||
int dict_f = 0;
|
||||
bool set_match_pos;
|
||||
pos_T prev_pos = { 0, 0, 0 };
|
||||
|
||||
assert(curbuf != NULL);
|
||||
|
||||
if (!compl_started) {
|
||||
FOR_ALL_BUFFERS(buf) {
|
||||
buf->b_scanned = false;
|
||||
}
|
||||
found_all = false;
|
||||
ins_buf = curbuf;
|
||||
e_cpt = (compl_cont_status & CONT_LOCAL) ? "." : (char *)curbuf->b_p_cpt;
|
||||
last_match_pos = first_match_pos = *ini;
|
||||
} else if (ins_buf != curbuf && !buf_valid(ins_buf)) {
|
||||
ins_buf = curbuf; // In case the buffer was wiped out.
|
||||
}
|
||||
assert(ins_buf != NULL);
|
||||
|
||||
compl_old_match = compl_curr_match; // remember the last current match
|
||||
pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
|
||||
|
||||
// For ^N/^P loop over all the flags/windows/buffers in 'complete'
|
||||
for (;;) {
|
||||
found_new_match = FAIL;
|
||||
set_match_pos = false;
|
||||
|
||||
// For ^N/^P pick a new entry from e_cpt if compl_started is off,
|
||||
// or if found_all says this entry is done. For ^X^L only use the
|
||||
// entries from 'complete' that look in loaded buffers.
|
||||
if ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval())
|
||||
&& (!compl_started || found_all)) {
|
||||
int status = process_next_cpt_value(&e_cpt, &type, &found_all,
|
||||
&ins_buf, ini, &first_match_pos, &last_match_pos,
|
||||
&set_match_pos, &dict, &dict_f);
|
||||
if (status == INS_COMPL_CPT_END) {
|
||||
break;
|
||||
}
|
||||
if (status == INS_COMPL_CPT_CONT) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// If complete() was called then compl_pattern has been reset.
|
||||
// The following won't work then, bail out.
|
||||
if (compl_pattern == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case -1:
|
||||
break;
|
||||
case CTRL_X_PATH_PATTERNS:
|
||||
case CTRL_X_PATH_DEFINES:
|
||||
get_next_include_file_completion(type);
|
||||
break;
|
||||
|
||||
case CTRL_X_DICTIONARY:
|
||||
case CTRL_X_THESAURUS:
|
||||
get_next_dict_tsr_completion(type, &dict, dict_f);
|
||||
break;
|
||||
|
||||
case CTRL_X_TAGS:
|
||||
get_next_tag_completion();
|
||||
break;
|
||||
|
||||
case CTRL_X_FILES:
|
||||
get_next_filename_completion();
|
||||
break;
|
||||
|
||||
case CTRL_X_CMDLINE:
|
||||
case CTRL_X_CMDLINE_CTRL_X:
|
||||
get_next_cmdline_completion();
|
||||
break;
|
||||
|
||||
case CTRL_X_FUNCTION:
|
||||
case CTRL_X_OMNI:
|
||||
expand_by_function(type, (char_u *)compl_pattern);
|
||||
break;
|
||||
|
||||
case CTRL_X_SPELL:
|
||||
get_next_spell_completion(first_match_pos.lnum);
|
||||
break;
|
||||
|
||||
default: // normal ^P/^N and ^X^L
|
||||
found_new_match = get_next_default_completion(ins_buf, ini, pos, &prev_pos,
|
||||
&set_match_pos, &first_match_pos,
|
||||
&last_match_pos, (*e_cpt == '.'));
|
||||
if (found_new_match == FAIL && ins_buf == curbuf) {
|
||||
found_all = true;
|
||||
}
|
||||
}
|
||||
|
||||
// check if compl_curr_match has changed, (e.g. other type of
|
||||
|
@@ -44,11 +44,11 @@ func Test_ins_complete()
|
||||
exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
|
||||
call assert_equal('run1 run2', getline('.'))
|
||||
|
||||
set cpt=.,w,i
|
||||
set cpt=.,\ ,w,i
|
||||
" i-add-expands and switches to local
|
||||
exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
|
||||
call assert_equal("Makefile\tto\trun3", getline('.'))
|
||||
" add-expands lines (it would end in an empty line if it didn't ignored
|
||||
" add-expands lines (it would end in an empty line if it didn't ignore
|
||||
" itself)
|
||||
exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
|
||||
call assert_equal("Makefile\tto\trun3", getline('.'))
|
||||
@@ -721,6 +721,17 @@ func Test_complete_across_line()
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" Test for completing words with a '.' at the end of a word.
|
||||
func Test_complete_joinspaces()
|
||||
new
|
||||
call setline(1, ['one two.', 'three. four'])
|
||||
set joinspaces
|
||||
exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
|
||||
call assert_equal("one two. three. four", getline(3))
|
||||
set joinspaces&
|
||||
bw!
|
||||
endfunc
|
||||
|
||||
" Test for using CTRL-L to add one character when completing matching
|
||||
func Test_complete_add_onechar()
|
||||
new
|
||||
@@ -741,6 +752,39 @@ func Test_complete_add_onechar()
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" Test for using CTRL-X CTRL-L to complete whole lines lines
|
||||
func Test_complete_wholeline()
|
||||
new
|
||||
" complete one-line
|
||||
call setline(1, ['a1', 'a2'])
|
||||
exe "normal ggoa\<C-X>\<C-L>"
|
||||
call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
|
||||
" go to the next match (wrapping around the buffer)
|
||||
exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
|
||||
call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
|
||||
" go to the next match
|
||||
exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
|
||||
call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
|
||||
exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
|
||||
call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
|
||||
" repeat the test using CTRL-L
|
||||
" go to the next match (wrapping around the buffer)
|
||||
exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
|
||||
call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
|
||||
" go to the next match
|
||||
exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
|
||||
call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
|
||||
exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
|
||||
call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
|
||||
%d
|
||||
" use CTRL-X CTRL-L to add one more line
|
||||
call setline(1, ['a1', 'b1'])
|
||||
setlocal complete=.
|
||||
exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
|
||||
call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
|
||||
bw!
|
||||
endfunc
|
||||
|
||||
" Test insert completion with 'cindent' (adjust the indent)
|
||||
func Test_complete_with_cindent()
|
||||
new
|
||||
|
Reference in New Issue
Block a user