-- Progress bar for inline edits (`:CodeCompanion`). While a request runs, a -- one-line float sits in the bottom-right corner of the window being edited: -- ⠹ Claude Code · Read ai.lua · 3 tools · 12s -- CodeCompanion itself shows nothing until the diff appears. It does receive -- the agent's progress over ACP (tool calls, thinking, reply), but inline -- mode only keeps the reply text. So this wraps the internal method those -- updates go through, and listens to the documented Inline* events for -- start/stop. HTTP adapters get the spinner and timer only. local api = vim.api local M = {} local SPINNER = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } local active = {} ---@type table inline bufnr -> state -- Tool titles contain paths. Inline agents edit a copy of the buffer in -- Neovim's temp dir (e.g. /tmp/nvim.user/AbC123/1/foo.lua), so show just the -- file name for those. Agents may write that path absolute or relative to -- their cwd (agy does ../../nvim.user/...), so match the temp dir's unique -- tail anywhere. Other absolute paths become cwd- or ~-relative. local tmp_tail = vim.fn.fnamemodify(vim.fn.tempname(), ":h"):match("[^/]+/[^/]+$") local function short_paths(s) return (s:gsub("[%w%._%-@+~]*/[%w%._%-/@+~]+", function(p) if tmp_tail and p:find(tmp_tail, 1, true) then return vim.fs.basename(p) end if p:match("^/[^/]") then return vim.fn.fnamemodify(p, ":~:.") end return p end)) end -- Window to anchor to: the current one if it shows the buffer, else any in -- this tab that does. local function target_win(bufnr) if api.nvim_win_get_buf(0) == bufnr then return api.nvim_get_current_win() end for _, win in ipairs(api.nvim_tabpage_list_wins(0)) do if api.nvim_win_get_buf(win) == bufnr and api.nvim_win_get_config(win).relative == "" then return win end end end local function close_float(st) if st.win and api.nvim_win_is_valid(st.win) then api.nvim_win_close(st.win, true) end st.win = nil end local function render(bufnr) local st = active[bufnr] if not st then return end local anchor = target_win(bufnr) if not anchor then return close_float(st) end st.frame = st.frame % #SPINNER + 1 local parts = { SPINNER[st.frame] .. " " .. st.name, st.activity } if st.tools > 0 then table.insert(parts, st.tools .. (st.tools == 1 and " tool" or " tools")) end table.insert(parts, math.floor((vim.uv.now() - st.started) / 1000) .. "s") local text = " " .. table.concat(parts, " · ") .. " " local max = api.nvim_win_get_width(anchor) - 2 if vim.fn.strdisplaywidth(text) > max then text = vim.fn.strcharpart(text, 0, max - 2) .. "… " end local width = vim.fn.strdisplaywidth(text) if not (st.buf and api.nvim_buf_is_valid(st.buf)) then st.buf = api.nvim_create_buf(false, true) vim.bo[st.buf].bufhidden = "wipe" end api.nvim_buf_set_lines(st.buf, 0, -1, false, { text }) local cfg = { relative = "win", win = anchor, anchor = "SE", row = api.nvim_win_get_height(anchor), col = api.nvim_win_get_width(anchor), width = width, height = 1, focusable = false, zindex = 60, border = "none", -- otherwise 'winborder' applies } if st.win and api.nvim_win_is_valid(st.win) then api.nvim_win_set_config(st.win, cfg) else cfg.style = "minimal" cfg.noautocmd = true st.win = api.nvim_open_win(st.buf, false, cfg) vim.wo[st.win].winhighlight = "Normal:StatusLine" end end local function stop(bufnr) local st = active[bufnr] if not st then return end active[bufnr] = nil st.timer:stop() st.timer:close() close_float(st) end local function start(bufnr, adapter) stop(bufnr) local st = { name = adapter and (adapter.formatted_name or adapter.name) or "AI", activity = "starting…", tools = 0, frame = 0, started = vim.uv.now(), timer = vim.uv.new_timer(), } active[bufnr] = st st.timer:start(0, 100, vim.schedule_wrap(function() render(bufnr) end)) end -- ACP session/update -> one-line activity local function on_update(st, u) local kind = u.sessionUpdate if kind == "tool_call" then st.tools = st.tools + 1 st.activity = short_paths(u.title or u.kind or "tool") elseif kind == "tool_call_update" then if u.title then st.activity = short_paths(u.title) end if u.status == "failed" then st.activity = st.activity .. " (failed)" end elseif kind == "agent_thought_chunk" then st.activity = "thinking…" elseif kind == "agent_message_chunk" then st.activity = "replying…" elseif kind == "plan" then st.activity = "planning…" end end function M.setup() local group = api.nvim_create_augroup("ai_inline_status", { clear = true }) api.nvim_create_autocmd("User", { group = group, pattern = "CodeCompanionInlineStarted", callback = function(ev) start(ev.data.bufnr, ev.data.adapter) end, }) api.nvim_create_autocmd("User", { group = group, pattern = { "CodeCompanionInlineFinished", "CodeCompanionRequestFinished" }, callback = function(ev) local d = ev.data or {} if ev.match == "CodeCompanionInlineFinished" or d.interaction == "inline" then stop(d.bufnr) end end, }) -- Internal API (not part of CodeCompanion's public interface); fine -- while the plugin is pinned to ^20. Prompt objects look methods up on -- this table, so the wrapper applies to every prompt sent from now on. local PromptBuilder = require("codecompanion.acp.prompt_builder") local orig = PromptBuilder.handle_session_update PromptBuilder.handle_session_update = function(self, params) local o = self.options local st = o and o.interaction == "inline" and active[o.bufnr] if st and type(params) == "table" then pcall(on_update, st, params) end return orig(self, params) end end return M