Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default condition for postfix #898

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 2 additions & 3 deletions doc/luasnip.txt
Original file line number Diff line number Diff line change
@@ -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*
Expand Down Expand Up @@ -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
{
Expand Down
26 changes: 16 additions & 10 deletions lua/luasnip/extras/postfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down