Skip to content

Commit 449052b

Browse files
committed
feat: context_file macro
1 parent 19f1c03 commit 449052b

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

lua/gp/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ local config = {
370370
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
371371
.. "\n\nRespond exclusively with the snippet that should be prepended before the selection above.",
372372
template_command = "{{command}}",
373+
template_context_file = "\n\nHere is a file {{filename}} for additional context:"
374+
.. "\n\n```\n{{content}}\n```\n\n",
373375

374376
-- https://platform.openai.com/docs/guides/speech-to-text/quickstart
375377
-- Whisper costs $0.006 / minute (rounded to the nearest second)

lua/gp/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,12 @@ M.setup = function(opts)
201201
local ft_completion = M.macro.build_completion({
202202
require("gp.macros.target_filetype"),
203203
require("gp.macros.agent"),
204+
require("gp.macros.context_file"),
204205
})
205206

206207
local base_completion = M.macro.build_completion({
207208
require("gp.macros.agent"),
209+
require("gp.macros.context_file"),
208210
})
209211

210212
M.logger.debug("ft_completion done")
@@ -214,6 +216,7 @@ M.setup = function(opts)
214216
require("gp.macros.target"),
215217
require("gp.macros.target_filetype"),
216218
require("gp.macros.target_filename"),
219+
require("gp.macros.context_file"),
217220
})
218221

219222
M.logger.debug("do_completion done")
@@ -223,6 +226,7 @@ M.setup = function(opts)
223226
require("gp.macros.target"),
224227
require("gp.macros.target_filetype"),
225228
require("gp.macros.target_filename"),
229+
require("gp.macros.context_file"),
226230
})
227231

228232
M.logger.debug("command_parser done")

lua/gp/macros/context_file.lua

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
local macro = require("gp.macro")
2+
local gp = require("gp")
3+
4+
local M = {}
5+
6+
---@type gp.Macro
7+
M = {
8+
name = "context_file`",
9+
description = "replaces the macro with the content of the specified file",
10+
default = nil,
11+
max_occurrences = 100,
12+
13+
triggered = function(params)
14+
local cropped_line = params.cropped_line
15+
return cropped_line:match("@context_file`[^`]*$")
16+
end,
17+
18+
completion = function(params)
19+
local root_dir = params.state.context_dir or vim.fn.getcwd()
20+
local files = vim.fn.globpath(root_dir, "**", false, true)
21+
local root_dir_length = #root_dir + 2
22+
files = vim.tbl_map(function(file)
23+
return file:sub(root_dir_length) .. " `"
24+
end, files)
25+
return files
26+
end,
27+
28+
parser = function(result)
29+
local template = result.template
30+
local macro_pattern = "@context_file`([^`]*)`"
31+
32+
for _ = 1, M.max_occurrences do
33+
local s, e, value = template:find(macro_pattern)
34+
if not value then
35+
break
36+
end
37+
38+
value = value:match("^%s*(.-)%s*$")
39+
local placeholder = macro.generate_placeholder(M.name, value)
40+
41+
local full_path = value
42+
if vim.fn.fnamemodify(full_path, ":p") ~= value then
43+
full_path = vim.fn.fnamemodify(result.state.context_dir .. "/" .. value, ":p")
44+
end
45+
46+
if vim.fn.filereadable(full_path) == 0 then
47+
result.artifacts[placeholder] = ""
48+
gp.logger.error("Context file not found: " .. full_path)
49+
else
50+
local content = table.concat(vim.fn.readfile(full_path), "\n")
51+
content = gp.render.template(gp.config.template_context_file, {
52+
["{{content}}"] = content,
53+
["{{filename}}"] = full_path,
54+
})
55+
result.artifacts[placeholder] = content
56+
end
57+
58+
template = template:sub(1, s - 1) .. placeholder .. template:sub(e + 1)
59+
end
60+
61+
result.template = template
62+
return result
63+
end,
64+
}
65+
66+
return M

0 commit comments

Comments
 (0)