Skip to content

Commit

Permalink
implement postfix using resolveExpandParams.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy authored and L3MON4D3 committed Aug 13, 2023
1 parent 7b3e865 commit d9c8310
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 163 deletions.
93 changes: 34 additions & 59 deletions lua/luasnip/extras/postfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,47 @@ local snip = require("luasnip.nodes.snippet").S
local events = require("luasnip.util.events")
local extend_decorator = require("luasnip.util.extend_decorator")
local node_util = require("luasnip.nodes.util")
local util = require("luasnip.util.util")

local matches = {
default = [[[%w%.%_%-%"%']+$]],
line = "^.+$",
}

local function generate_opts(match_pattern, user_callback)
return {
callbacks = {
[-1] = {
[events.pre_expand] = function(snippet, event_args)
local pos = event_args.expand_pos
-- [1]: returns table, gets text end-exclusive.
local line_to_cursor = vim.api.nvim_buf_get_text(
0,
pos[1],
0,
pos[1],
pos[2],
{}
)[1]
local postfix_match = line_to_cursor:match(match_pattern)
or ""
-- clear postfix_match-text.
vim.api.nvim_buf_set_text(
0,
pos[1],
pos[2] - #postfix_match,
pos[1],
pos[2],
{ "" }
)
local user_env = {}
if user_callback then
user_env = user_callback(snippet, event_args) or {}
end
local postfix_env_override = {
env_override = {
POSTFIX_MATCH = postfix_match,
},
}
local function wrap_resolve_expand_params(match_pattern, user_resolve)
return function(snippet, line_to_cursor, match, captures)
if line_to_cursor:sub(1, -1 - #match):match(match_pattern) == nil then
return nil
end

return vim.tbl_deep_extend(
"keep",
user_env,
postfix_env_override
)
end,
local pos = util.get_cursor_0ind()
local line_to_cursor_except_match =
line_to_cursor:sub(1, #line_to_cursor - #match)
local postfix_match = line_to_cursor_except_match:match(match_pattern)
or ""
local res = {
clear_region = {
from = { pos[1], pos[2] - #postfix_match - #match },
to = pos,
},
},
}
end

local function wrap_condition(user_condition, match_pattern)
if not user_condition then
user_condition = require("luasnip.util.util").yes
end
env_override = {
POSTFIX_MATCH = postfix_match,
},
}

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)
if user_resolve then
local user_res =
user_resolve(snippet, line_to_cursor, match, captures)
if user_res then
res.trigger = user_res.trigger
res.captures = user_res.captures
res.clear_region = user_res.clear_region or res.clear_region
res.env_override = vim.tbl_extend("force", res.env_override, user_res.env_override or {})
else
return nil
end
end
return res
end
end

Expand All @@ -80,13 +59,9 @@ local function postfix(context, nodes, opts)
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.resolveExpandParams =
wrap_resolve_expand_params(match_pattern, context.resolveExpandParams)

opts = vim.tbl_deep_extend(
"force",
opts,
generate_opts(match_pattern, user_callback)
)
return snip(context, nodes, opts)
end
extend_decorator.register(
Expand Down
Loading

0 comments on commit d9c8310

Please sign in to comment.