tsql.nvim/lua/tsql/token_select.lua

60 lines
2.1 KiB
Lua
Raw Normal View History

2023-05-30 07:29:49 +00:00
---@module 'tsql.token_select'
---@class mod_token_select
2023-05-30 04:39:15 +00:00
local M = {}
2023-05-30 07:29:49 +00:00
---@class mod_ts_query
M.ts_query = {}
---@class TSQuery
---@field query string the passthru for treesitter language parser
M.TSQuery = {}
2023-05-30 04:39:15 +00:00
---@class FileLoc
2023-05-30 07:29:49 +00:00
---@field row_0 number 0-index row location
---@field col_0 number 0-index col location
M.FileLoc = {}
M.FileLoc.__index = M.FileLoc
2023-05-30 04:39:15 +00:00
---@class QNode
2023-05-30 07:29:49 +00:00
---@field start FileLoc
---@field end_ex_col FileLoc
---@field buf QBuf
2023-05-30 04:39:15 +00:00
M.QNode = {}
2023-05-30 07:29:49 +00:00
M.QNode.__index = M.QNode
2023-05-30 04:39:15 +00:00
2023-05-30 07:29:49 +00:00
---@param treesitter_query string the passthru for treesitter language
---parser
2023-05-30 04:39:15 +00:00
---@return TSQuery
2023-05-30 07:29:49 +00:00
function M.ts_query.from_scm(treesitter_query)
return {
query = treesitter_query
}
2023-05-30 04:39:15 +00:00
end
2023-05-30 07:29:49 +00:00
---@param files QBuf[]
---@return QNode[]
function M.TSQuery:find_nodes(files)
local result = {}
for _, file in ipairs(files) do
local parser = vim.treesitter.get_parser(file.bufnr, file.filetype)
local tree = parser:parse()[1]
local root = tree:root()
---@type Query
2023-06-01 15:20:36 +00:00
local query = vim.treesitter.query.parse_query(file.lang, self.query)
2023-05-30 07:29:49 +00:00
for _, match, _ in query:iter_matches(root, file.bufnr, 0, -1) do
for id, node in pairs(match) do
local start_row, start_col, end_row, end_col = node:range(false)
local start = { row_0 = start_row, col_0 = start_col }
2023-06-01 15:20:36 +00:00
-- CORRECTNESS:
2023-05-30 07:29:49 +00:00
-- :lua local parser = vim.treesitter.get_parser(0, 'lua'); local tree = parser:parse()[1]; local query = vim.treesitter.parse_query('lua', '(identifier) @name'); for id, node in query:iter_captures(tree:root(), 0) do local name = query.captures[id]; if name == 'name' and vim.treesitter.get_node_text(node, 0) == 'TSQuery' then local sr, sc, er, ec = node:range(); print(string.format("TSQuery Start: (%d, %d), End: (%d, %d)", sr, sc, er, ec)); end; end
local end_ex_col = { row_0 = end_row, col_0 = end_col }
local qnode = { buf = file, start = start, end_ex_col = end_ex_col }
setmetatable(qnode, M.QNode)
table.insert(result, qnode)
end
end
end
return result
end
2023-05-30 04:39:15 +00:00
2023-05-30 07:29:49 +00:00
return M