local entry = require 'bugabinga.pivi.entry' local transcript = require 'bugabinga.pivi.transcript' local NAMESPACE = 'pivi-render' --- How each entry kind is framed. --- Framing is decoration, never buffer text, so the transcript keeps the --- message exactly as it was authored and copying it stays faithful. local FRAMES = { prompt = { label = 'you', label_hl = 'Title', prefix = '\u{258c} ', prefix_hl = 'Identifier', every_line = true, }, answer = { label = 'pi', label_hl = 'Title', }, tool = { prefix = '\u{25b8} ', prefix_hl = 'Function', line_hl = 'Comment', }, error = { prefix = '! ', prefix_hl = 'ErrorMsg', }, } local namespace = function() return vim.api.nvim_create_namespace( NAMESPACE ) end local clear_range = function( bufnr, first, last ) vim.api.nvim_buf_clear_namespace( bufnr, namespace(), first - 1, last ) end --- Decorates one entry. --- Only the entry's own lines are touched, so decoration cost stays --- proportional to the entry rather than to the whole transcript. local decorate = function( bufnr, id ) if not vim.api.nvim_buf_is_valid( bufnr ) then return end local item = nil for _, candidate in ipairs( entry.list( bufnr ) ) do if candidate.id == id then item = candidate end end if not item then return end local frame = FRAMES[item.kind] if not frame then return end clear_range( bufnr, item.first, item.last ) if frame.label then pcall( vim.api.nvim_buf_set_extmark, bufnr, namespace(), item.first - 1, 0, { virt_lines = { { { frame.label, frame.label_hl, }, }, }, virt_lines_above = true, right_gravity = false, priority = 60, } ) end if not frame.prefix and not frame.line_hl then return end for row = item.first - 1, item.last - 1 do local heads = row == item.first - 1 local prefix = frame.prefix and ( heads or frame.every_line ) and frame.prefix or nil pcall( vim.api.nvim_buf_set_extmark, bufnr, namespace(), row, 0, { virt_text = prefix and { { prefix, frame.prefix_hl, }, } or nil, virt_text_pos = prefix and 'inline' or nil, line_hl_group = frame.line_hl, right_gravity = false, priority = 60, } ) end end --- Renders message formatting, and presents embedded code in its own language. --- Parsing is driven by redraw, never forced per streamed delta, because --- forcing it costs orders of magnitude more on a growing transcript. local highlight = function( bufnr, language ) if not vim.api.nvim_buf_is_valid( bufnr ) then return false end return ( pcall( vim.treesitter.start, bufnr, language or 'markdown' ) ) end --- Whether formatting is being rendered rather than shown as markup. local highlighted = function( bufnr ) local active = vim.treesitter.highlighter.active or {} return active[bufnr] ~= nil end local conceal = function( win ) vim.wo[win].conceallevel = 2 vim.wo[win].concealcursor = 'nc' end local marks = function( bufnr ) if not vim.api.nvim_buf_is_valid( bufnr ) then return {} end return vim.api.nvim_buf_get_extmarks( bufnr, namespace(), 0, -1, { details = true, } ) end local clear = function( bufnr ) if not vim.api.nvim_buf_is_valid( bufnr ) then return end vim.api.nvim_buf_clear_namespace( bufnr, namespace(), 0, -1 ) end transcript.on_reset( clear ) transcript.on_buffer( highlight ) transcript.on_window( conceal ) return { namespace = namespace, frames = FRAMES, decorate = decorate, highlight = highlight, highlighted = highlighted, conceal = conceal, marks = marks, clear = clear, }