Skip to content

Commit a8bf93b

Browse files
committed
chore: better errors for chat checks (issue: #104)
1 parent a0ab753 commit a8bf93b

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lua/gp/init.lua

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ M.setup = function(opts)
806806
if cmd == "Agent" then
807807
local buf = vim.api.nvim_get_current_buf()
808808
local file_name = vim.api.nvim_buf_get_name(buf)
809-
if M.is_chat(buf, file_name) then
809+
if M.not_chat(buf, file_name) == nil then
810810
return M._chat_agents
811811
end
812812
return M._command_agents
@@ -1372,28 +1372,31 @@ M.prep_md = function(buf)
13721372
M._H.feedkeys("<esc>", "xn")
13731373
end
13741374

1375-
M.is_chat = function(buf, file_name)
1375+
---@param buf number # buffer number
1376+
---@param file_name string # file name
1377+
---@return string | nil # reason for not being a chat or nil if it is a chat
1378+
M.not_chat = function(buf, file_name)
13761379
if not _H.starts_with(file_name, M.config.chat_dir) then
1377-
return false
1380+
return "not in chat directory (" .. M.config.chat_dir .. ")"
13781381
end
13791382

13801383
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
13811384
if #lines < 4 then
1382-
return false
1385+
return "file too short"
13831386
end
13841387

13851388
if not lines[1]:match("^# ") then
1386-
return false
1389+
return "missing topic header"
13871390
end
13881391

13891392
if not (lines[3]:match("^- file: ") or lines[4]:match("^- file: ")) then
1390-
return false
1393+
return "missing file header"
13911394
end
1392-
return true
1395+
return nil
13931396
end
13941397

13951398
M.prep_chat = function(buf, file_name)
1396-
if not M.is_chat(buf, file_name) then
1399+
if M.not_chat(buf, file_name) then
13971400
return
13981401
end
13991402

@@ -1834,8 +1837,9 @@ M.chat_respond = function(params)
18341837

18351838
-- check if file looks like a chat file
18361839
local file_name = vim.api.nvim_buf_get_name(buf)
1837-
if not M.is_chat(buf, file_name) then
1838-
M.warning("File " .. vim.inspect(file_name) .. " does not look like a chat file")
1840+
local reason = M.not_chat(buf, file_name)
1841+
if reason then
1842+
M.warning("File " .. vim.inspect(file_name) .. " does not look like a chat file: " .. vim.inspect(reason))
18391843
return
18401844
end
18411845

@@ -2374,7 +2378,7 @@ M.cmd.Agent = function(params)
23742378

23752379
local buf = vim.api.nvim_get_current_buf()
23762380
local file_name = vim.api.nvim_buf_get_name(buf)
2377-
local is_chat = M.is_chat(buf, file_name)
2381+
local is_chat = M.not_chat(buf, file_name) == nil
23782382
if is_chat and M.agents[agent_name].chat then
23792383
M._state.chat_agent = agent_name
23802384
M.info("Chat agent: " .. M._state.chat_agent)
@@ -2393,7 +2397,7 @@ end
23932397
M.cmd.NextAgent = function()
23942398
local buf = vim.api.nvim_get_current_buf()
23952399
local file_name = vim.api.nvim_buf_get_name(buf)
2396-
local is_chat = M.is_chat(buf, file_name)
2400+
local is_chat = M.not_chat(buf, file_name) == nil
23972401
local current_agent, agent_list
23982402

23992403
if is_chat then

0 commit comments

Comments
 (0)