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

toggle autostnippets #1281

Open
SylvanFranklin opened this issue Dec 30, 2024 · 3 comments
Open

toggle autostnippets #1281

SylvanFranklin opened this issue Dec 30, 2024 · 3 comments

Comments

@SylvanFranklin
Copy link

SylvanFranklin commented Dec 30, 2024

I'm sure this is possible in the with some kind of scripting via the setup function, but it would be nice to have a dedicated method (unless I'm missing it from the docs). I have a bunch of snippets that are single letters that auto trigger, I would like to be able to enable and disable this functionality with a command. I came up with this to disable them:

local ls = require("luasnip")
vim.keymap.set({ "i", "s" }, "<C-c>", function()
    ls.setup({ enable_autosnippets = false })
end, { silent = false })

Which is most of what I want, but toggling them with the same shortcut would be nice. Maybe there is some way to do that in nvim like a state variable, or maybe some kind of query for the status of enable_autostnippets

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Jan 3, 2025

This seems like a nice addition, and, while I have not yet considered changing settings of a running session (how it interacts with already-expanded snippets, for example), en/disabling autosnippets should not cause any issues as that only means re/unregistering an autocommand :)

I'll look into what other settings can be sensibly changed during a running session, until then (and it may be a while because I don't have much time to spend on luasnip at the moment) you can also assing require("luasnip").expand_auto to an empty function (function() end). (This is a bit better than the setup-variant because it loses other settings you may have in other calls to setup)

@SylvanFranklin
Copy link
Author

Thanks for the advice and your work on this! Good call to use an empty function, I just have a question: how would you go about creating a single command to toggle with this approach? It seems like it would require some kind of state (which maybe is more of a neovim question), or some kind of luasnip status query function.

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Jan 5, 2025

Oh, for your config, I'd recommend something like

local enable_autosnippets = true
ls.setup({
	enable_autosnippets = enable_autosnippets
})

local expand_autosnippets_orig = ls.expand_auto
local nop = function() end
vim.keymap.set({ "i", "s" }, "<C-c>", function()
	enable_autosnippets = not enable_autosnippets
	if enable_autosnippets then
		ls.expand_auto = expand_autosnippets_orig
	else
		ls.expand_auto = nop
	end
end, { silent = false })

So, store the original function in a variable and then assign it or a function that does nothing based on enable_autosnippets, which tracks whether they are enabled.

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

2 participants