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

@ -7173,6 +7173,7 @@ A jump table for the options with a short description can be found at |Q_op|.
< 'wildchar' also enables completion in search pattern contexts such as < 'wildchar' also enables completion in search pattern contexts such as
|/|, |?|, |:s|, |:g|, |:v|, and |:vim|. To insert a literal <Tab> |/|, |?|, |:s|, |:g|, |:v|, and |:vim|. To insert a literal <Tab>
instead of triggering completion, type <C-V><Tab> or "\t". instead of triggering completion, type <C-V><Tab> or "\t".
See also |'wildoptions'|.
*'wildcharm'* *'wcm'* *'wildcharm'* *'wcm'*
'wildcharm' 'wcm' number (default 0) 'wildcharm' 'wcm' number (default 0)
@ -7319,6 +7320,20 @@ A jump table for the options with a short description can be found at |Q_op|.
global global
A list of words that change how |cmdline-completion| is done. A list of words that change how |cmdline-completion| is done.
The following values are supported: 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 fuzzy Use |fuzzy-matching| to find completion matches. When
this value is specified, wildcard expansion will not this value is specified, wildcard expansion will not
be used for completion. The matches will be sorted by be used for completion. The matches will be sorted by

View File

@ -7818,6 +7818,7 @@ vim.go.ww = vim.go.whichwrap
--- 'wildchar' also enables completion in search pattern contexts such as --- 'wildchar' also enables completion in search pattern contexts such as
--- `/`, `?`, `:s`, `:g`, `:v`, and `:vim`. To insert a literal <Tab> --- `/`, `?`, `:s`, `:g`, `:v`, and `:vim`. To insert a literal <Tab>
--- instead of triggering completion, type <C-V><Tab> or "\t". --- instead of triggering completion, type <C-V><Tab> or "\t".
--- See also `'wildoptions'`.
--- ---
--- @type integer --- @type integer
vim.o.wildchar = 9 vim.o.wildchar = 9
@ -8012,6 +8013,20 @@ vim.go.wim = vim.go.wildmode
--- A list of words that change how `cmdline-completion` is done. --- A list of words that change how `cmdline-completion` is done.
--- The following values are supported: --- 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 --- fuzzy Use `fuzzy-matching` to find completion matches. When
--- this value is specified, wildcard expansion will not --- this value is specified, wildcard expansion will not
--- be used for completion. The matches will be sorted by --- be used for completion. The matches will be sorted by

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'. /// 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) 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 if (start->lnum > end->lnum
|| (start->lnum == end->lnum && start->col >= end->col)) { || (start->lnum == end->lnum && start->col >= end->col)) {
return FAIL; // invalid range 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 segment_len = is_single_line ? (int)(end->col - start->col)
: (int)strlen(start_ptr); : (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); ga_concat_len(&ga, start_ptr, (size_t)segment_len);
if (!is_single_line) { 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 // Append full lines between start and end
if (!is_single_line) { if (!is_single_line) {
for (linenr_T lnum = start->lnum + 1; lnum < end->lnum; lnum++) { for (linenr_T lnum = start->lnum + 1; lnum < end->lnum; lnum++) {
char *line = ml_get(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_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 /// @param[out] numMatches number of matches
static int expand_pattern_in_buf(char *pat, Direction dir, char ***matches, int *numMatches) 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; bool has_range = search_first_line != 0;
*matches = NULL; *matches = NULL;
@ -4090,22 +4101,26 @@ static int expand_pattern_in_buf(char *pat, Direction dir, char ***matches, int
break; break;
} }
// Construct a new match from completed word appended to pattern itself if (exacttext) {
match = concat_pattern_with_buffer_match(pat, pat_len, &end_match_pos, false); 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 // 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 // buffer word as-is. If it doesn't match, try again with the lowercase
// version of the word to handle smartcase behavior. // 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);
if (!is_regex_match(match, full_match)) { if (!is_regex_match(match, full_match)) {
xfree(match); xfree(match);
xfree(full_match); match = concat_pattern_with_buffer_match(pat, pat_len, &end_match_pos, true);
continue; 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 // Include this match if it is not a duplicate
for (int i = 0; i < ga.ga_len; i++) { 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 < 'wildchar' also enables completion in search pattern contexts such as
|/|, |?|, |:s|, |:g|, |:v|, and |:vim|. To insert a literal <Tab> |/|, |?|, |:s|, |:g|, |:v|, and |:vim|. To insert a literal <Tab>
instead of triggering completion, type <C-V><Tab> or "\t". instead of triggering completion, type <C-V><Tab> or "\t".
See also |'wildoptions'|.
]=], ]=],
full_name = 'wildchar', full_name = 'wildchar',
scope = { 'global' }, scope = { 'global' },
@ -10311,12 +10312,26 @@ local options = {
{ {
abbreviation = 'wop', abbreviation = 'wop',
defaults = 'pum,tagfile', defaults = 'pum,tagfile',
values = { 'fuzzy', 'tagfile', 'pum' }, values = { 'fuzzy', 'tagfile', 'pum', 'exacttext' },
flags = true, flags = true,
deny_duplicates = true, deny_duplicates = true,
desc = [=[ desc = [=[
A list of words that change how |cmdline-completion| is done. A list of words that change how |cmdline-completion| is done.
The following values are supported: 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 fuzzy Use |fuzzy-matching| to find completion matches. When
this value is specified, wildcard expansion will not this value is specified, wildcard expansion will not
be used for completion. The matches will be sorted by be used for completion. The matches will be sorted by

View File

@ -4499,6 +4499,7 @@ func Test_search_complete()
" Match case correctly " Match case correctly
%d %d
call setline(1, ["foobar", "Foobar", "fooBAr", "FooBARR"]) call setline(1, ["foobar", "Foobar", "fooBAr", "FooBARR"])
call feedkeys("gg/f\<tab>\<f9>", 'tx') call feedkeys("gg/f\<tab>\<f9>", 'tx')
call assert_equal(['fooBAr', 'foobar'], g:compl_info.matches) call assert_equal(['fooBAr', 'foobar'], g:compl_info.matches)
call feedkeys("gg/Fo\<tab>\<f9>", 'tx') call feedkeys("gg/Fo\<tab>\<f9>", 'tx')
@ -4507,6 +4508,7 @@ func Test_search_complete()
call assert_equal({}, g:compl_info) call assert_equal({}, g:compl_info)
call feedkeys("gg/\\cFo\<tab>\<f9>", 'tx') call feedkeys("gg/\\cFo\<tab>\<f9>", 'tx')
call assert_equal(['\cFoobar', '\cFooBAr', '\cFooBARR'], g:compl_info.matches) call assert_equal(['\cFoobar', '\cFooBAr', '\cFooBARR'], g:compl_info.matches)
set ignorecase set ignorecase
call feedkeys("gg/f\<tab>\<f9>", 'tx') call feedkeys("gg/f\<tab>\<f9>", 'tx')
call assert_equal(['foobar', 'fooBAr', 'fooBARR'], g:compl_info.matches) call assert_equal(['foobar', 'fooBAr', 'fooBARR'], g:compl_info.matches)
@ -4516,6 +4518,7 @@ func Test_search_complete()
call assert_equal(['FOobar', 'FOoBAr', 'FOoBARR'], g:compl_info.matches) call assert_equal(['FOobar', 'FOoBAr', 'FOoBARR'], g:compl_info.matches)
call feedkeys("gg/\\Cfo\<tab>\<f9>", 'tx') call feedkeys("gg/\\Cfo\<tab>\<f9>", 'tx')
call assert_equal(['\CfooBAr', '\Cfoobar'], g:compl_info.matches) call assert_equal(['\CfooBAr', '\Cfoobar'], g:compl_info.matches)
set smartcase set smartcase
call feedkeys("gg/f\<tab>\<f9>", 'tx') call feedkeys("gg/f\<tab>\<f9>", 'tx')
call assert_equal(['foobar', 'fooBAr', 'foobarr'], g:compl_info.matches) call assert_equal(['foobar', 'fooBAr', 'foobarr'], g:compl_info.matches)
@ -4523,16 +4526,42 @@ func Test_search_complete()
call assert_equal(['Foobar', 'FooBARR'], g:compl_info.matches) call assert_equal(['Foobar', 'FooBARR'], g:compl_info.matches)
call feedkeys("gg/FO\<tab>\<f9>", 'tx') call feedkeys("gg/FO\<tab>\<f9>", 'tx')
call assert_equal({}, g:compl_info) call assert_equal({}, g:compl_info)
call feedkeys("gg/foob\<tab>\<f9>", 'tx')
call assert_equal(['foobar', 'foobarr'], g:compl_info.matches)
call feedkeys("gg/\\Cfo\<tab>\<f9>", 'tx') call feedkeys("gg/\\Cfo\<tab>\<f9>", 'tx')
call assert_equal(['\CfooBAr', '\Cfoobar'], g:compl_info.matches) call assert_equal(['\CfooBAr', '\Cfoobar'], g:compl_info.matches)
call feedkeys("gg/\\cFo\<tab>\<f9>", 'tx') call feedkeys("gg/\\cFo\<tab>\<f9>", 'tx')
call assert_equal(['\cFoobar', '\cFooBAr', '\cFooBARR'], g:compl_info.matches) call assert_equal(['\cFoobar', '\cFooBAr', '\cFooBARR'], g:compl_info.matches)
set wildoptions+=exacttext ignorecase& smartcase&
call feedkeys("gg/F\<tab>\<f9>", 'tx')
call assert_equal(['Foobar', 'FooBARR'], g:compl_info.matches)
call feedkeys("gg/foob\<tab>\<f9>", 'tx')
call assert_equal([], g:compl_info.matches)
call feedkeys("gg/r\\n.\<tab>\<f9>", 'tx')
call assert_equal(['r\nFoobar', 'r\nfooBAr', 'r\nFooBARR'], g:compl_info.matches)
set ignorecase
call feedkeys("gg/F\<tab>\<f9>", 'tx')
call assert_equal(['Foobar', 'fooBAr', 'FooBARR', 'foobar'], g:compl_info.matches)
call feedkeys("gg/R\\n.\<tab>\<f9>", 'tx')
call assert_equal(['r\nFoobar', 'r\nfooBAr', 'r\nFooBARR'], g:compl_info.matches)
set smartcase
call feedkeys("gg/f\<tab>\<f9>", 'tx')
call assert_equal(['Foobar', 'fooBAr', 'FooBARR', 'foobar'], g:compl_info.matches)
call feedkeys("gg/foob\<tab>\<f9>", 'tx')
call assert_equal(['Foobar', 'fooBAr', 'FooBARR', 'foobar'], g:compl_info.matches)
call feedkeys("gg/R\\n.\<tab>\<f9>", 'tx')
call assert_equal({}, g:compl_info)
call feedkeys("gg/r\\n.*\\n\<tab>\<f9>", 'tx')
call assert_equal(['r\nFoobar\nfooBAr', 'r\nfooBAr\nFooBARR'], g:compl_info.matches)
bw! bw!
call Ntest_override("char_avail", 0) call Ntest_override("char_avail", 0)
delfunc GetComplInfo delfunc GetComplInfo
unlet! g:compl_info unlet! g:compl_info
set wildcharm=0 incsearch& ignorecase& smartcase& set wildcharm=0 incsearch& ignorecase& smartcase& wildoptions&
endfunc endfunc
func Test_search_wildmenu_screendump() func Test_search_wildmenu_screendump()