vim-patch:9.1.1526: completion: search completion match may differ in case

Problem:  completion: search completion match may differ in case
          (techntools)
Solution: add "exacttext" to 'wildoptions' value (Girish Palya)

This flag does the following:

exacttext
      When this flag is present, search pattern completion
      (e.g., in |/|, |?|, |:s|, |:g|, |:v|, and |:vim|)
      shows exact buffer text as menu items, without
      preserving regex artifacts like position
      anchors (e.g., |/\<|). This provides more intuitive
      menu items that match the actual buffer text. However,
      searches may be less accurate since the pattern is not
      preserved exactly.
      By default, Vim preserves the typed pattern (with
      anchors) and appends the matched word. This preserves
      search correctness, especially when using regular
      expressions or with 'smartcase' enabled. However, the
      case of the appended matched word may not exactly
      match the case of the word in the buffer.

fixes: vim/vim#17654
closes: vim/vim#17667

93c2d5bf7f

Co-authored-by: Girish Palya <girishji@gmail.com>
This commit is contained in:
zeertzjq
2025-07-09 07:38:43 +08:00
parent db7c2acbc6
commit ef0ec7edac
5 changed files with 106 additions and 17 deletions

View File

@ -3893,6 +3893,8 @@ void f_cmdcomplete_info(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
/// matched text is returned in '*match_end'.
static int copy_substring_from_pos(pos_T *start, pos_T *end, char **match, pos_T *match_end)
{
bool exacttext = wop_flags & kOptWopFlagExacttext;
if (start->lnum > end->lnum
|| (start->lnum == end->lnum && start->col >= end->col)) {
return FAIL; // invalid range
@ -3909,19 +3911,27 @@ static int copy_substring_from_pos(pos_T *start, pos_T *end, char **match, pos_T
int segment_len = is_single_line ? (int)(end->col - start->col)
: (int)strlen(start_ptr);
ga_grow(&ga, segment_len + 1);
ga_grow(&ga, segment_len + 2);
ga_concat_len(&ga, start_ptr, (size_t)segment_len);
if (!is_single_line) {
ga_append(&ga, '\n');
if (exacttext) {
ga_concat_len(&ga, "\\n", 2);
} else {
ga_append(&ga, '\n');
}
}
// Append full lines between start and end
if (!is_single_line) {
for (linenr_T lnum = start->lnum + 1; lnum < end->lnum; lnum++) {
char *line = ml_get(lnum);
ga_grow(&ga, ml_get_len(lnum) + 1);
ga_grow(&ga, ml_get_len(lnum) + 2);
ga_concat(&ga, line);
ga_append(&ga, '\n');
if (exacttext) {
ga_concat_len(&ga, "\\n", 2);
} else {
ga_append(&ga, '\n');
}
}
}
@ -4004,6 +4014,7 @@ static char *concat_pattern_with_buffer_match(char *pat, int pat_len, pos_T *end
/// @param[out] numMatches number of matches
static int expand_pattern_in_buf(char *pat, Direction dir, char ***matches, int *numMatches)
{
bool exacttext = wop_flags & kOptWopFlagExacttext;
bool has_range = search_first_line != 0;
*matches = NULL;
@ -4090,22 +4101,26 @@ static int expand_pattern_in_buf(char *pat, Direction dir, char ***matches, int
break;
}
// Construct a new match from completed word appended to pattern itself
match = concat_pattern_with_buffer_match(pat, pat_len, &end_match_pos, false);
if (exacttext) {
match = full_match;
} else {
// Construct a new match from completed word appended to pattern itself
match = concat_pattern_with_buffer_match(pat, pat_len, &end_match_pos, false);
// The regex pattern may include '\C' or '\c'. First, try matching the
// buffer word as-is. If it doesn't match, try again with the lowercase
// version of the word to handle smartcase behavior.
if (!is_regex_match(match, full_match)) {
xfree(match);
match = concat_pattern_with_buffer_match(pat, pat_len, &end_match_pos, true);
// The regex pattern may include '\C' or '\c'. First, try matching the
// buffer word as-is. If it doesn't match, try again with the lowercase
// version of the word to handle smartcase behavior.
if (!is_regex_match(match, full_match)) {
xfree(match);
xfree(full_match);
continue;
match = concat_pattern_with_buffer_match(pat, pat_len, &end_match_pos, true);
if (!is_regex_match(match, full_match)) {
xfree(match);
xfree(full_match);
continue;
}
}
xfree(full_match);
}
xfree(full_match);
// Include this match if it is not a duplicate
for (int i = 0; i < ga.ga_len; i++) {

View File

@ -10117,6 +10117,7 @@ local options = {
< 'wildchar' also enables completion in search pattern contexts such as
|/|, |?|, |:s|, |:g|, |:v|, and |:vim|. To insert a literal <Tab>
instead of triggering completion, type <C-V><Tab> or "\t".
See also |'wildoptions'|.
]=],
full_name = 'wildchar',
scope = { 'global' },
@ -10311,12 +10312,26 @@ local options = {
{
abbreviation = 'wop',
defaults = 'pum,tagfile',
values = { 'fuzzy', 'tagfile', 'pum' },
values = { 'fuzzy', 'tagfile', 'pum', 'exacttext' },
flags = true,
deny_duplicates = true,
desc = [=[
A list of words that change how |cmdline-completion| is done.
The following values are supported:
exacttext When this flag is present, search pattern completion
(e.g., in |/|, |?|, |:s|, |:g|, |:v|, and |:vim|)
shows exact buffer text as menu items, without
preserving regex artifacts like position
anchors (e.g., |/\\<|). This provides more intuitive
menu items that match the actual buffer text.
However, searches may be less accurate since the
pattern is not preserved exactly.
By default, Vim preserves the typed pattern (with
anchors) and appends the matched word. This preserves
search correctness, especially when using regular
expressions or with 'smartcase' enabled. However, the
case of the appended matched word may not exactly
match the case of the word in the buffer.
fuzzy Use |fuzzy-matching| to find completion matches. When
this value is specified, wildcard expansion will not
be used for completion. The matches will be sorted by