irongit

My nvim configuration

refactor(claude): leadercp resumes session rather than starting a new one

huncholanehuncholaneauthored
parent 20b7eebcommit 4c290a0dc6cbc6fbce207b19a287eac8481ec85aBrowse files

2 files changed, +63 -13

+4-0claude-nvim/lua/claude/init.lua
@@ -41,6 +41,10 @@ function M.setup(opts)
4141 vim.keymap.set("v", "<leader>c", function()
4242 edit.prompt_visual()
4343 end, { desc = "Claude: Edit selection with prompt" })
44+
45+ vim.keymap.set("v", "<leader>p", function()
46+ ui.prompt_visual_chat()
47+ end, { desc = "Claude: Chat with selection as context" })
4448 end
4549
4650 return M
+59-13claude-nvim/lua/claude/ui.lua
@@ -270,6 +270,7 @@ function M.open_input(opts)
270270 end
271271 local sid = panel.sid
272272 local return_to = opts.return_to
273+ local context = opts.context
273274 local input_buf = vim.api.nvim_create_buf(false, true)
274275 vim.bo[input_buf].bufhidden = "wipe"
275276 vim.bo[input_buf].filetype = "markdown"
@@ -284,7 +285,8 @@ function M.open_input(opts)
284285 height = height,
285286 style = "minimal",
286287 border = "rounded",
287- title = " Claude prompt — <C-s> send <C-c> cancel ",
288+ title = context and " Claude prompt (with context) — <C-s> send <C-c> cancel "
289+ or " Claude prompt — <C-s> send <C-c> cancel ",
288290 title_pos = "center",
289291 })
290292 vim.cmd("startinsert")
@@ -307,6 +309,9 @@ function M.open_input(opts)
307309 local text = table.concat(lines, "\n")
308310 close()
309311 if text:match("^%s*$") then return end
312+ if context and context ~= "" then
313+ text = context .. "\n\n" .. text
314+ end
310315 local runner = require("claude.runner")
311316 local ok, err = runner.send(sid, text)
312317 if not ok then
@@ -320,13 +325,55 @@ function M.open_input(opts)
320325 vim.keymap.set("n", "<Esc>", close, km)
321326 end
322327
328+local function capture_visual_selection()
329+ local old_reg = vim.fn.getreg('"')
330+ local old_regtype = vim.fn.getregtype('"')
331+ vim.cmd('noautocmd normal! gv"vy')
332+ local sel = vim.fn.getreg("v")
333+ vim.fn.setreg('"', old_reg, old_regtype)
334+ return sel or ""
335+end
336+
337+function M.prompt_visual_chat()
338+ local origin_buf = vim.api.nvim_get_current_buf()
339+ local origin_win = vim.api.nvim_get_current_win()
340+ local sel = capture_visual_selection()
341+ if sel == "" then
342+ vim.notify("claude: empty selection", vim.log.levels.WARN)
343+ return
344+ end
345+ local s = vim.fn.getpos("'<")
346+ local e = vim.fn.getpos("'>")
347+ local fname = vim.api.nvim_buf_get_name(origin_buf)
348+ local rel = fname ~= "" and vim.fn.fnamemodify(fname, ":.") or "[No Name]"
349+ local ft = vim.bo[origin_buf].filetype or ""
350+ local context = string.format(
351+ "<context>\n%s:%d-%d\n```%s\n%s\n```\n</context>",
352+ rel, s[2], e[2], ft, sel
353+ )
354+
355+ if not M.is_open() then
356+ if panel.sid then
357+ M.open_panel()
358+ else
359+ M.start_new()
360+ end
361+ end
362+ M.open_input({ return_to = origin_win, context = context })
363+end
364+
323365 function M.open_prompt_keep_focus()
324366 local origin = vim.api.nvim_get_current_win()
325367 if not M.is_open() then
326368 if panel.sid then
327369 M.open_panel()
328370 else
329- M.start_new()
371+ local store = require("claude.store")
372+ if store.read_pointer() then
373+ M.resume_last()
374+ else
375+ M.start_new()
376+ end
330377 end
331378 end
332379 M.open_input({ return_to = origin })
@@ -336,17 +383,16 @@ end
336383
337384 function M.append_user_prompt(sid, text)
338385 if sid ~= panel.sid then return end
339- vim.schedule(function()
340- local lines = { "## You", "" }
341- for _, l in ipairs(vim.split(text, "\n", { plain = true })) do
342- table.insert(lines, l)
343- end
344- table.insert(lines, "")
345- table.insert(lines, "## Claude")
346- table.insert(lines, "")
347- append_lines(lines)
348- spinner_start()
349- end)
386+ local lines = { "## You", "" }
387+ for _, l in ipairs(vim.split(text, "\n", { plain = true })) do
388+ table.insert(lines, l)
389+ end
390+ table.insert(lines, "")
391+ table.insert(lines, "## Claude")
392+ table.insert(lines, "")
393+ append_lines(lines)
394+ spinner_start()
395+ vim.cmd("redraw")
350396 end
351397
352398 function M.append_assistant_text(sid, text)