robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
5.0 KiB178 linesraw
1---@brief
2--- Unlike `vim.pos`, this module is used to provide utility functions
3--- for unpacked `row`, `col`.
4---
5--- The variable names have some implications:
6---
7--- - `row`  is used to represent a 0-based index of a line.
8--- - `lnum` is used to represent a 1-based index of a line, short for "line number".
9
10local api = vim.api
11
12local M = {}
13
14---@param a_row integer
15---@param a_col integer
16---@param b_row integer
17---@param b_col integer
18---@return integer
19--- 1: a > b
20--- 0: a == b
21--- -1: a < b
22local function cmp_pos(a_row, a_col, b_row, b_col)
23  if a_row == b_row then
24    if a_col > b_col then
25      return 1
26    elseif a_col < b_col then
27      return -1
28    else
29      return 0
30    end
31  elseif a_row > b_row then
32    return 1
33  end
34
35  return -1
36end
37
38---@type table<'lt'|'le'|'gt'|'ge'|'eq'|'ne', fun(a_row: integer, a_col: integer, b_row: integer, b_col: integer): boolean>
39M.cmp_pos = {
40  lt = function(...)
41    return cmp_pos(...) == -1
42  end,
43  le = function(...)
44    return cmp_pos(...) ~= 1
45  end,
46  gt = function(...)
47    return cmp_pos(...) == 1
48  end,
49  ge = function(...)
50    return cmp_pos(...) ~= -1
51  end,
52  eq = function(...)
53    return cmp_pos(...) == 0
54  end,
55  ne = function(...)
56    return cmp_pos(...) ~= 0
57  end,
58}
59
60setmetatable(M.cmp_pos, { __call = cmp_pos })
61
62--- Gets the zero-indexed lines from the given buffer.
63--- Works on unloaded buffers by reading the file and bypass buf reading events.
64--- Falls back to loading the buffer and nvim_buf_get_lines for buffers with non-file URI.
65---
66---@param buf integer buffer handle to get the lines from
67---@param rows integer[] zero-indexed line numbers
68---@return table<integer, string> # a table mapping rows to lines
69function M.get_lines(buf, rows)
70  local function buf_lines()
71    local row_line = {} --- @type table<integer,string>
72    for _, row in ipairs(rows) do
73      row_line[row] = api.nvim_buf_get_lines(buf, row, row + 1, false)[1] or ''
74    end
75    return row_line
76  end
77
78  -- Use loaded buffers if available.
79  if vim.fn.bufloaded(buf) == 1 then
80    return buf_lines()
81  end
82
83  -- Load the buffer if this is not a file URI.
84  -- Custom language server protocol extensions can result in servers sending
85  -- URIs with custom schemes. Plugins are able to load these via `BufReadCmd` autocmds.
86  if not vim.startswith(vim.uri_from_bufnr(buf), 'file://') then
87    vim.fn.bufload(buf)
88    return buf_lines()
89  end
90
91  local row_line = {} --- @type table<integer, string>
92  for _, row in pairs(rows) do
93    row_line[row] = ''
94  end
95
96  -- Get the data from the file.
97  local success, data = pcall(vim.fn.readblob, api.nvim_buf_get_name(buf))
98  if not success then
99    return row_line
100  end
101
102  --- @cast data string
103  local need = vim.tbl_count(row_line)
104  local row = 0
105  for line in string.gmatch(data, '([^\n]*)\n?') do
106    if row_line[row] then
107      row_line[row] = line
108      need = need - 1
109      if need == 0 then
110        break
111      end
112    end
113    row = row + 1
114  end
115  return row_line
116end
117
118--- Gets the zero-indexed line from the given buffer.
119--- Works on unloaded buffers by reading the file and bypass buf reading events.
120--- Falls back to loading the buffer and nvim_buf_get_lines for buffers with non-file URI.
121---
122---@param buf integer buffer handle to get the lines from
123---@param row integer zero-indexed line number
124---@return string the line at row in filename
125function M.get_line(buf, row)
126  return M.get_lines(buf, { row })[row]
127end
128
129---@param buf integer
130---@param row integer
131---@param col integer
132---@param position_encoding lsp.PositionEncodingKind
133function M.to_lsp(buf, row, col, position_encoding)
134  -- When on the first character,
135  -- we can ignore the difference between byte and character.
136  if col > 0 then
137    col = vim.str_utfindex(M.get_line(buf, row), position_encoding, col, false)
138  elseif col == 0 and row == api.nvim_buf_line_count(buf) and not vim.bo[buf].endofline then
139    -- Some LSP servers reject ranges that end at the virtual EOF position
140    -- (i.e., `[line_count, 0]`) when the buffer has no trailing newline.
141    -- Normalize such positions to the end of the last real line instead.
142    row = row - 1
143    col = vim.str_utfindex(M.get_line(buf, row), position_encoding)
144  end
145  ---@type lsp.Position
146  return { line = row, character = col }
147end
148
149---@param buf integer
150---@param position lsp.Position
151---@param position_encoding lsp.PositionEncodingKind
152function M.from_lsp(buf, position, position_encoding)
153  local row, col = position.line, position.character
154  -- When on the first character,
155  -- we can ignore the difference between byte and character.
156  if col > 0 then
157    -- `strict_indexing` is disabled, because LSP responses are asynchronous,
158    -- and the buffer content may have changed, causing out-of-bounds errors.
159    col = vim.str_byteindex(M.get_line(buf, row), position_encoding, col, false)
160  end
161  return row, col
162end
163
164---@param row integer
165---@param col integer
166---@return integer lnum, integer col
167function M.to_mark(row, col)
168  return row + 1, col
169end
170
171---@param lnum integer
172---@param col integer
173---@return integer row, integer col
174function M.from_mark(lnum, col)
175  return lnum - 1, col
176end
177
178return M