patch 9.1.1311: completion: not possible to limit number of matches
Problem: completion: not possible to limit number of matches
Solution: allow to limit the matches for 'complete' sources by using the
"{flag}^{limit}" notation (Girish Palya)
This change extends the 'complete' option to support limiting the
number of matches returned from individual completion sources.
**Rationale:** In large files, certain sources (such as the current
buffer) can generate an overwhelming number of matches, which may cause
more relevant results from other sources (e.g., LSP or tags) to be
pushed out of view. By specifying per-source match limits, the
completion menu remains balanced and diverse, improving visibility and
relevance of suggestions.
A caret (`^`) followed by a number can be appended to a source flag to
specify the maximum number of matches for that source. For example:
```
:set complete=.^9,w,u,t^5
```
In this configuration:
- The current buffer (`.`) will return up to 9 matches.
- The tag completion (`t`) will return up to 5 matches.
- Other sources (`w`, `u`) are not limited.
This feature is fully backward-compatible and does not affect behavior
when the `^count` suffix is not used.
The caret (`^`) was chosen as the delimiter because it is least likely
to appear in file names.
closes: #17087
Signed-off-by: Girish Palya <girishji@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/optionstr.c b/src/optionstr.c
index 62a7086..90b8e52 100644
--- a/src/optionstr.c
+++ b/src/optionstr.c
@@ -1558,9 +1558,10 @@
did_set_complete(optset_T *args)
{
char_u **varp = (char_u **)args->os_varp;
- char_u *p = NULL;
+ char_u *p, *t;
char_u buffer[LSIZE];
char_u *buf_ptr;
+ char_u char_before = NUL;
int escape;
for (p = *varp; *p; )
@@ -1589,16 +1590,40 @@
if (vim_strchr((char_u *)".wbuksid]tUfo", *buffer) == NULL)
return illegal_char(args->os_errbuf, args->os_errbuflen, *buffer);
- if (!vim_strchr((char_u *)"ksf", *buffer) && *(buffer + 1) != NUL)
+ if (vim_strchr((char_u *)"ksf", *buffer) == NULL && *(buffer + 1) != NUL
+ && *(buffer + 1) != '^')
+ char_before = *buffer;
+ else
+ {
+ // Test for a number after '^'
+ if ((t = vim_strchr(buffer, '^')) != NULL)
+ {
+ *t++ = NUL;
+ if (!*t)
+ char_before = '^';
+ else
+ {
+ for (; *t; t++)
+ {
+ if (!vim_isdigit(*t))
+ {
+ char_before = '^';
+ break;
+ }
+ }
+ }
+ }
+ }
+ if (char_before != NUL)
{
if (args->os_errbuf)
{
vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
- _(e_illegal_character_after_chr), *buffer);
+ _(e_illegal_character_after_chr), char_before);
return args->os_errbuf;
}
+ return NULL;
}
-
// Skip comma and spaces
while (*p == ',' || *p == ' ')
p++;