Skip to content

Commit 1d47d0f

Browse files
committed
chore: working target_filename macro
1 parent 9de1920 commit 1d47d0f

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

lua/gp/init.lua

+12-6
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ M.setup = function(opts)
199199
M.logger.debug("hook setup done")
200200

201201
local ft_completion = M.macro.build_completion({
202-
require("gp.macros.target_filetype"),
203202
require("gp.macros.agent"),
204203
require("gp.macros.context_file"),
204+
require("gp.macros.target_filename"),
205+
require("gp.macros.target_filetype"),
205206
})
206207

207208
local base_completion = M.macro.build_completion({
@@ -213,20 +214,20 @@ M.setup = function(opts)
213214

214215
local do_completion = M.macro.build_completion({
215216
require("gp.macros.agent"),
217+
require("gp.macros.context_file"),
216218
require("gp.macros.target"),
217-
require("gp.macros.target_filetype"),
218219
require("gp.macros.target_filename"),
219-
require("gp.macros.context_file"),
220+
require("gp.macros.target_filetype"),
220221
})
221222

222223
M.logger.debug("do_completion done")
223224

224225
M.command_parser = M.macro.build_parser({
225226
require("gp.macros.agent"),
227+
require("gp.macros.context_file"),
226228
require("gp.macros.target"),
227-
require("gp.macros.target_filetype"),
228229
require("gp.macros.target_filename"),
229-
require("gp.macros.context_file"),
230+
require("gp.macros.target_filetype"),
230231
})
231232

232233
M.chat_parser = M.macro.build_parser({
@@ -1969,7 +1970,7 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
19691970
win = vim.api.nvim_get_current_win()
19701971
end
19711972

1972-
buf = vim.api.nvim_create_buf(true, true)
1973+
buf = vim.api.nvim_create_buf(true, false)
19731974
vim.api.nvim_set_current_buf(buf)
19741975

19751976
local group = M.helpers.create_augroup("GpScratchSave" .. M.helpers.uuid(), { clear = true })
@@ -1986,6 +1987,11 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
19861987

19871988
local ft = state.target_filetype or target.filetype or filetype
19881989
vim.api.nvim_set_option_value("filetype", ft, { buf = buf })
1990+
local name = state.target_filename
1991+
if name then
1992+
vim.api.nvim_buf_set_name(buf, name)
1993+
M.helpers.save_buffer(buf, "Prompt created buffer")
1994+
end
19891995

19901996
handler = M.dispatcher.create_handler(buf, win, 0, false, "", cursor)
19911997
end

lua/gp/macros/target_filename.lua

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local macro = require("gp.macro")
2+
local gp = require("gp")
23

34
local M = {}
45

@@ -15,11 +16,11 @@ M = {
1516
end,
1617

1718
completion = function(params)
18-
-- TODO state.root_dir ?
19-
local files = vim.fn.glob("**", true, true)
20-
-- local files = vim.fn.getcompletion("", "file")
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
2122
files = vim.tbl_map(function(file)
22-
return file .. " `"
23+
return file:sub(root_dir_length) .. " `"
2324
end, files)
2425
return files
2526
end,
@@ -34,9 +35,14 @@ M = {
3435
value = value:match("^%s*(.-)%s*$")
3536
local placeholder = macro.generate_placeholder(M.name, value)
3637

37-
result.template = template:sub(1, s - 2) .. placeholder .. template:sub(e + 1)
38-
result.state[M.name] = value
38+
local full_path = value
39+
if vim.fn.fnamemodify(full_path, ":p") ~= value then
40+
full_path = vim.fn.fnamemodify(result.state.context_dir .. "/" .. value, ":p")
41+
end
42+
3943
result.artifacts[placeholder] = ""
44+
result.template = template:sub(1, s - 1) .. placeholder .. template:sub(e + 1)
45+
result.state[M.name:sub(1, -2)] = full_path
4046
return result
4147
end,
4248
}

0 commit comments

Comments
 (0)