LuaSnip icon indicating copy to clipboard operation
LuaSnip copied to clipboard

Insert nodes not using autocomplete text from lsp

Open FFX3 opened this issue 2 years ago • 7 comments

This is possibly a feature request instead of a bug; if so I'm interested in adding the feature, but I'll need some hints for where to start looking.

I'm using lsp-zero with the defaults.

Let me know if I should ask the maintainers for nvim-cmp instead

luasnip config

ls.config.set_config {
    history = true,
    update_events = "TextChanged,TextChangedI",

    enable_autosnippets = true,

    ext_opts = {
        [types.choiceNode] = {
            active = {
                virt_text = { { "<--", "Error" } },
            },
        },
    }
}

image

image

Ideally, after confirming my selection the final result should be

package.loaded['snippets.react'] = nil
require('snippets.react')

instead of

package.loaded['sn'] = nil
require('snippets.react')

FFX3 avatar Oct 12 '23 04:10 FFX3

Ahh, the two lines are one snippet, and the part in package.loaded is a copy of the require'd string below? I assume nvim-cmp inserting text does not trigger either of TextChanged,TextChangedI.. you could manually call ls.active_update_dependents() in the mapping you use to accept the current selection by nvim-cmp, that should do it.

Not sure if/how to approach making something like this happen automatically though :/

L3MON4D3 avatar Oct 12 '23 08:10 L3MON4D3

If nvim-cmp doesn't trigger TextChangedI that's sounds weird and quite unexpected for exactly this kind of thing 👀

bew avatar Oct 12 '23 08:10 bew

True.. Maye it triggers/should trigger TextChangedP? Sounds a bit more appropriate, even though cmp's menu is not necessarily the popup menu

L3MON4D3 avatar Oct 12 '23 09:10 L3MON4D3

I've just tried using "active_update_dependents", no luck. (I even created a separate binding to invoke it manually)

I did notice that if I continue typing after confirming my selection the nodes don't update and I can't jump around in the snippet, implying that the snippet is no longer active after cmp inserts text.

And no, TextChangedP doesn't work either.

Is there a way to revive or preserve the snippet.

FFX3 avatar Oct 12 '23 17:10 FFX3

Oooh, okay, that sounds more like extmarks getting messed up due to some interaction with cmp, and the snippet not updating due to that.. What's your cmp-config? Are you using the native menu? If so, disabling it might make it work

L3MON4D3 avatar Oct 12 '23 19:10 L3MON4D3

I'm using lsp-zero and havent messed with the cmp configs.

If the docs are up to date my config is

local cmp = require('cmp')

cmp.setup({
  sources = {
    {name = 'nvim_lsp'},
  },
  mapping = {
    ['<C-y>'] = cmp.mapping.confirm({select = false}),
    ['<C-e>'] = cmp.mapping.abort(),
    ['<Up>'] = cmp.mapping.select_prev_item({behavior = 'select'}),
    ['<Down>'] = cmp.mapping.select_next_item({behavior = 'select'}),
    ['<C-p>'] = cmp.mapping(function()
      if cmp.visible() then
        cmp.select_prev_item({behavior = 'insert'})
      else
        cmp.complete()
      end
    end),
    ['<C-n>'] = cmp.mapping(function()
      if cmp.visible() then
        cmp.select_next_item({behavior = 'insert'})
      else
        cmp.complete()
      end
    end),
  },
  snippet = {
    expand = function(args)
      require('luasnip').lsp_expand(args.body)
    end,
  },
})

after some testing, I'm confirming using the custom menu and not the native one.

not familiar with extmarks looking into the subject.

here's the snippet incase you wanted to try and replicate the issue; (it's for lua)

    s('req_reload', fmt([[
            package.loaded['{}'] = nil
            require('{}')
        ]],
        { rep(1), i(1) }
    )),

FFX3 avatar Oct 12 '23 20:10 FFX3

I had the same issue. I was using native menu for cmp. Switching to the custom cmp menu helped me.

popovvg avatar Mar 08 '24 09:03 popovvg