vim-patch:9.1.0131: buffer-completion may not always find all matches (#27610)

Problem:  buffer-completion code too complicated and does not always
          find all matches (irisjae)
Solution: do not try to anchor pattern to beginning of line or
          directory-separator, always return all matches

Note: we are considering the non-fuzzy buffer-matching here.

Currently, the buffer-completion code makes 2 attempts to match a
pattern against the list of available patterns. First try is to match
the pattern and anchor it to either the beginning of the file name or
at a directory-separator (// or \\).

When a match is found, Vim returns the matching buffers and does not try
to find a match anywhere within a buffer name. So if you have opened two
buffers like /tmp/Foobar.c and /tmp/MyFoobar.c using `:b Foo` will only
complete to the first filename, but not the second (the same happens
with `getcompletion('Foo', 'buffer')`).

It may make sense, that completion priorities buffer names at directory
boundaries, but it inconsistent, may cause confusion why a certain
buffer name is not completed when typing `:b Foo<C-D>` which returns
only a single file name and then pressing Enter (to switch to that
buffer), Vim will error with 'E93: More than one match for Foo').
Similar things may happen when wiping the /tmp/Foobar.c pattern and
afterwards the completion starts completing other buffers.

So let's simplify the code and always match the pattern anywhere in the
buffer name, do not try to favor matches at directory boundaries. This
is also simplifies the code a bit, we do not need to run over the list
of buffers several times, but only twice.

fixes vim/vim#13894
closes: vim/vim#14082

0dc0bff000

Cherry-pick test_cmdline.vim from patch 9.1.0019 as it already passes.

Co-authored-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
zeertzjq 2024-02-24 22:59:28 +08:00 committed by GitHub
parent a0394b648c
commit 04f723f1a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 113 additions and 95 deletions

View File

@ -2377,10 +2377,8 @@ static int buf_time_compare(const void *s1, const void *s2)
/// @return OK if matches found, FAIL otherwise.
int ExpandBufnames(char *pat, int *num_file, char ***file, int options)
{
int count = 0;
int round;
char *p;
bufmatch_T *matches = NULL;
bool to_free = false;
*num_file = 0; // return values in case of FAIL
*file = NULL;
@ -2392,34 +2390,28 @@ int ExpandBufnames(char *pat, int *num_file, char ***file, int options)
const bool fuzzy = cmdline_fuzzy_complete(pat);
char *patc = NULL;
fuzmatch_str_T *fuzmatch = NULL;
regmatch_T regmatch;
// Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
// expression matching)
if (!fuzzy) {
if (*pat == '^') {
patc = xmalloc(strlen(pat) + 11);
STRCPY(patc, "\\(^\\|[\\/]\\)");
STRCPY(patc + 11, pat + 1);
if (*pat == '^' && pat[1] != NUL) {
patc = xstrdup(pat + 1);
to_free = true;
} else if (*pat == '^') {
patc = "";
} else {
patc = pat;
}
regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
}
fuzmatch_str_T *fuzmatch = NULL;
// attempt == 0: try match with '\<', match at start of word
// attempt == 1: try match without '\<', match anywhere
for (int attempt = 0; attempt <= (fuzzy ? 0 : 1); attempt++) {
regmatch_T regmatch;
if (!fuzzy) {
if (attempt > 0 && patc == pat) {
break; // there was no anchor, no need to try again
}
regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
}
int count = 0;
int score = 0;
// round == 1: Count the matches.
// round == 2: Build the array to keep the matches.
for (round = 1; round <= 2; round++) {
for (int round = 1; round <= 2; round++) {
count = 0;
FOR_ALL_BUFFERS(buf) {
if (!buf->b_p_bl) { // skip unlisted buffers
@ -2433,10 +2425,11 @@ int ExpandBufnames(char *pat, int *num_file, char ***file, int options)
}
}
char *p = NULL;
if (!fuzzy) {
if (regmatch.regprog == NULL) {
// invalid pattern, possibly after recompiling
if (patc != pat) {
if (to_free) {
xfree(patc);
}
return FAIL;
@ -2503,15 +2496,10 @@ int ExpandBufnames(char *pat, int *num_file, char ***file, int options)
if (!fuzzy) {
vim_regfree(regmatch.regprog);
if (count) { // match(es) found, break here
break;
}
}
}
if (!fuzzy && patc != pat) {
if (to_free) {
xfree(patc);
}
}
if (!fuzzy) {
if (matches != NULL) {

View File

@ -679,7 +679,7 @@ func Test_getcompletion()
bw Xtest\
endif
call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E866:')
call assert_fails('call getcompletion("", "burp")', 'E475:')
call assert_fails('call getcompletion("abc", [])', 'E1174:')
endfunc
@ -3918,4 +3918,34 @@ func Test_custom_completion_with_glob()
delfunc TestGlobComplete
endfunc
func Test_window_size_stays_same_after_changing_cmdheight()
set laststatus=2
let expected = winheight(0)
function! Function_name() abort
call feedkeys(":"..repeat('x', &columns), 'x')
let &cmdheight=2
let &cmdheight=1
redraw
endfunction
call Function_name()
call assert_equal(expected, winheight(0))
endfunc
" verify that buffer-completion finds all buffer names matching a pattern
func Test_buffer_completion()
" should return empty list
call assert_equal([], getcompletion('', 'buffer'))
call mkdir('Xbuf_complete', 'R')
e Xbuf_complete/Foobar.c
e Xbuf_complete/MyFoobar.c
e AFoobar.h
let expected = ["Xbuf_complete/Foobar.c", "Xbuf_complete/MyFoobar.c", "AFoobar.h"]
call assert_equal(3, len(getcompletion('Foo', 'buffer')))
call assert_equal(expected, getcompletion('Foo', 'buffer'))
call feedkeys(":b Foo\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"b Xbuf_complete/Foobar.c Xbuf_complete/MyFoobar.c AFoobar.h", @:)
endfunc
" vim: shiftwidth=2 sts=2 expandtab