From fadb17b1e46d1c73c03d9d88c08a77ec526952d4 Mon Sep 17 00:00:00 2001 From: Munif Tanjim Date: Fri, 26 May 2023 13:18:50 +0600 Subject: [PATCH 1/3] feat: add default condition for postfix --- lua/luasnip/extras/postfix.lua | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lua/luasnip/extras/postfix.lua b/lua/luasnip/extras/postfix.lua index a3bbef77f..d9b35ddc0 100644 --- a/lua/luasnip/extras/postfix.lua +++ b/lua/luasnip/extras/postfix.lua @@ -55,6 +55,18 @@ local function generate_opts(match_pattern, user_callback) } end +local function wrap_condition(user_condition, match_pattern) + if not user_condition then + user_condition = require("luasnip.util.util").yes + end + + return function(line_to_cursor, matched_trigger, captures) + return line_to_cursor:sub(1, -1 - #matched_trigger):match(match_pattern) + ~= nil + and user_condition(line_to_cursor, matched_trigger, captures) + end +end + local function postfix(context, nodes, opts) opts = opts or {} local user_callback = vim.tbl_get(opts, "callbacks", -1, events.pre_expand) @@ -65,17 +77,11 @@ local function postfix(context, nodes, opts) user_callback = { user_callback, { "nil", "function" } }, }) - local match_pattern - if type(context) == "string" then - context = { - trig = context, - } - match_pattern = matches.default - else - match_pattern = context.match_pattern or matches.default - end + context = node_util.wrap_context(context) + context.wordTrig = false + local match_pattern = context.match_pattern or matches.default + context.condition = wrap_condition(context.condition, match_pattern) - context = vim.tbl_deep_extend("keep", context, { wordTrig = false }) opts = vim.tbl_deep_extend( "force", opts, From 60cba6b92ccc24dc91649d71c835bb9b2807d776 Mon Sep 17 00:00:00 2001 From: Munif Tanjim Date: Fri, 26 May 2023 13:21:05 +0600 Subject: [PATCH 2/3] docs: cleanup misleading docs for postfix --- DOC.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DOC.md b/DOC.md index af0a27d4b..07637882d 100644 --- a/DOC.md +++ b/DOC.md @@ -1652,8 +1652,7 @@ The optional third argument is the same as the third (`opts`) argument to the The postfix snippet works using a callback on the pre_expand event of the snippet. If you pass a callback on the pre_expand event (structure example -below) it will get run after the builtin callback. This means that your -callback will have access to the `POSTFIX_MATCH` field as well. +below) it will get run after the builtin callback. ```lua { From 158c6b199217f5d54a0ddad580c75d3d71126c65 Mon Sep 17 00:00:00 2001 From: MunifTanjim Date: Fri, 26 May 2023 08:19:11 +0000 Subject: [PATCH 3/3] Auto generate docs --- doc/luasnip.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/luasnip.txt b/doc/luasnip.txt index 251c0d093..f8723c903 100644 --- a/doc/luasnip.txt +++ b/doc/luasnip.txt @@ -1,4 +1,4 @@ -*luasnip.txt* For NVIM v0.8.0 Last change: 2023 May 25 +*luasnip.txt* For NVIM v0.8.0 Last change: 2023 May 26 ============================================================================== Table of Contents *luasnip-table-of-contents* @@ -1562,8 +1562,7 @@ The optional third argument is the same as the third (`opts`) argument to the The postfix snippet works using a callback on the pre_expand event of the snippet. If you pass a callback on the pre_expand event (structure example -below) it will get run after the builtin callback. This means that your -callback will have access to the `POSTFIX_MATCH` field as well. +below) it will get run after the builtin callback. >lua {