@@ -52,6 +52,57 @@ conds.is_endwise_node = function(nodes)
5252 end
5353end
5454
55+conds.is_in_range = function(callback, position)
56+ assert(
57+ type(callback) == 'function' and type(position) == 'function',
58+ 'callback and position should be a function'
59+ )
60+ return function(opts)
61+ log.debug('is_in_range')
62+ if not parsers.has_parser() then
63+ return
64+ end
65+ local cursor = position()
66+ assert(
67+ type(cursor) == 'table' and #cursor == 2,
68+ 'position should be return a table like {line, col}'
69+ )
70+ local line = cursor[1]
71+ local col = cursor[2]
72+
73+ local bufnr = 0
74+ local root_lang_tree = parsers.get_parser(bufnr)
75+ local lang_tree = root_lang_tree:language_for_range({ line, col, line, col })
76+
77+ local result
78+
79+ for _, tree in ipairs(lang_tree:trees()) do
80+ local root = tree:root()
81+ if root and ts_utils.is_in_node_range(root, line, col) then
82+ local node = root:named_descendant_for_range(line, col, line, col)
83+ local anonymous_node = root:descendant_for_range(
84+ line,
85+ col,
86+ line,
87+ col
88+ )
89+
90+ result = {
91+ node = node,
92+ lang = lang_tree:lang(),
93+ type = node:type(),
94+ cursor = vim.api.nvim_win_get_cursor(0),
95+ line = vim.api.nvim_buf_get_lines(bufnr, line, line + 1, true)[1],
96+ range = { node:range() },
97+ anonymous = anonymous_node:type(),
98+ }
99+ end
100+ end
101+
102+ return callback(result)
103+ end
104+end
105+
55106conds.is_ts_node = function(nodes)
56107 if type(nodes) == 'string' then nodes = {nodes} end
57108 assert(nodes ~= nil, "ts nodes should be string or table")