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

allow undoing autoexpand #830

Open
max397574 opened this issue Mar 17, 2023 · 18 comments
Open

allow undoing autoexpand #830

max397574 opened this issue Mar 17, 2023 · 18 comments

Comments

@max397574
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor Author

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

@max397574
Copy link
Contributor Author

max397574 commented Mar 17, 2023

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
Copy link
Owner

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
Copy link
Contributor Author

max397574 commented Mar 18, 2023

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
Copy link
Owner

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

@A7R7
Copy link

A7R7 commented Mar 30, 2023

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
Copy link

A7R7 commented Mar 30, 2023

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

@max397574
Copy link
Contributor Author

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
Copy link

A7R7 commented Mar 30, 2023

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

@max397574
Copy link
Contributor Author

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
Copy link

A7R7 commented Mar 30, 2023

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

@max397574
Copy link
Contributor Author

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

@A7R7
Copy link

A7R7 commented Mar 30, 2023

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

@max397574
Copy link
Contributor Author

well you could just map that to something

@nakulsodhi
Copy link

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
Copy link
Contributor Author

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

yes

@project-repo
Copy link

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

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

5 participants