LuaSnip icon indicating copy to clipboard operation
LuaSnip copied to clipboard

allow undoing autoexpand

Open max397574 opened this issue 2 years ago • 17 comments

it should somehow be possible to undo autoexpand if a snippet autoexpanded when it shouldn't have I see two options for this either start a new undo sequence so you can just <c-o>u from insert mode or provide an api to do it which can be mapped to

max397574 avatar Mar 17 '23 08:03 max397574

This is similar to #797, a description for achieving this via api is in there, but I haven't considered just using undo for this. Is it possible to register a new undo-point (?) via lua?

L3MON4D3 avatar Mar 17 '23 15:03 L3MON4D3

yes looks like this could work for me regarding your question image

max397574 avatar Mar 17 '23 17:03 max397574

I can confirm it works with this hack

local auto_expand = require("luasnip").expand_auto
require("luasnip").expand_auto = function(...)
    vim.o.undolevels = vim.o.undolevels
    auto_expand(...)
end

max397574 avatar Mar 17 '23 17:03 max397574

Ahh okay, thank you! That seems like something we could just always do before expanding a snippet.. Although undo won't affect internal variables (the current node), which would have to be reset in some other way, so maybe a proper API is the better approach.

L3MON4D3 avatar Mar 17 '23 18:03 L3MON4D3

I think in the long run an api would be the better solution for now I just use this (for anyone searching for a solution):

local auto_expand = require("luasnip").expand_auto
require("luasnip").expand_auto = function(...)
    vim.o.undolevels = vim.o.undolevels
    auto_expand(...)
end

this isn't a nice solution though because it sets new undo everytime the function is called

max397574 avatar Mar 18 '23 07:03 max397574

Slightly better: set the new undo-point in (/around?) snip_expand, that's the function which actually expands the snippets

L3MON4D3 avatar Mar 18 '23 21:03 L3MON4D3

I tried both snip_expand and expand_auto, adding undo point in expand_auto will lead to neovim recording every character you type in an expansion trigger.

local auto_expand = require("luasnip").expand_auto
require("luasnip").expand_auto = function(...)
    vim.o.undolevels = vim.o.undolevels
    auto_expand(...)
end

however, adding undo in snip_expand seems just doesn't work. neovim still undoes everything in a single period of insert.

local auto_expand = require("luasnip").snip_expand
require("luasnip").snip_expand = function(...)
    vim.o.undolevels = vim.o.undolevels
    auto_expand(...)
end

Did I set it wrong? or is there a better alternative?

A7R7 avatar Mar 30 '23 09:03 A7R7

also, I'm as well looking forward for an api to came out, thank you L3MON4D3 :D

A7R7 avatar Mar 30 '23 09:03 A7R7

local snip_expand = require("luasnip").snip_expand
require("luasnip").snip_expand = function(...)
    vim.o.ul = vim.o.ul
    snip_expand(...)
end

works for me

max397574 avatar Mar 30 '23 09:03 max397574

strange, just doesn't work for me. Are you using an auto-trigger or expand by enter?

A7R7 avatar Mar 30 '23 09:03 A7R7

autoexpand snippets I see no reason why you'd want to undo manually expanding snippets if you do you could just put the vim.o.ul=vim.o.ul inside your mapping to expand

max397574 avatar Mar 30 '23 11:03 max397574

I'm also using autoexpand snippets. Then there might be something wrong in my configs somewhere.

A7R7 avatar Mar 30 '23 11:03 A7R7

you should be able to just do <c-o>u in insert mode

max397574 avatar Mar 30 '23 11:03 max397574

oh yes, and that worked, but it's a bit not that automatic.

A7R7 avatar Mar 30 '23 11:03 A7R7

well you could just map that to something

max397574 avatar Mar 30 '23 12:03 max397574

I think in the long run an api would be the better solution for now I just use this (for anyone searching for a solution):

local auto_expand = require("luasnip").expand_auto
require("luasnip").expand_auto = function(...)
    vim.o.undolevels = vim.o.undolevels
    auto_expand(...)
end

this isn't a nice solution though because it sets new undo everytime the function is called

How do you actually implement this solution? like do i just plop this into an init.lua?

ethernet-gravy avatar Jan 14 '24 01:01 ethernet-gravy

How do you actually implement this solution? like do i just plop this into an init.lua?

yes

max397574 avatar Jan 14 '24 09:01 max397574

Hi, I recently stumbled upon this issue. For me the solution of @max397574 didn't work. On investigating further, I realized that the issue can be resolved by applying the following simple patch:

@@ -363,7 +363,7 @@ local function expand_auto()
 				},
 				to = cursor,
 			}
-		snip = snip_expand(snip, {
+		snip = ls.snip_expand(snip, {
 			expand_params = expand_params,
 			-- clear trigger-text.
 			clear_region = clear_region,

to lua/luasnip/init.lua. If you are interested in changing this, I could submit this as a pull request. Cheers, project-repo

project-repo avatar Nov 10 '24 11:11 project-repo

Hi, I recently stumbled upon this issue. For me the solution of @max397574 didn't work. On investigating further, I realized that the issue can be resolved by applying the following simple patch:

@@ -363,7 +363,7 @@ local function expand_auto() }, to = cursor, }

  • snip = snip_expand(snip, {
    
  • snip = ls.snip_expand(snip, {
    	expand_params = expand_params,
    	-- clear trigger-text.
    	clear_region = clear_region,
    

to lua/luasnip/init.lua. If you are interested in changing this, I could submit this as a pull request. Cheers, project-repo

Looks like a recent commit took care of this: https://github.com/L3MON4D3/LuaSnip/commit/4c6e25ebde57ed276c47e761950b4a1651dd1b2f#diff-b508172a626a10bbefe65d26b9f7c5e775fe8fde73f21b2fb135cfeea0afc521 and the snip_expand solution is working for me now.

michaelfortunato avatar Oct 20 '25 18:10 michaelfortunato