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

cmp:visible() is slow when typing due to onetime config #1797

Open
2 tasks done
dhleong opened this issue Jan 12, 2024 · 0 comments
Open
2 tasks done

cmp:visible() is slow when typing due to onetime config #1797

dhleong opened this issue Jan 12, 2024 · 0 comments
Labels
bug Something isn't working valid

Comments

@dhleong
Copy link

dhleong commented Jan 12, 2024

FAQ

  • I have checked the FAQ and it didn't resolve my problem.

Announcement

Minimal reproducible full config

local function try_accept_completion(key)
  return cmp.mapping(function(fallback)
    if cmp:visible() and cmp.get_active_entry() then
      local entry = cmp.get_active_entry()

      cmp.confirm()

      if key and not entry_has_key(entry, key) then
        vim.api.nvim_feedkeys(key, 'nt', false)
      end
    else
      fallback()
    end
  end, { 'i', 'c' })
end

cmp.setup({
  -- ...
  mapping = {
    ['<Space>'] = try_accept_completion(' '),
  },
})

Description

I have this configured so, for example, I can naturally select and auto-import constants by continuing to type after selecting the suggestion. I use it with other characters as well, but space is the one that causes me the most trouble. The idea here is, if I have actually selected something for completion to "confirm" that selection (to trigger auto imports) and insert the key provided, if it wasn't already part of the completion (for example, some servers will include parens, in which case I don't want to insert a second one)

In most cases this hasn't been a problem, but I've noticed that especially with the Godot language server, and especially typing in a comment, this causes nvim to hang. Upon investigation, I finally narrowed it down to this try_accept_completion function and, in particular, cmp:visible(). I can fix the hangs by swapping cmp:visible() for this hack:

local function fast_cmp_visible()
  if not (cmp.core.view and cmp.core.view.custom_entries_view) then
    return false
  end
  return cmp.core.view.custom_entries_view:visible()
end

I believe what's happening is the config.get() call is essentially never hitting the cache, due to the oneshot revision constantly increasing. Just typing iasdf <esc> into a blank buffer in a fresh instance can result in require'cmp.config'.onetime == { revision = 8 } with the minimal vimrc.

As far as what's causing the onetime revisions, as far as I can tell it's the async_filter function in core.lua. I added some logging to the other usages of config.set_onetime in the repo and none of them triggered with the above repro except async_filter.

Steps to reproduce

For the many revisions issue, see above. For the slowness in Godot:

  1. Open the Godot editor so the language server is running
  2. Open a Godot file in vim
  3. Start a comment on a newline with eg o# and just type some words separated by spaces
  4. Notice how it hangs

Expected behavior

It should not hang

Actual behavior

It does

Additional context

Thank you!

@dhleong dhleong added the bug Something isn't working label Jan 12, 2024
dhleong added a commit to dhleong/dots that referenced this issue Jan 12, 2024
Opened hrsh7th/nvim-cmp#1797 to see about
getting an upstream fix so I can eventually remove this hack.
@hrsh7th hrsh7th added the valid label Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working valid
Projects
None yet
Development

No branches or pull requests

2 participants