Public Access
Chat: markdown rendered by render-markdown.nvim (chat buffer only, not markdown files), `## You` / `## Claude Code` headers as tinted bars, upright code, no line numbers. Winbar shows icon, title and agent · model · effort, with a spinner and timer while the agent works; a lualine extension shows context items and tokens. Highlights live in lua/ai_ui.lua and follow gruvbox's light/dark switch. Inline: the progress float is now a pill with per-part colours that shows done/failed/cancelled for a moment; the diff banner is a label. Inline edits always open the diff for review again (approval_mode auto was applying them straight away), and run at medium effort with every agent and model unless an adapter sets its own thought_level. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
231 lines
8.9 KiB
Lua
231 lines
8.9 KiB
Lua
-- Progress pill for inline edits (`:CodeCompanion`). While a request runs, a
|
|
-- one-line float sits in the bottom-right corner of the window being edited:
|
|
-- ( ⠹ Antigravity Read ai.lua · 3 tools · 12s )
|
|
-- and when it ends shows the outcome for a moment (" done", " failed").
|
|
-- 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 ui = require("ai_ui") -- spinner, agent colours, Cc* highlight groups
|
|
local M = {}
|
|
|
|
local ns = api.nvim_create_namespace("ai_inline_status")
|
|
local active = {} ---@type table<integer, table> inline bufnr -> state
|
|
local LINGER = 1500 -- ms the outcome stays up after the request ends
|
|
local CAP_L, CAP_R = "\238\130\182", "\238\130\180" -- Nerd Font round caps U+E0B6/U+E0B4
|
|
|
|
-- 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
|
|
|
|
-- Segments of the pill: { text, highlight }.
|
|
local function segments(st)
|
|
local agent = ui.agent(st.name)
|
|
local secs = math.floor(((st.ended or vim.uv.now()) - st.started) / 1000) .. "s"
|
|
local tools = st.tools > 0 and (st.tools .. (st.tools == 1 and " tool" or " tools")) or nil
|
|
local segs
|
|
if not st.ended then
|
|
segs = {
|
|
{ ui.SPINNER[st.frame] .. " ", agent.hl .. "Pill" },
|
|
{ st.name .. " ", agent.hl .. "Pill" },
|
|
{ st.activity, "CcPill" },
|
|
}
|
|
elseif st.status == "success" or st.status == nil then
|
|
segs = { { " ", "CcPillOk" }, { st.name .. " ", agent.hl .. "Pill" }, { "done", "CcPill" } }
|
|
elseif st.status == "cancelled" then
|
|
segs = { { " ", "CcPillDim" }, { st.name .. " ", agent.hl .. "Pill" }, { "cancelled", "CcPillDim" } }
|
|
else
|
|
segs = { { " ", "CcPillErr" }, { st.name .. " ", agent.hl .. "Pill" }, { "failed", "CcPillErr" } }
|
|
end
|
|
for _, meta in ipairs({ tools, secs }) do
|
|
if meta then table.insert(segs, { " · " .. meta, "CcPillDim" }) end
|
|
end
|
|
return segs
|
|
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
|
|
if not st.ended then st.frame = st.frame % #ui.SPINNER + 1 end
|
|
|
|
-- Rounded caps around the body; the activity gets cut short first.
|
|
local segs = segments(st)
|
|
local max = api.nvim_win_get_width(anchor) - 4
|
|
local width = 0
|
|
for _, seg in ipairs(segs) do width = width + vim.fn.strdisplaywidth(seg[1]) end
|
|
if width > max and segs[3] then
|
|
local keep = math.max(vim.fn.strdisplaywidth(segs[3][1]) - (width - max) - 1, 0)
|
|
segs[3][1] = vim.fn.strcharpart(segs[3][1], 0, keep) .. "…"
|
|
end
|
|
table.insert(segs, 1, { CAP_L, "CcPillCap" })
|
|
table.insert(segs, 2, { " ", "CcPill" })
|
|
table.insert(segs, { " ", "CcPill" })
|
|
table.insert(segs, { CAP_R, "CcPillCap" })
|
|
|
|
local text, marks = "", {}
|
|
for _, seg in ipairs(segs) do
|
|
table.insert(marks, { #text, #text + #seg[1], seg[2] })
|
|
text = text .. seg[1]
|
|
end
|
|
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 })
|
|
api.nvim_buf_clear_namespace(st.buf, ns, 0, -1)
|
|
for _, m in ipairs(marks) do
|
|
api.nvim_buf_set_extmark(st.buf, ns, 0, m[1], { end_col = m[2], hl_group = m[3] })
|
|
end
|
|
|
|
local cfg = {
|
|
relative = "win",
|
|
win = anchor,
|
|
anchor = "SE",
|
|
row = api.nvim_win_get_height(anchor),
|
|
col = api.nvim_win_get_width(anchor) - 1,
|
|
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)
|
|
-- Caps take the editor background; the body's colours come from the marks.
|
|
vim.wo[st.win].winhighlight = "Normal:Normal"
|
|
end
|
|
end
|
|
|
|
local function stop(bufnr)
|
|
local st = active[bufnr]
|
|
if not st then return end
|
|
active[bufnr] = nil
|
|
if not st.timer:is_closing() then
|
|
st.timer:stop()
|
|
st.timer:close()
|
|
end
|
|
close_float(st)
|
|
end
|
|
|
|
-- The request ended: freeze the pill on its outcome, then remove it. Both
|
|
-- RequestFinished (has the status) and InlineFinished (doesn't) end up here,
|
|
-- in either order, so the outcome is drawn on the next tick, not right away.
|
|
local function finish(bufnr, status)
|
|
local st = active[bufnr]
|
|
if not st then return end
|
|
st.status = status or st.status
|
|
if st.ended then return end
|
|
st.ended = vim.uv.now()
|
|
st.timer:stop()
|
|
vim.defer_fn(function()
|
|
if active[bufnr] ~= st then return end
|
|
render(bufnr)
|
|
vim.defer_fn(function()
|
|
if active[bufnr] == st then stop(bufnr) end
|
|
end, LINGER)
|
|
end, 50)
|
|
end
|
|
|
|
local function start(bufnr, adapter)
|
|
stop(bufnr)
|
|
local st = {
|
|
name = adapter and (adapter.formatted_name or adapter.name) or "AI",
|
|
status = nil, ---@type string? from RequestFinished: success|error|cancelled
|
|
ended = nil, ---@type integer? when the request ended (ms)
|
|
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
|
|
finish(d.bufnr, d.status)
|
|
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
|