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

Best Practice of insert node with LS_SELECT_RAW and default value? #1307

Open
hyaray opened this issue Mar 14, 2025 · 1 comment
Open

Best Practice of insert node with LS_SELECT_RAW and default value? #1307

hyaray opened this issue Mar 14, 2025 · 1 comment

Comments

@hyaray
Copy link

hyaray commented Mar 14, 2025

I want basic feature like ${1:${VISUAL:val}}, It's hard to done with lua format for a beginner.
now I found a way like this:

  1. define function return d function with index and default.
sn_i_visual = function(index, default)
	return d(index, function(args, parent)
		local raw = parent.snippet.env.LS_SELECT_RAW
        	if not raw or #raw == 0 then
			raw = default
		 end
		return sn(nil, i(index, raw))
	end)
end

used in snippets:
s("vimn", fmta("vim.notify(<>)", sn_i_visual(1, "abc"))),

I found fmta is much friednly than fmt in lua

@bew
Copy link
Contributor

bew commented Mar 14, 2025

Yes this is basically how I do it as well:
https://github.com/bew/dotfiles/blob/b6df42b46e372f821f4744ae7283bcdbf6d47729/nvim-wip/lua/mycfg/snippets_by_ft/_utils.lua#L161-L175
I use LS_SELECT_DEDENT to make it easily work with indented selection.
I also add an insert node after the 'visual' selection to have a 'tabstop', make it easier to add something after ;)

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