Files
nvim/lua/ai_ui.lua
T
jannikdonkerandClaude Opus 5.5 6fcc20b6ff polish the codecompanion look, review inline diffs, inline effort medium
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>
2026-11-18 10:01:03 +01:00

326 lines
13 KiB
Lua

-- The look of CodeCompanion: chat buffer (markdown rendering, role headers),
-- chat winbar and statusline, and the highlight groups shared with the inline
-- progress pill (lua/ai_status.lua). Behaviour is configured in lua/ai.lua.
--
-- winbar: 󰚩 Fix the parser Claude Code · Opus 5.5 · 󱐋 high
-- headers: `## You` / `## Claude Code` become full-width bars tinted in the
-- role's colour, with an icon (render-markdown, chat buffer only)
local api = vim.api
local M = {}
M.SPINNER = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" }
-- Agents by their display name (adapter.formatted_name). `color` lists
-- highlight groups to take the colour from, first one that exists wins.
local AGENTS = {
["Claude Code"] = { icon = "󰚩", color = { "GruvboxOrange", "Special" } },
["Antigravity"] = { icon = "󰚩", color = { "GruvboxBlue", "Function" } },
}
local OTHER_AGENT = { icon = "󰚩", color = { "GruvboxYellow", "Type" } }
local USER = { name = "You", icon = "󰀄", color = { "GruvboxAqua", "String" } }
-- Highlight group stem for an agent: CcClaudeCode, CcAntigravity, CcAgent.
local function stem(name)
return AGENTS[name] and ("Cc" .. name:gsub("%W", "")) or "CcAgent"
end
---@return { icon: string, hl: string } icon and its foreground group
function M.agent(name)
return { icon = (AGENTS[name] or OTHER_AGENT).icon, hl = stem(name) }
end
-- Highlights ------------------------------------------------------------------
-- Derived from the colorscheme's groups and rebuilt on ColorScheme, so they
-- follow gruvbox when the terminal flips between light and dark.
local function get(groups, attr)
for _, name in ipairs(groups) do
local hl = api.nvim_get_hl(0, { name = name, link = false })
if hl[attr] then return hl[attr] end
end
end
-- `amount` of colour a over b (24-bit ints)
local function blend(a, b, amount)
local function ch(c, shift) return math.floor(c / 2 ^ shift) % 256 end
local out = 0
for _, shift in ipairs({ 16, 8, 0 }) do
out = out + math.floor(ch(a, shift) * amount + ch(b, shift) * (1 - amount) + 0.5) * 2 ^ shift
end
return out
end
function M.highlights()
local set = function(name, val) api.nvim_set_hl(0, name, val) end
local bg = get({ "Normal" }, "bg") or 0x282828
local fg = get({ "Normal" }, "fg") or 0xebdbb2
local dim = get({ "GruvboxGray", "Comment" }, "fg") or fg
local muted = get({ "GruvboxFg4", "WinBar" }, "fg") or fg
local bg1 = get({ "GruvboxBg1", "CursorLine" }, "fg") or get({ "CursorLine" }, "bg") or bg
local bg2 = get({ "GruvboxBg2" }, "fg") or get({ "StatusLine" }, "bg") or bg1
local bar = get({ "WinBar" }, "bg") or bg
-- Role header bars (render-markdown backgrounds) and agent accents.
local roles = { { "CcUser", USER.color } }
for name, a in pairs(AGENTS) do table.insert(roles, { stem(name), a.color }) end
table.insert(roles, { "CcAgent", OTHER_AGENT.color })
for _, r in ipairs(roles) do
local c = get(r[2], "fg") or fg
set(r[1], { fg = c, bold = true })
set(r[1] .. "Bar", { fg = c, bg = blend(c, bg, 0.16), bold = true })
set(r[1] .. "Pill", { fg = c, bg = bg2, bold = true })
end
-- Winbar: one flat bar, same in focused and unfocused windows.
set("CcWinbarTitle", { fg = fg, bg = bar, bold = true })
set("CcWinbarText", { fg = muted, bg = bar })
set("CcWinbarDim", { fg = dim, bg = bar })
-- Inline progress pill (lua/ai_status.lua).
set("CcPill", { fg = fg, bg = bg2 })
set("CcPillDim", { fg = muted, bg = bg2 })
set("CcPillCap", { fg = bg2 })
set("CcPillOk", { fg = get({ "GruvboxGreen", "DiagnosticOk" }, "fg") or fg, bg = bg2, bold = true })
set("CcPillErr", { fg = get({ "GruvboxRed", "DiagnosticError" }, "fg") or fg, bg = bg2, bold = true })
-- CodeCompanion's own groups.
set("CodeCompanionDiffBannerInline", { fg = muted, bg = bg1, italic = true })
set("CodeCompanionDiffBanner", { fg = muted, bg = bg1, italic = true })
set("CodeCompanionVirtualText", { fg = dim, italic = true })
-- Markdown in the chat: code on a slightly raised background, upright
-- (gruvbox makes markdown code italic; remapped in the chat window only,
-- see winhighlight below).
local code = get({ "@markup.raw" }, "fg") or fg
set("RenderMarkdownCode", { bg = bg1 })
set("RenderMarkdownCodeInline", { fg = code, bg = bg1 })
set("CcCode", { fg = fg })
set("CcCodeInline", { fg = code })
end
-- Chat roles ------------------------------------------------------------------
-- Header lines are plain `## You` / `## Claude Code`; render-markdown turns
-- them into bars with an icon. Other `##` in replies stay ordinary headings.
M.roles = {
user = USER.name,
llm = function(adapter) return adapter.formatted_name or adapter.name end,
}
local function role_patterns()
local custom = {
user = { pattern = "^## " .. USER.name .. "%s*$", icon = USER.icon .. " ", background = "CcUserBar", foreground = "CcUser" },
}
for name, a in pairs(AGENTS) do
custom[stem(name)] = {
pattern = "^## " .. vim.pesc(name) .. "%s*$", -- node text ends in \n
icon = a.icon .. " ",
background = stem(name) .. "Bar",
foreground = stem(name),
}
end
return custom
end
-- Busy chats (prompt sent, agent not done yet) ---------------------------------
local busy = {} ---@type table<integer, integer> chat bufnr -> start time (ms)
local frame, ticker = 1, nil
local function chat_wins(bufnr)
return vim.tbl_filter(function(w) return api.nvim_win_get_buf(w) == bufnr end, api.nvim_list_wins())
end
local function tick()
frame = frame % #M.SPINNER + 1
for bufnr in pairs(busy) do
if not api.nvim_buf_is_valid(bufnr) then busy[bufnr] = nil end
for _, win in ipairs(api.nvim_buf_is_valid(bufnr) and chat_wins(bufnr) or {}) do
api.nvim__redraw({ win = win, winbar = true, statusline = true })
end
end
if next(busy) == nil and ticker then
ticker:stop()
ticker:close()
ticker = nil
end
end
local function set_busy(bufnr, on)
busy[bufnr] = on and vim.uv.now() or nil
if on and not ticker then
ticker = vim.uv.new_timer()
ticker:start(0, 100, vim.schedule_wrap(tick))
end
for _, win in ipairs(chat_wins(bufnr)) do
api.nvim__redraw({ win = win, winbar = true, statusline = true })
end
end
-- Winbar ------------------------------------------------------------------------
local function esc(s) return (tostring(s):gsub("%%", "%%%%")) end
local function bufnr_of(md)
for b, m in pairs(_G.codecompanion_chat_metadata or {}) do
if m == md then return b end
end
end
-- Display name of an ACP chat's current value for an option category, read
-- live from the agent connection. (The metadata CodeCompanion hands the
-- winbar caches its value->name map before the agent has sent its options,
-- so it only has raw ids there, e.g. Claude's model is just "default".)
local function acp_option_name(bufnr, category)
local chat = bufnr and require("codecompanion").buf_get_chat(bufnr)
local conn = chat and chat.acp_connection
if not conn then return nil end
for _, opt in ipairs(conn:get_config_options()) do
if opt.category == category and opt.currentValue then
for _, item in ipairs(opt.options or {}) do
for _, v in ipairs(item.options or { item }) do -- items may be grouped
if v.value == opt.currentValue then return v.name end
end
end
return opt.currentValue
end
end
end
-- Left: agent icon (a spinner while it works) + chat title (+ elapsed time).
-- Right: agent · model · effort. `%<` truncates the title first.
function M.winbar(md)
local bufnr = bufnr_of(md)
local agent = M.agent(md.adapter.name)
local started = bufnr and busy[bufnr]
local left = string.format("%%#%s# %s %%<%%#CcWinbarTitle#%s",
agent.hl, started and M.SPINNER[frame] or agent.icon, esc(md.title or "New chat"))
if started then
left = left .. string.format("%%#CcWinbarDim# %ds", (vim.uv.now() - started) / 1000)
end
local sep = "%#CcWinbarDim# · "
local right = { "%#" .. agent.hl .. "#" .. esc(md.adapter.name) }
local model = acp_option_name(bufnr, "model") or md.adapter.model -- HTTP: model id
if model then table.insert(right, "%#CcWinbarText#" .. esc(model)) end
local effort = acp_option_name(bufnr, "thought_level")
if effort then table.insert(right, "%#CcWinbarDim#󱐋 " .. esc(effort:lower())) end
return left .. "%#CcWinbarDim# %=" .. table.concat(right, sep) .. " "
end
-- Statusline (lualine extension for the chat window) -----------------------------
local function win_chat_md()
local win = vim.g.statusline_winid or api.nvim_get_current_win()
return (_G.codecompanion_chat_metadata or {})[api.nvim_win_get_buf(win)]
end
local function tokens()
local md = win_chat_md()
local n = md and md.tokens or 0
if n == 0 then return "" end
return (n >= 1000 and string.format("%.1fk", n / 1000) or tostring(n)) .. " tokens"
end
local function context()
local md = win_chat_md()
local n = md and md.context_items or 0
return n > 0 and string.format("󰈙 %d in context", n) or ""
end
M.lualine = {
filetypes = { "codecompanion" },
sections = {
lualine_a = { "mode" },
lualine_b = { function() return "󰭹 Chat" end },
lualine_c = { context },
lualine_x = { tokens },
lualine_z = { "location" },
},
inactive_sections = {
lualine_c = { function() return "󰭹 Chat" end, context },
lualine_x = { tokens },
},
}
-- Chat display options (merged into codecompanion's `display.chat`) --------------
M.chat_display = {
window = {
opts = {
number = false,
relativenumber = false,
signcolumn = "no",
foldcolumn = "0",
statuscolumn = " ", -- one column of left padding
cursorline = false,
spell = false,
fillchars = "eob: ", -- no `~` below the last line
winhighlight = table.concat({
"WinBarNC:WinBar",
"@markup.raw.block.markdown:CcCode",
"@markup.raw.markdown_inline:CcCodeInline",
}, ","),
},
},
title = { position = "winbar", text = { winbar = M.winbar } },
intro_message = "<C-s> send · ga agent · ge effort · ? keymaps",
show_token_count = false, -- shown in the statusline instead
}
-- Setup -------------------------------------------------------------------------
function M.setup()
M.highlights()
local group = api.nvim_create_augroup("ai_ui", { clear = true })
api.nvim_create_autocmd("ColorScheme", { group = group, callback = M.highlights })
api.nvim_create_autocmd("User", {
group = group,
pattern = "CodeCompanionChatSubmitted",
callback = function(ev) set_busy(ev.data.bufnr, true) end,
})
api.nvim_create_autocmd("User", {
group = group,
pattern = { "CodeCompanionChatDone", "CodeCompanionChatStopped", "CodeCompanionChatClosed" },
callback = function(ev) set_busy(ev.data.bufnr, false) end,
})
-- Renders only the chat buffer: plain markdown files stay unrendered.
require("render-markdown").setup({
file_types = { "codecompanion" },
render_modes = true, -- also while typing a prompt
sign = { enabled = false },
heading = {
position = "inline",
width = "full",
left_pad = 1,
backgrounds = {},
custom = role_patterns(),
},
-- Fence lines stay hidden: Neovim's markdown query conceals them and
-- render-markdown only lifts that for the `markdown` filetype.
code = { width = "full", left_pad = 1, language = false },
bullet = { icons = { "•", "◦", "▪", "▫" } },
-- Context items are written as <tag>id</tag>: show an icon instead.
html = {
tag = {
buf = { icon = "󰈙 ", highlight = "Directory" },
file = { icon = "󰈔 ", highlight = "Directory" },
rules = { icon = "󰦨 ", highlight = "Comment" },
url = { icon = "󰖟 ", highlight = "Directory" },
image = { icon = "󰋩 ", highlight = "Directory" },
tool = { icon = "󰖷 ", highlight = "Special" },
group = { icon = "󰖷 ", highlight = "Special" },
help = { icon = "󰋖 ", highlight = "Directory" },
symbols = { icon = "󰆧 ", highlight = "Directory" },
quickfix = { icon = "󰁨 ", highlight = "Directory" },
skill = { icon = "󱐋 ", highlight = "Special" },
viewport = { icon = "󰈈 ", highlight = "Directory" },
editor_context = { icon = "󰆍 ", highlight = "Directory" },
},
},
})
end
return M