Skip to content

Commit fe19556

Browse files
committed
improve extra-doc.
1 parent 9bfd039 commit fe19556

File tree

1 file changed

+147
-83
lines changed

1 file changed

+147
-83
lines changed

DOC.md

Lines changed: 147 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -959,114 +959,178 @@ ai[1][2][3] == ai(1, 2, 3) == ai{1, 2, 3}
959959

960960
# EXTRAS
961961

962-
The module `"luasnip.extras"` contains nodes that ease writing snippets (This
963-
is only a short outline, their usage is shown more expansively in
964-
`Examples/snippets.lua`):
965-
966-
- `lambda`: A shortcut for `functionNode`s that only do very basic string-
967-
manipulation.
968-
For example, to generate text where all occurences of "a" in the text of the
969-
node with jump-index `n` are replaced with "e", one could use
970-
`lambda(lambda._1:gsub("a", "e"), n)` (signature is similar to that of
971-
`functionNode`).
972-
`lambda._n` returns the text of the `n`th argnode (`args[n]`, in the
973-
functionNode-function). If this text has multiple lines, they are concatenated
974-
with "\n".
975-
976-
- `match`: Can insert text based on a predicate (shorthand for `functionNode`s).
977-
The complete signature for the node is `match(argnodes, condition, then, else)`, where
978-
* `argnodes` can be specified as in `functionNode`,
979-
* `condition` may be a
980-
* string: interpreted as a lua-pattern. Matched on the `\n`-joined (in case
962+
## Lambda
963+
A shortcut for `functionNode`s that only do very basic string-
964+
manipulation.
965+
`l(lambda, argnodes)`:
966+
- `lambda`: An object created by applying string-operations to `l._n`, objects
967+
representing the `n`th argnode.
968+
For example:
969+
- `l._1:gsub("a", "e")` replaces all occurences of "a" in the text of the
970+
first argnode with "e", or
971+
- `l._1 .. l._2` concats text of the first and second argnode.
972+
If an argnode contains multiple lines of text, they are concatenated with
973+
`"\n"` prior to any operation.
974+
- `argnodes`, [`node-references`](#node_reference), just like in function- and
975+
dynamicNode.
976+
977+
There are many examples for `lamda` in `Examples/snippets.lua`
978+
979+
## Match
980+
`match` can insert text based on a predicate (again, a shorthand for `functionNode`).
981+
982+
`match(argnodes, condition, then, else)`, where
983+
* `argnode`: A single [`node-reference`](#node_reference). May not be nil, or
984+
a table.
985+
* `condition` may be either of
986+
* `string`: interpreted as a lua-pattern. Matched on the `\n`-joined (in case
981987
it's multiline) text of the first argnode (`args[1]:match(condition)`).
982-
* function: `fn(args, snip) -> bool`: takes the same parameters as the
988+
* `function`: `fn(args, snip) -> bool`: takes the same parameters as the
983989
`functionNode`-function, any value other than nil or false is interpreted
984990
as a match.
985-
* lambda: `l._n` is the `\n`-joined text of the nth argnode.
986-
Useful if string-manipulations have to be performed before the string is matched.
987-
988-
* `then` is inserted if the condition matches, `else` if it doesn't. They can
989-
both be either text, lambda or function (with the same parameters as
990-
specified above).
991-
If `then` is not given, the `then`-value depends on what was specified as the
992-
`condition`:
993-
* pattern: Simply the return value from the `match`, e.g. the entire match,
994-
or, if there were capture groups, the first capture group.
995-
* function: the return value of the function if it is either a string, or a
996-
table (if there is no `then`, the function cannot return a table containing
997-
something other than strings).
998-
* lambda: Simply the first value returned by the lambda.
999-
1000-
Examples:
1001-
* `match(n, "^ABC$", "A")` inserts "A" if the node with jump-index `n` matches
1002-
"ABC" exactly, nothing otherwise.
1003-
* `match(n, lambda._1:match(lambda._1:reverse()), "PALINDROME")` inserts
1004-
"PALINDROME" if the node with jump-index `n`th palindrome.
991+
* `lambda`: `l._n` is the `\n`-joined text of the nth argnode.
992+
Useful if string-manipulations have to be performed before the string is matched.
993+
Should end with `match`, but any other truthy result will be interpreted
994+
as matching.
995+
996+
* `then` is inserted if the condition matches,
997+
* `else` if it does not.
998+
999+
Both `then` and `else` can be either text, lambda or function (with the same parameters as
1000+
specified above).
1001+
`then`'s default-value depends on the `condition`:
1002+
* `pattern`: Simply the return value from the `match`, e.g. the entire match,
1003+
or, if there were capture groups, the first capture group.
1004+
* `function`: the return value of the function if it is either a string, or a
1005+
table (if there is no `then`, the function cannot return a table containing
1006+
something other than strings).
1007+
* `lambda`: Simply the first value returned by the lambda.
1008+
1009+
Examples:
1010+
* `match(n, "^ABC$", "A")` .
1011+
* `match(n, lambda._1:match(lambda._1:reverse()), "PALINDROME")`
10051012

1006-
```lua
1007-
s("trig", {
1008-
i(1), t":",
1009-
i(2), t"::",
1010-
m({1, 2}, l._1:match("^"..l._2.."$"), l._1:gsub("a", "e"))
1011-
})
1012-
```
1013-
This inserts the text of the node with jump-index 1, with all occurences of
1014-
`a` replaced with `e` if the second insertNode matches the first exactly.
1015-
1016-
- `rep`: repeats the node with the passed index.
1017-
For example `rep(1)` to repeat the content of the node at jump-index 1.
1013+
```lua
1014+
s("trig", {
1015+
i(1), t":",
1016+
i(2), t"::",
1017+
m({1, 2}, l._1:match("^"..l._2.."$"), l._1:gsub("a", "e"))
1018+
})
1019+
```
10181020

1019-
- `partial`: directly inserts the output of a function.
1020-
Useful for e.g. `partial(os.date, "%Y")` (all arguments passed after the
1021-
function are passed to it).
10221021

1023-
- `nonempty`: inserts text if the node at the given jump-index doesn't contain
1024-
any text.
1025-
`nonempty(n, "not empty!", "empty!")` inserts "empty!" if the node
1026-
at jump-index n is empty, "not empty!" if it isn't.
1022+
* ```lua
1023+
s("extras1", {
1024+
i(1), t { "", "" }, m(1, "^ABC$", "A")
1025+
}),
1026+
```
1027+
Inserts "A" if the node with jump-index `n` matches "ABC" exactly, nothing otherwise.
10271028

1028-
- `dynamic_lambda`: Operates almost exactly like `lambda`, only that it can be
1029-
jumped to, and its' contents therefore easily overridden (this is basically a
1030-
dynamicNode generating a single insertNode).
1031-
`dynamic_lambda(2, lambda._1..lambda._1, 1)` will first contain the content of
1032-
the node at jump-index 1 appended to itself, but the second jump (jump-index
1033-
is 2) will lead to it, making it easy to override the generated text. The text
1034-
will only be changed when an argnode updates it.
1029+
<!-- panvimdoc-ignore-start -->
1030+
1031+
![extras1](https://user-images.githubusercontent.com/25300418/184359431-50f90599-3db0-4df0-a3a9-27013e663649.gif)
1032+
1033+
<!-- panvimdoc-ignore-end -->
10351034

1036-
```lua
1037-
ls.add_snippets("all", {
1038-
s("extras1", {
1039-
i(1), t { "", "" }, m(1, "^ABC$", "A")
1040-
}),
1035+
* ```lua
10411036
s("extras2", {
10421037
i(1, "INPUT"), t { "", "" }, m(1, l._1:match(l._1:reverse()), "PALINDROME")
10431038
}),
1039+
```
1040+
Inserts `"PALINDROME"` if i(1) contains a palindrome.
1041+
1042+
<!-- panvimdoc-ignore-start -->
1043+
1044+
![extras2](https://user-images.githubusercontent.com/25300418/184359435-21e4de9f-c56b-4ee1-bff4-331b68e1c537.gif)
1045+
1046+
<!-- panvimdoc-ignore-end -->
1047+
* ```lua
10441048
s("extras3", {
10451049
i(1), t { "", "" }, i(2), t { "", "" },
10461050
m({ 1, 2 }, l._1:match("^" .. l._2 .. "$"), l._1:gsub("a", "e"))
10471051
}),
1048-
s("extras4", { i(1), t { "", "" }, extras.rep(1) }),
1049-
s("extras5", { extras.partial(os.date, "%Y") }),
1050-
s("extras6", { i(1, ""), t { "", "" }, extras.nonempty(1, "not empty!", "empty!") }),
1051-
s("extras7", { i(1), t { "", "" }, extras.dynamic_lambda(2, l._1 .. l._1, 1) }),
1052-
})
1052+
```
1053+
This inserts the text of the node with jump-index 1, with all occurences of
1054+
`a` replaced with `e`, if the second insertNode matches the first exactly.
1055+
1056+
<!-- panvimdoc-ignore-start -->
1057+
1058+
![extras3](https://user-images.githubusercontent.com/25300418/184359436-515ca1cc-207f-400d-98ba-39fa166e22e4.gif)
1059+
1060+
<!-- panvimdoc-ignore-end -->
1061+
1062+
## Repeat
1063+
1064+
Inserts the text of the passed node.
1065+
1066+
`rep(node_reference)`
1067+
- `node_reference`, a single [`node-reference`](#node_reference).
1068+
1069+
```lua
1070+
s("extras4", { i(1), t { "", "" }, extras.rep(1) }),
10531071
```
10541072

10551073
<!-- panvimdoc-ignore-start -->
10561074

1057-
extras1: ![extras1](https://user-images.githubusercontent.com/25300418/184359431-50f90599-3db0-4df0-a3a9-27013e663649.gif)
1075+
![extras4](https://user-images.githubusercontent.com/25300418/184359193-6525d60d-8fd8-4fbd-9d3f-e3e7d5a0259f.gif)
1076+
1077+
<!-- panvimdoc-ignore-end -->
1078+
1079+
## Partial
1080+
1081+
Evaluates a function on expand and inserts its' value.
1082+
1083+
`partial(fn, params...)`
1084+
- `fn`: any function
1085+
- `params`: varargs, any, will be passed to `fn`.
10581086

1059-
extras2: ![extras2](https://user-images.githubusercontent.com/25300418/184359435-21e4de9f-c56b-4ee1-bff4-331b68e1c537.gif)
1087+
For example `partial(os.date, "%Y")` inserts the current year on expansion.
10601088

1061-
extras3: ![extras3](https://user-images.githubusercontent.com/25300418/184359436-515ca1cc-207f-400d-98ba-39fa166e22e4.gif)
10621089

1063-
extras4: ![extras4](https://user-images.githubusercontent.com/25300418/184359193-6525d60d-8fd8-4fbd-9d3f-e3e7d5a0259f.gif)
1090+
```lua
1091+
s("extras5", { extras.partial(os.date, "%Y") }),
1092+
```
1093+
1094+
<!-- panvimdoc-ignore-start -->
10641095

1065-
extras5: ![extras5](https://user-images.githubusercontent.com/25300418/184359206-6c25fc3b-69e1-4529-9ebf-cb92148f3597.gif)
1096+
![extras5](https://user-images.githubusercontent.com/25300418/184359206-6c25fc3b-69e1-4529-9ebf-cb92148f3597.gif)
10661097

1067-
extras6: ![extras6](https://user-images.githubusercontent.com/25300418/184359213-79a71d1e-079c-454d-a092-c231ac5a98f9.gif)
1098+
<!-- panvimdoc-ignore-end -->
1099+
1100+
## Nonempty
1101+
Inserts text if the referenced node doesn't contain any text.
1102+
`nonempty(node_reference, not_empty, empty)`
1103+
- `node_reference`, a single [node-reference](#node_reference).
1104+
- `not_empty`, `string`: inserted if the node is not empty.
1105+
- `empty`, `string`: inserted if the node is empty.
1106+
1107+
```lua
1108+
s("extras6", { i(1, ""), t { "", "" }, extras.nonempty(1, "not empty!", "empty!") }),
1109+
```
1110+
1111+
<!-- panvimdoc-ignore-start -->
1112+
1113+
![extras6](https://user-images.githubusercontent.com/25300418/184359213-79a71d1e-079c-454d-a092-c231ac5a98f9.gif)
1114+
1115+
<!-- panvimdoc-ignore-end -->
1116+
1117+
## Dynamic Lambda
1118+
1119+
Pretty much the same as lambda, it just inserts the resulting text as an
1120+
insertNode, and, as such, it can be quickly overridden.
1121+
1122+
`dynamic_lambda(jump_indx, lambda, node_references)`
1123+
- `jump_indx`, as usual, the jump-indx.
1124+
1125+
The remaining arguments carry over from lambda.
1126+
1127+
```lua
1128+
s("extras7", { i(1), t { "", "" }, extras.dynamic_lambda(2, l._1 .. l._1, 1) }),
1129+
```
1130+
1131+
<!-- panvimdoc-ignore-start -->
10681132

1069-
extras7: ![extras7](https://user-images.githubusercontent.com/25300418/184359221-1f090895-bc59-44b0-a984-703bf8d278a3.gif)
1133+
![extras7](https://user-images.githubusercontent.com/25300418/184359221-1f090895-bc59-44b0-a984-703bf8d278a3.gif)
10701134

10711135
<!-- panvimdoc-ignore-end -->
10721136

0 commit comments

Comments
 (0)