diff options
Diffstat (limited to 'lua/nvim-treesitter/tsrange.lua')
| -rw-r--r-- | lua/nvim-treesitter/tsrange.lua | 106 |
1 files changed, 57 insertions, 49 deletions
diff --git a/lua/nvim-treesitter/tsrange.lua b/lua/nvim-treesitter/tsrange.lua index f9de5f3d..767cab77 100644 --- a/lua/nvim-treesitter/tsrange.lua +++ b/lua/nvim-treesitter/tsrange.lua @@ -3,75 +3,72 @@ local TSRange = {} TSRange.__index = TSRange local api = vim.api -local ts_utils = require'nvim-treesitter.ts_utils' +local ts_utils = require "nvim-treesitter.ts_utils" local function get_byte_offset(buf, row, col) - return api.nvim_buf_get_offset(buf, row) - + vim.fn.byteidx(api.nvim_buf_get_lines(buf, row, row + 1, false), col) + return api.nvim_buf_get_offset(buf, row) + vim.fn.byteidx(api.nvim_buf_get_lines(buf, row, row + 1, false), col) end function TSRange.new(buf, start_row, start_col, end_row, end_col) - return setmetatable( - { - start_pos = {start_row, start_col, get_byte_offset(buf, start_row, start_col)}, - end_pos = {end_row, end_col, get_byte_offset(buf, end_row, end_col)}, - buf = buf, - [1] = start_row, - [2] = start_col, - [3] = end_row, - [4] = end_col, - }, - TSRange) + return setmetatable({ + start_pos = { start_row, start_col, get_byte_offset(buf, start_row, start_col) }, + end_pos = { end_row, end_col, get_byte_offset(buf, end_row, end_col) }, + buf = buf, + [1] = start_row, + [2] = start_col, + [3] = end_row, + [4] = end_col, + }, TSRange) end function TSRange.from_nodes(buf, start_node, end_node) TSRange.__index = TSRange - local start_pos = start_node and {start_node:start()} or {end_node:start()} - local end_pos = end_node and {end_node:end_()} or {start_node:end_()} - return setmetatable( - { - start_pos = {start_pos[1], start_pos[2], start_pos[3]}, - end_pos = {end_pos[1], end_pos[2], end_pos[3]}, - buf = buf, - [1] = start_pos[1], - [2] = start_pos[2], - [3] = end_pos[1], - [4] = end_pos[2], - }, - TSRange) + local start_pos = start_node and { start_node:start() } or { end_node:start() } + local end_pos = end_node and { end_node:end_() } or { start_node:end_() } + return setmetatable({ + start_pos = { start_pos[1], start_pos[2], start_pos[3] }, + end_pos = { end_pos[1], end_pos[2], end_pos[3] }, + buf = buf, + [1] = start_pos[1], + [2] = start_pos[2], + [3] = end_pos[1], + [4] = end_pos[2], + }, TSRange) end function TSRange.from_table(buf, range) - return setmetatable( - { - start_pos = {range[1], range[2], get_byte_offset(buf, range[1], range[2])}, - end_pos = {range[3], range[4], get_byte_offset(buf, range[3], range[4])}, - buf = buf, - [1] = range[1], - [2] = range[2], - [3] = range[3], - [4] = range[4], - }, - TSRange) + return setmetatable({ + start_pos = { range[1], range[2], get_byte_offset(buf, range[1], range[2]) }, + end_pos = { range[3], range[4], get_byte_offset(buf, range[3], range[4]) }, + buf = buf, + [1] = range[1], + [2] = range[2], + [3] = range[3], + [4] = range[4], + }, TSRange) end function TSRange:parent() local root = ts_utils.get_root_for_position(self[1], self[2]) - return root - and root:named_descendant_for_range(self.start_pos[1], self.start_pos[2], self.end_pos[1], self.end_pos[2]) - or nil + return root and root:named_descendant_for_range( + self.start_pos[1], + self.start_pos[2], + self.end_pos[1], + self.end_pos[2] + ) or nil end -function TSRange:field() -end +function TSRange:field() end function TSRange:child_count() return #self:collect_children() end function TSRange:named_child_count() - return #self:collect_children(function(c) return c:named() end) + return #self:collect_children(function(c) + return c:named() + end) end function TSRange:iter_children() @@ -79,7 +76,9 @@ function TSRange:iter_children() return function() while true do local node = raw_iterator() - if not node then return end + if not node then + return + end local _, _, start_byte = node:start() local _, _, end_byte = node:end_() if start_byte >= self.start_pos[3] and end_byte <= self.end_pos[3] then @@ -104,7 +103,9 @@ function TSRange:child(index) end function TSRange:named_child(index) - return self:collect_children(function(c) return c.named() end)[index + 1] + return self:collect_children(function(c) + return c.named() + end)[index + 1] end function TSRange:start() @@ -120,7 +121,7 @@ function TSRange:range() end function TSRange:type() - return 'nvim-treesitter-range' + return "nvim-treesitter-range" end function TSRange:symbol() @@ -136,11 +137,18 @@ function TSRange:missing() end function TSRange:has_error() - return #self:collect_children(function(c) return c:has_error() end) > 0 and true or false + return #self:collect_children(function(c) + return c:has_error() + end) > 0 and true or false end function TSRange:sexpr() - return table.concat(vim.tbl_map(function(c) return c:sexpr() end, self:collect_children()), ' ') + return table.concat( + vim.tbl_map(function(c) + return c:sexpr() + end, self:collect_children()), + " " + ) end M.TSRange = TSRange |
