Skip to content

Commit 9867a30

Browse files
Docs/Examples on condition objects
1 parent 3838cb3 commit 9867a30

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

DOC.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,31 @@ ls.add_snippets("all", {
10641064
})
10651065
```
10661066

1067+
- `conditions.show`: Contains typical predicates/functions used as
1068+
`show`-condition. Currently this is just `line_end`
1069+
- `conditions.expand`: Contains typical predicates/functions used as
1070+
`expand`-condition. Currently this is just `line_begin`
1071+
Contains everything from `conditions.show` as well.
1072+
- `conditions`: Provides a function `make_condition(foo)` which takes a function
1073+
as argument and returns a *condition object* for which several operators are
1074+
defined:
1075+
- `c1 + c2 -> c1 or c2`
1076+
- `c1 * c2 -> c1 and c2`
1077+
- `-c1 -> not c1`
1078+
- `c1 ^ c2 -> c1 xor/!= c2`
1079+
- `c1 % c2 -> c1 xnor/== c2`: This decision may look weird but as we weren't
1080+
able to use `==`, we decided to take something that makes one scratch ones
1081+
head (and thus avoid making false assumptions).
1082+
For more details look at [this comment](https://github.com/L3MON4D3/LuaSnip/pull/612#issuecomment-1264487743).
1083+
1084+
`conditions.show`s and `conditions.expand`s members all are also condition
1085+
objects so you can work with those too.
1086+
1087+
Thus you can easily combine existing predicates. Like in
1088+
`conditions.expand.line_end + conditions.expand.line_begin` instead of doing
1089+
something like
1090+
`function(...) return conditions.expand.line_end(...) or conditions.expand.line_begin(...) end`.
1091+
10671092
<!-- panvimdoc-ignore-start -->
10681093

10691094
extras1: ![extras1](https://user-images.githubusercontent.com/25300418/184359431-50f90599-3db0-4df0-a3a9-27013e663649.gif)

Examples/snippets.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ local dl = require("luasnip.extras").dynamic_lambda
1717
local fmt = require("luasnip.extras.fmt").fmt
1818
local fmta = require("luasnip.extras.fmt").fmta
1919
local types = require("luasnip.util.types")
20-
local conds = require("luasnip.extras.expand_conditions")
20+
local conds = require("luasnip.extras.conditions")
21+
local conds_expand = require("luasnip.extras.conditions.expand")
2122

2223
-- If you're reading this file for the first time, best skip to around line 190
2324
-- where the actual snippet-definitions start.
@@ -320,16 +321,27 @@ ls.add_snippets("all", {
320321
return line_to_cursor:match("%s*//")
321322
end,
322323
}),
323-
-- there's some built-in conditions in "luasnip.extras.expand_conditions".
324+
-- there's some built-in conditions in "luasnip.extras.conditions.expand" and "luasnip.extras.conditions.show".
324325
s("cond2", {
325326
t("will only expand at the beginning of the line"),
326327
}, {
327-
condition = conds.line_begin,
328+
condition = conds_expand.line_begin,
328329
}),
329330
s("cond3", {
330331
t("will only expand at the end of the line"),
331332
}, {
332-
condition = conds.line_end,
333+
condition = conds_expand.line_end,
334+
}),
335+
-- on conditions some logic operators are defined
336+
s("cond4", {
337+
t("will only expand at the end and the start of the line"),
338+
}, {
339+
-- last function is just an example how to make own function objects and apply operators on them
340+
condition = conds_expand.line_end
341+
+ conds_expand.line_begin
342+
* conds.make_condition(function()
343+
return true
344+
end),
333345
}),
334346
-- The last entry of args passed to the user-function is the surrounding snippet.
335347
s(

doc/luasnip.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*luasnip.txt* For NVIM v0.5.0 Last change: 2022 September 28
1+
*luasnip.txt* For NVIM v0.5.0 Last change: 2022 October 08
22

33
==============================================================================
44
Table of Contents *luasnip-table-of-contents*
@@ -1049,6 +1049,31 @@ is only a short outline, their usage is shown more expansively in
10491049
<
10501050

10511051

1052+
1053+
- `conditions.show`: Contains typical predicats/functions used as
1054+
`show`-condition. Currently this is just `line_end`
1055+
- `conditions.expand`: Contains typical predicats/functions used as
1056+
`expand`-condition. Currently this is just `line_begin` Contains everything
1057+
from `conditions.show` as well.
1058+
- `conditions`: Provides a function `make_condition(foo)` which takes a function
1059+
as argument and returns a _condition object_ for which several operators are
1060+
defined:
1061+
- `c1 + c2 -> c1 or c2`
1062+
- `c1 * c2 -> c1 and c2`
1063+
- `-c1 -> not c1`
1064+
- `c1 ^ c2 -> c1 xor/!= c2`
1065+
- `c1 % c2 -> c1 xnor/== c2`: This decision may look weird but as we weren’t
1066+
able to use `==`, we decided to take something that makes one scratch ones
1067+
head (and thus avoid making false assumptions).
1068+
For more details look at this comment <https://github.com/L3MON4D3/LuaSnip/pull/612#issuecomment-1264487743>.
1069+
`conditions.show`s and `conditions.expand`s members all are also condition
1070+
objects so you can work with those too.
1071+
Thus you can easily combine existing predicats. Like in
1072+
`conditions.expand.line_end + conditions.expand.line_begin` instead of doing
1073+
something like `function(...) return conditions.expand.line_end(...) or
1074+
conditions.expand.line_begin(...) end`.
1075+
1076+
10521077
FMT *luasnip-fmt*
10531078

10541079
`require("luasnip.extras.fmt").fmt` can be used to create snippets in a more

0 commit comments

Comments
 (0)