Skip to content

Commit

Permalink
update MatchTSNodeOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Sep 1, 2023
1 parent 79bcc0c commit 4c1e9b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lua/luasnip/extras/_extra_types.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
---@class LuaSnip.extra.MatchTSNodeOpts
---@field query string|{[1]: string, [2]: string?} query_text or (query_grous, lang)
---@field query LuaSnip.extra.QueryOpts
---@field select LuaSnip.extra.SelectTSNodeOpts|LuaSnip.extra.SelectTSNodeFunc|nil

---@class LuaSnip.extra.MatchedTSNodeInfo
---@field capture_name string
---@field node TSNode

---@class LuaSnip.extra.QueryOpts
---@field query_name? string
---@field query_text? string
---@field lang? string

---@class LuaSnip.extra.MatchTSNodeResult
---@field best_match TSNode
---@field matches LuaSnip.extra.MatchedTSNodeInfo[]
Expand Down
11 changes: 4 additions & 7 deletions lua/luasnip/extras/_treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,12 @@ function TSParser:prepare_query(opts, root)
---@type Query?
local query

if type(opts.query) == "string" then
query = vim.treesitter.query.parse(
self.buf_lang,
opts.query --[[@as string]]
)
if opts.query and opts.query.query_text then
query = vim.treesitter.query.parse(self.buf_lang, opts.query.query_text)
else
opts.query = opts.query or {}
local query_group = opts.query[1] or "luasnip"
local lang = opts.query[2] or self.buf_lang
local query_group = opts.query.query_name or "luasnip"
local lang = opts.query.lang or self.buf_lang
query = vim.treesitter.query.get(lang, query_group)
end

Expand Down
11 changes: 9 additions & 2 deletions lua/luasnip/extras/treesitter_postfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ local util = require("luasnip.util.util")
---@return LuaSnip.extra.MatchTSNodeFunc
local function generate_match_tsnode_func(opts)
if type(opts) == "string" then
opts = { query = opts }
opts = {
query = {
query_text = opts,
},
}
elseif vim.tbl_isarray(opts) then
opts = { query = { "luasnip" }, select = { captures = opts } }
opts = {
query = { query_name = "luasnip" },
select = { captures = opts },
}
end

---@param parser LuaSnip.extra.TSParser
Expand Down

0 comments on commit 4c1e9b2

Please sign in to comment.