Skip to content

Commit b0c23ee

Browse files
committed
feat: adding more macros
- context_file => with_file - with_current_file - with_repo_instructions
1 parent 0ea6f7e commit b0c23ee

File tree

5 files changed

+131
-33
lines changed

5 files changed

+131
-33
lines changed

after/ftplugin/gpchat.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ if not has_cmp then
159159
end
160160

161161
M.macro.build_cmp_source("gpchat", {
162-
require("gp.macros.context_file"),
162+
require("gp.macros.with_file"),
163+
require("gp.macros.with_repo_instructions"),
163164
})
164165

165166
local sources = {

lua/gp/init.lua

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -200,38 +200,47 @@ M.setup = function(opts)
200200

201201
local ft_completion = M.macro.build_completion({
202202
require("gp.macros.agent"),
203-
require("gp.macros.context_file"),
204203
require("gp.macros.target_filename"),
205204
require("gp.macros.target_filetype"),
205+
require("gp.macros.with_current_buf"),
206+
require("gp.macros.with_file"),
207+
require("gp.macros.with_repo_instructions"),
206208
})
207209

208210
local base_completion = M.macro.build_completion({
209211
require("gp.macros.agent"),
210-
require("gp.macros.context_file"),
212+
require("gp.macros.with_current_buf"),
213+
require("gp.macros.with_file"),
214+
require("gp.macros.with_repo_instructions"),
211215
})
212216

213217
M.logger.debug("ft_completion done")
214218

215219
local do_completion = M.macro.build_completion({
216220
require("gp.macros.agent"),
217-
require("gp.macros.context_file"),
218221
require("gp.macros.target"),
219222
require("gp.macros.target_filename"),
220223
require("gp.macros.target_filetype"),
224+
require("gp.macros.with_current_buf"),
225+
require("gp.macros.with_file"),
226+
require("gp.macros.with_repo_instructions"),
221227
})
222228

223229
M.logger.debug("do_completion done")
224230

225231
M.command_parser = M.macro.build_parser({
226232
require("gp.macros.agent"),
227-
require("gp.macros.context_file"),
228233
require("gp.macros.target"),
229234
require("gp.macros.target_filename"),
230235
require("gp.macros.target_filetype"),
236+
require("gp.macros.with_current_buf"),
237+
require("gp.macros.with_file"),
238+
require("gp.macros.with_repo_instructions"),
231239
})
232240

233241
M.chat_parser = M.macro.build_parser({
234-
require("gp.macros.context_file"),
242+
require("gp.macros.with_file"),
243+
require("gp.macros.with_repo_instructions"),
235244
})
236245

237246
local completions = {
@@ -1665,25 +1674,6 @@ M.get_chat_agent = function(name)
16651674
}
16661675
end
16671676

1668-
-- tries to find an .gp.md file in the root of current git repo
1669-
---@return string # returns instructions from the .gp.md file
1670-
M.repo_instructions = function()
1671-
local git_root = M.helpers.find_git_root()
1672-
1673-
if git_root == "" then
1674-
return ""
1675-
end
1676-
1677-
local instruct_file = git_root .. "/.gp.md"
1678-
1679-
if vim.fn.filereadable(instruct_file) == 0 then
1680-
return ""
1681-
end
1682-
1683-
local lines = vim.fn.readfile(instruct_file)
1684-
return table.concat(lines, "\n")
1685-
end
1686-
16871677
M.cmd.Context = function(params)
16881678
M._toggle_close(M._toggle_kind.popup)
16891679
-- if there is no selection, try to close context toggle
@@ -1893,11 +1883,6 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
18931883
sys_prompt = sys_prompt or ""
18941884
table.insert(messages, { role = "system", content = sys_prompt })
18951885

1896-
local repo_instructions = M.repo_instructions()
1897-
if repo_instructions ~= "" then
1898-
table.insert(messages, { role = "system", content = repo_instructions })
1899-
end
1900-
19011886
local user_prompt = M.render.prompt_template(template, command, selection, filetype, filename)
19021887
table.insert(messages, { role = "user", content = user_prompt })
19031888

@@ -2034,6 +2019,8 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
20342019
command = command .. filetype
20352020
whisper = whisper and " " .. whisper or ""
20362021
command = command .. whisper
2022+
command = command .. " @with_repo_instructions"
2023+
command = command .. " @with_current_buf"
20372024
command = command .. " "
20382025

20392026
vim.api.nvim_feedkeys(command, "n", false)

lua/gp/macros/with_current_buf.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
local macro = require("gp.macro")
2+
local gp = require("gp")
3+
4+
local M = {}
5+
6+
---@type gp.Macro
7+
M = {
8+
name = "with_current_buf",
9+
description = "replaces the macro with the content of the current file",
10+
default = nil,
11+
max_occurrences = 1,
12+
13+
triggered = function(_)
14+
return false
15+
end,
16+
17+
completion = function(_)
18+
return {}
19+
end,
20+
21+
parser = function(result)
22+
local template = result.template
23+
local macro_pattern = "@with_current_buf"
24+
25+
local s, e = template:find(macro_pattern)
26+
if not s then
27+
return result
28+
end
29+
30+
local placeholder = macro.generate_placeholder(M.name, "")
31+
32+
local current_buf = vim.api.nvim_get_current_buf()
33+
local content = table.concat(vim.api.nvim_buf_get_lines(current_buf, 0, -1, false), "\n")
34+
local full_path = vim.api.nvim_buf_get_name(current_buf)
35+
36+
content = gp.render.template(gp.config.template_context_file, {
37+
["{{content}}"] = content,
38+
["{{filename}}"] = full_path,
39+
})
40+
result.artifacts[placeholder] = content
41+
42+
result.template = template:sub(1, s - 1) .. placeholder .. template:sub(e + 1)
43+
44+
return result
45+
end,
46+
}
47+
48+
return M

lua/gp/macros/context_file.lua renamed to lua/gp/macros/with_file.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ local M = {}
55

66
---@type gp.Macro
77
M = {
8-
name = "context_file`",
8+
name = "with_file`",
99
description = "replaces the macro with the content of the specified file",
1010
default = nil,
1111
max_occurrences = 100,
1212

1313
triggered = function(params)
1414
local cropped_line = params.cropped_line
15-
return cropped_line:match("@context_file`[^`]*$")
15+
return cropped_line:match("@with_file`[^`]*$")
1616
end,
1717

1818
completion = function(params)
@@ -27,7 +27,7 @@ M = {
2727

2828
parser = function(result)
2929
local template = result.template
30-
local macro_pattern = "@context_file`([^`]*)`"
30+
local macro_pattern = "@with_file`([^`]*)`"
3131

3232
for _ = 1, M.max_occurrences do
3333
local s, e, value = template:find(macro_pattern)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
local macro = require("gp.macro")
2+
local gp = require("gp")
3+
4+
---@param git_root? string # optional git root directory
5+
---@return string # returns instructions from the .gp.md file
6+
local repo_instructions = function(git_root)
7+
git_root = git_root or gp.helpers.find_git_root()
8+
9+
if git_root == "" then
10+
return ""
11+
end
12+
13+
local instruct_file = (git_root:gsub("/$", "")) .. "/.gp.md"
14+
15+
if vim.fn.filereadable(instruct_file) == 0 then
16+
return ""
17+
end
18+
19+
local lines = vim.fn.readfile(instruct_file)
20+
return table.concat(lines, "\n")
21+
end
22+
23+
local M = {}
24+
25+
---@type gp.Macro
26+
M = {
27+
name = "with_repo_instructions",
28+
description = "replaces the macro with the content of the .gp.md file in the git root",
29+
default = nil,
30+
max_occurrences = 1,
31+
32+
triggered = function(_)
33+
return false
34+
end,
35+
36+
completion = function(_)
37+
return {}
38+
end,
39+
40+
parser = function(result)
41+
local template = result.template
42+
local macro_pattern = "@with_repo_instructions"
43+
44+
local s, e = template:find(macro_pattern)
45+
if not s then
46+
return result
47+
end
48+
49+
local placeholder = macro.generate_placeholder(M.name, "")
50+
51+
local instructions = repo_instructions(result.state.context_dir)
52+
result.artifacts[placeholder] = gp.render.template(gp.config.template_context_file, {
53+
["{{content}}"] = instructions,
54+
["{{filename}}"] = ".repository_instructions.md",
55+
})
56+
57+
result.template = template:sub(1, s - 1) .. placeholder .. template:sub(e + 1)
58+
return result
59+
end,
60+
}
61+
62+
return M

0 commit comments

Comments
 (0)