1- *luasnip.txt* For NVIM v0.8.0 Last change: 2023 December 17
1+ *luasnip.txt* For NVIM v0.8.0 Last change: 2023 December 26
22
33==============================================================================
44Table of Contents *luasnip-table-of-contents*
@@ -203,17 +203,19 @@ contains more nodes of the parent. Root-snippets are of course characterised by
203203not being child-snippets. When expanding a new snippet, it becomes a child of
204204the snippet whose region it is expanded inside, and a root if it is not inside
205205any snippet’s region. If it is inside another snippet, the specific node it
206- is inside is determined, and the snippet then nested inside that node. * If
207- that node is interactive (for example, an `insertNode` ), the new snippet will
208- be traversed when the node is visited, as long as the configuration-option
209- `link_children` is enabled. If it is not enabled, it is possible to jump from
210- the snippet to the node, but not the other way around. * If that node is not
211- interactive, the snippet will be linked to the currently active node, also such
212- that it will not be jumped to again once it is left. This is to prevent jumping
213- large distances across the buffer as much as possible. There may still be one
214- large jump from the snippet back to the current node it is nested inside, but
215- that seems hard to avoid. Thus, one should design snippets such that the
216- regions where other snippets may be expanded are inside `insertNodes` .
206+ is inside is determined, and the snippet then nested inside that node.
207+
208+ - If that node is interactive (for example, an `insertNode` ), the new snippet
209+ will be traversed when the node is visited, as long as the
210+ configuration-option `link_children` is enabled. If it is not enabled, it is
211+ possible to jump from the snippet to the node, but not the other way around.
212+ - If that node is not interactive, the snippet will be linked to the currently
213+ active node, also such that it will not be jumped to again once it is left.
214+ This is to prevent jumping large distances across the buffer as much as
215+ possible. There may still be one large jump from the snippet back to the
216+ current node it is nested inside, but that seems hard to avoid.
217+ Thus, one should design snippets such that the regions where other snippets
218+ may be expanded are inside `insertNodes` .
217219
218220If the snippet is not a child, but a root, it can be linked up with the roots
219221immediately adjacent to it by enabling `link_roots` in `setup` . Since by
@@ -235,15 +237,16 @@ There are some common ones (which are listed here), and some that only apply to
235237some nodes (`user_args` for function/dynamicNode). These `opts` are only
236238mentioned if they accept options that are not common to all nodes.
237239
238- Common opts: * `node_ext_opts` and `merge_node_ext_opts` : Control `ext_opts`
239- (most likely highlighting) of the node. Described in detail in
240- | luasnip-ext_opts | * `key ` : The node can be reffered to by this key. Useful for
241- either | luasnip-key-indexer | or for finding the node at runtime (See
242- | luasnip-snippets-api | ), for example inside a `dynamicNode` . The keys do not
243- have to be unique across the entire lifetime of the snippet, but at any point
244- in time, the snippet may contain each key only once. This means it is fine to
245- return a keyed node from a `dynamicNode` , because even if it will be generated
246- multiple times, those will not be valid at the same time.
240+ Common opts:
241+
242+ - `node_ext_opts` and `merge_node_ext_opts` : Control `ext_opts` (most likely
243+ highlighting) of the node. Described in detail in | luasnip-ext_opts |
244+ - `key ` : The node can be reffered to by this key. Useful for either | luasnip-key-indexer | or for finding the node at runtime (See
245+ | luasnip-snippets-api | ), for example inside a `dynamicNode` . The keys
246+ do not have to be unique across the entire lifetime of the snippet, but at any
247+ point in time, the snippet may contain each key only once. This means it is
248+ fine to return a keyed node from a `dynamicNode` , because even if it will be
249+ generated multiple times, those will not be valid at the same time.
247250
248251
249252API *luasnip-node-api*
@@ -701,23 +704,27 @@ If the function only performs simple operations on text, consider using the
701704
702705Node references are used to refer to other nodes in various parts of
703706luasnip’s API. For example, argnodes in functionNode, dynamicNode or lambda
704- are node references. These references can be either of: - `number ` : the
705- jump-index of the node. This will be resolved relative to the parent of the
706- node this is passed to. (So, only nodes with the same parent can be referenced.
707- This is very easy to grasp, but also limiting) - `key_indexer` : the key of the
708- node, if it is present. This will come in handy if the node that is being
709- referred to is not in the same snippet/snippetNode as the one the node
710- reference is passed to. Also, it is the proper way to refer to a
711- non-interactive node (a functionNode, for example) - `absolute_indexer` : the
712- absolute position of the node. Just like `key_indexer` , it allows addressing
713- non-sibling nodes, but is a bit more awkward to handle since a path from root
714- to node has to be determined, whereas `key_indexer` just needs the key to
715- match. Due to this, `key_indexer` should be generally preferred. (More
716- information in | luasnip-absolute-indexer | ). - `node` : just the node. Usage of
717- this is discouraged since it can lead to subtle errors (for example, if the
718- node passed here is captured in a closure and therefore not copied with the
719- remaining tables in the snippet; there’s a big comment about just this in
720- commit 8bfbd61).
707+ are node references. These references can be either of:
708+
709+ - `number ` : the jump-index of the node.
710+ This will be resolved relative to the parent of the node this is passed to.
711+ (So, only nodes with the same parent can be referenced. This is very easy to
712+ grasp, but also limiting)
713+ - `key_indexer` : the key of the node, if it is present. This will come in
714+ handy if the node that is being referred to is not in the same
715+ snippet/snippetNode as the one the node reference is passed to.
716+ Also, it is the proper way to refer to a non-interactive node (a
717+ functionNode, for example)
718+ - `absolute_indexer` : the absolute position of the node. Just like
719+ `key_indexer` , it allows addressing non-sibling nodes, but is a bit more
720+ awkward to handle since a path from root to node has to be determined,
721+ whereas `key_indexer` just needs the key to match.
722+ Due to this, `key_indexer` should be generally preferred.
723+ (More information in | luasnip-absolute-indexer | ).
724+ - `node` : just the node. Usage of this is discouraged since it can lead to
725+ subtle errors (for example, if the node passed here is captured in a closure
726+ and therefore not copied with the remaining tables in the snippet; there’s a
727+ big comment about just this in commit 8bfbd61).
721728
722729
723730==============================================================================
@@ -1168,8 +1175,13 @@ generated snippetNode, and that restoreNodes (internally) always store a
11681175snippetNode, so even if the restoreNode only contains one node, that node has
11691176to be accessed as `ai [restoreNodeIndx][0 ][1 ]` .
11701177
1171- `absolute_indexer` s’ can be constructed in different ways: * `ai [1 ][2 ][3 ]` *
1172- `ai(1, 2, 3)` * `ai{1, 2, 3}` are all the same node.
1178+ `absolute_indexer` s’ can be constructed in different ways:
1179+
1180+ - `ai [1 ][2 ][3 ]`
1181+ - `ai(1, 2, 3)`
1182+ - `ai{1, 2, 3}`
1183+
1184+ are all the same node.
11731185
11741186
11751187==============================================================================
@@ -1473,12 +1485,15 @@ This module (`luasnip.extras.condition`) contains functions that can be passed
14731485to a snippet’s `condition` or `show_condition` . These are grouped accordingly
14741486into `luasnip.extras.conditions.expand ` and `luasnip.extras.conditions.show` :
14751487
1476- **expand**: - `line_begin` : only expand if the cursor is at the beginning of
1477- the line.
1488+ **expand**:
1489+
1490+ - `line_begin` : only expand if the cursor is at the beginning of the line.
1491+
1492+ **show**:
14781493
1479- **show**: - `line_end` : only expand at the end of the line. -
1480- `has_selected_text` : only expand if there’s selected text stored after
1481- pressing `store_selection_keys` .
1494+ - `line_end` : only expand at the end of the line.
1495+ - `has_selected_text` : only expand if there’s selected text stored after pressing
1496+ `store_selection_keys` .
14821497
14831498Additionally, `expand ` contains all conditions provided by `show` .
14841499
@@ -1719,12 +1734,13 @@ surround/are in front of the trigger. While this functionality can also be
17191734implemented by a cusutom `resolveExpandParams` , this helper simplifies the
17201735common cases.
17211736
1722- This matching of treesitter-nodes can be done either * by providing a query and
1723- the name of the capture that should be in front of the trigger (in most cases,
1724- the complete match, but requiring specific nodes before/after the matched node
1725- may be useful as well), or * by providing a function that manually walks the
1726- node-tree, and returns the node in front of the trigger on success (for
1727- increased flexibility).
1737+ This matching of treesitter-nodes can be done either
1738+
1739+ - by providing a query and the name of the capture that should be in front of
1740+ the trigger (in most cases, the complete match, but requiring specific nodes
1741+ before/after the matched node may be useful as well), or
1742+ - by providing a function that manually walks the node-tree, and returns the
1743+ node in front of the trigger on success (for increased flexibility).
17281744
17291745A simple example, which surrounds the previous node’s text preceeding the
17301746`.mv` with `std:: move ()` in cpp files, looks like:
@@ -1832,14 +1848,34 @@ matched against
18321848 end
18331849<
18341850
1835- `snip.env` would contain: * `LS_TSMATCH` : `{ "function add(a, b)", "\treturn a
1836- + b", "end" }` * `LS_TSDATA` : `lua { body = { range = { { 1, 1 }, { 1, 13 } },
1837- type = "block" }, fname = { range = { { 0, 9 }, { 0, 12 } }, type =
1838- "identifier" }, params = { range = { { 0, 12 }, { 0, 18 } }, type =
1839- "parameters" }, prefix = { range = { { 0, 0 }, { 2, 3 } }, type =
1840- "function_declaration" } }` * `LS_TSCAPTURE_FNAME` : `{ "add" }` *
1841- `LS_TSCAPTURE_PARAMS` : `{ "(a, b)" }` * `LS_TSCAPTURE_BODY` : `{ "return a + b"
1842- }` * `LS_TSCAPTURE_PREFIX` : `{ "function add(a, b)", "\treturn a + b", "end" }`
1851+ `snip.env` would contain:
1852+
1853+ - `LS_TSMATCH` : `{ "function add(a, b)", "\treturn a + b", "end" }`
1854+ - `LS_TSDATA` :
1855+ >lua
1856+ {
1857+ body = {
1858+ range = { { 1, 1 }, { 1, 13 } },
1859+ type = "block"
1860+ },
1861+ fname = {
1862+ range = { { 0, 9 }, { 0, 12 } },
1863+ type = "identifier"
1864+ },
1865+ params = {
1866+ range = { { 0, 12 }, { 0, 18 } },
1867+ type = "parameters"
1868+ },
1869+ prefix = {
1870+ range = { { 0, 0 }, { 2, 3 } },
1871+ type = "function_declaration"
1872+ }
1873+ }
1874+ <
1875+ - `LS_TSCAPTURE_FNAME` : `{ "add" }`
1876+ - `LS_TSCAPTURE_PARAMS` : `{ "(a, b)" }`
1877+ - `LS_TSCAPTURE_BODY` : `{ "return a + b" }`
1878+ - `LS_TSCAPTURE_PREFIX` : `{ "function add(a, b)", "\treturn a + b", "end" }`
18431879
18441880(note that all variables containing text of nodes are string-arrays, one entry
18451881for each line)
@@ -1883,13 +1919,16 @@ One more example, which actually uses a few captures:
18831919
18841920The module `luasnip.extras.treesitter_postfix` contains a few functions that
18851921may be useful for creating more efficient ts-postfix-snippets. Nested in
1886- `builtin.tsnode_matcher` are: * `fun find_topmost_types(types: string[]):
1887- MatchTSNodeFunc`: Generates a `LuaSnip.extra.MatchTSNodeFunc` which returns the
1888- last parent whose type is in `types` . * `fun find_first_types(types: string[]):
1889- MatchTSNodeFunc`: Similar to `find_topmost_types` , only this one matches the
1890- first parent whose type is in types. * `find_nth_parent(n: number):
1891- MatchTSNodeFunc`: Simply matches the `n ` -th parent of the innermost node
1892- infront of the trigger.
1922+ `builtin.tsnode_matcher` are:
1923+
1924+ - `fun find_topmost_types(types: string[]): MatchTSNodeFunc`: Generates
1925+ a `LuaSnip.extra.MatchTSNodeFunc` which returns the last parent whose type
1926+ is in `types` .
1927+ - `fun find_first_types(types: string[]): MatchTSNodeFunc`: Similar to
1928+ `find_topmost_types` , only this one matches the first parent whose type is in
1929+ types.
1930+ - `find_nth_parent(n: number): MatchTSNodeFunc`: Simply matches the `n ` -th
1931+ parent of the innermost node infront of the trigger.
18931932
18941933With `find_topmost_types` , the first example can be implemented more
18951934efficiently (without needing a whole query):
@@ -2055,17 +2094,21 @@ the location referred by it. This is primarily implemented for snippet which
20552094got their source from one of the loaders, but might also work for snippets
20562095where the source was set manually.
20572096
2058- `require (" luasnip.extras.snip_location" )` : *
2059- `snip_location.jump_to_snippet(snip, opts)` Jump to the definition of `snip` . *
2060- `snip` : a snippet with attached source-data. * `opts` : `nil|table` , optional
2061- arguments, valid keys are: * `hl_duration_ms` : `number ` , duration for which the
2062- definition should be highlighted, in milliseconds. 0 disables the highlight. *
2063- `edit_fn` : `function (file )` , this function will be called with the file the
2064- snippet is located in, and is responsible for jumping to it. We assume that
2065- after it has returned, the current buffer contains `file ` . *
2066- `snip_location.jump_to_active_snippet (opts)` Jump to definition of active
2067- snippet. * `opts` : `nil|table` , accepts the same keys as the `opts` -parameter
2068- of `jump_to_snippet` .
2097+ `require (" luasnip.extras.snip_location" )` :
2098+
2099+ - `snip_location.jump_to_snippet(snip, opts)`
2100+ Jump to the definition of `snip` .
2101+ - `snip` : a snippet with attached source-data.
2102+ - `opts` : `nil|table` , optional arguments, valid keys are:
2103+ - `hl_duration_ms` : `number ` , duration for which the definition should be highlighted,
2104+ in milliseconds. 0 disables the highlight.
2105+ - `edit_fn` : `function (file )` , this function will be called with the file
2106+ the snippet is located in, and is responsible for jumping to it.
2107+ We assume that after it has returned, the current buffer contains `file ` .
2108+ - `snip_location.jump_to_active_snippet (opts)`
2109+ Jump to definition of active snippet.
2110+ - `opts` : `nil|table` , accepts the same keys as the `opts` -parameter of
2111+ `jump_to_snippet` .
20692112
20702113
20712114==============================================================================
@@ -2168,9 +2211,12 @@ This behaviour can be modified by changing `parser_nested_assembler` in
21682211`ls .setup ()` .
21692212
21702213LuaSnip will also modify some snippets that it is incapable of representing
2171- accurately: - if the `$0 ` is a placeholder with something other than just text
2172- inside - if the `$0 ` is a choice - if the `$0 ` is not an immediate child of the
2173- snippet (it could be inside a placeholder: `"${1: $0 }"`)
2214+ accurately:
2215+
2216+ - if the `$0 ` is a placeholder with something other than just text inside
2217+ - if the `$0 ` is a choice
2218+ - if the `$0 ` is not an immediate child of the snippet (it could be inside a
2219+ placeholder: `"${1: $0 }"`)
21742220
21752221To remedy those incompatibilities, the invalid `$0 ` will be replaced with a
21762222tabstop/placeholder/choice which will be visited just before the new `$0 ` . This
0 commit comments