Skip to content

Conversation

@abeldekat
Copy link
Contributor

Hello @saghen,

I recently fixed this issue in nvim-cmp.

The problem also occurs in blink, and the fix is very similar.

Are there any drawbacks to the following approach?

.../completion/init.lua:

  -- trigger -> sources: request completion items from the sources on show
  trigger.show_emitter:on(function(event)
    vim.schedule(function()
      -- schedule to avoid outdated completion items
      sources.request_completions(event.context)
    end)
  end)

@saghen
Copy link
Owner

saghen commented Jan 20, 2025

This would add latency to all of the sources. We want to get the request out to the LSP ASAP. I'll read up on that issue you linked soon

@echasnovski
Copy link

The ASAP part might indeed be the culprit here, because after that there might be other TextChanged{I,P} callbacks that modify buffer text immediately which causes sent request to be for an outdated buffer state. Here are more details.

@abeldekat
Copy link
Contributor Author

abeldekat commented Jan 22, 2025

For nvim-cmp, we might have a solution fixing this concrete problem, without falling back to vim.schedule. I think that a similar approach for blink is more complex unfortunately.

So, for now, in my config I apply this hack before the setup:

-- HACK: See blink cmp completion init, line 20
-- Fix outdated completion items:
local sources_lib = require("blink.cmp.sources.lib")
local orig_request_completions = sources_lib.request_completions
sources_lib.request_completions = vim.schedule_wrap(orig_request_completions)

local blink = require("blink.cmp")
blink.setup(opts)

@abeldekat abeldekat closed this Jan 27, 2025
@abeldekat abeldekat reopened this Jan 27, 2025
@abeldekat
Copy link
Contributor Author

Hello @saghen,

I implemented the same approach for blink.cmp as I did for nvim-cmp.

There is no vim.schedule. This is the relevant fragment in blink.cmp.config.snippets.lua:

-- ... expand
      mini_snippets = function(snippet)
        if not _G.MiniSnippets then error('mini.snippets has not been setup') end
        local insert = MiniSnippets.config.expand.insert or MiniSnippets.default_insert
        insert({ body = snippet })
        require('blink.cmp').resubscribe()
      end,

-- ...

I profiled the solution. For each expand using mini.snippets, an extra 0.057723 ms is added. Measured on an old laptop... For nvim-cmp I measured 0.0475465 ms.

@saghen saghen merged commit 7f3d982 into saghen:main Jan 28, 2025
6 checks passed
@saghen
Copy link
Owner

saghen commented Jan 28, 2025

Brilliant, thank you! This does seem a bit hacky, albeit less so than vim.schedule so I'll add some notes around the code to try to solve the core issue eventually. It would be great if neovim had a way to control autocmd ordering

@abeldekat
Copy link
Contributor Author

It would be great if neovim had a way to control autocmd ordering

Indeed!

folke pushed a commit to LazyVim/LazyVim that referenced this pull request Feb 16, 2025
## Description


When using blink.cmp and mini.snippets, the same problem regarding
outdated completion items as solved in
[nvim-cmp](hrsh7th/nvim-cmp#2126) can arise.

[This](saghen/blink.cmp#1035) PR has been
included into main.
When the PR is included into the next stable release, this PR can be
merged.
Current blink release: 0.11.0


## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.

Co-authored-by: abeldekat <abel@nomail.com>
doodleEsc pushed a commit to doodleEsc/LazyVim that referenced this pull request Feb 17, 2025
## Description


When using blink.cmp and mini.snippets, the same problem regarding
outdated completion items as solved in
[nvim-cmp](hrsh7th/nvim-cmp#2126) can arise.

[This](saghen/blink.cmp#1035) PR has been
included into main.
When the PR is included into the next stable release, this PR can be
merged.
Current blink release: 0.11.0


## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.

Co-authored-by: abeldekat <abel@nomail.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