irongit

My nvim configuration

fix(claude): correct context in highlight mode

huncholanehuncholaneauthored
parent 33dc5bdcommit 6458fa5c25b4c02c0c06e520a9fe58875cd4cf58Browse files

1 file changed, +21 -8

+21-8claude-nvim/lua/claude/ui.lua
@@ -337,19 +337,32 @@ function M.open_input(opts)
337337 vim.keymap.set("n", "<Esc>", close, km)
338338 end
339339
340-local function capture_visual_selection()
341- local old_reg = vim.fn.getreg('"')
342- local old_regtype = vim.fn.getregtype('"')
343- vim.cmd('noautocmd normal! gv"vy')
344- local sel = vim.fn.getreg("v")
345- vim.fn.setreg('"', old_reg, old_regtype)
346- return sel or ""
340+local function capture_visual_selection(bufnr)
341+ local s = vim.fn.getpos("'<")
342+ local e = vim.fn.getpos("'>")
343+ local sr, sc = s[2] - 1, s[3] - 1
344+ local er, ec = e[2] - 1, e[3]
345+ if sr < 0 or er < 0 then return "" end
346+ local mode = vim.fn.visualmode()
347+ if mode == "V" then
348+ local lines = vim.api.nvim_buf_get_lines(bufnr, sr, er + 1, false)
349+ return table.concat(lines, "\n")
350+ end
351+ local last_line = vim.api.nvim_buf_get_lines(bufnr, er, er + 1, false)[1] or ""
352+ ec = math.min(ec, #last_line)
353+ sc = math.max(0, sc)
354+ local ok, lines = pcall(vim.api.nvim_buf_get_text, bufnr, sr, sc, er, ec, {})
355+ if ok and lines then return table.concat(lines, "\n") end
356+ return ""
347357 end
348358
349359 function M.prompt_visual_chat()
350360 local origin_buf = vim.api.nvim_get_current_buf()
351361 local origin_win = vim.api.nvim_get_current_win()
352- local sel = capture_visual_selection()
362+ if vim.fn.mode():match("^[vV\22]") then
363+ vim.cmd("noautocmd normal! \27")
364+ end
365+ local sel = capture_visual_selection(origin_buf)
353366 if sel == "" then
354367 vim.notify("claude: empty selection", vim.log.levels.WARN)
355368 return