robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
8.8 KiB355 linesraw
1---@brief
2---
3--- EXPERIMENTAL: This API is unstable, do not use it. Its semantics are not yet finalized.
4--- Subscribe to this issue to stay updated: https://github.com/neovim/neovim/issues/25509
5---
6--- Provides operations to compare, calculate, and convert positions represented by |vim.Pos|
7--- objects.
8
9local api = vim.api
10local validate = vim.validate
11local util = require('vim.pos._util')
12
13--- Represents a buffer position based on [api-indexing] (0-indexed, end-exclusive ranges).
14--- Call `vim.pos()` to create a new `vim.Pos` by passing the {buf}, {row}, and {col} of a position.
15---
16--- Example:
17--- ```lua
18--- local pos1 = vim.pos(0, 3, 5)
19--- local pos2 = vim.pos(0, 4, 0)
20---
21--- -- Operators are overloaded for comparing two `vim.Pos` objects.
22--- if pos1 < pos2 then
23---   print("pos1 comes before pos2")
24--- end
25---
26--- if pos1 ~= pos2 then
27---   print("pos1 and pos2 are different positions")
28--- end
29--- ```
30---
31--- It may include optional fields that enable additional capabilities,
32--- such as format conversions.
33---
34---@class vim.Pos
35---@field row integer 0-based byte index.
36---@field col integer 0-based byte index.
37---@field buf integer buffer handle.
38---@field private [1] integer underlying representation of row
39---@field private [2] integer underlying representation of col
40---@field private [3] integer underlying representation of buf
41local M = {}
42
43---@private
44---@param pos vim.Pos
45---@param key any
46function M.__index(pos, key)
47  if key == 'row' then
48    return pos[1]
49  elseif key == 'col' then
50    return pos[2]
51  elseif key == 'buf' then
52    return pos[3]
53  end
54
55  return M[key]
56end
57
58---@private
59---@param pos vim.Pos
60---@param key any
61---@param value any
62function M.__newindex(pos, key, value)
63  if key == 'row' then
64    validate('row', value, 'number')
65    pos[1] = value
66  elseif key == 'col' then
67    validate('col', value, 'number')
68    pos[2] = value
69  elseif key == 'buf' then
70    validate('buf', value, 'number')
71    if value == 0 then
72      value = api.nvim_get_current_buf()
73    end
74    pos[3] = value
75  else
76    rawset(pos, key, value)
77  end
78end
79
80---@package
81---@param buf integer
82---@param row integer
83---@param col integer
84function M.new(buf, row, col)
85  validate('buf', buf, 'number')
86  validate('row', row, 'number')
87  validate('col', col, 'number')
88
89  if buf == 0 then
90    buf = api.nvim_get_current_buf()
91  end
92
93  ---@type vim.Pos
94  local self = setmetatable({
95    row,
96    col,
97    buf,
98  }, M)
99
100  return self
101end
102
103---@private
104---@param p1 vim.Pos
105---@param p2 vim.Pos
106function M.__lt(p1, p2)
107  return util.cmp_pos.lt(p1[1], p1[2], p2[1], p2[2])
108end
109
110---@private
111---@param p1 vim.Pos
112---@param p2 vim.Pos
113function M.__le(p1, p2)
114  return util.cmp_pos.le(p1[1], p1[2], p2[1], p2[2])
115end
116
117---@private
118---@param p1 vim.Pos
119---@param p2 vim.Pos
120function M.__eq(p1, p2)
121  return util.cmp_pos.eq(p1[1], p1[2], p2[1], p2[2])
122end
123
124--- Converts |vim.Pos| to `lsp.Position`.
125---
126--- Example:
127--- ```lua
128--- local pos = vim.pos(0, 3, 5)
129---
130--- -- Convert to LSP position, you can call it in a method style.
131--- local lsp_pos = pos:to_lsp('utf-16')
132--- ```
133---@param pos vim.Pos
134---@param position_encoding lsp.PositionEncodingKind
135---@return lsp.Position
136function M.to_lsp(pos, position_encoding)
137  validate('pos', pos, 'table')
138  validate('position_encoding', position_encoding, 'string')
139
140  return util.to_lsp(pos.buf, pos[1], pos[2], position_encoding)
141end
142
143--- Creates a new |vim.Pos| from `lsp.Position`.
144---
145--- Example:
146--- ```lua
147--- local lsp_pos = {
148---   line = 3,
149---   character = 5
150--- }
151---
152--- local pos = vim.pos.lsp(0, lsp_pos, 'utf-16')
153--- ```
154---@param buf integer
155---@param pos lsp.Position
156---@param position_encoding lsp.PositionEncodingKind
157---@return vim.Pos
158function M.lsp(buf, pos, position_encoding)
159  validate('buf', buf, 'number')
160  validate('pos', pos, 'table')
161  validate('position_encoding', position_encoding, 'string')
162
163  local row, col = util.from_lsp(buf, pos, position_encoding)
164  return M.new(buf, row, col)
165end
166
167--- Converts |vim.Pos| to cursor position (see |api-indexing|).
168---
169--- Example:
170--- ```lua
171--- local pos = vim.pos(0, 3, 5)
172---
173--- -- Convert to cursor position, you can call it in a method style.
174--- local cursor_pos = pos:to_cursor()
175--- vim.api.nvim_win_set_cursor(0, cursor_pos)
176--- ```
177---@param pos vim.Pos
178---@return [integer, integer] (lnum, col) tuple
179function M.to_cursor(pos)
180  validate('pos', pos, 'table')
181  return { util.to_mark(pos[1], pos[2]) }
182end
183
184--- Creates a new |vim.Pos| from cursor position (see |api-indexing|).
185---
186--- If {pos} is omitted, the first argument is treated as {win} instead of {buf},
187--- and the current cursor position of {win} is used. If {win} is also omitted,
188--- it defaults to the current window.
189---
190--- Example:
191--- ```lua
192--- local buf = vim.api.nvim_win_get_buf(0)
193--- local cursor_pos = vim.api.nvim_win_get_cursor(0)
194--- local pos = vim.pos.cursor(buf, cursor_pos)
195--- -- This is equivalent:
196--- local pos = vim.pos.cursor(0)
197--- ```
198---@param buf integer
199---@param pos [integer, integer] (lnum, col) tuple
200---@return vim.Pos
201---@overload fun(win?: integer): vim.Pos
202function M.cursor(buf, pos)
203  validate('pos', pos, 'table', true)
204
205  if pos then
206    validate('buf', buf, 'number')
207  else
208    local win = buf
209    validate('win', win, 'number', true)
210    if win == 0 or win == nil then
211      win = api.nvim_get_current_win()
212    end
213
214    buf = api.nvim_win_get_buf(win)
215    pos = api.nvim_win_get_cursor(win)
216  end
217
218  return M.new(buf, util.from_mark(pos[1], pos[2]))
219end
220
221--- Converts |vim.Pos| to mark position (see |api-indexing|).
222---
223--- Example:
224--- ```lua
225--- local pos = vim.pos(0, 3, 5)
226---
227--- -- Convert to mark position, you can call it in a method style.
228--- local lnum, col = pos:to_mark()
229--- vim.api.nvim_buf_set_mark(0, 'M', lnum, col)
230--- ```
231---@param pos vim.Pos
232---@return integer lnum, integer col
233function M.to_mark(pos)
234  validate('pos', pos, 'table')
235  return util.to_mark(pos[1], pos[2])
236end
237
238--- Creates a new |vim.Pos| from mark position (see |api-indexing|).
239---
240--- Example:
241--- ```lua
242--- local mark_info = vim.api.nvim_get_mark('M')
243--- local lnum, col, buf, name = unpack(mark_info)
244---
245--- if lnum == 0 and col == 0 and buf == 0 then
246---   return -- mark 'M' is not set.
247--- end
248---
249--- local pos = vim.pos.mark(buf, lnum, col)
250--- ```
251---@param buf integer
252---@param lnum integer
253---@param col integer
254---@return vim.Pos
255function M.mark(buf, lnum, col)
256  validate('buf', buf, 'number')
257  validate('lnum', lnum, 'number')
258  validate('col', col, 'number')
259
260  return M.new(buf, util.from_mark(lnum, col))
261end
262
263--- Converts |vim.Pos| to extmark position (see |api-indexing|).
264---
265--- Example:
266--- ```lua
267--- local pos = vim.pos(0, 3, 5)
268---
269--- -- Convert to extmark position, you can call it in a method style.
270--- local extmark_pos = pos:to_extmark()
271--- ```
272---@param pos vim.Pos
273---@return integer row, integer col
274function M.to_extmark(pos)
275  validate('pos', pos, 'table')
276  return pos[1], pos[2]
277end
278
279--- Creates a new |vim.Pos| from extmark position (see |api-indexing|).
280---
281--- Example:
282--- ```lua
283--- local pos = vim.pos.extmark(0, 3, 5)
284--- ```
285---@param buf integer
286---@param row integer
287---@param col integer
288---@return vim.Pos
289function M.extmark(buf, row, col)
290  validate('buf', buf, 'number')
291  validate('row', row, 'number')
292  validate('col', col, 'number')
293
294  return M.new(buf, row, col)
295end
296
297--- Converts |vim.Pos| to buffer (bytes) offset.
298---
299--- Example:
300--- ```lua
301--- local p1 = vim.pos(0, 3, 5)
302--- local p2 = vim.pos(0, 4, 0)
303---
304--- -- Convert to buffer offset, you can call it in a method style.
305--- local offset1 = p1:to_offset()
306--- local offset2 = p2:to_offset()
307--- -- Can be used to calculate the distance between two locations.
308--- local distance = offset2 - offset1
309--- ```
310---@param pos vim.Pos
311---@return integer
312function M.to_offset(pos)
313  validate('pos', pos, 'table')
314  return api.nvim_buf_get_offset(pos.buf, pos[1]) + pos[2]
315end
316
317--- Creates a new |vim.Pos| from buffer (bytes) offset.
318---
319--- Example:
320--- ```lua
321--- local offset = vim.api.nvim_buf_get_offset(0, vim.api.nvim_buf_line_count(0))
322--- local pos = vim.pos.offset(0, offset)
323--- ```
324---@param buf integer
325---@param offset integer
326---@return vim.Pos
327function M.offset(buf, offset)
328  validate('buf', buf, 'number')
329  validate('offset', offset, 'number')
330
331  local lnum = vim.list.bisect(
332    setmetatable({}, {
333      --- @param idx integer
334      __index = function(_, idx)
335        return api.nvim_buf_get_offset(buf, idx - 1)
336      end,
337    }),
338    offset,
339    { lo = 1, hi = api.nvim_buf_line_count(buf) + 2, bound = 'upper' }
340  ) - 1
341
342  local row = lnum - 1
343  local col = offset - api.nvim_buf_get_offset(buf, row)
344  return M.new(buf, row, col)
345end
346
347-- Overload `Range.new` to allow calling this module as a function.
348setmetatable(M, {
349  __call = function(_, ...)
350    return M.new(...)
351  end,
352})
353---@cast M vim.Pos & fun(buf: integer, row: integer, col: integer): vim.Pos
354
355return M