|
| 1 | +local M = {} |
| 2 | + |
| 3 | +-- TODO: hook into prompt._submit, stash result on state.tagged_files |
| 4 | + |
| 5 | +M.MAX_BYTES = 256 * 1024 |
| 6 | +M.MAX_LINES = 2000 |
| 7 | + |
| 8 | +local RESERVED = { |
| 9 | + lsp = true, |
| 10 | +} |
| 11 | + |
| 12 | +local function word_boundary_before(text, pos) |
| 13 | + if pos <= 1 then |
| 14 | + return true |
| 15 | + end |
| 16 | + return not text:sub(pos - 1, pos - 1):match("[%w@]") |
| 17 | +end |
| 18 | + |
| 19 | +local function word_boundary_after(text, pos) |
| 20 | + if pos >= #text then |
| 21 | + return true |
| 22 | + end |
| 23 | + return not text:sub(pos + 1, pos + 1):match("[%w]") |
| 24 | +end |
| 25 | + |
| 26 | +function M.find_mentions(text) |
| 27 | + local mentions = {} |
| 28 | + local seen = {} |
| 29 | + local search_from = 1 |
| 30 | + |
| 31 | + while search_from <= #text do |
| 32 | + local at = text:find("@", search_from, true) |
| 33 | + if not at then |
| 34 | + break |
| 35 | + end |
| 36 | + |
| 37 | + if word_boundary_before(text, at) then |
| 38 | + local rest = text:sub(at + 1) |
| 39 | + local path = rest:match("^([%.%w%-_/]+)") |
| 40 | + if path and path ~= "" and not RESERVED[path] and word_boundary_after(text, at + #path) then |
| 41 | + if not seen[path] then |
| 42 | + seen[path] = true |
| 43 | + table.insert(mentions, path) |
| 44 | + end |
| 45 | + search_from = at + #path + 1 |
| 46 | + else |
| 47 | + search_from = at + 1 |
| 48 | + end |
| 49 | + else |
| 50 | + search_from = at + 1 |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + return mentions |
| 55 | +end |
| 56 | + |
| 57 | +local function trim(text) |
| 58 | + return (text:gsub("^%s+", ""):gsub("%s+$", "")) |
| 59 | +end |
| 60 | + |
| 61 | +function M.strip_mentions(text) |
| 62 | + local stripped = text |
| 63 | + for _, path in ipairs(M.find_mentions(text)) do |
| 64 | + stripped = stripped:gsub("%f[%w@]@" .. path:gsub("([%-%.%+%[%]%(%)%$%^%%%?%*])", "%%%1") .. "%f[%W]", "") |
| 65 | + end |
| 66 | + return trim((stripped:gsub("%s+", " "))) |
| 67 | +end |
| 68 | + |
| 69 | +function M.normalize_abs(path) |
| 70 | + if vim and vim.fn and vim.fn.fnamemodify then |
| 71 | + path = vim.fn.fnamemodify(path, ":p") |
| 72 | + end |
| 73 | + if path:sub(-1) == "/" then |
| 74 | + path = path:sub(1, -2) |
| 75 | + end |
| 76 | + return path |
| 77 | +end |
| 78 | + |
| 79 | +function M.resolve_path(raw_path, root) |
| 80 | + root = M.normalize_abs(root or (vim and vim.fn and vim.fn.getcwd() or ".")) |
| 81 | + if raw_path:sub(1, 1) == "/" then |
| 82 | + return M.normalize_abs(raw_path) |
| 83 | + end |
| 84 | + return M.normalize_abs(root .. "/" .. raw_path) |
| 85 | +end |
| 86 | + |
| 87 | +function M.rel_path(abs_path, root) |
| 88 | + abs_path = M.normalize_abs(abs_path) |
| 89 | + root = M.normalize_abs(root) |
| 90 | + local prefix = root .. "/" |
| 91 | + if abs_path:sub(1, #prefix) == prefix then |
| 92 | + return abs_path:sub(#prefix + 1) |
| 93 | + end |
| 94 | + return abs_path |
| 95 | +end |
| 96 | + |
| 97 | +local function slice_lines(lines, count) |
| 98 | + local out = {} |
| 99 | + for i = 1, math.min(count, #lines) do |
| 100 | + out[i] = lines[i] |
| 101 | + end |
| 102 | + return out |
| 103 | +end |
| 104 | + |
| 105 | +function M.truncate_lines(lines) |
| 106 | + local truncated = false |
| 107 | + if #lines > M.MAX_LINES then |
| 108 | + lines = slice_lines(lines, M.MAX_LINES) |
| 109 | + truncated = true |
| 110 | + end |
| 111 | + return lines, truncated |
| 112 | +end |
| 113 | + |
| 114 | +function M.project_root() |
| 115 | + local cwd = vim.fn.getcwd() |
| 116 | + if vim.system then |
| 117 | + local result = vim.system({ "git", "rev-parse", "--show-toplevel" }, { cwd = cwd }):wait() |
| 118 | + if result.code == 0 then |
| 119 | + local root = vim.trim(result.stdout or "") |
| 120 | + if root ~= "" then |
| 121 | + return M.normalize_abs(root) |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | + return M.normalize_abs(cwd) |
| 126 | +end |
| 127 | + |
| 128 | +function M.find_bufnr(abs_path) |
| 129 | + -- TODO: open the file if no buffer exists yet (probably at apply time) |
| 130 | + abs_path = M.normalize_abs(abs_path) |
| 131 | + for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do |
| 132 | + if vim.api.nvim_buf_is_loaded(bufnr) then |
| 133 | + local name = vim.api.nvim_buf_get_name(bufnr) |
| 134 | + if name ~= "" and M.normalize_abs(name) == abs_path then |
| 135 | + return bufnr |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | + return nil |
| 140 | +end |
| 141 | + |
| 142 | +function M.read_lines(abs_path, opts) |
| 143 | + opts = opts or {} |
| 144 | + |
| 145 | + if opts.read_file then |
| 146 | + return opts.read_file(abs_path) |
| 147 | + end |
| 148 | + |
| 149 | + local bufnr = M.find_bufnr(abs_path) |
| 150 | + if bufnr then |
| 151 | + local lines, truncated = M.truncate_lines(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)) |
| 152 | + local err = truncated and string.format("file exceeds %d line limit: %s", M.MAX_LINES, abs_path) or nil |
| 153 | + return lines, err, bufnr |
| 154 | + end |
| 155 | + |
| 156 | + local fd = vim.uv and vim.uv.fs_open(abs_path, "r", 438) or nil |
| 157 | + if not fd then |
| 158 | + return nil, "file not found: " .. abs_path |
| 159 | + end |
| 160 | + |
| 161 | + local stat = vim.uv.fs_fstat(fd) |
| 162 | + if stat and stat.size > M.MAX_BYTES then |
| 163 | + vim.uv.fs_close(fd) |
| 164 | + return nil, string.format("file exceeds %d byte limit: %s", M.MAX_BYTES, abs_path) |
| 165 | + end |
| 166 | + |
| 167 | + local data = vim.uv.fs_read(fd, M.MAX_BYTES) |
| 168 | + vim.uv.fs_close(fd) |
| 169 | + |
| 170 | + if not data then |
| 171 | + return nil, "could not read file: " .. abs_path |
| 172 | + end |
| 173 | + |
| 174 | + if data:sub(-1) == "\n" then |
| 175 | + data = data:sub(1, -2) |
| 176 | + end |
| 177 | + |
| 178 | + local lines = data == "" and {} or vim.split(data, "\n", { plain = true }) |
| 179 | + local truncated |
| 180 | + lines, truncated = M.truncate_lines(lines) |
| 181 | + if truncated then |
| 182 | + return lines, string.format("file exceeds %d line limit: %s", M.MAX_LINES, abs_path) |
| 183 | + end |
| 184 | + |
| 185 | + return lines, nil, nil |
| 186 | +end |
| 187 | + |
| 188 | +function M.parse(prompt_text, opts) |
| 189 | + -- TODO: should probably always include the source buffer too, even without @ |
| 190 | + opts = opts or {} |
| 191 | + local root = opts.root or M.project_root() |
| 192 | + local mentions = M.find_mentions(prompt_text) |
| 193 | + local tagged = {} |
| 194 | + local errors = {} |
| 195 | + |
| 196 | + for _, raw_path in ipairs(mentions) do |
| 197 | + local abs_path = M.resolve_path(raw_path, root) |
| 198 | + local lines, err, bufnr = M.read_lines(abs_path, opts) |
| 199 | + |
| 200 | + if not lines then |
| 201 | + table.insert(errors, err or ("could not read: " .. raw_path)) |
| 202 | + else |
| 203 | + table.insert(tagged, { |
| 204 | + path = M.rel_path(abs_path, root), |
| 205 | + abs_path = abs_path, |
| 206 | + lines = lines, |
| 207 | + bufnr = bufnr, |
| 208 | + }) |
| 209 | + if err then |
| 210 | + table.insert(errors, err) |
| 211 | + end |
| 212 | + end |
| 213 | + end |
| 214 | + |
| 215 | + return { |
| 216 | + tagged = tagged, |
| 217 | + cleaned_prompt = M.strip_mentions(prompt_text), |
| 218 | + errors = errors, |
| 219 | + } |
| 220 | +end |
| 221 | + |
| 222 | +return M |
0 commit comments