Skip to content

Commit d6252cb

Browse files
committed
feat: rm symlink dependency => last chat part of state
1 parent c1028a5 commit d6252cb

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

lua/gp/health.lua

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ function M.check()
3232
vim.health.error("grep is not installed")
3333
end
3434

35-
if vim.fn.executable("ln") == 1 then
36-
vim.health.ok("ln is installed")
37-
else
38-
vim.health.error("ln is not installed")
39-
end
40-
4135
require("gp.whisper").check_health()
4236
require("gp.deprecator").check_health()
4337
end

lua/gp/init.lua

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ M.setup = function(opts)
190190
-- register default commands
191191
for cmd, _ in pairs(M.cmd) do
192192
if M.hooks[cmd] == nil then
193-
M.helpers.create_user_command(M.config.cmd_prefix .. cmd, M.cmd[cmd], completions[cmd])
193+
M.helpers.create_user_command(M.config.cmd_prefix .. cmd, function(params)
194+
M.refresh_state()
195+
M.cmd[cmd](params)
196+
end, completions[cmd])
194197
end
195198
end
196199

@@ -213,16 +216,30 @@ M.refresh_state = function()
213216

214217
M.logger.debug("loaded state: " .. vim.inspect(state))
215218

216-
M._state.chat_agent = M._state.chat_agent or state.chat_agent or nil
217-
if M._state.chat_agent == nil or not M.agents[M._state.chat_agent] then
219+
if not state.updated then
220+
local last = M.config.chat_dir .. "/last.md"
221+
if vim.fn.filereadable(last) == 1 then
222+
os.remove(last)
223+
end
224+
end
225+
226+
if not M._state.updated or (state.updated and M._state.updated < state.updated) then
227+
M._state = state
228+
end
229+
M._state.updated = os.time()
230+
231+
if not M._state.chat_agent or not M.agents[M._state.chat_agent] then
218232
M._state.chat_agent = M._chat_agents[1]
219233
end
220234

221-
M._state.command_agent = M._state.command_agent or state.command_agent or nil
222-
if not M._state.command_agent == nil or not M.agents[M._state.command_agent] then
235+
if not M._state.command_agent or not M.agents[M._state.command_agent] then
223236
M._state.command_agent = M._command_agents[1]
224237
end
225238

239+
if M._state.last_chat and vim.fn.filereadable(M._state.last_chat) == 0 then
240+
M._state.last_chat = nil
241+
end
242+
226243
M.logger.debug("stored state: " .. vim.inspect(M._state))
227244
M.helpers.table_to_file(M._state, state_file)
228245

@@ -404,10 +421,7 @@ end
404421
M.not_chat = function(buf, file_name)
405422
file_name = vim.fn.resolve(file_name)
406423
local chat_dir = vim.fn.resolve(M.config.chat_dir)
407-
if vim.fn.has("win32") == 1 then
408-
file_name = file_name:gsub("\\", "/")
409-
chat_dir = chat_dir:gsub("\\", "/")
410-
end
424+
411425
if not M.helpers.starts_with(file_name, chat_dir) then
412426
return "resolved file (" .. file_name .. ") not in chat dir (" .. chat_dir .. ")"
413427
end
@@ -524,13 +538,8 @@ M.prep_chat = function(buf, file_name)
524538
vim.fn.matchadd("Conceal", [[^- role: .[^\\]*\zs\\.*\ze]], 10, -1, { conceal = "" })
525539
end
526540

527-
-- make last.md a symlink to the last opened chat file
528-
local last = M.config.chat_dir .. "/last.md"
529-
if file_name ~= last and vim.fn.has("win32") == 1 then
530-
os.execute("pwsh -Noprofile -c New-Item -Force -ItemType SymbolicLink -Path " .. last .. " -Target " .. file_name)
531-
elseif file_name ~= last then
532-
os.execute("ln -sf " .. file_name .. " " .. last)
533-
end
541+
M._state.last_chat = file_name
542+
M.refresh_state()
534543
end
535544

536545
M.buf_handler = function()
@@ -810,10 +819,8 @@ M.cmd.ChatToggle = function(params, system_prompt, agent)
810819

811820
-- if the range is 2, we want to create a new chat file with the selection
812821
if params.range ~= 2 then
813-
-- check if last.md chat file exists and open it
814-
local last = M.config.chat_dir .. "/last.md"
815-
if vim.fn.filereadable(last) == 1 then
816-
-- resolve symlink
822+
local last = M._state.last_chat
823+
if last and vim.fn.filereadable(last) == 1 then
817824
last = vim.fn.resolve(last)
818825
M.open_buf(last, M.resolve_buf_target(params), M._toggle_kind.chat, true)
819826
return
@@ -833,10 +840,9 @@ M.cmd.ChatPaste = function(params)
833840
-- get current buffer
834841
local cbuf = vim.api.nvim_get_current_buf()
835842

836-
local last = M.config.chat_dir .. "/last.md"
837-
838843
-- make new chat if last doesn't exist
839-
if vim.fn.filereadable(last) ~= 1 then
844+
local last = M._state.last_chat
845+
if not last or vim.fn.filereadable(last) ~= 1 then
840846
-- skip rest since new chat will handle snippet on it's own
841847
M.cmd.ChatNew(params, nil, nil)
842848
return

0 commit comments

Comments
 (0)