summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/tsrange.lua
blob: b215189f1900849c1f9b7169a2cd5a323608c900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
local M = {}
local TSRange = {}
TSRange.__index = TSRange

local api = vim.api
local parsers = require'nvim-treesitter.parsers'

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)
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)
end

function TSRange.from_nodes(buf, start_node, end_node)
  TSRange.__index = TSRange
  local start_pos = {start_node:start()}
  local end_pos = {end_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)
end

function TSRange:parent(range)
  local parser = parsers.get_parser(self.buf, parsers.get_buf_lang(range))
  local root = parser:parse():root()
  return root:named_descendant_for_range(self.start_pos[1], self.start_pos[2], self.end_pos[1], self.end_pos[2])
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)
end

function TSRange:iter_children()
  local raw_iterator = self:parent().iter_children()
  return function()
    while true do
      local node = raw_iterator()
      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
        return node
      end
    end
  end
end

function TSRange:collect_children(filter_fun)
  local children = {}
  for _, c in self:iter_children() do
    if not filter_fun or filter_fun(c) then
      table.insert(children, c)
    end
  end
  return children
end

function TSRange:child(index)
  return self:collect_children()[index + 1]
end

function TSRange:named_child(index)
  return self:collect_children(function(c) return c.named() end)[index + 1]
end

function TSRange:start()
  return unpack(self.start_pos)
end

function TSRange:end_()
  return unpack(self.end_pos)
end

function TSRange:range()
  return self.start_pos[1], self.start_pos[2], self.end_pos[1], self.end_pos[2]
end

function TSRange:type()
  return 'nvim-treesitter-range'
end

function TSRange:symbol()
  return -1
end

function TSRange:named()
  return false
end

function TSRange:missing()
  return false
end

function TSRange:has_error()
  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()), ' ')
end

M.TSRange = TSRange
return M