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

Accepting suggestion with <M-l> ruins expanded snippet (LuaSnip) #315

Open
tmillr opened this issue Sep 6, 2024 · 0 comments
Open

Accepting suggestion with <M-l> ruins expanded snippet (LuaSnip) #315

tmillr opened this issue Sep 6, 2024 · 0 comments

Comments

@tmillr
Copy link

tmillr commented Sep 6, 2024

Probably by disturbing/clobbering extmarks in the current line.

How I stumbled upon the bug

  1. Expand a LuSnip snippet that includes choices (i.e. includes a single choice node within, somewhere in the middle among a couple other nodes). For simplicity, just use a single-line snippet.
  2. <Esc> to normal mode, and then manually position the cursor at EOL (if it isn't already there).
  3. Accept copilot suggestion with <M-l> "accept" which extends the current line.
  4. Now, cycle snippet choices (e.g. via keymap). Instead of the choices cycling at the proper position within the line (as they would otherwise), the position will be all screwed up (e.g. always at the very begging of the current line).

I know that LuSnip uses extmarks to track the position of the expanded snippet, so I believe that the issue has something to do with this plugin (copilot.lua) disturbing those extmarks when inserting the suggestion text. I mean it could also be a LuaSnip issue (extmark config/gravity settings), but I think that this is less likely. Might need further investigation.

Idk how this plugin handles inserting the suggestion text, but if it always just replaces the entire line, then that would explain why the extmarks are getting ruined.

Example/Reproduction

-- You will need to have both luasnip and copilot.lua installed/setup, and the
-- latter should be active for this buffer (or on Lua buffers).
--
-- 1. Paste this into a new buffer.
-- 2. Run `:%lua` at the cmdline.
-- 3. Press `<Esc>` to exit select mode.
-- 4. Press `A` to enter insert mode at the EOL.
-- 5. Type a space and wait for copilot suggestion.
-- 6. Accept the suggestion (e.g. `<M-l>`).
-- 7. Cycle snippet choices with `<S-Tab>` and observe the bug.
--
-- To observe the correct behavior, repeat these steps but skip step 6.

local ls = require 'luasnip'
vim.bo.ft = 'lua'
vim.keymap.set(
  { 'n', 'i', 'v' },
  '<S-Tab>',
  function() ls.change_choice(1) end,
  { buffer = 0 }
)
vim.cmd [[normal! G2o]]

ls.snip_expand(ls.s({}, {
  ls.t '-- What does ',
  ls.c(1, { ls.i(1, '1 + 2'), ls.i(1, '2 + 3') }), -- Choice node
  ls.t ' equal? The answer is',
}))
@tmillr tmillr changed the title Accepting suggestion with <M-l> ruins current expanded snippet (LuaSnip) Accepting suggestion with <M-l> ruins expanded snippet (LuaSnip) Sep 6, 2024
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

No branches or pull requests

1 participant