Skip to content

Conversation

@girishji
Copy link
Contributor

@girishji girishji commented Apr 9, 2025

⚠️ This PR builds on top of #17065 and should be reviewed/merged after that.

This change extends the 'complete' ('cpt') 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.


After limiting the matches to 5, the LSP-sourced matches (indented ones) become visible:

Screenshot 2025-04-09 at 10 41 17 PM

Without this change, only matches from the current buffer are visible, and LSP-sourced matches remain hidden:

Screenshot 2025-04-09 at 10 43 37 PM

M  src/insexpand.c
M  src/optionstr.c
M  src/insexpand.c
M  src/insexpand.c
M  src/insexpand.c
M  runtime/doc/options.txt
M  src/insexpand.c
M  src/insexpand.c
M  src/insexpand.c
M  runtime/doc/options.txt
M  runtime/doc/options.txt
M  src/insexpand.c
M  src/optionstr.c
M  src/testdir/test_ins_complete.vim
M  src/testdir/test_options.vim
M  src/insexpand.c
M  src/optionstr.c
M  runtime/doc/tags
@chrisbra chrisbra closed this in 0ac1eb3 Apr 16, 2025
@girishji
Copy link
Contributor Author

Thanks!

@girishji girishji deleted the pr_matchcount branch April 16, 2025 21:13
@habamax
Copy link
Contributor

habamax commented May 26, 2025

Hi @girishji,

Is it supposed to work with o, like in set complete^=o^5?

image

If I prepend o^5 expecting to see max 5 omnicomplete items first, I wouldn't be able to:

image

Maybe the doc should be updated?

@habamax
Copy link
Contributor

habamax commented May 26, 2025

Hm, I can see you use quite a few f^2 in test cases but fail to see why it has no effect in my case:

set cpt=.^1,f^2

image

@habamax
Copy link
Contributor

habamax commented May 26, 2025

Tried test case with vim --clean:

image

func ComplFunc(findstart, base)
    if a:findstart
        return col(".")
    endif
    return ["foo1", "foo2", "foo3", "foo4"]
endfunc

set completefunc=ComplFunc
set cpt=f^2

...

oh, it works only for C-n!

However it still fails to limit for the o^5 when I press C-n.

@habamax
Copy link
Contributor

habamax commented May 26, 2025

ok, when completeopt has fuzzy limits just don't work, take prev example:

func ComplFunc(findstart, base)
    if a:findstart
        return col(".")
    endif
    return ["foo1", "foo2", "foo3", "foo4"]
endfunc

set completeopt+=fuzzy
set completefunc=ComplFunc
set cpt=f^2

image

let me create a separate issue

@girishji
Copy link
Contributor Author

Looks like a bug. Will look into this.

zeertzjq added a commit to zeertzjq/neovim that referenced this pull request May 31, 2025
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: vim/vim#17087

vim/vim@0ac1eb3

Cherry-pick test_options.vim change from patch 9.1.1325.

Co-authored-by: Girish Palya <girishji@gmail.com>
zeertzjq added a commit to zeertzjq/neovim that referenced this pull request Jun 2, 2025
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: vim/vim#17087

vim/vim@0ac1eb3

Cherry-pick test_options.vim change from patch 9.1.1325.

Co-authored-by: Girish Palya <girishji@gmail.com>
zeertzjq added a commit to zeertzjq/neovim that referenced this pull request Jun 2, 2025
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: vim/vim#17087

vim/vim@0ac1eb3

Cherry-pick test_options.vim change from patch 9.1.1325.

Co-authored-by: Girish Palya <girishji@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants