From 6fcc20b6ff35100179009995d5091c6ad984e117 Mon Sep 17 00:00:00 2001 From: Jannik Donker Date: Wed, 18 Nov 2026 10:01:03 +0100 Subject: [PATCH] polish the codecompanion look, review inline diffs, inline effort medium MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lua/ai.lua | 104 ++++++++------- lua/ai_status.lua | 103 ++++++++++++--- lua/ai_ui.lua | 325 ++++++++++++++++++++++++++++++++++++++++++++++ lua/plugins.lua | 7 +- 4 files changed, 470 insertions(+), 69 deletions(-) create mode 100644 lua/ai_ui.lua diff --git a/lua/ai.lua b/lua/ai.lua index e059ac8..02fc831 100644 --- a/lua/ai.lua +++ b/lua/ai.lua @@ -4,12 +4,13 @@ -- which reuses the `claude` CLI login. -- antigravity -> `agy acp` (Antigravity CLI: Gemini, plus Claude/GPT-OSS via -- Google). Uses the `agy` Google login. --- Chat uses Claude Code, inline uses Antigravity (Gemini 4 Hydrogen, high). +-- Chat uses Claude Code, inline uses Antigravity (Gemini 4 Hydrogen). +-- Inline always runs at medium effort, whichever agent/model (see below). -- Agents read their own CLAUDE.md / GEMINI.md / AGENTS.md and settings, so -- model, permissions and MCP servers come from the CLI configs unless pinned -- here (only inline is). -- In a chat: `ga` switches agent/model, `ge` effort, `?` lists all keymaps. --- The winbar shows the current agent · model · effort. +-- The winbar shows the current agent · model · effort (see lua/ai_ui.lua). -- The ACP module directly: the generic adapters.extend() decides http vs acp -- by looking the name up in the config, and gemini_cli is hidden from it. @@ -55,41 +56,7 @@ local function pick_effort(chat) vim.log.levels.WARN) 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(md, category) - local bufnr - for b, m in pairs(_G.codecompanion_chat_metadata or {}) do - if m == md then bufnr = b end - end - 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 - --- Chat winbar: title on the left, `agent · model · effort` on the right. -local function chat_winbar(md) - local title = (md.title or "CodeCompanion"):gsub("%%", "%%%%") - local parts = { md.adapter.name } - local model = acp_option_name(md, "model") or md.adapter.model -- HTTP: model id - if model then table.insert(parts, model) end - local effort = acp_option_name(md, "thought_level") - if effort then table.insert(parts, "effort " .. effort:lower()) end - local right = table.concat(parts, " · "):gsub("%%", "%%%%") - return string.format("%%#CodeCompanionChatHeader# %s%%*%%=%%#Comment#%s ", title, right) -end +local ui = require("ai_ui") -- look of the chat (lua/ai_ui.lua) require("codecompanion").setup({ adapters = { @@ -108,19 +75,16 @@ require("codecompanion").setup({ antigravity = function() return antigravity("antigravity", { mode = "auto" }) end, - -- Inline edits: Gemini 4 Hydrogen at high effort. A separate - -- adapter because inline can only pin a model, not effort, and - -- setting effort on `antigravity` would change chats too. Hidden - -- from the `ga` picker below. No auto mode here on purpose: + -- Inline edits: Gemini 4 Hydrogen. A separate adapter so the + -- model pin doesn't change chats. Effort comes from the inline + -- default further down (set `thought_level` here to override it). + -- Hidden from the `ga` picker below. No auto mode here on purpose: -- inline never prompts you anyway (CodeCompanion answers the -- agent's permission requests itself), and those requests are -- how it keeps the agent to editing only the buffer copy. An -- agent in auto mode would skip them. antigravity_inline = function() - return antigravity("antigravity_inline", { - model = "gemini-4-hydrogen", - thought_level = "high", - }) + return antigravity("antigravity_inline", { model = "gemini-4-hydrogen" }) end, }, -- Merged into the default hidden list. All HTTP adapters get hidden @@ -131,6 +95,7 @@ require("codecompanion").setup({ interactions = { chat = { adapter = "claude_code", + roles = ui.roles, -- `## You` / `## Claude Code` headers -- CodeCompanion's own approval layer (`gty` to change per chat). -- Auto: the agent's reads, searches, edits and fetches go through; -- shell commands outside tools.run_command.opts.safe_commands @@ -174,11 +139,11 @@ require("codecompanion").setup({ }, display = { - chat = { + -- Winbar title, padding instead of line numbers, etc. + chat = vim.tbl_deep_extend("force", ui.chat_display, { window = { position = "right", width = 0.4 }, - -- Winbar instead of a heading in the buffer, so it stays visible. - title = { position = "winbar", text = { winbar = chat_winbar } }, - }, + }), + input = { title = "󰚩 Inline edit" }, -- `:CodeCompanion` with no prompt }, }) @@ -191,7 +156,46 @@ for name in pairs(http_adapters) do if name ~= "opts" and name ~= "extend" then http_adapters.opts.hidden[name] = true end end --- Progress float while an inline edit runs (see lua/ai_status.lua). +-- Inline edits always open the diff for review. CodeCompanion decides that +-- with the same approval check as chat tools, keyed by the edited buffer, and +-- buffers without a mode of their own fall back to approval_mode ("auto" +-- above), which would apply the edit straight away. So give each buffer that +-- gets an inline edit "ask". Only that check looks at a file buffer's mode +-- (chat tools look up the chat's); `g1` (always accept for this buffer) in +-- the diff still works. Internal API, fine while pinned to ^20. +vim.api.nvim_create_autocmd("User", { + pattern = "CodeCompanionInlineStarted", + callback = function(ev) + require("codecompanion.interactions.chat.tools.approvals"):set_mode(ev.data.bufnr, { mode = "ask" }) + end, +}) + +-- Inline edits run at medium effort with every agent and model, including +-- ones picked with `ga` in the inline prompt: those come with the chat +-- adapter's settings, which leave effort to the agent (and so to whatever the +-- CLI was last set to). When CodeCompanion opens an inline session it applies +-- the adapter's session_config_options; this adds `thought_level` to them +-- unless the adapter sets its own. Chats are untouched (the caller check), +-- and a model without a "medium" level keeps its default (logged, no popup). +-- Internal API, fine while pinned to ^20. +local INLINE_EFFORT = "medium" +local acp_defaults = require("codecompanion.interactions.chat.acp.defaults") +local apply_defaults = acp_defaults.apply +acp_defaults.apply = function(adapter, connection) + local caller = debug.getinfo(2, "S").source -- the module opening the session + local opts = vim.tbl_get(adapter or {}, "defaults", "session_config_options") or {} + if caller:find("/interactions/inline/", 1, true) and opts.thought_level == nil then + local defaults = vim.tbl_extend("force", adapter.defaults or {}, { + session_config_options = vim.tbl_extend("force", opts, { thought_level = INLINE_EFFORT }), + }) + adapter = setmetatable({ defaults = defaults }, { __index = adapter }) + end + return apply_defaults(adapter, connection) +end + +-- Highlights, markdown rendering in the chat, busy spinner in its winbar. +ui.setup() +-- Progress pill while an inline edit runs (see lua/ai_status.lua). require("ai_status").setup() -- Keymaps: everything under a. Visual-mode maps use `:` rather than diff --git a/lua/ai_status.lua b/lua/ai_status.lua index 03f239d..6367902 100644 --- a/lua/ai_status.lua +++ b/lua/ai_status.lua @@ -1,16 +1,20 @@ --- Progress bar for inline edits (`:CodeCompanion`). While a request runs, a +-- 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: --- ⠹ Claude Code · Read ai.lua · 3 tools · 12s +-- ( ⠹ 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 SPINNER = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } +local ns = api.nvim_create_namespace("ai_inline_status") local active = {} ---@type 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 @@ -42,36 +46,75 @@ local function close_float(st) 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 - 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) .. "… " + -- 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 - local width = vim.fn.strdisplaywidth(text) + 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), + col = api.nvim_win_get_width(anchor) - 1, width = width, height = 1, focusable = false, @@ -84,7 +127,8 @@ local function render(bufnr) cfg.style = "minimal" cfg.noautocmd = true st.win = api.nvim_open_win(st.buf, false, cfg) - vim.wo[st.win].winhighlight = "Normal:StatusLine" + -- Caps take the editor background; the body's colours come from the marks. + vim.wo[st.win].winhighlight = "Normal:Normal" end end @@ -92,15 +136,38 @@ local function stop(bufnr) local st = active[bufnr] if not st then return end active[bufnr] = nil - st.timer:stop() - st.timer:close() + 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, @@ -142,7 +209,7 @@ function M.setup() callback = function(ev) local d = ev.data or {} if ev.match == "CodeCompanionInlineFinished" or d.interaction == "inline" then - stop(d.bufnr) + finish(d.bufnr, d.status) end end, }) diff --git a/lua/ai_ui.lua b/lua/ai_ui.lua new file mode 100644 index 0000000..e6364d3 --- /dev/null +++ b/lua/ai_ui.lua @@ -0,0 +1,325 @@ +-- 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 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 = " 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 id: 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 diff --git a/lua/plugins.lua b/lua/plugins.lua index 5e6aea1..0855fc2 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -34,6 +34,8 @@ vim.pack.add({ { src = "https://github.com/r-pletnev/pdfreader.nvim" }, -- Pinned to the major version: breaking changes land on major bumps. { src = "https://github.com/olimorris/codecompanion.nvim", version = vim.version.range("^20") }, + -- Renders the CodeCompanion chat only, not markdown files (lua/ai_ui.lua). + { src = "https://github.com/MeanderingProgrammer/render-markdown.nvim" }, }) pcall(vim.cmd.colorscheme, "gruvbox") @@ -287,7 +289,10 @@ vim.api.nvim_create_autocmd("FileType", { -- "gruvbox" picks gruvbox_light or gruvbox_dark from 'background', and lualine -- reloads it when that changes. -require("lualine").setup({ options = { theme = "gruvbox" } }) +require("lualine").setup({ + options = { theme = "gruvbox" }, + extensions = { require("ai_ui").lualine }, -- CodeCompanion chat window +}) -- Treesitter (main branch: no configs.setup; install parsers manually) require("nvim-treesitter").install({