Skip to content

Commit

Permalink
introduce select_node_func
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Aug 18, 2023
1 parent b4a35a6 commit 492580f
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lua/luasnip/extras/treesitter_postfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local func = require("luasnip.util.functions")
---@field node TSNode

---@alias LuaSnip.extra.MatchTSNodeFunc fun(parser: LuaSnip.extra.TSParser, pos: { [1]: number, [2]: number }?):LuaSnip.extra.MatchedTSNode[]?
---@alias LuaSnip.extra.SelectBestNodeFunc fun(nodes: LuaSnip.extra.MatchedTSNode[]):TSNode?

---@param capture string|string[]|LuaSnip.extra.MatchTSNodeFromCaptures
---@return LuaSnip.extra.MatchTSNodeFunc
Expand Down Expand Up @@ -116,7 +117,11 @@ local function wrap_with_reparse_context(reparse, real_resolver)
end
end

local function generate_resolve_expand_param(match_tsnode, user_resolver)
local function generate_resolve_expand_param(
match_tsnode,
select_matches,
user_resolver
)
---@param snippet any
---@param line_to_cursor any
---@param matched_trigger any
Expand Down Expand Up @@ -171,11 +176,14 @@ local function generate_resolve_expand_param(match_tsnode, user_resolver)
})
end

local best_match = select_matches(matched_nodes)
local start_row, start_col, _, _ =
vim.treesitter.get_node_range(best_match)

local ret = {
trigger = matched_trigger,
captures = captures,
clear_region = {
--- FIXME(hawtian): which should be the clear_region, if no node has been selected now
from = {
start_row,
start_col,
Expand Down Expand Up @@ -300,8 +308,15 @@ local function treesitter_postfix(context, nodes, opts)
---@type string|string[]|LuaSnip.extra.MatchTSNodeFromCaptures|LuaSnip.extra.MatchTSNodeFunc
local match_tsnode = context.matchTSNode
local user_resolve = context.resolveExpandParams
local expand_params_resolver =
generate_resolve_expand_param(match_tsnode, user_resolve)
local select_node_func = context.selectTSNode
or function(matches)
return matches[1].node
end
local expand_params_resolver = generate_resolve_expand_param(
match_tsnode,
select_node_func,
user_resolve
)
context.resolveExpandParams =
wrap_with_reparse_context(context.reparseBuffer, expand_params_resolver)

Expand Down

0 comments on commit 492580f

Please sign in to comment.