hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
7.79k stars 389 forks source link

compare.scope freezes neovim for cmdline functions in large files #1681

Open jrwrigh opened 1 year ago

jrwrigh commented 1 year ago

FAQ

Announcement

Minimal reproducible full config

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvim/site]]
local package_root = '/tmp/nvim/site/pack'
local install_path = package_root .. '/packer/start/packer.nvim'
local function load_plugins()
  require('packer').startup {
    {
      'wbthomason/packer.nvim',
    { 'nvim-treesitter/nvim-treesitter', cond = true},
    { 'hrsh7th/nvim-cmp',
      requires = {
        { 'hrsh7th/cmp-buffer' },
        { 'hrsh7th/cmp-path' },
      },
      cond = true,
    }
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. '/plugin/packer_compiled.lua',
      display = { non_interactive = true },
    },
  }
end
_G.load_config = function()
  require('nvim-treesitter.configs').setup({})

  local cmp = require('cmp')
  cmp.setup({
    window = {
      completion = cmp.config.window.bordered({
        col_offset = -3,
        side_padding = 0,
      }),
      documentation = cmp.config.window.bordered(),
    },
    sources = cmp.config.sources({
      { name = 'path' },
    }),
    sorting = {
      comparators = {
        cmp.config.compare.offset,
        cmp.config.compare.exact,
        cmp.config.compare.scopes,
        cmp.config.compare.score,
        cmp.config.compare.recently_used,
        cmp.config.compare.locality,
        cmp.config.compare.kind,
        cmp.config.compare.sort_text,
        cmp.config.compare.length,
        cmp.config.compare.order,
      },
    },
    view = {
      entries = { name = 'custom', selection_order = 'near_cursor' }
    },
  })

  cmp.setup.cmdline('/', {
    sources = {
      -- { name = 'buffer' }
    },
    completion = {
      autocomplete = false
    },
    sorting = {
      comparators = {
        cmp.config.compare.offset,
        cmp.config.compare.exact,
        cmp.config.compare.scopes,
        cmp.config.compare.score,
        cmp.config.compare.recently_used,
        cmp.config.compare.locality,
        cmp.config.compare.kind,
        cmp.config.compare.sort_text,
        cmp.config.compare.length,
        cmp.config.compare.order,
      },
    },
  })

  cmp.setup.cmdline(':', {
    sources = cmp.config.sources({
      -- { name = 'cmdline' },
      -- { name = 'path' },
    }),
    completion = {
      autocomplete = false
    },
    sorting = {
      comparators = {
        cmp.config.compare.offset,
        cmp.config.compare.exact,
        cmp.config.compare.scopes,
        cmp.config.compare.score,
        cmp.config.compare.recently_used,
        cmp.config.compare.locality,
        cmp.config.compare.kind,
        cmp.config.compare.sort_text,
        cmp.config.compare.length,
        cmp.config.compare.order,
      },
    },
  })
  -- ADD INIT.LUA SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
end
if vim.fn.isdirectory(install_path) == 0 then
  print("Installing Telescope and dependencies.")
  vim.fn.system { 'git', 'clone', '--depth=1', 'https://github.com/wbthomason/packer.nvim', install_path }
end
load_plugins()
require('packer').sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]]

Description

When using cmp.config.compare.scopes on large files with : and /, this will freeze neovim for several seconds. I believe this is probably because the "scope" in treesitter somehow gets broadened to the entire file.

If I disable compare.scopes for : and /, then then there is no freezing.

Below is an demonstrative gif, using the above minimal config. With compare.scopes enabled at all times, going into command mode causes a very long delay. Commenting out compare.scopes from the config of just : and / fixes the issue.

cmp_scopes_example Note the gif only demonstrates the issue with : for time-sake, but the exact same effect is observed with /

Steps to reproduce

  1. Open a large file while having compare.scopes enabled for : and/or /
  2. Enter search or cmdline mode via : and /, respectively
  3. See neovim freeze for awhile.

Expected behavior

don't freeze

Actual behavior

freezes

Additional context

I'm not sure if it's possible async calls to treesitter, but that'd be an obvious solution here. Otherwise possibly disable scopes when in : and / mode, as if it's treating the entire file as "in scope", then it's not that useful of a comparator.

jrwrigh commented 1 year ago

Alternatively, removing this line would also fix the issue (I'm pretty sure anyways): https://github.com/hrsh7th/nvim-cmp/blob/51f1e11a89ec701221877532ee1a23557d291dd5/lua/cmp/init.lua#L332