L3MON4D3 / LuaSnip

Snippet Engine for Neovim written in Lua.
Apache License 2.0
3.5k stars 245 forks source link

Nvim-cmp is disrupted when with the TextChanged, TextChangedI config and function/dynamic nodes on the same line as the insert node them depend on #503

Open haoming-li-ling opened 2 years ago

haoming-li-ling commented 2 years ago

If I have a snippet that contains a function/dynamic node that depends on the input to an insert node on the same line, e.g., i(1), d(2, function(args) ... end, {1}), if I set update_events = "TextChanged,TextChangedI", then nvim-cmp will fail to properly provide completion items when I am in the i(1) node. Nvim-cmp will refresh its completion items each time I press <c-n> or <c-p> to select (and insert) the next or previous items, which is unusable.

Even if I use nvim-cmp's SelectBebavhior.Select, that is, the current autocomplete selection is not automatically inserted at the cursor but merely selected, nvim-cmp stills provides problematic completion items (only items from the buffer source are provided; nothing else is).

Note that if the dynamic/function node is on a different line than the one where the insert node it depends on is, nvim-cmp works properly with update_events set to "TextChanged,TextChangedI", regardless of the SelectBebavior setting.

I don't know if this is a luasnip or a nvim-cmp problem though...

L3MON4D3 commented 2 years ago

Hmm, probably because we nvim_buf_set_text on the line where nvim-cmp is currently operating.. I think your best bet to get this fixed is creating a minimal example that does not depend on luasnip, and opening an issue in nvim-cmp with that, there's nothing (I think) we can do about this, here :/

For that minimal example, you could try registering an autocommand on TextChangedI, which just inserts some characters somewhere in the line (the beginning could be easiest).

schardev commented 1 year ago

I'm also facing somewhat related issue, where I have a snippet like this:

  s(
    { trig = "import", name = "Import Name" },
    fmt('import {} from "{}";', {
      c(2, {
        d(nil, function(args)
          local specifier = args[1][1]
          local import = utils.to_pascal_case(specifier)
          return sn(nil, { i(1, import) })
        end, { 1 }),
        sn(nil, { t("{ "), i(1, "named import"), t(" }") }),
        sn(nil, { t("* as "), i(1, "namespace import") }),
      }),
      i(1, "specifier"),
    })
  ),

And while the cmp menu is visible I try to select a completion item by <Tab>-ing, it then jumps the cursor to jump index 2 (the choice nodes here) instead of selecting the next completion item.

I was able to reproduce it using kickstart.nvim with the above snippet.