My nvim configuration
feat(claude): per tab directory for new sessions
3 files changed, +257 -246
+5-3claude-nvim/lua/claude/runner.lua
| @@ -275,10 +275,11 @@ function M.attach_existing(session) | ||
| 275 | 275 | return session.id |
| 276 | 276 | end |
| 277 | 277 | |
| 278 | -function M.resume_from_pointer() | |
| 279 | - local ptr = store.read_pointer() | |
| 278 | +function M.resume_from_pointer(cwd) | |
| 279 | + cwd = cwd or vim.fn.getcwd() | |
| 280 | + local ptr = store.read_pointer(cwd) | |
| 280 | 281 | if not ptr then return nil end |
| 281 | - local existing = ptr.session_id and store.load(ptr.session_id) or nil | |
| 282 | + local existing = ptr.session_id and store.load(ptr.session_id, cwd) or nil | |
| 282 | 283 | if existing then |
| 283 | 284 | return M.attach_existing(existing) |
| 284 | 285 | end |
| @@ -324,6 +325,7 @@ function M.send(sid, prompt_text, opts) | ||
| 324 | 325 | log("CMD", table.concat(cmd, " | ")) |
| 325 | 326 | |
| 326 | 327 | st.job = vim.fn.jobstart(cmd, { |
| 328 | + cwd = st.session.cwd, | |
| 327 | 329 | stdout_buffered = false, |
| 328 | 330 | on_stdout = function(_, data) on_stdout(sid, data) end, |
| 329 | 331 | on_stderr = function(_, data) |
+16-16claude-nvim/lua/claude/store.lua
| @@ -6,32 +6,32 @@ local function data_dir() | ||
| 6 | 6 | return d |
| 7 | 7 | end |
| 8 | 8 | |
| 9 | -local function cwd_dir() | |
| 10 | - local cwd = vim.fn.getcwd() | |
| 9 | +local function cwd_dir(cwd) | |
| 10 | + cwd = cwd or vim.fn.getcwd() | |
| 11 | 11 | local enc = cwd:gsub("/", "%%") |
| 12 | 12 | local d = data_dir() .. "/" .. enc |
| 13 | 13 | vim.fn.mkdir(d, "p") |
| 14 | 14 | return d |
| 15 | 15 | end |
| 16 | 16 | |
| 17 | -function M.session_path(id) | |
| 18 | - return cwd_dir() .. "/session-" .. id .. ".json" | |
| 17 | +function M.session_path(id, cwd) | |
| 18 | + return cwd_dir(cwd) .. "/session-" .. id .. ".json" | |
| 19 | 19 | end |
| 20 | 20 | |
| 21 | -local function pointer_path() | |
| 22 | - return cwd_dir() .. "/last.json" | |
| 21 | +local function pointer_path(cwd) | |
| 22 | + return cwd_dir(cwd) .. "/last.json" | |
| 23 | 23 | end |
| 24 | 24 | |
| 25 | 25 | function M.write_pointer(info) |
| 26 | - local f = io.open(pointer_path(), "w") | |
| 26 | + local f = io.open(pointer_path(info and info.cwd), "w") | |
| 27 | 27 | if not f then return false end |
| 28 | 28 | f:write(vim.json.encode(info)) |
| 29 | 29 | f:close() |
| 30 | 30 | return true |
| 31 | 31 | end |
| 32 | 32 | |
| 33 | -function M.read_pointer() | |
| 34 | - local f = io.open(pointer_path(), "r") | |
| 33 | +function M.read_pointer(cwd) | |
| 34 | + local f = io.open(pointer_path(cwd), "r") | |
| 35 | 35 | if not f then return nil end |
| 36 | 36 | local content = f:read("*a") |
| 37 | 37 | f:close() |
| @@ -40,8 +40,8 @@ function M.read_pointer() | ||
| 40 | 40 | return data |
| 41 | 41 | end |
| 42 | 42 | |
| 43 | -function M.list_sessions() | |
| 44 | - local d = cwd_dir() | |
| 43 | +function M.list_sessions(cwd) | |
| 44 | + local d = cwd_dir(cwd) | |
| 45 | 45 | local files = vim.fn.glob(d .. "/session-*.json", false, true) |
| 46 | 46 | local out = {} |
| 47 | 47 | for _, f in ipairs(files) do |
| @@ -64,21 +64,21 @@ function M.load_path(path) | ||
| 64 | 64 | return data |
| 65 | 65 | end |
| 66 | 66 | |
| 67 | -function M.load(id) | |
| 68 | - return M.load_path(M.session_path(id)) | |
| 67 | +function M.load(id, cwd) | |
| 68 | + return M.load_path(M.session_path(id, cwd)) | |
| 69 | 69 | end |
| 70 | 70 | |
| 71 | 71 | function M.save(session) |
| 72 | - local f = io.open(M.session_path(session.id), "w") | |
| 72 | + local f = io.open(M.session_path(session.id, session.cwd), "w") | |
| 73 | 73 | if not f then return false end |
| 74 | 74 | f:write(vim.json.encode(session)) |
| 75 | 75 | f:close() |
| 76 | 76 | return true |
| 77 | 77 | end |
| 78 | 78 | |
| 79 | -function M.all_prompts() | |
| 79 | +function M.all_prompts(cwd) | |
| 80 | 80 | local out = {} |
| 81 | - for _, entry in ipairs(M.list_sessions()) do | |
| 81 | + for _, entry in ipairs(M.list_sessions(cwd)) do | |
| 82 | 82 | local s = M.load_path(entry.path) |
| 83 | 83 | if s and s.prompts then |
| 84 | 84 | for _, p in ipairs(s.prompts) do |
+236-227claude-nvim/lua/claude/ui.lua
| @@ -1,96 +1,122 @@ | ||
| 1 | 1 | local M = {} |
| 2 | 2 | |
| 3 | -local panel = { | |
| 4 | - buf = nil, | |
| 5 | - win = nil, | |
| 6 | - sid = nil, | |
| 7 | - width = 80, | |
| 8 | -} | |
| 9 | - | |
| 10 | -local spinner = { | |
| 11 | - timer = nil, | |
| 12 | - frame = 1, | |
| 13 | - frames = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" }, | |
| 14 | - active = false, | |
| 15 | - row = nil, | |
| 16 | -} | |
| 17 | - | |
| 18 | -local function spinner_text() | |
| 19 | - return spinner.frames[spinner.frame] .. " thinking…" | |
| 20 | -end | |
| 21 | - | |
| 22 | -local function scroll_to_bottom_now() | |
| 23 | - if panel.win and vim.api.nvim_win_is_valid(panel.win) then | |
| 24 | - pcall(vim.api.nvim_win_call, panel.win, function() | |
| 3 | +local default_width = 80 | |
| 4 | + | |
| 5 | +-- panels keyed by tabpage handle | |
| 6 | +local panels = {} | |
| 7 | + | |
| 8 | +local SPINNER_FRAMES = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } | |
| 9 | + | |
| 10 | +local function new_panel(tab) | |
| 11 | + return { | |
| 12 | + tab = tab, | |
| 13 | + buf = nil, | |
| 14 | + win = nil, | |
| 15 | + sid = nil, | |
| 16 | + width = default_width, | |
| 17 | + spinner = { | |
| 18 | + timer = nil, | |
| 19 | + frame = 1, | |
| 20 | + active = false, | |
| 21 | + row = nil, | |
| 22 | + }, | |
| 23 | + } | |
| 24 | +end | |
| 25 | + | |
| 26 | +local function get_panel() | |
| 27 | + local tab = vim.api.nvim_get_current_tabpage() | |
| 28 | + local p = panels[tab] | |
| 29 | + if not p then | |
| 30 | + p = new_panel(tab) | |
| 31 | + panels[tab] = p | |
| 32 | + end | |
| 33 | + return p | |
| 34 | +end | |
| 35 | + | |
| 36 | +local function find_panel_by_sid(sid) | |
| 37 | + if not sid then return nil end | |
| 38 | + for _, p in pairs(panels) do | |
| 39 | + if p.sid == sid then return p end | |
| 40 | + end | |
| 41 | + return nil | |
| 42 | +end | |
| 43 | + | |
| 44 | +local function spinner_text(p) | |
| 45 | + return SPINNER_FRAMES[p.spinner.frame] .. " thinking…" | |
| 46 | +end | |
| 47 | + | |
| 48 | +local function scroll_to_bottom_now(p) | |
| 49 | + if p.win and vim.api.nvim_win_is_valid(p.win) then | |
| 50 | + pcall(vim.api.nvim_win_call, p.win, function() | |
| 25 | 51 | vim.cmd("normal! G") |
| 26 | 52 | end) |
| 27 | 53 | end |
| 28 | 54 | end |
| 29 | 55 | |
| 30 | -local function spinner_remove_if_present() | |
| 31 | - if spinner.row == nil then return end | |
| 32 | - if not panel.buf or not vim.api.nvim_buf_is_valid(panel.buf) then | |
| 33 | - spinner.row = nil | |
| 56 | +local function spinner_remove_if_present(p) | |
| 57 | + if p.spinner.row == nil then return end | |
| 58 | + if not p.buf or not vim.api.nvim_buf_is_valid(p.buf) then | |
| 59 | + p.spinner.row = nil | |
| 34 | 60 | return |
| 35 | 61 | end |
| 36 | - local lc = vim.api.nvim_buf_line_count(panel.buf) | |
| 37 | - if spinner.row < lc then | |
| 38 | - vim.bo[panel.buf].modifiable = true | |
| 39 | - pcall(vim.api.nvim_buf_set_lines, panel.buf, spinner.row, spinner.row + 1, false, {}) | |
| 40 | - vim.bo[panel.buf].modifiable = false | |
| 62 | + local lc = vim.api.nvim_buf_line_count(p.buf) | |
| 63 | + if p.spinner.row < lc then | |
| 64 | + vim.bo[p.buf].modifiable = true | |
| 65 | + pcall(vim.api.nvim_buf_set_lines, p.buf, p.spinner.row, p.spinner.row + 1, false, {}) | |
| 66 | + vim.bo[p.buf].modifiable = false | |
| 41 | 67 | end |
| 42 | - spinner.row = nil | |
| 68 | + p.spinner.row = nil | |
| 43 | 69 | end |
| 44 | 70 | |
| 45 | -local function spinner_render() | |
| 46 | - if not spinner.active then return end | |
| 47 | - if not panel.buf or not vim.api.nvim_buf_is_valid(panel.buf) then return end | |
| 48 | - vim.bo[panel.buf].modifiable = true | |
| 49 | - if spinner.row ~= nil and spinner.row < vim.api.nvim_buf_line_count(panel.buf) then | |
| 50 | - pcall(vim.api.nvim_buf_set_lines, panel.buf, spinner.row, spinner.row + 1, false, { spinner_text() }) | |
| 71 | +local function spinner_render(p) | |
| 72 | + if not p.spinner.active then return end | |
| 73 | + if not p.buf or not vim.api.nvim_buf_is_valid(p.buf) then return end | |
| 74 | + vim.bo[p.buf].modifiable = true | |
| 75 | + if p.spinner.row ~= nil and p.spinner.row < vim.api.nvim_buf_line_count(p.buf) then | |
| 76 | + pcall(vim.api.nvim_buf_set_lines, p.buf, p.spinner.row, p.spinner.row + 1, false, { spinner_text(p) }) | |
| 51 | 77 | else |
| 52 | - local lc = vim.api.nvim_buf_line_count(panel.buf) | |
| 53 | - pcall(vim.api.nvim_buf_set_lines, panel.buf, lc, lc, false, { spinner_text() }) | |
| 54 | - spinner.row = lc | |
| 55 | - end | |
| 56 | - vim.bo[panel.buf].modifiable = false | |
| 57 | - scroll_to_bottom_now() | |
| 58 | -end | |
| 59 | - | |
| 60 | -local function spinner_stop() | |
| 61 | - spinner.active = false | |
| 62 | - if spinner.timer then | |
| 63 | - spinner.timer:stop() | |
| 64 | - if not spinner.timer:is_closing() then spinner.timer:close() end | |
| 65 | - spinner.timer = nil | |
| 66 | - end | |
| 67 | - spinner_remove_if_present() | |
| 68 | -end | |
| 69 | - | |
| 70 | -local function spinner_start() | |
| 71 | - spinner_stop() | |
| 72 | - spinner.active = true | |
| 73 | - spinner.frame = 1 | |
| 74 | - spinner_render() | |
| 75 | - spinner.timer = vim.uv.new_timer() | |
| 76 | - spinner.timer:start(90, 90, vim.schedule_wrap(function() | |
| 77 | - if not spinner.active then return end | |
| 78 | - spinner.frame = (spinner.frame % #spinner.frames) + 1 | |
| 79 | - spinner_render() | |
| 78 | + local lc = vim.api.nvim_buf_line_count(p.buf) | |
| 79 | + pcall(vim.api.nvim_buf_set_lines, p.buf, lc, lc, false, { spinner_text(p) }) | |
| 80 | + p.spinner.row = lc | |
| 81 | + end | |
| 82 | + vim.bo[p.buf].modifiable = false | |
| 83 | + scroll_to_bottom_now(p) | |
| 84 | +end | |
| 85 | + | |
| 86 | +local function spinner_stop(p) | |
| 87 | + p.spinner.active = false | |
| 88 | + if p.spinner.timer then | |
| 89 | + p.spinner.timer:stop() | |
| 90 | + if not p.spinner.timer:is_closing() then p.spinner.timer:close() end | |
| 91 | + p.spinner.timer = nil | |
| 92 | + end | |
| 93 | + spinner_remove_if_present(p) | |
| 94 | +end | |
| 95 | + | |
| 96 | +local function spinner_start(p) | |
| 97 | + spinner_stop(p) | |
| 98 | + p.spinner.active = true | |
| 99 | + p.spinner.frame = 1 | |
| 100 | + spinner_render(p) | |
| 101 | + p.spinner.timer = vim.uv.new_timer() | |
| 102 | + p.spinner.timer:start(90, 90, vim.schedule_wrap(function() | |
| 103 | + if not p.spinner.active then return end | |
| 104 | + p.spinner.frame = (p.spinner.frame % #SPINNER_FRAMES) + 1 | |
| 105 | + spinner_render(p) | |
| 80 | 106 | end)) |
| 81 | 107 | end |
| 82 | 108 | |
| 83 | -local function ensure_buf() | |
| 84 | - if panel.buf and vim.api.nvim_buf_is_valid(panel.buf) then | |
| 85 | - return panel.buf | |
| 109 | +local function ensure_buf(p) | |
| 110 | + if p.buf and vim.api.nvim_buf_is_valid(p.buf) then | |
| 111 | + return p.buf | |
| 86 | 112 | end |
| 87 | 113 | local buf = vim.api.nvim_create_buf(false, true) |
| 88 | 114 | vim.bo[buf].buftype = "nofile" |
| 89 | 115 | vim.bo[buf].swapfile = false |
| 90 | 116 | vim.bo[buf].filetype = "markdown" |
| 91 | 117 | vim.bo[buf].buflisted = false |
| 92 | - pcall(vim.api.nvim_buf_set_name, buf, "claude://panel") | |
| 93 | - panel.buf = buf | |
| 118 | + pcall(vim.api.nvim_buf_set_name, buf, "claude://panel/" .. tostring(p.tab)) | |
| 119 | + p.buf = buf | |
| 94 | 120 | |
| 95 | 121 | local function open_input() M.open_input() end |
| 96 | 122 | vim.keymap.set("n", "i", open_input, { buffer = buf, desc = "Claude: prompt" }) |
| @@ -102,25 +128,21 @@ local function ensure_buf() | ||
| 102 | 128 | return buf |
| 103 | 129 | end |
| 104 | 130 | |
| 105 | -local function scroll_to_bottom() | |
| 106 | - scroll_to_bottom_now() | |
| 107 | -end | |
| 108 | - | |
| 109 | -local function append_lines(lines) | |
| 110 | - local buf = panel.buf | |
| 131 | +local function append_lines(p, lines) | |
| 132 | + local buf = p.buf | |
| 111 | 133 | if not buf or not vim.api.nvim_buf_is_valid(buf) then return end |
| 112 | - spinner_remove_if_present() | |
| 134 | + spinner_remove_if_present(p) | |
| 113 | 135 | vim.bo[buf].modifiable = true |
| 114 | 136 | vim.api.nvim_buf_set_lines(buf, -1, -1, false, lines) |
| 115 | 137 | vim.bo[buf].modifiable = false |
| 116 | - spinner_render() | |
| 117 | - scroll_to_bottom() | |
| 138 | + spinner_render(p) | |
| 139 | + scroll_to_bottom_now(p) | |
| 118 | 140 | end |
| 119 | 141 | |
| 120 | -local function append_text_streaming(text) | |
| 121 | - local buf = panel.buf | |
| 142 | +local function append_text_streaming(p, text) | |
| 143 | + local buf = p.buf | |
| 122 | 144 | if not buf or not vim.api.nvim_buf_is_valid(buf) then return end |
| 123 | - spinner_remove_if_present() | |
| 145 | + spinner_remove_if_present(p) | |
| 124 | 146 | vim.bo[buf].modifiable = true |
| 125 | 147 | local lc = vim.api.nvim_buf_line_count(buf) |
| 126 | 148 | local last = vim.api.nvim_buf_get_lines(buf, lc - 1, lc, false)[1] or "" |
| @@ -128,56 +150,72 @@ local function append_text_streaming(text) | ||
| 128 | 150 | parts[1] = last .. parts[1] |
| 129 | 151 | vim.api.nvim_buf_set_lines(buf, lc - 1, lc, false, parts) |
| 130 | 152 | vim.bo[buf].modifiable = false |
| 131 | - spinner_render() | |
| 132 | - scroll_to_bottom() | |
| 153 | + spinner_render(p) | |
| 154 | + scroll_to_bottom_now(p) | |
| 133 | 155 | end |
| 134 | 156 | |
| 135 | -local function clear_buf() | |
| 136 | - local buf = panel.buf | |
| 157 | +local function clear_buf(p) | |
| 158 | + local buf = p.buf | |
| 137 | 159 | if not buf or not vim.api.nvim_buf_is_valid(buf) then return end |
| 138 | - spinner.row = nil | |
| 160 | + p.spinner.row = nil | |
| 139 | 161 | vim.bo[buf].modifiable = true |
| 140 | 162 | vim.api.nvim_buf_set_lines(buf, 0, -1, false, {}) |
| 141 | 163 | vim.bo[buf].modifiable = false |
| 142 | 164 | end |
| 143 | 165 | |
| 144 | 166 | function M.setup(opts) |
| 145 | - panel.width = opts.width or 80 | |
| 167 | + default_width = opts.width or 80 | |
| 168 | + | |
| 169 | + vim.api.nvim_create_autocmd("TabClosed", { | |
| 170 | + callback = function(ev) | |
| 171 | + local tabnr = tonumber(ev.file) | |
| 172 | + if not tabnr then return end | |
| 173 | + for tab, _ in pairs(panels) do | |
| 174 | + if not vim.api.nvim_tabpage_is_valid(tab) then | |
| 175 | + panels[tab] = nil | |
| 176 | + end | |
| 177 | + end | |
| 178 | + end, | |
| 179 | + }) | |
| 146 | 180 | end |
| 147 | 181 | |
| 148 | 182 | function M.open_panel() |
| 149 | - local buf = ensure_buf() | |
| 150 | - if panel.win and vim.api.nvim_win_is_valid(panel.win) then | |
| 151 | - vim.api.nvim_set_current_win(panel.win) | |
| 183 | + local p = get_panel() | |
| 184 | + local buf = ensure_buf(p) | |
| 185 | + if p.win and vim.api.nvim_win_is_valid(p.win) then | |
| 186 | + vim.api.nvim_set_current_win(p.win) | |
| 152 | 187 | return |
| 153 | 188 | end |
| 154 | 189 | vim.cmd("botright vsplit") |
| 155 | - panel.win = vim.api.nvim_get_current_win() | |
| 156 | - vim.api.nvim_win_set_buf(panel.win, buf) | |
| 157 | - vim.api.nvim_win_set_width(panel.win, panel.width) | |
| 158 | - vim.wo[panel.win].wrap = true | |
| 159 | - vim.wo[panel.win].linebreak = true | |
| 160 | - vim.wo[panel.win].number = false | |
| 161 | - vim.wo[panel.win].relativenumber = false | |
| 162 | - scroll_to_bottom() | |
| 190 | + p.win = vim.api.nvim_get_current_win() | |
| 191 | + vim.api.nvim_win_set_buf(p.win, buf) | |
| 192 | + vim.api.nvim_win_set_width(p.win, p.width) | |
| 193 | + vim.wo[p.win].wrap = true | |
| 194 | + vim.wo[p.win].linebreak = true | |
| 195 | + vim.wo[p.win].number = false | |
| 196 | + vim.wo[p.win].relativenumber = false | |
| 197 | + scroll_to_bottom_now(p) | |
| 163 | 198 | end |
| 164 | 199 | |
| 165 | 200 | function M.close() |
| 166 | - if panel.win and vim.api.nvim_win_is_valid(panel.win) then | |
| 167 | - vim.api.nvim_win_hide(panel.win) | |
| 168 | - panel.win = nil | |
| 201 | + local p = get_panel() | |
| 202 | + if p.win and vim.api.nvim_win_is_valid(p.win) then | |
| 203 | + vim.api.nvim_win_hide(p.win) | |
| 204 | + p.win = nil | |
| 169 | 205 | end |
| 170 | 206 | end |
| 171 | 207 | |
| 172 | 208 | function M.is_open() |
| 173 | - return panel.win and vim.api.nvim_win_is_valid(panel.win) | |
| 209 | + local p = get_panel() | |
| 210 | + return p.win and vim.api.nvim_win_is_valid(p.win) | |
| 174 | 211 | end |
| 175 | 212 | |
| 176 | 213 | function M.toggle() |
| 214 | + local p = get_panel() | |
| 177 | 215 | if M.is_open() then |
| 178 | 216 | M.close() |
| 179 | 217 | else |
| 180 | - if not panel.sid then | |
| 218 | + if not p.sid then | |
| 181 | 219 | M.start_new() |
| 182 | 220 | else |
| 183 | 221 | M.open_panel() |
| @@ -186,12 +224,13 @@ function M.toggle() | ||
| 186 | 224 | end |
| 187 | 225 | |
| 188 | 226 | function M.start_new() |
| 227 | + local p = get_panel() | |
| 189 | 228 | local runner = require("claude.runner") |
| 190 | 229 | local sid = runner.start_session() |
| 191 | - panel.sid = sid | |
| 192 | - clear_buf() | |
| 230 | + p.sid = sid | |
| 231 | + clear_buf(p) | |
| 193 | 232 | M.open_panel() |
| 194 | - append_lines({ | |
| 233 | + append_lines(p, { | |
| 195 | 234 | "# Claude — new session", |
| 196 | 235 | "_id: " .. sid:sub(1, 8) .. "_", |
| 197 | 236 | "", |
| @@ -200,16 +239,16 @@ function M.start_new() | ||
| 200 | 239 | }) |
| 201 | 240 | end |
| 202 | 241 | |
| 203 | -local function claude_jsonl_path(claude_id) | |
| 242 | +local function claude_jsonl_path(claude_id, cwd) | |
| 204 | 243 | if not claude_id then return nil end |
| 205 | - local cwd = vim.fn.getcwd() | |
| 244 | + cwd = cwd or vim.fn.getcwd() | |
| 206 | 245 | local enc = cwd:gsub("[/%.]", "-") |
| 207 | 246 | local home = vim.uv.os_homedir() or os.getenv("HOME") or "" |
| 208 | 247 | return home .. "/.claude/projects/" .. enc .. "/" .. claude_id .. ".jsonl" |
| 209 | 248 | end |
| 210 | 249 | |
| 211 | -local function replay_history(claude_id) | |
| 212 | - local path = claude_jsonl_path(claude_id) | |
| 250 | +local function replay_history(p, claude_id, cwd) | |
| 251 | + local path = claude_jsonl_path(claude_id, cwd) | |
| 213 | 252 | if not path then return false end |
| 214 | 253 | local f = io.open(path, "r") |
| 215 | 254 | if not f then return false end |
| @@ -232,11 +271,11 @@ local function replay_history(claude_id) | ||
| 232 | 271 | text = table.concat(parts, "\n") |
| 233 | 272 | end |
| 234 | 273 | if text and text ~= "" and not text:match("^<") then |
| 235 | - append_lines({ "## You", "" }) | |
| 274 | + append_lines(p, { "## You", "" }) | |
| 236 | 275 | for _, l in ipairs(vim.split(text, "\n", { plain = true })) do |
| 237 | - append_lines({ l }) | |
| 276 | + append_lines(p, { l }) | |
| 238 | 277 | end |
| 239 | - append_lines({ "" }) | |
| 278 | + append_lines(p, { "" }) | |
| 240 | 279 | turns = turns + 1 |
| 241 | 280 | end |
| 242 | 281 | elseif msg.role == "assistant" and type(msg.content) == "table" then |
| @@ -247,22 +286,22 @@ local function replay_history(claude_id) | ||
| 247 | 286 | if b.type == "text" and b.text then |
| 248 | 287 | table.insert(parts, b.text) |
| 249 | 288 | elseif b.type == "tool_use" then |
| 250 | - local p = (b.input and b.input.file_path) or "" | |
| 251 | - table.insert(tools, "> **" .. (b.name or "tool") .. "** `" .. p .. "`") | |
| 289 | + local fp = (b.input and b.input.file_path) or "" | |
| 290 | + table.insert(tools, "> **" .. (b.name or "tool") .. "** `" .. fp .. "`") | |
| 252 | 291 | end |
| 253 | 292 | end |
| 254 | 293 | end |
| 255 | 294 | if #parts > 0 or #tools > 0 then |
| 256 | - append_lines({ "## Claude", "" }) | |
| 295 | + append_lines(p, { "## Claude", "" }) | |
| 257 | 296 | for _, t in ipairs(parts) do |
| 258 | 297 | for _, l in ipairs(vim.split(t, "\n", { plain = true })) do |
| 259 | - append_lines({ l }) | |
| 298 | + append_lines(p, { l }) | |
| 260 | 299 | end |
| 261 | 300 | end |
| 262 | 301 | for _, t in ipairs(tools) do |
| 263 | - append_lines({ "", t }) | |
| 302 | + append_lines(p, { "", t }) | |
| 264 | 303 | end |
| 265 | - append_lines({ "", "---", "" }) | |
| 304 | + append_lines(p, { "", "---", "" }) | |
| 266 | 305 | end |
| 267 | 306 | end |
| 268 | 307 | end |
| @@ -276,11 +315,12 @@ function M.resume_session(claude_id) | ||
| 276 | 315 | vim.notify("claude: missing claude_id", vim.log.levels.WARN) |
| 277 | 316 | return |
| 278 | 317 | end |
| 318 | + local p = get_panel() | |
| 279 | 319 | local runner = require("claude.runner") |
| 280 | 320 | local store = require("claude.store") |
| 281 | - -- find existing session.json with this claude_id, else build thin session | |
| 321 | + local cwd = vim.fn.getcwd() | |
| 282 | 322 | local session |
| 283 | - for _, entry in ipairs(store.list_sessions()) do | |
| 323 | + for _, entry in ipairs(store.list_sessions(cwd)) do | |
| 284 | 324 | local s = store.load_path(entry.path) |
| 285 | 325 | if s and s.claude_id == claude_id then session = s break end |
| 286 | 326 | end |
| @@ -288,80 +328,51 @@ function M.resume_session(claude_id) | ||
| 288 | 328 | session = { |
| 289 | 329 | id = store.uuid(), |
| 290 | 330 | started_at = os.time(), |
| 291 | - cwd = vim.fn.getcwd(), | |
| 331 | + cwd = cwd, | |
| 292 | 332 | claude_id = claude_id, |
| 293 | 333 | prompts = {}, |
| 294 | 334 | } |
| 295 | 335 | end |
| 296 | 336 | local sid = runner.attach_existing(session) |
| 297 | - panel.sid = sid | |
| 337 | + p.sid = sid | |
| 298 | 338 | store.write_pointer({ session_id = session.id, claude_id = claude_id, ts = os.time(), cwd = session.cwd }) |
| 299 | - clear_buf() | |
| 339 | + clear_buf(p) | |
| 300 | 340 | M.open_panel() |
| 301 | - append_lines({ | |
| 341 | + append_lines(p, { | |
| 302 | 342 | "# Claude — resumed", |
| 303 | 343 | "_id: " .. sid:sub(1, 8) .. " ← " .. claude_id:sub(1, 8) .. "_", |
| 304 | 344 | "", |
| 305 | 345 | }) |
| 306 | - local replayed = replay_history(claude_id) | |
| 346 | + local replayed = replay_history(p, claude_id, session.cwd) | |
| 307 | 347 | if not replayed then |
| 308 | - append_lines({ "_(no prior transcript found on disk)_", "" }) | |
| 348 | + append_lines(p, { "_(no prior transcript found on disk)_", "" }) | |
| 309 | 349 | end |
| 310 | - append_lines({ "Press `i` to send a prompt.", "" }) | |
| 350 | + append_lines(p, { "Press `i` to send a prompt.", "" }) | |
| 311 | 351 | end |
| 312 | 352 | |
| 313 | 353 | function M.resume_last() |
| 354 | + local p = get_panel() | |
| 314 | 355 | local runner = require("claude.runner") |
| 315 | - local sid = runner.resume_from_pointer() | |
| 356 | + local sid = runner.resume_from_pointer(vim.fn.getcwd()) | |
| 316 | 357 | if not sid then |
| 317 | 358 | vim.notify("claude: no previous session to resume", vim.log.levels.WARN) |
| 318 | 359 | M.start_new() |
| 319 | 360 | return |
| 320 | 361 | end |
| 321 | - panel.sid = sid | |
| 322 | - clear_buf() | |
| 362 | + p.sid = sid | |
| 363 | + clear_buf(p) | |
| 323 | 364 | M.open_panel() |
| 324 | 365 | local session = runner.get_session(sid) |
| 325 | - append_lines({ | |
| 366 | + append_lines(p, { | |
| 326 | 367 | "# Claude — resumed", |
| 327 | 368 | "_id: " .. sid:sub(1, 8) .. (session.claude_id and (" ← " .. session.claude_id:sub(1, 8)) or "") .. "_", |
| 328 | 369 | "", |
| 329 | 370 | }) |
| 330 | - local replayed = replay_history(session.claude_id) | |
| 331 | - if not replayed then | |
| 332 | - append_lines({ "_(no prior transcript found on disk)_", "" }) | |
| 333 | - end | |
| 334 | - append_lines({ "Press `i` to send a prompt.", "" }) | |
| 335 | -end | |
| 336 | - | |
| 337 | -function M.resume_session(claude_id) | |
| 338 | - if not claude_id or claude_id == "" then | |
| 339 | - vim.notify("claude: missing session id", vim.log.levels.WARN) | |
| 340 | - return | |
| 341 | - end | |
| 342 | - local runner = require("claude.runner") | |
| 343 | - local store = require("claude.store") | |
| 344 | - local session = { | |
| 345 | - id = store.uuid(), | |
| 346 | - started_at = os.time(), | |
| 347 | - cwd = vim.fn.getcwd(), | |
| 348 | - claude_id = claude_id, | |
| 349 | - prompts = {}, | |
| 350 | - } | |
| 351 | - local sid = runner.attach_existing(session) | |
| 352 | - panel.sid = sid | |
| 353 | - clear_buf() | |
| 354 | - M.open_panel() | |
| 355 | - append_lines({ | |
| 356 | - "# Claude — resumed", | |
| 357 | - "_id: " .. sid:sub(1, 8) .. " ← " .. claude_id:sub(1, 8) .. "_", | |
| 358 | - "", | |
| 359 | - }) | |
| 360 | - local replayed = replay_history(claude_id) | |
| 371 | + local replayed = replay_history(p, session.claude_id, session.cwd) | |
| 361 | 372 | if not replayed then |
| 362 | - append_lines({ "_(no prior transcript found on disk)_", "" }) | |
| 373 | + append_lines(p, { "_(no prior transcript found on disk)_", "" }) | |
| 363 | 374 | end |
| 364 | - append_lines({ "Press `i` to send a prompt.", "" }) | |
| 375 | + append_lines(p, { "Press `i` to send a prompt.", "" }) | |
| 365 | 376 | end |
| 366 | 377 | |
| 367 | 378 | local prompt_buf = nil |
| @@ -380,10 +391,11 @@ end | ||
| 380 | 391 | |
| 381 | 392 | function M.open_input(opts) |
| 382 | 393 | opts = opts or {} |
| 383 | - if not panel.sid then | |
| 394 | + local p = get_panel() | |
| 395 | + if not p.sid then | |
| 384 | 396 | M.start_new() |
| 385 | 397 | end |
| 386 | - local sid = panel.sid | |
| 398 | + local sid = p.sid | |
| 387 | 399 | local return_to = opts.return_to |
| 388 | 400 | local context = opts.context |
| 389 | 401 | local input_buf = ensure_prompt_buf() |
| @@ -402,7 +414,6 @@ function M.open_input(opts) | ||
| 402 | 414 | or " Claude prompt — <C-s> send <C-c> cancel ", |
| 403 | 415 | title_pos = "center", |
| 404 | 416 | }) |
| 405 | - -- Place cursor at end of existing text so user can keep typing | |
| 406 | 417 | local lc = vim.api.nvim_buf_line_count(input_buf) |
| 407 | 418 | local last = vim.api.nvim_buf_get_lines(input_buf, lc - 1, lc, false)[1] or "" |
| 408 | 419 | pcall(vim.api.nvim_win_set_cursor, win, { lc, #last }) |
| @@ -425,7 +436,6 @@ function M.open_input(opts) | ||
| 425 | 436 | local lines = vim.api.nvim_buf_get_lines(input_buf, 0, -1, false) |
| 426 | 437 | local text = table.concat(lines, "\n") |
| 427 | 438 | if text:match("^%s*$") then close_keep(); return end |
| 428 | - -- clear the persistent prompt buffer since prompt was sent | |
| 429 | 439 | if vim.api.nvim_buf_is_valid(input_buf) then |
| 430 | 440 | vim.api.nvim_buf_set_lines(input_buf, 0, -1, false, {}) |
| 431 | 441 | end |
| @@ -441,21 +451,12 @@ function M.open_input(opts) | ||
| 441 | 451 | vim.notify("claude: " .. tostring(err), vim.log.levels.WARN) |
| 442 | 452 | end |
| 443 | 453 | end |
| 444 | - if not prompt_keymaps_set then | |
| 445 | - local km = { buffer = input_buf, nowait = true, silent = true } | |
| 446 | - vim.keymap.set({ "n", "i" }, "<C-s>", send, km) | |
| 447 | - vim.keymap.set({ "n", "i" }, "<C-c>", close_keep, km) | |
| 448 | - vim.keymap.set("n", "q", close_keep, km) | |
| 449 | - vim.keymap.set("n", "<Esc>", close_keep, km) | |
| 450 | - prompt_keymaps_set = true | |
| 451 | - else | |
| 452 | - -- rebind send/close so they reference current win/sid/context closures | |
| 453 | - local km = { buffer = input_buf, nowait = true, silent = true } | |
| 454 | - vim.keymap.set({ "n", "i" }, "<C-s>", send, km) | |
| 455 | - vim.keymap.set({ "n", "i" }, "<C-c>", close_keep, km) | |
| 456 | - vim.keymap.set("n", "q", close_keep, km) | |
| 457 | - vim.keymap.set("n", "<Esc>", close_keep, km) | |
| 458 | - end | |
| 454 | + local km = { buffer = input_buf, nowait = true, silent = true } | |
| 455 | + vim.keymap.set({ "n", "i" }, "<C-s>", send, km) | |
| 456 | + vim.keymap.set({ "n", "i" }, "<C-c>", close_keep, km) | |
| 457 | + vim.keymap.set("n", "q", close_keep, km) | |
| 458 | + vim.keymap.set("n", "<Esc>", close_keep, km) | |
| 459 | + prompt_keymaps_set = true | |
| 459 | 460 | end |
| 460 | 461 | |
| 461 | 462 | local function capture_visual_selection(bufnr) |
| @@ -491,8 +492,9 @@ function M.prompt_visual_chat() | ||
| 491 | 492 | local ft = vim.bo[origin_buf].filetype or "" |
| 492 | 493 | local context = string.format("```%s\n%s\n```", ft, sel) |
| 493 | 494 | |
| 495 | + local p = get_panel() | |
| 494 | 496 | if not M.is_open() then |
| 495 | - if panel.sid then | |
| 497 | + if p.sid then | |
| 496 | 498 | M.open_panel() |
| 497 | 499 | else |
| 498 | 500 | M.start_new() |
| @@ -503,25 +505,22 @@ end | ||
| 503 | 505 | |
| 504 | 506 | function M.open_prompt_keep_focus() |
| 505 | 507 | local origin = vim.api.nvim_get_current_win() |
| 508 | + local p = get_panel() | |
| 506 | 509 | if not M.is_open() then |
| 507 | - if panel.sid then | |
| 510 | + if p.sid then | |
| 508 | 511 | M.open_panel() |
| 509 | 512 | else |
| 510 | - local store = require("claude.store") | |
| 511 | - if store.read_pointer() then | |
| 512 | - M.resume_last() | |
| 513 | - else | |
| 514 | - M.start_new() | |
| 515 | - end | |
| 513 | + M.start_new() | |
| 516 | 514 | end |
| 517 | 515 | end |
| 518 | 516 | M.open_input({ return_to = origin }) |
| 519 | 517 | end |
| 520 | 518 | |
| 521 | --- runner callbacks | |
| 519 | +-- runner callbacks (resolve panel by sid) | |
| 522 | 520 | |
| 523 | 521 | function M.append_user_prompt(sid, text) |
| 524 | - if sid ~= panel.sid then return end | |
| 522 | + local p = find_panel_by_sid(sid) | |
| 523 | + if not p then return end | |
| 525 | 524 | local lines = { "## You", "" } |
| 526 | 525 | for _, l in ipairs(vim.split(text, "\n", { plain = true })) do |
| 527 | 526 | table.insert(lines, l) |
| @@ -529,28 +528,30 @@ function M.append_user_prompt(sid, text) | ||
| 529 | 528 | table.insert(lines, "") |
| 530 | 529 | table.insert(lines, "## Claude") |
| 531 | 530 | table.insert(lines, "") |
| 532 | - append_lines(lines) | |
| 533 | - spinner_start() | |
| 534 | - scroll_to_bottom() | |
| 531 | + append_lines(p, lines) | |
| 532 | + spinner_start(p) | |
| 533 | + scroll_to_bottom_now(p) | |
| 535 | 534 | pcall(vim.cmd, "redraw!") |
| 536 | 535 | end |
| 537 | 536 | |
| 538 | 537 | function M.append_assistant_text(sid, text) |
| 539 | - if sid ~= panel.sid then return end | |
| 540 | 538 | vim.schedule(function() |
| 541 | - append_text_streaming(text) | |
| 542 | - spinner_render() | |
| 539 | + local p = find_panel_by_sid(sid) | |
| 540 | + if not p then return end | |
| 541 | + append_text_streaming(p, text) | |
| 542 | + spinner_render(p) | |
| 543 | 543 | end) |
| 544 | 544 | end |
| 545 | 545 | |
| 546 | 546 | local ns_diff = vim.api.nvim_create_namespace("claude_panel_diff") |
| 547 | 547 | |
| 548 | 548 | function M.append_diff(sid, lines) |
| 549 | - if sid ~= panel.sid then return end | |
| 550 | 549 | vim.schedule(function() |
| 551 | - local buf = panel.buf | |
| 550 | + local p = find_panel_by_sid(sid) | |
| 551 | + if not p then return end | |
| 552 | + local buf = p.buf | |
| 552 | 553 | if not buf or not vim.api.nvim_buf_is_valid(buf) then return end |
| 553 | - spinner_remove_if_present() | |
| 554 | + spinner_remove_if_present(p) | |
| 554 | 555 | vim.bo[buf].modifiable = true |
| 555 | 556 | local start_row = vim.api.nvim_buf_line_count(buf) |
| 556 | 557 | vim.api.nvim_buf_set_lines(buf, start_row, start_row, false, lines) |
| @@ -567,44 +568,48 @@ function M.append_diff(sid, lines) | ||
| 567 | 568 | pcall(vim.api.nvim_buf_set_extmark, buf, ns_diff, row, 0, { line_hl_group = hl }) |
| 568 | 569 | end |
| 569 | 570 | end |
| 570 | - spinner_render() | |
| 571 | - scroll_to_bottom_now() | |
| 571 | + spinner_render(p) | |
| 572 | + scroll_to_bottom_now(p) | |
| 572 | 573 | end) |
| 573 | 574 | end |
| 574 | 575 | |
| 575 | 576 | function M.append_tool_use(sid, name, summary) |
| 576 | - if sid ~= panel.sid then return end | |
| 577 | 577 | vim.schedule(function() |
| 578 | + local p = find_panel_by_sid(sid) | |
| 579 | + if not p then return end | |
| 578 | 580 | local line |
| 579 | 581 | if summary and summary ~= "" then |
| 580 | 582 | line = "> **" .. name .. "** `" .. summary .. "`" |
| 581 | 583 | else |
| 582 | 584 | line = "> **" .. name .. "**" |
| 583 | 585 | end |
| 584 | - append_lines({ "", line, "" }) | |
| 585 | - spinner_render() | |
| 586 | + append_lines(p, { "", line, "" }) | |
| 587 | + spinner_render(p) | |
| 586 | 588 | end) |
| 587 | 589 | end |
| 588 | 590 | |
| 589 | 591 | function M.append_separator(sid) |
| 590 | - if sid ~= panel.sid then return end | |
| 591 | 592 | vim.schedule(function() |
| 592 | - spinner_stop() | |
| 593 | - append_lines({ "", "---", "" }) | |
| 593 | + local p = find_panel_by_sid(sid) | |
| 594 | + if not p then return end | |
| 595 | + spinner_stop(p) | |
| 596 | + append_lines(p, { "", "---", "" }) | |
| 594 | 597 | end) |
| 595 | 598 | end |
| 596 | 599 | |
| 597 | 600 | function M.append_cancelled(sid) |
| 598 | - if sid ~= panel.sid then return end | |
| 599 | 601 | vim.schedule(function() |
| 600 | - spinner_stop() | |
| 601 | - append_lines({ "", "_— cancelled —_", "", "---", "" }) | |
| 602 | + local p = find_panel_by_sid(sid) | |
| 603 | + if not p then return end | |
| 604 | + spinner_stop(p) | |
| 605 | + append_lines(p, { "", "_— cancelled —_", "", "---", "" }) | |
| 602 | 606 | end) |
| 603 | 607 | end |
| 604 | 608 | |
| 605 | 609 | function M.cancel_current() |
| 610 | + local p = get_panel() | |
| 606 | 611 | local runner = require("claude.runner") |
| 607 | - local sid = panel.sid | |
| 612 | + local sid = p.sid | |
| 608 | 613 | if not sid then |
| 609 | 614 | vim.notify("claude: no active session", vim.log.levels.WARN) |
| 610 | 615 | return |
| @@ -615,22 +620,26 @@ function M.cancel_current() | ||
| 615 | 620 | end |
| 616 | 621 | |
| 617 | 622 | function M.append_stderr(sid, line) |
| 618 | - if sid ~= panel.sid then return end | |
| 619 | - vim.schedule(function() append_lines({ "_stderr: " .. line .. "_" }) end) | |
| 623 | + vim.schedule(function() | |
| 624 | + local p = find_panel_by_sid(sid) | |
| 625 | + if not p then return end | |
| 626 | + append_lines(p, { "_stderr: " .. line .. "_" }) | |
| 627 | + end) | |
| 620 | 628 | end |
| 621 | 629 | |
| 622 | 630 | function M.on_prompt_complete(sid, prompt, had_changes) |
| 623 | - if sid ~= panel.sid then return end | |
| 624 | - vim.schedule(function() spinner_stop() end) | |
| 625 | - if had_changes then | |
| 626 | - vim.schedule(function() | |
| 627 | - append_lines({ "_recorded — " .. #(prompt.files or {}) .. " file(s) changed_", "" }) | |
| 628 | - end) | |
| 629 | - end | |
| 631 | + vim.schedule(function() | |
| 632 | + local p = find_panel_by_sid(sid) | |
| 633 | + if not p then return end | |
| 634 | + spinner_stop(p) | |
| 635 | + if had_changes then | |
| 636 | + append_lines(p, { "_recorded — " .. #(prompt.files or {}) .. " file(s) changed_", "" }) | |
| 637 | + end | |
| 638 | + end) | |
| 630 | 639 | end |
| 631 | 640 | |
| 632 | 641 | function M.current_sid() |
| 633 | - return panel.sid | |
| 642 | + return get_panel().sid | |
| 634 | 643 | end |
| 635 | 644 | |
| 636 | 645 | return M |