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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
--- TODO: rewrite : functions to . functions where they dont referencegeo
--- self.
---
local project_row = 100000000
--- @param point_or_row _99.Point | number
--- @param col number | nil
--- @return number
local function project(point_or_row, col)
if type(point_or_row) == "number" then
return point_or_row * project_row + col
end
return point_or_row.row * project_row + point_or_row.col
end
--- stores all values as 1 based
--- @class _99.Point
--- @field row number
--- @field col number
local Point = {}
Point.__index = Point
function Point:to_string()
return string.format("point(%d,%d)", self.row, self.col)
end
--- @param buffer number
--- @return string
function Point:get_text_line(buffer)
local r, _ = self:to_vim()
return vim.api.nvim_buf_get_lines(buffer, r, r + 1, true)[1]
end
--- @param buffer number
--- @param text string
function Point:set_text_line(buffer, text)
local r, _ = self:to_vim()
vim.api.nvim_buf_set_lines(buffer, r, r + 1, false, { text })
end
function Point:update_to_end_of_line()
self.col = vim.fn.col("$") + 1
local r, c = self:to_one_zero_index()
vim.api.nvim_win_set_cursor(0, { r, c })
end
--- 1 based point
--- @param row number
--- @param col number
--- @return _99.Point
function Point:from_1_based(row, col)
assert(type(row) == "number", "expected row to be a number")
assert(type(col) == "number", "expected col to be a number")
return setmetatable({
row = row,
col = col,
}, self)
end
--- 0 based point
--- @param row number
--- @param col number
--- @return _99.Point
function Point.from_0_based(row, col)
assert(type(row) == "number", "expected row to be a number")
assert(type(col) == "number", "expected col to be a number")
return setmetatable({
row = row + 1,
col = col + 1,
}, Point)
end
function Point:from_cursor()
local point = setmetatable({
row = 0,
col = 0,
}, self)
--- NOTE: win_get_cursor 1, 0 based return
local cursor = vim.api.nvim_win_get_cursor(0)
local cursor_row, cursor_col = cursor[1], cursor[2]
point.row = cursor_row
point.col = cursor_col + 1
return point
end
--- Point from nvim_buf_get_extmark_by_id returns which is 0 based
--- @param mark _99.Mark
function Point.from_extmark(mark)
local buffer = mark.buffer
local ns_id = mark.nsid
local mark_id = mark.id
local row, col = vim.api.nvim_buf_get_extmark_by_id(buffer, ns_id, mark_id)
return setmetatable({
row = row + 1,
col = col + 1,
}, Point)
end
--- @param row number
---@param col number
--- @return _99.Point
function Point:from_ts_point(row, col)
return setmetatable({
row = row + 1,
col = col + 1,
}, self)
end
--- @param lsp_point {character: number, line: number}
function Point.from_lsp_point(lsp_point)
return setmetatable({
row = lsp_point.line + 1,
col = lsp_point.character + 1,
}, Point)
end
--- stores all 2 points
--- @param range _99.Range
--- @return boolean
function Point:in_ts_range(range)
return range:contains(self)
end
--- vim.api.nvim_buf_get_text uses 0 based row and col
--- @return number, number
function Point:to_lua()
return self.row, self.col
end
--- @return number, number
function Point:to_lsp()
return self.row - 1, self.col - 1
end
--- vim.api.nvim_buf_get_text uses 0 based row and col
--- @return number, number
function Point:to_vim()
return self.row - 1, self.col - 1
end
function Point:to_one_zero_index()
return self.row, self.col - 1
end
--- treesitter uses 0 based row and col
--- @return number, number
function Point:to_ts()
return self.row - 1, self.col - 1
end
--- @param point _99.Point
--- @return boolean
function Point:gt(point)
return project(self) > project(point)
end
--- @param point _99.Point
--- @return boolean
function Point:lt(point)
return project(self) < project(point)
end
--- @param point _99.Point
--- @return boolean
function Point:lte(point)
return project(self) <= project(point)
end
--- @param point _99.Point
--- @return boolean
function Point:gte(point)
return project(self) >= project(point)
end
--- @param point _99.Point
--- @return boolean
function Point:eq(point)
return project(self) == project(point)
end
--- @param point _99.Point
--- @return _99.Point
function Point:add(point)
return Point:from_1_based(self.row + point.row, self.col + point.col)
end
--- @param point _99.Point
--- @return _99.Point
function Point:sub(point)
return Point:from_1_based(self.row - point.row, self.col - point.col)
end
--- @param mark _99.Mark
--- @return _99.Point
function Point.from_mark(mark)
--- buf extmark by id is a 0 based api
local pos =
vim.api.nvim_buf_get_extmark_by_id(mark.buffer, mark.nsid, mark.id, {})
return setmetatable({
row = pos[1] + 1,
col = pos[2] + 1,
}, Point)
end
--- Returns the line in the editor that this point is on
--- @param buffer number
function Point:line(buffer)
local row, _ = self:to_vim()
local lines = vim.api.nvim_buf_get_lines(buffer, row, row + 1, false)
return lines[1]
end
function Point.zero()
return Point.from_0_based(0, 0)
end
--- @class _99.Range
--- @field start _99.Point
--- @field end_ _99.Point
--- @field buffer number
local Range = {}
Range.__index = Range
---@param buffer number
--- @param start _99.Point
---@param end_ _99.Point
function Range:new(buffer, start, end_)
return setmetatable({
start = start,
end_ = end_,
buffer = buffer,
}, self)
end
function Range.from_visual_selection()
local buffer = vim.api.nvim_get_current_buf()
local start_pos = vim.fn.getpos("'<")
local end_pos = vim.fn.getpos("'>")
local start = Point:from_1_based(start_pos[2], start_pos[3])
local end_ = Point:from_1_based(end_pos[2], end_pos[3])
--- visual line mode will select the end point for each row to be int max
--- which will cause marks to fail. so we have to correct it to the literal
--- row length
local end_row, _ = end_:to_vim()
local end_line =
vim.api.nvim_buf_get_lines(buffer, end_row, end_row + 1, false)
--- @type number
local end_col
--- another bug where mark >' is beyond the editor.
--- therefore will just grab from start to end and use the last captured row
--- as the means to discover the proper end col and end row
if #end_line == 0 then
local start_r, _ = start:to_vim()
local selected_lines =
vim.api.nvim_buf_get_lines(buffer, start_r, end_row, false)
end_row = start_r + #selected_lines
--- another edge case, the buffer may be empty...
--- sentry literally caught this one
if #selected_lines == 0 then
--- an edge to the edge case. we are in 1 based indexing... f
end_col = 1
else
end_col = #selected_lines[#selected_lines]
end
--- here is confusing part, we are now in 1 based values
--- in the geo_spec test, this would result in end_row = 2, end_col = 8
--- so, there is this -1 because we are going to go from 1 based to 0 based
end_row = end_row - 1
--- we need to capture the whole line, therefore its end of line + 1
end_col = end_col
else
--- we are using zero based point, which means length of line includes the new_line character
end_col = #end_line[1]
end
local actual_end = Point.from_0_based(end_row, end_col)
return Range:new(buffer, start, actual_end)
end
---@param node _99.treesitter.Node
---@param buffer number
---@return _99.Range
function Range:from_ts_node(node, buffer)
-- ts is zero based
local start_row, start_col, _ = node:start()
local end_row, end_col, _ = node:end_()
local range = {
start = Point:from_ts_point(start_row, start_col),
end_ = Point:from_ts_point(end_row, end_col),
buffer = buffer,
}
return setmetatable(range, self)
end
---@param start _99.Mark
---@param end_ _99.Mark
---@return _99.Range
function Range.from_marks(start, end_)
local start_point = Point.from_mark(start)
local end_point = Point.from_mark(end_)
return Range:new(start.buffer, start_point, end_point)
end
--- @param replace_with string[]
function Range:replace_text(replace_with)
local s_row, s_col = self.start:to_vim()
local e_row, e_col = self.end_:to_vim()
vim.api.nvim_buf_set_text(
self.buffer,
s_row,
s_col,
e_row,
e_col,
replace_with
)
end
--- @param point _99.Point
--- @return boolean
function Range:contains(point)
local start = project(self.start)
local stop = project(self.end_)
local p = project(point)
return start <= p and p <= stop
end
--- @return string
function Range:to_text()
local sr, sc = self.start:to_vim()
local er, ec = self.end_:to_vim()
--- blank line vis selection
if sr == er and sc == ec then
ec = ec + 1
end
local text = vim.api.nvim_buf_get_text(self.buffer, sr, sc, er, ec, {})
return table.concat(text, "\n")
end
--- @param range _99.Range
--- @return boolean
function Range:contains_range(range)
return self.start:lte(range.start) and self.end_:gte(range.end_)
end
function Range:area()
local start = project(self.start)
local end_ = project(self.end_)
return end_ - start
end
function Range:to_string()
return string.format(
"range(%s,%s)",
self.start:to_string(),
self.end_:to_string()
)
end
--- @return _99.Range
function Range.zero()
return Range:new(0, Point.zero(), Point.zero())
end
return {
Point = Point,
Range = Range,
}
|