Skip to content

Commit

Permalink
chore: working target_filename macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Robitx committed Sep 19, 2024
1 parent 9de1920 commit 1d47d0f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
18 changes: 12 additions & 6 deletions lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand Down Expand Up @@ -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 })
Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions lua/gp/macros/target_filename.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local macro = require("gp.macro")
local gp = require("gp")

local M = {}

Expand All @@ -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,
Expand All @@ -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,
}
Expand Down

0 comments on commit 1d47d0f

Please sign in to comment.