-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtags.lua
More file actions
240 lines (204 loc) · 5.81 KB
/
Copy pathtags.lua
File metadata and controls
240 lines (204 loc) · 5.81 KB
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
local path = require("jumpy.path")
local M = {}
local function trim(text)
if vim and vim.trim then
return vim.trim(text)
end
return (text:gsub("^%s+", ""):gsub("%s+$", ""))
end
M.MAX_BYTES = 256 * 1024
M.MAX_LINES = 2000
local RESERVED = {
lsp = true,
}
M.MENTION_CHARS = "[%.%w%-_/]"
local function word_boundary_before(text, pos)
if pos <= 1 then
return true
end
return not text:sub(pos - 1, pos - 1):match("[%w@]")
end
local function word_boundary_after(text, pos)
if pos >= #text then
return true
end
return not text:sub(pos + 1, pos + 1):match("[%w]")
end
local function normalize_mention_path(mention_path)
mention_path = mention_path:gsub("/+$", "")
mention_path = mention_path:gsub("%.$", "")
return mention_path
end
local function mention_remove_len(raw)
local norm_path = normalize_mention_path(raw)
if raw:sub(-1) == "." and #raw == #norm_path + 1 then
return #norm_path
end
return #raw
end
-- Walks `text` and calls `on_match(at, raw, norm_path)` for every valid
-- file mention (non-reserved, correct word boundaries). Returns the
-- (possibly mutated) text after all callbacks have run.
local function scan_mentions(text, on_match)
local search_from = 1
while search_from <= #text do
local at = text:find("@", search_from, true)
if not at then
break
end
if word_boundary_before(text, at) then
local raw = text:sub(at + 1):match("^(" .. M.MENTION_CHARS .. "+)")
local norm_path = raw and normalize_mention_path(raw) or nil
if norm_path and norm_path ~= "" and not RESERVED[norm_path] and word_boundary_after(text, at + #raw) then
local next_pos
text, next_pos = on_match(text, at, raw, norm_path)
search_from = next_pos
else
search_from = at + 1
end
else
search_from = at + 1
end
end
return text
end
function M.find_mentions(text)
local mentions = {}
local seen = {}
scan_mentions(text, function(t, at, raw, norm_path)
if not seen[norm_path] then
seen[norm_path] = true
table.insert(mentions, norm_path)
end
return t, at + #raw + 1
end)
return mentions
end
function M.strip_mentions(text)
local stripped = scan_mentions(text, function(t, at, raw, norm_path)
local remove_len = mention_remove_len(raw)
local next_t = t:sub(1, at - 1) .. t:sub(at + remove_len + 1)
return next_t, at
end)
return trim(stripped:gsub("%s+", " "))
end
local function slice_lines(lines, count)
local out = {}
for i = 1, math.min(count, #lines) do
out[i] = lines[i]
end
return out
end
function M.truncate_lines(lines)
local truncated = false
if #lines > M.MAX_LINES then
lines = slice_lines(lines, M.MAX_LINES)
truncated = true
end
return lines, truncated
end
function M.find_bufnr(abs_path)
abs_path = path.normalize_abs(abs_path)
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(bufnr) then
local name = vim.api.nvim_buf_get_name(bufnr)
if name ~= "" and path.normalize_abs(name) == abs_path then
return bufnr
end
end
end
return nil
end
function M.open_buffer(abs_path)
abs_path = path.normalize_abs(abs_path)
local bufnr = M.find_bufnr(abs_path)
if bufnr then
return bufnr
end
if vim.fn.filereadable(abs_path) ~= 1 then
return nil
end
bufnr = vim.fn.bufadd(abs_path)
vim.fn.bufload(bufnr)
return bufnr
end
function M.read_lines(abs_path, opts)
opts = opts or {}
if opts.read_file then
return opts.read_file(abs_path)
end
local bufnr = M.find_bufnr(abs_path)
if bufnr then
local lines, truncated = M.truncate_lines(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false))
local err = truncated and string.format("file exceeds %d line limit: %s", M.MAX_LINES, abs_path) or nil
return lines, err, bufnr
end
local fd = vim.uv and vim.uv.fs_open(abs_path, "r", 438) or nil
if not fd then
return nil, "file not found: " .. abs_path
end
local stat = vim.uv.fs_fstat(fd)
if stat and stat.size > M.MAX_BYTES then
vim.uv.fs_close(fd)
return nil, string.format("file exceeds %d byte limit: %s", M.MAX_BYTES, abs_path)
end
local data = vim.uv.fs_read(fd, M.MAX_BYTES)
vim.uv.fs_close(fd)
if not data then
return nil, "could not read file: " .. abs_path
end
if data:sub(-1) == "\n" then
data = data:sub(1, -2)
end
local lines = data == "" and {} or vim.split(data, "\n", { plain = true })
local truncated
lines, truncated = M.truncate_lines(lines)
if truncated then
return lines, string.format("file exceeds %d line limit: %s", M.MAX_LINES, abs_path)
end
return lines, nil, nil
end
function M.parse(prompt_text, opts)
opts = opts or {}
local root = opts.root or path.project_root()
local mentions = M.find_mentions(prompt_text)
local tagged = {}
local errors = {}
local seen_abs = {}
if opts.source then
local src = opts.source
local abs_path = path.normalize_abs(src.abs_path or path.resolve_path(src.path, root))
seen_abs[abs_path] = true
table.insert(tagged, {
path = src.path or path.rel_path(abs_path, root),
abs_path = abs_path,
lines = src.lines,
bufnr = src.bufnr,
})
end
for _, raw_path in ipairs(mentions) do
local abs_path = path.resolve_path(raw_path, root)
if not seen_abs[abs_path] then
local lines, err, bufnr = M.read_lines(abs_path, opts)
if not lines then
table.insert(errors, err or ("could not read: " .. raw_path))
else
table.insert(tagged, {
path = path.rel_path(abs_path, root),
abs_path = abs_path,
lines = lines,
bufnr = bufnr,
})
if err then
table.insert(errors, err)
end
end
end
end
return {
tagged = tagged,
cleaned_prompt = M.strip_mentions(prompt_text),
errors = errors,
}
end
return M