Skip to content

Commit

Permalink
feat: context_file macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Robitx committed Sep 19, 2024
1 parent 19f1c03 commit 449052b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lua/gp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ local config = {
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
.. "\n\nRespond exclusively with the snippet that should be prepended before the selection above.",
template_command = "{{command}}",
template_context_file = "\n\nHere is a file {{filename}} for additional context:"
.. "\n\n```\n{{content}}\n```\n\n",

-- https://platform.openai.com/docs/guides/speech-to-text/quickstart
-- Whisper costs $0.006 / minute (rounded to the nearest second)
Expand Down
4 changes: 4 additions & 0 deletions lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ M.setup = function(opts)
local ft_completion = M.macro.build_completion({
require("gp.macros.target_filetype"),
require("gp.macros.agent"),
require("gp.macros.context_file"),
})

local base_completion = M.macro.build_completion({
require("gp.macros.agent"),
require("gp.macros.context_file"),
})

M.logger.debug("ft_completion done")
Expand All @@ -214,6 +216,7 @@ M.setup = function(opts)
require("gp.macros.target"),
require("gp.macros.target_filetype"),
require("gp.macros.target_filename"),
require("gp.macros.context_file"),
})

M.logger.debug("do_completion done")
Expand All @@ -223,6 +226,7 @@ M.setup = function(opts)
require("gp.macros.target"),
require("gp.macros.target_filetype"),
require("gp.macros.target_filename"),
require("gp.macros.context_file"),
})

M.logger.debug("command_parser done")
Expand Down
66 changes: 66 additions & 0 deletions lua/gp/macros/context_file.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local macro = require("gp.macro")
local gp = require("gp")

local M = {}

---@type gp.Macro
M = {
name = "context_file`",
description = "replaces the macro with the content of the specified file",
default = nil,
max_occurrences = 100,

triggered = function(params)
local cropped_line = params.cropped_line
return cropped_line:match("@context_file`[^`]*$")
end,

completion = function(params)
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:sub(root_dir_length) .. " `"
end, files)
return files
end,

parser = function(result)
local template = result.template
local macro_pattern = "@context_file`([^`]*)`"

for _ = 1, M.max_occurrences do
local s, e, value = template:find(macro_pattern)
if not value then
break
end

value = value:match("^%s*(.-)%s*$")
local placeholder = macro.generate_placeholder(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

if vim.fn.filereadable(full_path) == 0 then
result.artifacts[placeholder] = ""
gp.logger.error("Context file not found: " .. full_path)
else
local content = table.concat(vim.fn.readfile(full_path), "\n")
content = gp.render.template(gp.config.template_context_file, {
["{{content}}"] = content,
["{{filename}}"] = full_path,
})
result.artifacts[placeholder] = content
end

template = template:sub(1, s - 1) .. placeholder .. template:sub(e + 1)
end

result.template = template
return result
end,
}

return M

0 comments on commit 449052b

Please sign in to comment.