feat(neovim): make pivi reachable, self-explaining and legible at a glance
ae83d153f940400288ed5acd895d58b42710f219
Oliver Krylow <o.krylow@isp-insoft.de> ·
SSH signature · not checked
- Parent 1: 01166bd0546f
bugabingas dorkfiles
ae83d153f940400288ed5acd895d58b42710f219
Oliver Krylow <o.krylow@isp-insoft.de> ·
SSH signature · not checked
+400 / −17 text lines
+2 / −0 text lines
+55 / −0 text lines
-- pivi publishes <Plug> mappings and claims no keys of its own, so the choice-- of keys lives here with every other choice this configuration makes.map.normal { description = 'Open the pi prompt', category = 'pi', keys = '<leader>pp', command = '<Plug>(pivi-prompt)', remap = true,}map.normal { description = 'Show the pi transcript', category = 'pi', keys = '<leader>ps', command = '<Plug>(pivi-show)', remap = true,}map.normal { description = 'Abort the current pi run', category = 'pi', keys = '<leader>pa', command = '<Plug>(pivi-abort)', remap = true,}map.normal { description = 'Toggle following the agent', category = 'pi', keys = '<leader>pf', command = '<Plug>(pivi-follow)', remap = true,}map.normal { description = 'Ask pi about neovim, pivi or pi', category = 'pi', keys = '<leader>ph', command = ':Pi help ',}map.normal { description = 'Ask pi about the project', category = 'pi', keys = '<leader>pk', command = ':Pi ask ',}map.visual { description = 'Ask pi about the selection', category = 'pi', keys = '<leader>pk', command = ':Pi ask ',}+9 / −1 text lines
22. **Entry shape changes without the buffer changing.** Re-anchoring an entry moves its bounds while touching no character, so anything cached per buffer must key on `entry.version` as well as `b:changedtick`.23. **Contract tests guard assumptions,** not features. They skip unless `pi` is on `PATH` and opt-in is set. When Pi changes behavior, they fail before users do.23. **A hook only reaches buffers made after it.** `transcript.on_buffer`, `on_window` and `on_reset` listeners register at module load, so every module that binds keys or decorates must be required before the first transcript exists. `bugabinga.pivi` requires them all up front; a spec that requires only one of them silently gets a buffer with fewer mappings.24. **A nameless buffer reads as the working directory.** `fnamemodify('', ':~:.')` returns the cwd, so an unnamed scratch buffer makes a statusline claim the user is editing a file in it, which is where `system32` came from under a launcher that starts in `C:\Windows\system32`. Name every buffer pivi creates, floats included.25. **Contract tests guard assumptions,** not features. They skip unless `pi` is on `PATH` and opt-in is set. When Pi changes behavior, they fail before users do.| Reported change | written as the tool formatted it and highlighted with `DiffAdd`/`DiffDelete` per line; `bugabinga.pivi.diff` rebuilds the prior content from the change itself and states when the file has moved on || Streaming output | rewrite only the entry tail, skip per-line decoration while it runs, and never run `fold.apply` mid-stream || Output language | parsed as a standalone string and applied as highlight extmarks, never as a fence, so transcript text stays what the tool produced || Keys | pivi binds buffer-locally and publishes `<Plug>` mappings; the configuration chooses the keys in `options/keymaps.lua`. `]t` and `[t` are real global maps (`:tnext`, `:tprevious`) that pivi shadows inside its own buffers only || Guide | `?` reads the mappings that exist in the buffer rather than a list written beside them, so it cannot drift |+11 / −1 text lines
local guide = require 'bugabinga.pivi.guide' vim.keymap.set( 'n', '<Plug>(pivi-prompt)', function() subcommands.prompt() end, { desc = 'pivi open prompt', } ) vim.keymap.set( 'n', '<Plug>(pivi-abort)', function() subcommands.abort() end, { desc = 'pivi abort run', } ) vim.keymap.set( 'n', '<Plug>(pivi-follow)', function() subcommands.follow '' end, { desc = 'pivi toggle follow', } ) vim.keymap.set( 'n', '<Plug>(pivi-guide)', function() subcommands.guide() end, { desc = 'pivi show the guide', } )+7 / −0 text lines
+10 / −6 text lines
new file mode 100644+97 / −0 text lines
local transcript = require 'bugabinga.pivi.transcript'local glyph = require 'std.icon'--- What the keys of a pivi buffer are for.--- The guide reads the mappings that actually exist rather than a list written--- beside them, so it cannot drift from the buffer it describes.local describe = function( bufnr ) local found = {} for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do local description = mapping.desc or '' if description:find( 'pivi', 1, true ) and not mapping.lhs:find( '<Plug>', 1, true ) then table.insert( found, { keys = mapping.lhs, what = description:gsub( '^pivi ', '' ), } ) end end table.sort( found, function( left, right ) return left.keys < right.keys end ) return foundendlocal commands = function() local ok, command = pcall( require, 'bugabinga.pivi.command' ) if not ok then return {} end return command.names()endlocal lines = function( bufnr ) local written = { '# ' .. glyph.help .. ' pivi', '', } local keys = describe( bufnr ) if #keys > 0 then table.insert( written, '## ' .. glyph.keyword .. ' keys here' ) table.insert( written, '' ) for _, mapping in ipairs( keys ) do table.insert( written, ( '- `%s` %s' ):format( mapping.keys, mapping.what ) ) end table.insert( written, '' ) end local names = commands() if #names > 0 then table.insert( written, '## ' .. glyph.command .. ' commands' ) table.insert( written, '' ) for _, name in ipairs( names ) do table.insert( written, '- `:Pi ' .. name .. '`' ) end table.insert( written, '' ) table.insert( written, 'Bare `:Pi` opens the prompt.' ) table.insert( written, '' ) end table.insert( written, '## ' .. glyph.list .. ' elsewhere' ) table.insert( written, '' ) table.insert( written, '- `zR` / `zM` expand or collapse every entry' ) table.insert( written, '- `]c` / `[c` move between changes inside a comparison' ) table.insert( written, '- `:help pivi` if the documentation is installed' ) return writtenend--- Shows the guide without leaving the buffer it describes.--- @return number? bufnr--- @return number? winidlocal show = function( bufnr ) bufnr = bufnr or vim.api.nvim_get_current_buf() if not vim.api.nvim_buf_is_valid( bufnr ) then return nil end local ok, float, window = pcall( vim.lsp.util.open_floating_preview, lines( bufnr ), 'markdown', { border = 'rounded', focus = false, focusable = true, max_width = 80, max_height = 30, title = ' pivi ', } ) if not ok then return nil end if float and vim.api.nvim_buf_is_valid( float ) then pcall( vim.api.nvim_buf_set_name, float, 'pivi://guide' ) end return float, windowend--- Buffer-local only, so `?` keeps searching backwards everywhere else.local bind = function( bufnr ) local action = function() show( bufnr ) end vim.keymap.set( 'n', '?', action, { buffer = bufnr, desc = 'pivi show this guide', } ) vim.keymap.set( 'n', '<Plug>(pivi-guide)', action, { buffer = bufnr, desc = 'pivi show this guide', } )endtranscript.on_buffer( bind )return { describe = describe, lines = lines, show = show, bind = bind,}+2 / −0 text lines
+14 / −3 text lines
local glyph = require 'std.icon' error = { prefix = '! ', prefix_hl = 'ErrorMsg', }, error = { prefix = glyph.error .. ' ', prefix_hl = 'ErrorMsg', }, if heads and opening then prefix = opening end+3 / −0 text lines
| `?` | show the guide of keys and commands for this buffer |These keys are buffer-local, and `<Plug>(pivi-toggle-entry)`, `<Plug>(pivi-next-entry)`, `<Plug>(pivi-previous-entry)`, `<Plug>(pivi-next-tool)`, `<Plug>(pivi-previous-tool)`, `<Plug>(pivi-goto-location)`, `<Plug>(pivi-inspect-entry)` and `<Plug>(pivi-compare-change)` exist alongside them.| `:Pi` | open the prompt, starting a session when there is none || `:Pi help {question}` | this ephemeral run: no persisted session, no working-session history, queue, or status touched || `:Pi guide` | show the guide of keys and commands |+30 / −0 text lines
--- What each tool does, at a glance.--- A reader scans a transcript for the shape of the work, so the icon says--- what kind of work it was rather than which tool performed it.local ICONS = { read = glyph.file, write = glyph.edit, edit = glyph.edit, bash = glyph.shell, powershell = glyph.shell, grep = glyph.search, find = glyph.search, ls = glyph.folder, nvim_help = glyph.help, nvim_state = glyph.vim, nvim_lua = glyph.vim, nvim_findings = glyph.list,}local DEFAULT_ICON = glyph.command--- The icon for a tool entry.local icon = function( meta ) local name = meta and meta.tool return ICONS[name] or DEFAULT_ICONend+12 / −5 text lines
local glyph = require 'std.icon'local SPINNER = { '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏', } if entry.error then return '%#ErrorMsg#π ' .. ( entry.tool and entry.tool.name or 'error' ) .. '%*' end if entry.compacting then return '%#WarningMsg#π compacting%*' end if entry.run == 'retrying' then return '%#WarningMsg#π retry%*' end if entry.error then return '%#ErrorMsg#' .. glyph.error .. ' ' .. ( entry.tool and entry.tool.name or 'error' ) .. '%*' end if entry.compacting then return '%#WarningMsg#π ' .. glyph.lazy .. ' compacting%*' end if entry.run == 'retrying' then return '%#WarningMsg#π ' .. glyph.warn .. ' retry%*' end+6 / −1 text lines
local path = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ':~:.')+16 / −0 text lines
it( 'opens the prompt when given no subcommand', function() local channel = fake.new() session.create { spawn = channel.spawn, name = 'spec', extension = false, skill = false, env = {}, } command.dispatch { args = '', } local opened for _, bufnr in ipairs( vim.api.nvim_list_bufs() ) do if vim.bo[bufnr].filetype == command.input_filetype then opened = bufnr end end assert.is_number( opened ) assert.is_true( vim.fn.bufwinid( opened ) > 0 ) vim.cmd.stopinsert() end )new file mode 100644+87 / −0 text lines
---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch, redundant-parameter-- Every module that binds a key registers its hook on load, and a hook only-- reaches buffers created after it, so they all load before the first one.require 'bugabinga.pivi.fold'require 'bugabinga.pivi.navigate'require 'bugabinga.pivi.detail'require 'bugabinga.pivi.diff'local guide = require 'bugabinga.pivi.guide'local transcript = require 'bugabinga.pivi.transcript'local fake = dofile 'tests/helpers/pivi_fake.lua'local text_of = function( lines ) return table.concat( lines, '\n' ) enddescribe( 'pivi guide', function() local bufnr before_each( function() bufnr = transcript.ensure 'guided' transcript.clear( bufnr ) end ) after_each( function() transcript.forget_all() fake.close_extra_windows() end ) it( 'reads the keys that actually exist in the buffer', function() local described = {} for _, mapping in ipairs( guide.describe( bufnr ) ) do described[mapping.keys] = mapping.what end for _, keys in ipairs { '?', '<CR>', ']m', '[m', ']t', '[t', 'gf', 'gD', 'K', } do assert.is_string( described[keys], keys .. ' is missing from the guide' ) end end ) it( 'drops the plug mappings a reader cannot type', function() for _, mapping in ipairs( guide.describe( bufnr ) ) do assert.is_nil( mapping.keys:find( 'Plug', 1, true ) ) end end ) it( 'lists every command the user command offers', function() local written = text_of( guide.lines( bufnr ) ) for _, name in ipairs { 'ask', 'help', 'prompt', 'diff', 'guide', } do assert.is_true( written:find( ':Pi ' .. name, 1, true ) ~= nil, name .. ' is missing' ) end assert.is_true( written:find( 'Bare `:Pi` opens the prompt', 1, true ) ~= nil ) end ) it( 'explains what each key does, not only that it exists', function() local written = text_of( guide.lines( bufnr ) ) assert.is_true( written:find( 'collapse or expand', 1, true ) ~= nil ) assert.is_true( written:find( 'next entry', 1, true ) ~= nil ) assert.is_true( written:find( 'compare', 1, true ) ~= nil ) end ) it( 'shows itself without leaving the transcript', function() local win = transcript.show( bufnr ) vim.api.nvim_set_current_win( win ) local float, floating = guide.show( bufnr ) assert.is_number( float ) assert.equal( win, vim.api.nvim_get_current_win() ) assert.is_true( vim.api.nvim_buf_get_name( float ):find( 'pivi://guide', 1, true ) ~= nil ) if floating and vim.api.nvim_win_is_valid( floating ) then vim.api.nvim_win_close( floating, true ) end end ) it( 'leaves a backwards search alone outside pivi', function() vim.cmd 'enew' local ordinary = vim.api.nvim_get_current_buf() for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( ordinary, 'n' ) ) do assert.is_not.equal( '?', mapping.lhs ) end for _, mapping in ipairs( vim.api.nvim_get_keymap 'n' ) do assert.is_not.equal( '?', mapping.lhs ) end end )end )+22 / −0 text lines
describe( 'icons', function() it( 'shows the kind of work rather than the name of the tool', function() local glyph = require 'std.icon' assert.equal( glyph.edit, tool.icon { tool = 'edit', } ) assert.equal( glyph.edit, tool.icon { tool = 'write', } ) assert.equal( glyph.file, tool.icon { tool = 'read', } ) assert.equal( glyph.shell, tool.icon { tool = 'bash', } ) assert.equal( glyph.search, tool.icon { tool = 'grep', } ) assert.equal( glyph.search, tool.icon { tool = 'find', } ) assert.equal( glyph.folder, tool.icon { tool = 'ls', } ) assert.equal( glyph.help, tool.icon { tool = 'nvim_help', } ) end ) it( 'falls back for a tool it has never heard of', function() local glyph = require 'std.icon' assert.equal( glyph.command, tool.icon { tool = 'something_new', } ) assert.equal( glyph.command, tool.icon( nil ) ) end ) end )+17 / −0 text lines
describe('file_component', function() it('names a buffer that has no name instead of the working directory', function() vim.cmd 'enew' local shown = BugabingaStatusline.render() assert.is_true(shown:find('[No Name]', 1, true) ~= nil) assert.is_nil(shown:find(vim.fn.fnamemodify(vim.uv.cwd() or '', ':~'), 1, true)) end) it('names a buffer that has one', function() vim.cmd 'enew' vim.api.nvim_buf_set_name(0, 'pivi://work') assert.is_true(BugabingaStatusline.render():find('pivi://work', 1, true) ~= nil) end) end)