Skip to content

Commit 9385962

Browse files
committed
feat: buffer state with context dir
1 parent bba6b9e commit 9385962

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

after/ftplugin/gpchat.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ vim.api.nvim_create_autocmd({ "BufEnter", "TextChanged", "InsertLeave" }, {
103103
M.helpers.delete_file(filename)
104104
end
105105

106+
local context_dir = headers["contextDir"] or "?"
107+
local new_context_dir = nil
108+
if context_dir ~= "?" and context_dir ~= "" then
109+
local full_path = vim.fn.fnamemodify(context_dir, ":p")
110+
if vim.fn.isdirectory(full_path) == 1 then
111+
new_context_dir = vim.fn.resolve(full_path)
112+
else
113+
M.logger.warning("gpchat: contextDir " .. full_path .. " is not a directory")
114+
end
115+
end
116+
M.buffer_state.set(buf, "context_dir", new_context_dir)
117+
106118
M.helpers.save_buffer(buf, "gpchat TextChanged InsertLeave autocmd")
107119
end,
108120
})

lua/gp/buffer_state.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
local logger = require("gp.logger")
2+
3+
local M = {}
4+
5+
local state = {}
6+
7+
---@param buf number # buffer number
8+
M.clear = function(buf)
9+
logger.debug("buffer state[" .. buf .. "] clear: current state: " .. vim.inspect(state[buf]))
10+
state[buf] = nil
11+
end
12+
13+
---@param buf number # buffer number
14+
---@return table # buffer state
15+
M.get = function(buf)
16+
logger.debug("buffer state[" .. buf .. "]: get: " .. vim.inspect(state[buf]))
17+
return state[buf] or {}
18+
end
19+
20+
---@param buf number # buffer number
21+
---@param key string # key to get
22+
---@return any # value of the key
23+
M.get_key = function(buf, key)
24+
local value = state[buf] and state[buf][key] or nil
25+
logger.debug("buffer state[" .. buf .. "] get_key: key '" .. key .. "' value: " .. vim.inspect(value))
26+
return value
27+
end
28+
29+
---@param buf number # buffer number
30+
---@param key string # key to set
31+
---@param value any # value to set
32+
M.set = function(buf, key, value)
33+
logger.debug("buffer state[" .. buf .. "]: set: key '" .. key .. "' to value: " .. vim.inspect(value))
34+
state[buf] = state[buf] or {}
35+
state[buf][key] = value
36+
logger.debug("buffer state[" .. buf .. "]: set: updated state: " .. vim.inspect(state[buf]))
37+
end
38+
39+
return M

lua/gp/defaults.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ M.chat_help = [[
1818
# Response generation can be terminated by using `{{stop_shortcut}}` or :{{cmd_prefix}}ChatStop command.
1919
# Chats are saved automatically. To delete this chat, use `{{delete_shortcut}}` or :{{cmd_prefix}}ChatDelete.
2020
# Be cautious of very long chats. Start a fresh chat by using `{{new_shortcut}}` or :{{cmd_prefix}}ChatNew.
21-
# See available macros by typing @ in the chat. Toggle this help by using `{{help_shortcut}}` or :{{cmd_prefix}}ChatHelp.]]
21+
# Add context macros by typing @ in the chat. Toggle this help by `{{help_shortcut}}` or :{{cmd_prefix}}ChatHelp.]]
2222

2323
M.chat_template = [[
2424
---

lua/gp/init.lua

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ local M = {
2828
vault = require("gp.vault"), -- handles secrets
2929
whisper = require("gp.whisper"), -- whisper module
3030
macro = require("gp.macro"), -- builder for macro completion
31+
buffer_state = require("gp.buffer_state"), -- buffer state module
3132
}
3233

3334
--------------------------------------------------------------------------------
@@ -267,6 +268,23 @@ M.setup = function(opts)
267268
end,
268269
})
269270

271+
vim.api.nvim_create_autocmd("BufEnter", {
272+
callback = function(ev)
273+
local buf = ev.buf
274+
local context_dir = M.buffer_state.get_key(buf, "context_dir")
275+
context_dir = context_dir or M.helpers.find_git_root()
276+
if context_dir == "" then
277+
context_dir = vim.fn.getcwd()
278+
end
279+
280+
local full_path = vim.fn.fnamemodify(context_dir, ":p")
281+
if vim.fn.isdirectory(full_path) == 1 then
282+
full_path = vim.fn.resolve(full_path)
283+
M.buffer_state.set(buf, "context_dir", full_path)
284+
end
285+
end,
286+
})
287+
270288
if vim.fn.executable("curl") == 0 then
271289
M.logger.error("curl is not installed, run :checkhealth gp")
272290
end
@@ -695,9 +713,16 @@ M.new_chat = function(params, toggle, system_prompt, agent)
695713
system_prompt = ""
696714
end
697715

716+
local context_dir = M.buffer_state.get_key(vim.api.nvim_get_current_buf(), "context_dir")
717+
context_dir = context_dir or M.helpers.find_git_root()
718+
if context_dir == "" then
719+
context_dir = vim.fn.getcwd()
720+
end
721+
context_dir = "contextDir: " .. context_dir .. "\n"
722+
698723
local template = M.render.template(M.config.chat_template or require("gp.defaults").chat_template, {
699724
["{{filename}}"] = string.match(filename, "([^/]+)$"),
700-
["{{optional_headers}}"] = model .. provider .. system_prompt,
725+
["{{optional_headers}}"] = model .. provider .. system_prompt .. context_dir,
701726
["{{user_prefix}}"] = M.config.chat_user_prefix,
702727
["{{respond_shortcut}}"] = M.config.chat_shortcut_respond.shortcut,
703728
["{{cmd_prefix}}"] = M.config.cmd_prefix,
@@ -1065,8 +1090,6 @@ M.chat_header_lines = function(lines)
10651090
return lines, 0, 0
10661091
end
10671092

1068-
local header_lines = table.concat(vim.list_slice(lines, 0, header_end + 1), "\n")
1069-
10701093
local help_template = M.render.template(M.defaults.chat_help, {
10711094
["{{user_prefix}}"] = M.config.chat_user_prefix,
10721095
["{{respond_shortcut}}"] = M.config.chat_shortcut_respond.shortcut,

lua/gp/macro.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
local logger = require("gp.logger")
2+
local buffer_state = require("gp.buffer_state")
23

34
---@class gp.Macro_cmd_params
45
---@field arg_lead string
56
---@field cmd_line string
67
---@field cursor_pos number
78
---@field cropped_line string
9+
---@field state table
810

911
---@class gp.Macro_parser_result
1012
---@field template string
@@ -65,7 +67,7 @@ M.build_parser = function(macros)
6567
local result = {
6668
template = " " .. template .. " ",
6769
artifacts = artifacts or {},
68-
state = state or {},
70+
state = state or buffer_state.get(vim.api.nvim_get_current_buf()),
6971
}
7072
logger.debug("macro parser input: " .. vim.inspect(result))
7173

@@ -103,6 +105,7 @@ M.build_completion = function(macros, raw)
103105
cmd_line = cmd_line,
104106
cursor_pos = cursor_pos,
105107
cropped_line = cropped_line,
108+
state = buffer_state.get(vim.api.nvim_get_current_buf()),
106109
}
107110

108111
cropped_line = " " .. cropped_line

0 commit comments

Comments
 (0)