1- *luasnip.txt* For NVIM v0.5.0 Last change: 2022 August 30
1+ *luasnip.txt* For NVIM v0.5.0 Last change: 2022 August 31
22
33==============================================================================
44Table of Contents *luasnip-table-of-contents*
55
661. BASICS | luasnip-basics |
7+ - Jump-index | luasnip-jump-index |
8+ - Adding Snippets | luasnip-adding-snippets |
792. NODE | luasnip-node |
8103. SNIPPETS | luasnip-snippets |
911 - Api: | luasnip-api: |
10124. TEXTNODE | luasnip-textnode |
11135. INSERTNODE | luasnip-insertnode |
12146. FUNCTIONNODE | luasnip-functionnode |
15+ - NODE_REFERENCE | luasnip-node_reference |
13167. CHOICENODE | luasnip-choicenode |
14178. SNIPPETNODE | luasnip-snippetnode |
15189. INDENTSNIPPETNODE | luasnip-indentsnippetnode |
@@ -106,10 +109,41 @@ Snippets are always created using the `s(trigger:string,
106109nodes:table)`-function. It is explained in more detail in | luasnip-snippets | ,
107110but the gist is that it creates a snippet that contains the nodes specified in
108111`nodes` , which will be inserted into a buffer if the text before the cursor
109- matches `trigger` when `expand ` is called. The snippets for a given filetype
110- have to be added to luasnip via `ls.add_snippets(filetype, snippets)`. Snippets
111- that should be accessible globally (in all filetypes) have to be added to the
112- special filetype `all ` .
112+ matches `trigger` when `ls .expand ` is called.
113+
114+ JUMP-INDEX *luasnip-jump-index*
115+
116+ Nodes that can be jumped to (`insertNode` , `choiceNode` , `dynamicNode` ,
117+ `restoreNode` , `snippetNode` ) all require a "jump-index" so luasnip knows the
118+ order in which these nodes are supposed to be visited ("jumped to").
119+
120+ >
121+ s("trig", {
122+ i(1), t"text", i(2), t"text again", i(3)
123+ })
124+ <
125+
126+
127+ These indices don’t "run" through the entire snippet, like they do in
128+ textmate-snippets (`"$1 ${2: $3 $4}"`), they restart at 1 in each nested
129+ snippetNode:
130+
131+ >
132+ s("trig", {
133+ i(1), t" ", sn(2, {
134+ t" ", i(1), t" ", i(2)
135+ })
136+ })
137+ <
138+
139+
140+ (roughly equivalent to the given textmate-snippet).
141+
142+ ADDING SNIPPETS *luasnip-adding-snippets*
143+
144+ The snippets for a given filetype have to be added to luasnip via
145+ `ls.add_snippets(filetype, snippets)`. Snippets that should be accessible
146+ globally (in all filetypes) have to be added to the special filetype `all ` .
113147
114148>
115149 ls.add_snippets("all", {
@@ -353,7 +387,7 @@ textmate-snippet will expand correctly when parsed).
353387`i(jump_index, text, node_opts)`
354388
355389
356- - `jump_index` : `number ` , this determines when this node will be jumped to.
390+ - | luasnip- `jump_index`| : `number ` , this determines when this node will be jumped to.
357391- `text` : `string |string []` , a single string for just one line, a list with >1
358392 entries for multiple lines.
359393 This text will be SELECTed when the `insertNode` is jumped into.
@@ -410,18 +444,12 @@ for multiline-string, here all lines following the first will be prefixed with
410444the snippets’ indentation.
411445
412446
413- - `argnode_references` : List or just one of either:
414- - `number ` : the jump-index of the argnode.
415- This will be resolved inside the parent of this `functionNode` .
416- - | luasnip-`absolute_indexer` | : the absolute position of the
417- argnode.
418- - `node` : the argnode. Usage of this is discouraged, since it can lead to
419- subtle errors (if the node passed here is for example captured in a
420- closure (there’s a big comment about just this in commit 8bfbd61)).
421- - `nil` : if there are no argnodes, the function will be evaluated once upon
422- expansion.
447+ - `argnode_references` : `node_reference[]|node_refernce|nil` . Either no, a
448+ single, or multiple | luasnip-node-references | . Changing any of these will
449+ trigger a re-evaluation of `fn` , and insertion of the updated text. If no
450+ node-reference is passed, the `functionNode` is evaluated once upon expansion.
423451- `node_opts` : `table` , see | luasnip-here | . One additional key is supported:
424- - `user_args` : any[], these will be passed to `fn` as `user_arg1` -`user_argn` .
452+ - `user_args` : ` any[]` , these will be passed to `fn` as `user_arg1` -`user_argn` .
425453 These make it easier to reuse similar functions, for example a functionNode
426454 that wraps some text in different delimiters (`()` , `[]` , …).
427455 >
@@ -483,6 +511,19 @@ the snippets’ indentation.
483511 If the function only performs simple operations on text, consider using the
484512`lambda` from | luasnip-`luasnip.extras` |
485513
514+ NODE_REFERENCE *luasnip-node_reference*
515+
516+ Node-references are used to refer to other nodes in various parts of
517+ luasnip’s API. For example, argnodes in functionNode, dynamicNode or lambda
518+ are node-references. These references can be either of: - `number ` : the
519+ jump-index of the node. This will be resolved inside the parent of the node
520+ this is passed to. - | luasnip-`absolute_indexer` | : the absolute position of the
521+ node. This will come in handy if the referred-to node is not in the same
522+ snippet/snippetNode as the one this node-reference is passed to. - `node` : just
523+ the node. Usage of this is discouraged, since it can lead to subtle errors (if
524+ the node passed here is for example captured in a closure (there’s a big
525+ comment about just this in commit 8bfbd61)).
526+
486527==============================================================================
4875287. CHOICENODE *luasnip-choicenode*
488529
@@ -497,16 +538,16 @@ ChoiceNodes allow choosing between multiple nodes.
497538<
498539
499540
500- `c(jump_indx , choices, node_opts)` - `jump_indx` : `number ` , since choiceNodes
501- can be jumped to, they need their jump-indx. - `choices` : `node[]|node` , the
502- choices. The first will be initialliy active. A list of nodes will be turned
503- into a `snippetNode` . - `node_opts` , `table` . `choiceNode` supports the keys
504- common to all nodes described | luasnip-here | , and one additional key: -
505- `restore_cursor` : `false` by default. If it is set, and the node that was being
506- edited also appears in the switched-to choice (can be the case if a
507- `restoreNode` is present in both choice) the cursor is restored relative to
508- that node. The default is `false` as enabling might lead to decreased
509- performance. It’s possible to override the default by wrapping the
541+ `c(jump_index , choices, node_opts)` - | luasnip-`jump_index` | : `number ` , since
542+ choiceNodes can be jumped to, they need their jump-indx. - `choices` :
543+ `node[]|node` , the choices. The first will be initialliy active. A list of
544+ nodes will be turned into a `snippetNode` . - `node_opts` : `table` . `choiceNode`
545+ supports the keys common to all nodes described | luasnip-here | , and one
546+ additional key: - `restore_cursor` : `false` by default. If it is set, and the
547+ node that was being edited also appears in the switched-to choice (can be the
548+ case if a `restoreNode` is present in both choice) the cursor is restored
549+ relative to that node. The default is `false` as enabling might lead to
550+ decreased performance. It’s possible to override the default by wrapping the
510551`choiceNode` -constructor in another function that sets `opts.restore_cursor` to
511552`true` and then using that to construct `choiceNode` s: `lua local function
512553restore_cursor_choice(pos, choices, opts) if opts then opts.restore_cursor =
@@ -553,11 +594,11 @@ and any choice can be selected right away, via `vim.ui.select`.
5535948. SNIPPETNODE *luasnip-snippetnode*
554595
555596SnippetNodes directly insert their contents into the surrounding snippet. This
556- is useful for choiceNodes , which only accept one child, or dynamicNodes, where
557- nodes are created at runtime and inserted as a snippetNode.
597+ is useful for `choiceNode` s , which only accept one child, or `dynamicNode` s,
598+ where nodes are created at runtime and inserted as a ` snippetNode` .
558599
559- Syntax is similar to snippets , however, where snippets require a table
560- specifying when to expand, snippetNodes , similar to insertNodes , expect their
600+ Their syntax is similar to ` s ` , however, where snippets require a table
601+ specifying when to expand, `snippetNode` s , similar to `insertNode` s , expect a
561602jump-index.
562603
563604>
@@ -568,8 +609,19 @@ jump-index.
568609<
569610
570611
571- Note that snippetNodes don’t accept an `i (0 )` , so the jump-indices of the
572- nodes inside them have to be in `1 ,2 ,... ,n ` .
612+ `sn(jump_index, nodes, node_opts)`
613+
614+
615+ - | luasnip-`jump_index` | : `number ` , the usual.
616+ - `nodes` : `node[]|node` , just like for `s ` .
617+ Note that `snippetNode` s don’t accept an `i (0 )` , so the jump-indices of the nodes
618+ inside them have to be in `1 ,2 ,... ,n ` .
619+ - `node_opts` : `table` : again, all | luasnip-common | keys are supported, but also
620+ - `callbacks` ,
621+ - `child_ext_opts` and
622+ - `merge_child_ext_opts` ,
623+ which are further explained in | luasnip-`snippets` | .
624+
573625
574626==============================================================================
5756279. INDENTSNIPPETNODE *luasnip-indentsnippetnode*
@@ -606,50 +658,60 @@ comment- string before the nodes of the snippet:
606658
607659Here the `// ` before `This is` is important, once again, because indent is only
608660applied after linebreaks. To enable such usage, `$PARENT_INDENT ` in the
609- indentstring is replaced by the parents’ indent (duh).
661+ indentstring is replaced by the parent’s indent (duh).
610662
611- ==============================================================================
612- 10. DYNAMICNODE *luasnip-dynamicnode*
663+ `isn(jump_index, nodes, indentstring, node_opts)`
613664
614- Very similar to functionNode, but returns a snippetNode instead of just text,
615- which makes them very powerful as parts of the snippet can be changed based on
616- user-input.
665+ All of these except `indentstring` are exactly the same as
666+ | luasnip-`snippetnode` | .
617667
618- The prototype for the dynamicNodes’ constructor is `d(jump_index:int,
619- function, argnodes:table of nodes, opts: table)`:
620668
669+ - `indentstring` : `string ` , will be used to indent the nodes inside this
670+ `snippetNode` .
671+ All occurences of `" $PARENT_INDENT" ` are replaced with the actual indent of
672+ the parent.
621673
622- 1. `jump_index` : just like all jumpable nodes, its’ position in the jump-list.
623- 2. `function ` : `fn(args, parent, old_state, user_args1, ..., user_argsn) -> snippetNode`
624- This function is called when the argnodes’ text changes. It generates and
625- returns (wrapped inside a `snippetNode` ) the nodes that should be inserted
626- at the dynamicNodes place.
627- `args ` , `parent` and `user_args` are also explained in
628- | luasnip-functionnode |
629674
675+ ==============================================================================
676+ 10. DYNAMICNODE *luasnip-dynamicnode*
630677
631- - `args ` : `table of text` (`{{"node1line1", "node1line2"}, {"node2line1"} }`)
632- from nodes the dynamicNode depends on.
633- - `parent` : the immediate parent of the `dynamicNode` ).
634- - `old_state` : a user-defined table. This table may contain
635- anything, its intended usage is to preserve information from the previously
636- generated `snippetNode` : If the `dynamicNode` depends on other nodes it may
637- be reconstructed, which means all user input (text inserted in `insertNodes` ,
638- changed choices) to the previous dynamicNode is lost.
639- The `old_state` table must be stored in `snippetNode` returned by
640- the function (`snippetNode.old_state` ).
641- The second example below illustrates the usage of `old_state` .
642- - `user_args1, ..., user_argsn`: passed through from `dynamicNode` -opts.
678+ Very similar to functionNode, but returns a snippetNode instead of just text,
679+ which makes them very powerful as parts of the snippet can be changed based on
680+ user-input.
643681
644- 3. `argnodes` : Indices of nodes the dynamicNode depends on: if any of these trigger an
645- update, the `dynamicNode` s’ function will be executed, and the result inserted at
646- the `dynamicNodes` place.
647- Can be a single index or a table of indices.
648- 4. `opts` : Just like `functionNode` , `dynamicNode` also accepts `user_args` in
649- addition to options common to all nodes.
682+ `d(jump_index, function, node-references, opts)`:
683+
684+
685+ - | luasnip-`jump_index` | : `number ` , just like all jumpable nodes, its’ position
686+ in the jump-list.
687+ - `function ` : `fn(args, parent, old_state, user_args) -> snippetNode` This
688+ function is called when the argnodes’ text changes. It should generate and
689+ returns (wrapped inside a `snippetNode` ) nodes, these will be inserted at the
690+ dynamicNodes place. `args ` , `parent` and `user_args` are also explained in
691+ | luasnip-functionnode |
692+ - `args ` : `table of text` (`{{"node1line1", "node1line2"}, {"node2line1"} }`)
693+ from nodes the `dynamicNode` depends on.
694+ - `parent` : the immediate parent of the `dynamicNode` .
695+ - `old_state` : a user-defined table. This table may contain anything, its
696+ intended usage is to preserve information from the previously generated
697+ `snippetNode` : If the `dynamicNode` depends on other nodes it may be
698+ reconstructed, which means all user input (text inserted in `insertNodes` ,
699+ changed choices) to the previous dynamicNode is lost.
700+ The `old_state` table must be stored in `snippetNode` returned by
701+ the function (`snippetNode.old_state` ).
702+ The second example below illustrates the usage of `old_state` .
703+ - `user_args` : passed through from `dynamicNode` -opts, may be more than one
704+ argument.
705+ - `node_references` : `node_reference[]|node_references|nil` , | luasnip-references |
706+ to the nodes the dynamicNode depends on: if any of these trigger an update (for
707+ example if the text inside them changes), the `dynamicNode` s’ function will
708+ be executed, and the result inserted at the `dynamicNodes` place.
709+ (`dynamicNode` behaves exactly the same as `functionNode` in this regard).
710+ - `opts` : In addition to the | luasnip-usual | keys, there is, again,
711+ - `user_args` , which is described in | luasnip-functionnode | .
650712
651713
652- Examples:
714+ ** Examples** :
653715
654716This `dynamicNode` inserts an `insertNode` which copies the text inside the
655717first `insertNode` .
@@ -730,15 +792,16 @@ example:
730792Here the text entered into `user_text` is preserved upon changing choice.
731793
732794The constructor for the restoreNode, `r ` , takes (at most) three parameters: -
733- `jump_index` , when to jump to this node. - `key ` , the key that identifies which
734- `restoreNode` s should share their content. - `nodes` , the contents of the
735- `restoreNode` . Can either be a single node, or a table of nodes (both of which
736- will be wrapped inside a `snippetNode` , except if the single node already is a
737- `snippetNode` ). The content of a given key may be defined multiple times, but
738- if the contents differ, it’s undefined which will actually be used. If a keys
739- content is defined in a `dynamicNode` , it will not be used for `restoreNodes`
740- outside that `dynamicNode` . A way around this limitation is defining the
741- content in the `restoreNode` outside the `dynamicNode` .
795+ | luasnip-`jump_index` | , when to jump to this node. - `key ` , the key that
796+ identifies which `restoreNode` s should share their content. - `nodes` , the
797+ contents of the `restoreNode` . Can either be a single node, or a table of nodes
798+ (both of which will be wrapped inside a `snippetNode` , except if the single
799+ node already is a `snippetNode` ). The content of a given key may be defined
800+ multiple times, but if the contents differ, it’s undefined which will
801+ actually be used. If a keys content is defined in a `dynamicNode` , it will not
802+ be used for `restoreNodes` outside that `dynamicNode` . A way around this
803+ limitation is defining the content in the `restoreNode` outside the
804+ `dynamicNode` .
742805
743806The content for a key may also be defined in the `opts` -parameter of the
744807snippet-constructor, as seen in the example above. The `stored` -table accepts
0 commit comments