Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Aug 21, 2023
1 parent da4410a commit 7aafe6d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
24 changes: 13 additions & 11 deletions lua/luasnip/extras/_treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ local builtin_tsnode_selectors = {
any = function(captures)
local best_match
return {
match = function(node)
if captures and not captures[node:type()] then
match = function(capture_name, node)
if captures and not captures[capture_name] then
return false
end
if best_match == nil then
Expand All @@ -142,8 +142,8 @@ local builtin_tsnode_selectors = {
local best_match
local best_match_start
return {
match = function(node)
if captures and not captures[node:type()] then
match = function(capture_name, node)
if captures and not captures[capture_name] then
return false
end

Expand Down Expand Up @@ -171,8 +171,8 @@ local builtin_tsnode_selectors = {
local best_match
local best_match_start
return {
match = function(node)
if captures and not captures[node:type()] then
match = function(capture_name, node)
if captures and not captures[capture_name] then
return false
end

Expand All @@ -199,7 +199,7 @@ local builtin_tsnode_selectors = {
}

---@param opts LuaSnip.extra.SelectTSNodeOpts?
---@return { match: (fun(node:TSNode):boolean), get_best_match: fun():TSNode? }
---@return { match: (fun(cap_name: string, node:TSNode):boolean), get_best_match: fun():TSNode? }
local function generate_select_tsnode_func(opts)
---@type LuaSnip.extra.SelectTSNodeOpts
opts = vim.F.if_nil(opts, {})
Expand All @@ -210,7 +210,7 @@ local function generate_select_tsnode_func(opts)
if selector == nil then
error("Unknown select_capture: " .. select_capture)
end
return selector.new_state(captures)
return selector(captures)
end

---@class LuaSnip.extra.TSParser
Expand Down Expand Up @@ -292,7 +292,8 @@ function TSParser:prepare_query(opts, root)
opts.query --[[@as string]]
)
else
local query_group = opts.query[1]
opts.query = opts.query or {}
local query_group = opts.query[1] or "luasnip"
local lang = opts.query[2] or self.buf_lang
query = vim.treesitter.query.get(lang, query_group)
end
Expand Down Expand Up @@ -346,8 +347,8 @@ function TSParser:captures_at_pos(captures, pos)
local capture_name = query.captures[id]

if
(vim.treesitter.is_in_node_range(match.node, pos[1], pos[2]))
and (selector == nil or selector.match(node))
(vim.treesitter.is_in_node_range(node, pos[1], pos[2]))
and (selector == nil or selector.match(capture_name, node))
then
results[#results + 1] = {
capture_name = capture_name,
Expand All @@ -364,6 +365,7 @@ function TSParser:captures_at_pos(captures, pos)
local select_best_match = captures --[[@as LuaSnip.extra.SelectTSNodeFunc]]
best_match = select_best_match(results)
end

if best_match ~= nil then
return {
best_match = best_match,
Expand Down
10 changes: 7 additions & 3 deletions lua/luasnip/extras/treesitter_postfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ local function generate_resolve_expand_param(match_tsnode, user_resolver)
})
end

local start_row, start_col, _, _ =
vim.treesitter.get_node_range(match_result.best_match)
local start_row, start_col, best_match_text =
ts_parser(match_result.best_match, cursor, matched_trigger)

local ret = {
trigger = matched_trigger,
Expand All @@ -174,6 +174,11 @@ local function generate_resolve_expand_param(match_tsnode, user_resolver)
},
env_override = {
TREESITTER_MATCHES = matches,
TREESITTER_BEST_MATCH = {
start = { start_col, start_col },
text = best_match_text,
type = match_result.best_match:type(),
},
},
}

Expand Down Expand Up @@ -279,7 +284,6 @@ local function treesitter_postfix(context, nodes, opts)
context = { context, { "string", "table" } },
nodes = { nodes, "table" },
opts = { opts, "table" },
match_tsnode = { opts.match_tsnode, "table" },
})

context = node_util.wrap_context(context)
Expand Down

0 comments on commit 7aafe6d

Please sign in to comment.