L3MON4D3 / LuaSnip

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

allow undoing autoexpand #830

Open max397574 opened 1 year ago

max397574 commented 1 year ago

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

L3MON4D3 commented 1 year ago

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?

max397574 commented 1 year ago

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

max397574 commented 1 year ago

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
L3MON4D3 commented 1 year ago

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.

max397574 commented 1 year ago

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

L3MON4D3 commented 1 year ago

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

A7R7 commented 1 year ago

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 commented 1 year ago

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

max397574 commented 1 year ago
local snip_expand = require("luasnip").snip_expand
require("luasnip").snip_expand = function(...)
    vim.o.ul = vim.o.ul
    snip_expand(...)
end

works for me

A7R7 commented 1 year ago

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

max397574 commented 1 year ago

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

A7R7 commented 1 year ago

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

max397574 commented 1 year ago

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

A7R7 commented 1 year ago

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

max397574 commented 1 year ago

well you could just map that to something

nakulsodhi commented 10 months ago

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?

max397574 commented 10 months ago

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

yes

project-repo commented 2 days ago

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