LuaSnip icon indicating copy to clipboard operation
LuaSnip copied to clipboard

Clearing the snippets' jumplist

Open vining-onward opened this issue 2 years ago • 4 comments

Is there a way to clear the snippet jumplist so, for example, I can guarantee that pressing <Tab> will add 4 spaces instead of finding a previous snippet jump location? The problem usually happens because I have several nested snippets (many using $0 to get out of the snippet; e.g., a for loop with $0 outside of the loop), and then if I want to use <Tab> I have to circle through many past jumps I never used to finally clear the jumplist and use <Tab> as if no snippets were expanded.

vining-onward avatar Jul 13 '23 15:07 vining-onward

You can set history=false and region_check_events = {"CursorHold"} (in ls.setup({...})), this way you will jump to the end of a snippet as soon as you leave it with the cursor (and CursorHold is triggered), and with history=false, you will also forget previous snippets, so you won't jump back into them.

But, I'd really recommend not overloading tab for snippets, I've migrated away from it to these

vim.keymap.set({"i"}, "<C-K>", function() ls.expand() end, {silent = true})
vim.keymap.set({"i", "s"}, "<C-L>", function() ls.jump( 1) end, {silent = true})
vim.keymap.set({"i", "s"}, "<C-J>", function() ls.jump(-1) end, {silent = true})

vim.keymap.set({"i", "s"}, "<C-E>", function()
	if ls.choice_active() then
		ls.change_choice(1)
	end
end, {silent = true})

keymappings a while ago, and it's much better (no accidential expanding of snippets when I want to jump, no accidential jumping when I want to insert a tab)

L3MON4D3 avatar Jul 13 '23 16:07 L3MON4D3

Thanks, I appreciate the suggestions! Unfortunately history=false and region_check_events = {"CursorHold"} don't seem to be enough because some snippets might never be left (e.g., for($1) $2 end for $0 and the $0 mark still exists if I leave the loop without triggering the $0 mark). There is no way to simply clear all of the existing marks in the jumplist?

vining-onward avatar Jul 14 '23 09:07 vining-onward

Ah alright, exit_out_of_region (which is called by region_check_events) misbehaves if history is false, it will re-enable the snippet (which is a bug, I'll look into fixing it) :/ For now, you can manually set require("luasnip").session.current_nodes[vim.api.nvim_get_current_buf()] = nil whenever you want to clear the jumplist

L3MON4D3 avatar Jul 14 '23 11:07 L3MON4D3

Thank you so much, I change my binds like ours and now it's working like a charm

mendesbarreto avatar Jul 15 '23 19:07 mendesbarreto