-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathregion.lua
326 lines (286 loc) · 8.53 KB
/
region.lua
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
local Point = require("refactoring.point")
local api = vim.api
---@class refactor.Region
---@field start_row number: The 1-based row
---@field start_col number: The 1-based col
---@field end_row number: The 1-based row
---@field end_col number: The 1-based col
---@field bufnr number: the buffer that the region is from
---@field type "v" | "V" | ""
local Region = {}
Region.__index = Region
--- Get a Region from motion (marks [ and ])
---@param opts {include_end_of_line: boolean, type :"v" | "V" | "" | nil, bufnr: integer} | nil
---@return refactor.Region
function Region:from_motion(opts)
local type = opts and opts.type or "v"
local bufnr = opts and opts.bufnr or api.nvim_get_current_buf()
local start_row = vim.fn.line("'[")
local start_col = vim.fn.col("'[")
local end_row = vim.fn.line("']")
local end_col = type == "V" and vim.v.maxcol or vim.fn.col("']")
if opts and opts.include_end_of_line then
local last_line =
api.nvim_buf_get_lines(0, end_row - 1, end_row, true)[1]
local line_length = vim.str_utfindex(last_line, #last_line)
end_col = math.min(end_col, line_length) --[[@as integer]]
end
return setmetatable({
bufnr = bufnr,
start_row = start_row,
start_col = start_col,
end_row = end_row,
end_col = end_col,
type = type,
}, self)
end
---@param bufnr integer
---@param start_row integer
---@param start_col integer
---@param end_row integer
---@param end_col integer
---@param type "v" | "V" | "" | nil
---@return refactor.Region
function Region:from_values(bufnr, start_row, start_col, end_row, end_col, type)
type = type or "v"
return setmetatable({
start_row = start_row,
start_col = start_col,
end_row = end_row,
end_col = end_col,
bufnr = bufnr,
type = type,
}, self)
end
---@param bufnr integer
---@return refactor.Region
function Region:empty(bufnr)
return setmetatable({
bufnr = bufnr,
type = "v",
}, self)
end
---@return boolean
function Region:is_empty()
if
self.start_row == 0
and self.start_col == 0
and self.end_row == 0
and self.end_col == 0
then
return true
end
return false
end
--- Get a region from a Treesitter Node
---@param node TSNode
---@param bufnr? number
---@return refactor.Region
function Region:from_node(node, bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
local start_row, start_col, end_row, end_col = node:range()
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, true)
local start_line = lines[start_row + 1]
start_col = vim.fn.charidx(start_line, start_col)
-- the parent node may have an end_row #lines + 1
local end_i = math.min(end_row + 1, #lines)
local end_line = lines[end_i]
end_col = vim.fn.charidx(end_line, end_col)
return setmetatable({
bufnr = bufnr,
start_row = start_row + 1,
start_col = start_col + 1,
end_row = end_row + 1,
end_col = end_col,
type = "v",
}, self)
end
--- Get a region from a given point.
---@param point refactor.Point the point to use as start- and endpoint
---@param bufnr? number the bufnr for the region
---@return refactor.Region
function Region:from_point(point, bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
return setmetatable({
bufnr = bufnr,
start_row = point.row,
start_col = point.col,
end_row = point.row,
end_col = point.col,
type = "v",
}, self)
end
---@param lsp_range lsp.Range
---@param bufnr integer|nil
---@return refactor.Region
function Region:from_lsp_range_insert(lsp_range, bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
return setmetatable({
bufnr = bufnr,
start_row = lsp_range.start.line + 1,
start_col = lsp_range.start.character + 1,
end_row = lsp_range["end"].line + 1,
end_col = lsp_range["end"].character + 1,
type = "v",
}, self)
end
---@param lsp_range lsp.Range
---@param bufnr integer|nil
---@return refactor.Region
function Region:from_lsp_range_replace(lsp_range, bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
return setmetatable({
bufnr = bufnr,
start_row = lsp_range.start.line + 1,
start_col = lsp_range.start.character + 1,
end_row = lsp_range["end"].line + 1,
end_col = lsp_range["end"].character,
type = "v",
}, self)
end
---@param root TSNode
---@return TSNode? # the node contained by this region
function Region:to_ts_node(root)
local s_row, s_col, e_row, e_col = self:to_ts()
return root:named_descendant_for_range(s_row, s_col, e_row, e_col)
end
--- Convert a region to a treesitter region
---@return integer start_row, integer start_col, integer end_row, integer end_col
function Region:to_ts()
return self.start_row - 1,
self.start_col - 1,
self.end_row - 1,
self.end_col
end
--- Get the lines contained in the region
---@return string[]
function Region:get_lines()
local offset = 0
local text = vim.fn.getregion({
self.bufnr,
self.start_row,
self.start_col,
offset,
}, {
self.bufnr,
self.end_row,
self.end_col,
offset,
}, { type = "V" })
return text
end
--- Get the left boundary of the region
---@return refactor.Point
function Region:get_start_point()
return Point:from_values(self.start_row, self.start_col)
end
--- Get the right boundary of the region
---@return refactor.Point
function Region:get_end_point()
return Point:from_values(self.end_row, self.end_col)
end
---@return string[]
function Region:get_text()
local offset = 0
local text = vim.fn.getregion({
self.bufnr,
self.start_row,
self.start_col,
offset,
}, {
self.bufnr,
self.end_row,
self.end_col,
offset,
}, { type = self.type })
return text
end
--- Convert a region to an LSP Range inteded to be used to insert text (start and end should the same despite end being exclusive)
---@return lsp.Range
function Region:to_lsp_range_insert()
return {
["start"] = {
line = self.start_row - 1,
character = self.start_col - 1,
},
["end"] = {
line = self.end_row - 1,
character = self.end_col - 1,
},
}
end
--- Convert a region to an LSP Range intended to be used to replace text (end should be exclusive)
---@return lsp.Range
function Region:to_lsp_range_replace()
return {
["start"] = {
line = self.start_row - 1,
character = self.start_col - 1,
},
["end"] = {
line = self.end_row - 1,
character = self.end_col,
},
}
end
---@class refactor.TextEdit : lsp.TextEdit
---@field bufnr integer?
---@param text string
---@return refactor.TextEdit
function Region:to_lsp_text_edit_insert(text)
return {
range = self:to_lsp_range_insert(),
newText = text,
}
end
---@param text string
---@return refactor.TextEdit
function Region:to_lsp_text_edit_replace(text)
return {
range = self:to_lsp_range_replace(),
newText = text,
}
end
---@return refactor.Region
function Region:clone()
local clone = Region:empty(self.bufnr)
clone.start_row = self.start_row
clone.start_col = self.start_col
clone.end_row = self.end_row
clone.end_col = self.end_col
clone.type = self.type
return clone
end
--- Returns true if self contains region.
---@param region refactor.Region
---@return boolean
function Region:contains(region)
if region.bufnr ~= self.bufnr then
return false
end
return self:get_start_point():leq(region:get_start_point())
and self:get_end_point():geq(region:get_end_point())
end
--- Returns true if self is above region
---@param region refactor.Region
---@return boolean
function Region:above(region)
if region.bufnr ~= self.bufnr then
return false
end
return self:get_start_point():lt(region:get_start_point())
and self:get_end_point():lt(region:get_start_point())
end
--- Returns true if self contains point.
---@param point refactor.Point
---@return boolean
function Region:contains_point(point)
return self:get_start_point():leq(point) and self:get_end_point():geq(point)
end
--- Return true if the position of self lies after the position of region
---@param region refactor.Region
---@return boolean
function Region:is_after(region)
return self:get_start_point():gt(region:get_end_point())
end
return Region