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

Keyed nodes #838

Merged
merged 3 commits into from
May 21, 2023
Merged

Keyed nodes #838

merged 3 commits into from
May 21, 2023

Conversation

L3MON4D3
Copy link
Owner

@L3MON4D3 L3MON4D3 commented Mar 22, 2023

This adds a way to address some node in the expanded snippet, both at runtime (for example when evaluating a dynamic/functionNode-function) and when passing the nodes they should depend on.

For example:

ls.setup_snip_env()

local k = require("luasnip.nodes.key_indexer").new_key

ls.snip_expand(s("a", {
	-- set key here
	i(1, "preset", {key="outer_i1"}),
	c(2, {
		t"some text",
		{
			i(1), t"repeated: ", f(function(args)
				return args[1]
			-- reference node via key!!
			end, k("outer_i1"))
		}
	})
}), {
	key = "adsf"
})

or, a bit more complicated:

ls.setup_snip_env()

local k = require("luasnip.nodes.key_indexer").new_key

ls.snip_expand(s("a", {
	i(1, "preset", {key="outer_i1"}),
	c(2, {
		t"some text",
		{
			i(1), t"repeated: ", f(function(_, snip)
				-- map nodes inside add_sNode to their text.
				local node_contents = vim.tbl_map(function(node)
					return node:get_text()[1]
				end, snip:get_keyed_node("add_sNode").insert_nodes)

				-- add numbers together.
				return tostring(node_contents[1] + node_contents[2] + node_contents[3])
			end, k("outer_i1"))
		}
	}),
	sn(3, {
		t" ", i(1, "1"), t" ", i(2, "2"), t" ", i(3, "3")
	}, {key="add_sNode"})
}), {
	key = "adsf"
})

I believe key_indexer is an improvement upon absolute_indexer, since that required specifying the "address" to some node as a sequence of jump-indices, whereas key_indexer allows much clearer addresses.

Needs:

  • Doc
  • Tests

@L3MON4D3 L3MON4D3 linked an issue Mar 22, 2023 that may be closed by this pull request
@L3MON4D3 L3MON4D3 force-pushed the key_nodes branch 4 times, most recently from 53f902d to 406bd78 Compare May 19, 2023 13:15
@L3MON4D3
Copy link
Owner Author

Okay this somehow blindsided me completely: with this, it's now also possible to depend on a functionNode!
(as usual, not completely sure how it can be useful, but want to give it a tad more visibility, thus this comment :D)

@L3MON4D3 L3MON4D3 force-pushed the key_nodes branch 2 times, most recently from 6350a6f to 338cecb Compare May 20, 2023 10:59
Nodes can be explicitly given some key, and this key can be used in the
args-parameter passed to function/dynamicNode.

This basically makes absolute_indexer useless, since non-sibling nodes
can be addressed much clearer by giving them some key/name, and using
that to reference them, as opposed to reconstructing their exact path
from the snippet-root and passing that.

Furthermore, now all kinds of nodes can be depended on (for example
functionNode)

Lastly, a keyed node can be retrieved via `snippet:get_keyed_node(key)`
in dynamicNode, which can enable more complicated snippets.
@L3MON4D3 L3MON4D3 merged commit 9f050da into master May 21, 2023
@bew
Copy link
Contributor

bew commented May 21, 2023

Thank you for this! 👌

@L3MON4D3
Copy link
Owner Author

Glad it helps you :)

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

Successfully merging this pull request may close these issues.

Better reference system
2 participants