From 1d47d0fd6c3fd537b871ede2a0b52b89337f87aa Mon Sep 17 00:00:00 2001 From: Tibor Schmidt Date: Sat, 14 Sep 2024 10:19:24 +0200 Subject: [PATCH] chore: working target_filename macro --- lua/gp/init.lua | 18 ++++++++++++------ lua/gp/macros/target_filename.lua | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lua/gp/init.lua b/lua/gp/init.lua index cb5157b..073fc35 100644 --- a/lua/gp/init.lua +++ b/lua/gp/init.lua @@ -199,9 +199,10 @@ M.setup = function(opts) M.logger.debug("hook setup done") local ft_completion = M.macro.build_completion({ - require("gp.macros.target_filetype"), require("gp.macros.agent"), require("gp.macros.context_file"), + require("gp.macros.target_filename"), + require("gp.macros.target_filetype"), }) local base_completion = M.macro.build_completion({ @@ -213,20 +214,20 @@ M.setup = function(opts) local do_completion = M.macro.build_completion({ require("gp.macros.agent"), + require("gp.macros.context_file"), require("gp.macros.target"), - require("gp.macros.target_filetype"), require("gp.macros.target_filename"), - require("gp.macros.context_file"), + require("gp.macros.target_filetype"), }) M.logger.debug("do_completion done") M.command_parser = M.macro.build_parser({ require("gp.macros.agent"), + require("gp.macros.context_file"), require("gp.macros.target"), - require("gp.macros.target_filetype"), require("gp.macros.target_filename"), - require("gp.macros.context_file"), + require("gp.macros.target_filetype"), }) M.chat_parser = M.macro.build_parser({ @@ -1969,7 +1970,7 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback) win = vim.api.nvim_get_current_win() end - buf = vim.api.nvim_create_buf(true, true) + buf = vim.api.nvim_create_buf(true, false) vim.api.nvim_set_current_buf(buf) 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) local ft = state.target_filetype or target.filetype or filetype vim.api.nvim_set_option_value("filetype", ft, { buf = buf }) + local name = state.target_filename + if name then + vim.api.nvim_buf_set_name(buf, name) + M.helpers.save_buffer(buf, "Prompt created buffer") + end handler = M.dispatcher.create_handler(buf, win, 0, false, "", cursor) end diff --git a/lua/gp/macros/target_filename.lua b/lua/gp/macros/target_filename.lua index 47becec..e5d6d20 100644 --- a/lua/gp/macros/target_filename.lua +++ b/lua/gp/macros/target_filename.lua @@ -1,4 +1,5 @@ local macro = require("gp.macro") +local gp = require("gp") local M = {} @@ -15,11 +16,11 @@ M = { end, completion = function(params) - -- TODO state.root_dir ? - local files = vim.fn.glob("**", true, true) - -- local files = vim.fn.getcompletion("", "file") + local root_dir = params.state.context_dir or vim.fn.getcwd() + local files = vim.fn.globpath(root_dir, "**", false, true) + local root_dir_length = #root_dir + 2 files = vim.tbl_map(function(file) - return file .. " `" + return file:sub(root_dir_length) .. " `" end, files) return files end, @@ -34,9 +35,14 @@ M = { value = value:match("^%s*(.-)%s*$") local placeholder = macro.generate_placeholder(M.name, value) - result.template = template:sub(1, s - 2) .. placeholder .. template:sub(e + 1) - result.state[M.name] = value + local full_path = value + if vim.fn.fnamemodify(full_path, ":p") ~= value then + full_path = vim.fn.fnamemodify(result.state.context_dir .. "/" .. value, ":p") + end + result.artifacts[placeholder] = "" + result.template = template:sub(1, s - 1) .. placeholder .. template:sub(e + 1) + result.state[M.name:sub(1, -2)] = full_path return result end, }