Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve auto_show flexibility #697

Merged
merged 2 commits into from
Dec 21, 2024
Merged

Conversation

guill
Copy link
Contributor

@guill guill commented Dec 21, 2024

This PR gives more control over when and where we show automatic completion. It does this with two public-facing changes:

  1. completion.menu.auto_show can now be either a function or a constant boolean. This gives users the ability to make more complex decisions about whether to automatically show completions.
  2. blink.cmp.Context contains a new field -- update_type. This field provides additional information on what triggered the update. This allows users to vary behavior when completion is manually triggered (as opposed to being automatically triggered by keywords or trigger characters).

This PR gives more control over when and where we show automatic
completion. It does this with two public-facing changes:

1. `completion.menu.auto_show` can now be either a function in addition
   to a constant boolean. This gives users the ability to make more
   complex decisions about whether to automatically show completions.
2. `blink.cmp.Context` contains a new field -- `update_type`. This field
   provides additional information on what triggered the update. This
   allows users to vary behavior when completion is manually triggered
   (as opposed to being automatically triggered by keywords or trigger
   characters).
@guill guill force-pushed the context_update_type branch from 5fd7ac5 to 496e428 Compare December 21, 2024 08:51

-- check if we've entered insert mode on a trigger character
elseif insert_enter_on_trigger_character then
trigger.context = nil
trigger.show({ trigger_character = char_under_cursor })
trigger.show({ trigger_character = char_under_cursor, update_type = 'auto_show_trigger_char' })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have a separate update type for this -- interested in thoughts on whether that would actually be useful.

@Saghen
Copy link
Owner

Saghen commented Dec 21, 2024

I believe the global min_keyword_length (notably not the source-level min_keyword_length) should be ignored on manual trigger by default. I like the idea of indicating whether the context was created due to user input or not. Perhaps we could repurpose the trigger.kind field for this, and reduce the number of types a bit: 'manual' | 'keyword' | 'trigger_character'. We'll need this for prefetching regardless so it seems useful!

@Saghen Saghen force-pushed the context_update_type branch from 2631f4b to 9b4b7af Compare December 21, 2024 22:31
@Saghen
Copy link
Owner

Saghen commented Dec 21, 2024

I've also added an initial_kind which could be useful, for example, disabling snippets for trigger characters via

should_show_items = function(ctx) ctx.trigger.initial_kind ~= 'trigger_character' end

@Saghen Saghen merged commit a937edd into Saghen:main Dec 21, 2024
@Saghen
Copy link
Owner

Saghen commented Dec 21, 2024

Thank you!

@Saghen Saghen mentioned this pull request Dec 21, 2024
5 tasks
guill added a commit to guill/blink.cmp that referenced this pull request Dec 21, 2024
This code was reduced by a line before merging PR Saghen#697, but it's now
hitting the case where the Lua 'ternary' doesn't behave like a proper
ternary operator. Specifically, when the `auto_show` function returns
false, the result of
```lua
auto_show = type(auto_show) == "function" and auto_show(...) or auto_show
```
becomes
```lua
auto_show = (true and false) or <function>
```
which is the truthy function reference itself.
Saghen pushed a commit that referenced this pull request Dec 21, 2024
This code was reduced by a line before merging PR #697, but it's now
hitting the case where the Lua 'ternary' doesn't behave like a proper
ternary operator. Specifically, when the `auto_show` function returns
false, the result of
```lua
auto_show = type(auto_show) == "function" and auto_show(...) or auto_show
```
becomes
```lua
auto_show = (true and false) or <function>
```
which is the truthy function reference itself.
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.

2 participants