repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

refactor(neovim): let pivi publish key intents instead of claiming keys

f14cc9833776d2b4fe037db85b61970bfc3cb873

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (18)

neovim/init.lua

Modified
84 84 defer 'bugabinga.d2'
85 85 defer 'bugabinga.pivi'
86 86 defer 'bugabinga.pivi_status'
87 +defer 'bugabinga.pivi_keys'
88 89 -- Plugins handle deferred loading via puk.
89 90 -- No need to use `defer(...)`

neovim/lua/bugabinga/options/keymaps.lua

Modified
117 --- pivi publishes <Plug> mappings and claims no keys of its own, so the choice
118 --- of keys lives here with every other choice this configuration makes.
119 -map.normal {
120 - description = 'Open the pi prompt',
121 - category = 'pi',
122 - keys = '<leader>pp',
123 - command = '<Plug>(pivi-prompt)',
124 - remap = true,
127 -map.normal {
128 - description = 'Show the pi transcript',
129 - category = 'pi',
130 - keys = '<leader>ps',
131 - command = '<Plug>(pivi-show)',
132 - remap = true,
135 -map.normal {
136 - description = 'Abort the current pi run',
137 - category = 'pi',
138 - keys = '<leader>pa',
139 - command = '<Plug>(pivi-abort)',
140 - remap = true,
143 -map.normal {
144 - description = 'Toggle following the agent',
145 - category = 'pi',
146 - keys = '<leader>pf',
147 - command = '<Plug>(pivi-follow)',
148 - remap = true,
151 -map.normal {
152 - description = 'Ask pi about neovim, pivi or pi',
153 - category = 'pi',
154 - keys = '<leader>ph',
155 - command = ':Pi help ',
158 -map.normal {
159 - description = 'Ask pi about the project',
160 - category = 'pi',
161 - keys = '<leader>pk',
162 - command = ':Pi ask ',
165 -map.visual {
166 - description = 'Ask pi about the selection',
167 - category = 'pi',
168 - keys = '<leader>pk',
169 - command = ':Pi ask ',
172 117 map.normal {
173 118 description = 'save',
174 119 category = 'file',

neovim/lua/bugabinga/pivi/AGENTS.md

Modified
90 90 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`.
91 91 Name every buffer pivi creates, floats included.
93 -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.
93 +25. **Never shadow a key whose meaning you are changing.** pivi publishes `<Plug>` intents; a configuration binds them.
94 + `tests/bugabinga/pivi/keys_spec.lua` asserts pivi claims no bare key in any of its buffers, with `/` in a prompt as the single declared exception.
95 + Accepting a completion and accepting a prompt are the same intent, so `<Plug>(pivi-accept)` yields to an open popup instead of replacing it.
97 +26. **`std.auto` clears its group on every call.** Registering two autocmds with the same group name in two calls silently deletes the first; pass one list instead.
99 +27. **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.
95 101 ## Commands
132 138 | 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 |
133 139 | Streaming output | rewrite only the entry tail, skip per-line decoration while it runs, and never run `fold.apply` mid-stream |
134 140 | Output language | parsed as a standalone string and applied as highlight extmarks, never as a fence, so transcript text stays what the tool produced |
135 -| 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 |
141 +| Keys | pivi binds no bare key and publishes `<Plug>` intents only; `lua/bugabinga/pivi_keys.lua` chooses the keys. A key is reused only where the local behaviour is the same idea as the global one, so `<CR>` activates, `K` shows information, `]]` and `[[` move by section, and folding needs no key because entries are real folds |
136 142 | Guide | `?` reads the mappings that exist in the buffer rather than a list written beside them, so it cannot drift |
138 144 ## Gotchas

neovim/lua/bugabinga/pivi/command.lua

Modified
114 114 guide.bind( bufnr )
116 - vim.keymap.set( { 'n', 'i', }, '<c-s>', function()
116 + local send = function()
117 117 local lines = vim.api.nvim_buf_get_lines( bufnr, 0, -1, false )
118 118 local text = vim.trim( table.concat( lines, '\n' ) )
119 119 if text == '' then return end
125 125 vim.api.nvim_buf_set_lines( bufnr, 0, -1, false, { '', } )
126 126 -- A submission the user cannot read the answer to is a dead end.
127 127 transcript.show( current.buffer )
128 - end, { buffer = bufnr, desc = 'pivi submit prompt', } )
128 + end
130 + vim.keymap.set( { 'n', 'i', }, '<Plug>(pivi-submit)', send,
131 + { buffer = bufnr, desc = 'pivi submit the prompt', } )
133 + -- Accepting a completion and accepting a prompt are the same intent, so a
134 + -- key bound to this yields to the popup whenever one is open.
135 + vim.keymap.set( 'i', '<Plug>(pivi-accept)', function()
136 + if vim.fn.pumvisible() == 1 then return '<C-y>' end
137 + vim.schedule( send )
138 + return ''
139 + end, { buffer = bufnr, expr = true, desc = 'pivi accept the completion, else the prompt', } )
130 141 if pcall( vim.cmd.split ) then
131 142 local win = vim.api.nvim_get_current_win()
133 144 pcall( vim.api.nvim_win_set_height, win, 6 )
134 145 transcript.decorate( win, current.name )
135 146 vim.wo[win].winbar = '%#Title#π ' .. current.name ..
136 - '%* %#Comment#<c-s> send / commands%*'
147 + '%* %#Comment#' .. ( vim.g.pivi_submit_hint or 'send' ) .. ' / commands%*'
137 148 vim.cmd.startinsert()
139 150 return bufnr

neovim/lua/bugabinga/pivi/detail.lua

Modified
122 122 local bind = function( bufnr )
123 - local action = function() inspect( bufnr ) end
124 - vim.keymap.set( 'n', 'K', action, { buffer = bufnr, desc = 'pivi inspect the entry', } )
125 - vim.keymap.set( 'n', '<Plug>(pivi-inspect-entry)', action,
123 + vim.keymap.set( 'n', '<Plug>(pivi-inspect-entry)', function() inspect( bufnr ) end,
126 124 { buffer = bufnr, desc = 'pivi inspect the entry', } )

neovim/lua/bugabinga/pivi/diff.lua

Modified
121 121 local bind = function( bufnr )
122 - local action = function() open( bufnr ) end
123 - vim.keymap.set( 'n', 'gD', action, { buffer = bufnr, desc = 'pivi compare the change of the entry', } )
124 - vim.keymap.set( 'n', '<Plug>(pivi-compare-change)', action,
122 + vim.keymap.set( 'n', '<Plug>(pivi-compare-change)', function() open( bufnr ) end,
125 123 { buffer = bufnr, desc = 'pivi compare the change of the entry', } )

neovim/lua/bugabinga/pivi/fold.lua

Modified
143 143 wanted[bufnr] = nil
146 ---- Buffer-local keys only: pivi never claims a key outside its own buffers.
146 +--- pivi publishes intent and never chooses a key.
147 +--- Ordinary fold keys already work here, because entries are real folds.
147 148 local bind = function( bufnr )
148 - vim.keymap.set( 'n', '<CR>', function()
149 - toggle( bufnr, vim.api.nvim_win_get_cursor( 0 )[1] )
150 - end, { buffer = bufnr, desc = 'pivi collapse or expand the entry', } )
152 149 vim.keymap.set( 'n', '<Plug>(pivi-toggle-entry)', function()
153 150 toggle( bufnr, vim.api.nvim_win_get_cursor( 0 )[1] )
154 151 end, { buffer = bufnr, desc = 'pivi collapse or expand the entry', } )

neovim/lua/bugabinga/pivi/guide.lua

Modified
1 1 local transcript = require 'bugabinga.pivi.transcript'
2 2 local glyph = require 'std.icon'
4 ---- What the keys of a pivi buffer are for.
5 ---- The guide reads the mappings that actually exist rather than a list written
6 ---- beside them, so it cannot drift from the buffer it describes.
7 -local describe = function( bufnr )
8 - local found = {}
9 - for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
10 - local description = mapping.desc or ''
11 - if description:find( 'pivi', 1, true ) and not mapping.lhs:find( '<Plug>', 1, true ) then
12 - table.insert( found, { keys = mapping.lhs, what = description:gsub( '^pivi ', '' ), } )
4 +local MODES = { 'n', 'i', 'x', }
6 +--- What every published intent is for, by its name.
7 +local intents = function( bufnr )
8 + local known = {}
9 + for _, mode in ipairs( MODES ) do
10 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, mode ) ) do
11 + local plug = mapping.lhs:match '^<Plug>%((pivi%-[%w%-]+)%)$'
12 + if plug then known[plug] = ( mapping.desc or plug ):gsub( '^pivi ', '' ) end
15 + return known
16 +end
18 +--- What the keys of a pivi buffer are for.
19 +--- pivi chooses no keys, so the guide reports the ones the configuration bound
20 +--- to pivi's intents. It describes what is actually reachable rather than what
21 +--- anyone intended, and cannot drift from either.
22 +local describe = function( bufnr )
23 + local known = intents( bufnr )
24 + local found = {}
25 + local seen = {}
27 + local collect = function( mappings )
28 + for _, mapping in ipairs( mappings ) do
29 + if not mapping.lhs:find( '<Plug>', 1, true ) then
30 + local target = ( mapping.rhs or '' ):match '<Plug>%((pivi%-[%w%-]+)%)'
31 + local what = target and known[target] or nil
33 + -- A key pivi binds itself, such as slash-command entry, is described
34 + -- by its own mapping rather than by an intent.
35 + if not what and ( mapping.desc or '' ):find( 'pivi', 1, true ) then
36 + what = mapping.desc:gsub( '^pivi ', '' )
37 + end
39 + if what and not seen[mapping.lhs] then
40 + seen[mapping.lhs] = true
41 + table.insert( found, { keys = mapping.lhs, what = what, } )
42 + end
43 + end
44 + end
45 + end
47 + for _, mode in ipairs( MODES ) do
48 + collect( vim.api.nvim_buf_get_keymap( bufnr, mode ) )
49 + collect( vim.api.nvim_get_keymap( mode ) )
50 + end
15 52 table.sort( found, function( left, right ) return left.keys < right.keys end )
16 53 return found
47 84 table.insert( written, '' )
50 - table.insert( written, '## ' .. glyph.list .. ' elsewhere' )
87 + table.insert( written, '## ' .. glyph.list .. ' ordinary neovim' )
51 88 table.insert( written, '' )
89 + table.insert( written, '- `za` `zo` `zc` collapse or expand the entry under the cursor' )
52 90 table.insert( written, '- `zR` / `zM` expand or collapse every entry' )
53 91 table.insert( written, '- `]c` / `[c` move between changes inside a comparison' )
54 - table.insert( written, '- `:help pivi` if the documentation is installed' )
92 + table.insert( written, '' )
93 + table.insert( written, 'Entries are real folds, so no key here replaces one you already know.' )
56 95 return written
80 119 return float, window
83 ---- Buffer-local only, so `?` keeps searching backwards everywhere else.
84 122 local bind = function( bufnr )
85 - local action = function() show( bufnr ) end
86 - vim.keymap.set( 'n', '?', action, { buffer = bufnr, desc = 'pivi show this guide', } )
87 - vim.keymap.set( 'n', '<Plug>(pivi-guide)', action, { buffer = bufnr, desc = 'pivi show this guide', } )
123 + vim.keymap.set( 'n', '<Plug>(pivi-guide)', function() show( bufnr ) end,
124 + { buffer = bufnr, desc = 'pivi show this guide', } )
90 127 transcript.on_buffer( bind )

neovim/lua/bugabinga/pivi/navigate.lua

Modified
73 73 return follow.visit( location )
76 ---- Buffer-local motions only: a transcript never claims a key elsewhere.
76 +--- pivi publishes intent; the configuration chooses the keys.
77 77 local bind = function( bufnr )
78 78 local motions = {
79 - { keys = ']m', plug = '<Plug>(pivi-next-entry)', kind = nil, move = next_entry,
79 + { plug = '<Plug>(pivi-next-entry)', kind = nil, move = next_entry,
80 80 description = 'pivi next entry', },
81 - { keys = '[m', plug = '<Plug>(pivi-previous-entry)', kind = nil, move = previous_entry,
81 + { plug = '<Plug>(pivi-previous-entry)', kind = nil, move = previous_entry,
82 82 description = 'pivi previous entry', },
83 - { keys = ']t', plug = '<Plug>(pivi-next-tool)', kind = 'tool', move = next_entry,
83 + { plug = '<Plug>(pivi-next-tool)', kind = 'tool', move = next_entry,
84 84 description = 'pivi next tool call', },
85 - { keys = '[t', plug = '<Plug>(pivi-previous-tool)', kind = 'tool', move = previous_entry,
85 + { plug = '<Plug>(pivi-previous-tool)', kind = 'tool', move = previous_entry,
86 86 description = 'pivi previous tool call', },
89 89 for _, motion in ipairs( motions ) do
90 - local action = function() motion.move( bufnr, motion.kind ) end
91 - vim.keymap.set( 'n', motion.keys, action, { buffer = bufnr, desc = motion.description, } )
92 - vim.keymap.set( 'n', motion.plug, action, { buffer = bufnr, desc = motion.description, } )
90 + vim.keymap.set( 'n', motion.plug, function() motion.move( bufnr, motion.kind ) end,
91 + { buffer = bufnr, desc = motion.description, } )
95 - local visit = function() locate( bufnr ) end
96 - vim.keymap.set( 'n', 'gf', visit, { buffer = bufnr, desc = 'pivi go to the location of the entry', } )
97 - vim.keymap.set( 'n', '<Plug>(pivi-goto-location)', visit,
94 + vim.keymap.set( 'n', '<Plug>(pivi-goto-location)', function() locate( bufnr ) end,
98 95 { buffer = bufnr, desc = 'pivi go to the location of the entry', } )

neovim/lua/bugabinga/pivi/skills/pivi/SKILL.md

Modified
48 48 - Output of a running tool appears as it arrives, and output that came from a known language is presented in it.
49 49 - Nothing here is invented: a tool that reports nothing about its own work simply names its subject.
51 -| Key in a transcript | Effect |
51 +| Intent in a transcript | Bound here to |
52 52 | --- | --- |
53 -| `<CR>` | collapse or expand the entry under the cursor |
54 -| `]m` / `[m` | move to the next or previous entry |
55 -| `]t` / `[t` | move to the next or previous tool call |
56 -| `gf` | go to the location the entry names |
57 -| `K` | inspect the entry, including tool arguments and result |
58 -| `gD` | compare a completed change as a real editor diff |
59 -| `?` | show the guide of keys and commands for this buffer |
60 -| `zR` / `zM` | expand or collapse everything, as in any buffer |
53 +| go to what the entry names | `<CR>` |
54 +| inspect the entry | `K` |
55 +| next or previous entry | `]]` / `[[` |
56 +| next or previous tool call | `<leader>pn` / `<leader>pN` |
57 +| compare the change | `<leader>pd` |
58 +| show this guide | `<leader>p?` |
59 +| collapse or expand | `za` `zo` `zc` `zR` `zM`, because entries are real folds |
62 -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.
61 +In the prompt, `<c-y>` accepts: the completion when one is offered, the prompt otherwise. `<CR>` in normal mode sends it.
63 +pivi chooses none of these. It publishes `<Plug>(pivi-goto-location)`, `<Plug>(pivi-inspect-entry)`, `<Plug>(pivi-next-entry)`, `<Plug>(pivi-previous-entry)`, `<Plug>(pivi-next-tool)`, `<Plug>(pivi-previous-tool)`, `<Plug>(pivi-compare-change)`, `<Plug>(pivi-toggle-entry)`, `<Plug>(pivi-guide)`, `<Plug>(pivi-submit)` and `<Plug>(pivi-accept)`, and the configuration decides the keys. A key is only reused where the local behaviour is the same idea as the global one.
65 +The single key pivi binds itself is `/` in a prompt, and only at the first character, because there the behaviour *is* the key.
64 67 ## Surfaces the user has

neovim/lua/bugabinga/pivi_keys.lua

Added
new file mode 100644
1 +--- Keys for pivi.
2 +--- pivi publishes intents and chooses no keys, so every choice below is this
3 +--- configuration's. A key that already exists keeps its meaning: it is only
4 +--- reused where the local behaviour is the same idea as the global one.
5 +local auto = require 'std.auto'
6 +local map = require 'std.map'
8 +--- Inside a transcript.
9 +--- `<CR>` activates the thing under the cursor, as it does in a quickfix list,
10 +--- a help buffer or a file explorer.
11 +--- `K` shows what is known about the thing under the cursor.
12 +--- `]]` and `[[` move by section, and an entry is exactly a section.
13 +--- Folding needs no key at all, because entries are real folds.
14 +local TRANSCRIPT = {
15 + { keys = '<cr>', intent = 'goto-location', description = 'Go to what this entry names', },
16 + { keys = 'K', intent = 'inspect-entry', description = 'Inspect this entry', },
17 + { keys = ']]', intent = 'next-entry', description = 'Next entry', },
18 + { keys = '[[', intent = 'previous-entry', description = 'Previous entry', },
19 + { keys = '<leader>pd', intent = 'compare-change', description = 'Compare this change', },
20 + { keys = '<leader>p?', intent = 'guide', description = 'Show the pivi guide', },
21 + { keys = '<leader>pn', intent = 'next-tool', description = 'Next tool call', },
22 + { keys = '<leader>pN', intent = 'previous-tool', description = 'Previous tool call', },
25 +--- Inside the prompt.
26 +--- `\u003cc-y\u003e` accepts: the completion when one is offered, the prompt otherwise.
27 +--- `\u003ccr\u003e` in normal mode activates what has been composed, and stays a newline
28 +--- while typing.
29 +local PROMPT = {
30 + { keys = '<c-y>', intent = 'accept', modes = { 'i', }, description = 'Accept the completion, else send', },
31 + { keys = '<cr>', intent = 'submit', modes = { 'n', }, description = 'Send the prompt', },
32 + { keys = '<leader>p?', intent = 'guide', modes = { 'n', }, description = 'Show the pivi guide', },
35 +local bind = function( bufnr, choices )
36 + for _, choice in ipairs( choices ) do
37 + for _, mode in ipairs( choice.modes or { 'n', } ) do
38 + vim.keymap.set( mode, choice.keys, '<Plug>(pivi-' .. choice.intent .. ')', {
39 + buffer = bufnr,
40 + remap = true,
41 + desc = 'pi: ' .. choice.description,
42 + } )
43 + end
44 + end
45 +end
47 +-- One call: re-calling with the same group name clears everything registered
48 +-- under it, so both surfaces are declared together.
49 +auto 'pivi_keys' {
50 + {
51 + events = 'FileType',
52 + pattern = 'pivi',
53 + description = 'Choose the keys of a pivi transcript.',
54 + command = function( event ) bind( event.buf, TRANSCRIPT ) end,
55 + },
56 + {
57 + events = 'FileType',
58 + pattern = 'pivi-input',
59 + description = 'Choose the keys of a pivi prompt.',
60 + command = function( event ) bind( event.buf, PROMPT ) end,
61 + },
64 +-- What the prompt window says about sending, so the hint follows the choice.
65 +vim.g.pivi_submit_hint = '<c-y> send'
67 +map.normal {
68 + description = 'Open the pi prompt',
69 + category = 'pi',
70 + keys = '<leader>pp',
71 + command = '<Plug>(pivi-prompt)',
72 + remap = true,
75 +map.normal {
76 + description = 'Show the pi transcript',
77 + category = 'pi',
78 + keys = '<leader>ps',
79 + command = '<Plug>(pivi-show)',
80 + remap = true,
83 +map.normal {
84 + description = 'Abort the current pi run',
85 + category = 'pi',
86 + keys = '<leader>pa',
87 + command = '<Plug>(pivi-abort)',
88 + remap = true,
91 +map.normal {
92 + description = 'Toggle following the agent',
93 + category = 'pi',
94 + keys = '<leader>pf',
95 + command = '<Plug>(pivi-follow)',
96 + remap = true,
99 +map.normal {
100 + description = 'Ask pi about neovim, pivi or pi',
101 + category = 'pi',
102 + keys = '<leader>ph',
103 + command = ':Pi help ',
106 +map.normal {
107 + description = 'Ask pi about the project',
108 + category = 'pi',
109 + keys = '<leader>pk',
110 + command = ':Pi ask ',
113 +map.visual {
114 + description = 'Ask pi about the selection',
115 + category = 'pi',
116 + keys = '<leader>pk',
117 + command = ':Pi ask ',
120 +return {
121 + transcript = TRANSCRIPT,
122 + prompt = PROMPT,
123 + bind = bind,

neovim/tests/bugabinga/pivi/command_spec.lua

Modified
48 48 assert.is_true( win > 0 )
49 49 assert.is_true( vim.wo[win].winbar:find( 'spec', 1, true ) ~= nil )
50 - assert.is_true( vim.wo[win].winbar:find( 'c-s', 1, true ) ~= nil )
50 + assert.is_true( vim.wo[win].winbar:find( 'send', 1, true ) ~= nil )
51 51 assert.is_true( vim.api.nvim_buf_get_name( bufnr ):find( 'pivi://', 1, true ) ~= nil )
52 52 assert.equal( 'spec', vim.b[bufnr].pivi_session )
65 65 local mapping
66 66 for _, entry in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
67 - if entry.lhs == '<C-S>' then mapping = entry end
67 + if entry.lhs == '<Plug>(pivi-submit)' then mapping = entry end
69 69 assert.is_table( mapping )

neovim/tests/bugabinga/pivi/editor/detail_spec.lua

Modified
105 105 assert.is_nil( detail.inspect( bufnr, 1 ) )
108 - it( 'binds inspection in its own buffers only', function()
109 - local bound = {}
108 + it( 'publishes inspection as an intent', function()
109 + local published
110 110 for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
111 - bound[mapping.lhs] = mapping.desc or ''
111 + if mapping.lhs == '<Plug>(pivi-inspect-entry)' then published = mapping end
114 - assert.is_string( bound.K )
115 - assert.is_true( bound.K:find( 'pivi', 1, true ) ~= nil )
117 - for _, mapping in ipairs( vim.api.nvim_get_keymap 'n' ) do
118 - assert.is_nil( ( mapping.desc or '' ):find( 'pivi', 1, true ) )
119 - end
114 + assert.is_table( published )

neovim/tests/bugabinga/pivi/editor/diff_spec.lua

Modified
155 155 assert.equal( win, vim.api.nvim_get_current_win() )
158 - it( 'binds comparison in its own buffers only', function()
159 - local bound = {}
158 + it( 'publishes comparison as an intent', function()
159 + local published
160 160 for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
161 - bound[mapping.lhs] = mapping.desc or ''
161 + if mapping.lhs == '<Plug>(pivi-compare-change)' then published = mapping end
164 - assert.is_string( bound.gD )
165 - assert.is_true( bound.gD:find( 'pivi', 1, true ) ~= nil )
164 + assert.is_table( published )

neovim/tests/bugabinga/pivi/editor/fold_spec.lua

Modified
124 124 assert.equal( 'tool', entry.at( bufnr, first ).kind )
127 - it( 'binds a buffer-local key without claiming a global one', function()
128 - local found
127 + it( 'publishes collapsing as an intent and claims no key for it', function()
128 + local published
129 129 for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
130 - if mapping.lhs == '<CR>' then found = mapping end
130 + if mapping.lhs == '<Plug>(pivi-toggle-entry)' then published = mapping end
133 - assert.is_table( found )
134 - for _, mapping in ipairs( vim.api.nvim_get_keymap 'n' ) do
135 - assert.is_nil( ( mapping.desc or '' ):find( 'pivi', 1, true ) )
136 - end
133 + assert.is_table( published )

neovim/tests/bugabinga/pivi/editor/guide_spec.lua

Modified
5 5 require 'bugabinga.pivi.navigate'
6 6 require 'bugabinga.pivi.detail'
7 7 require 'bugabinga.pivi.diff'
8 +require 'bugabinga.pivi_keys'
8 9 local guide = require 'bugabinga.pivi.guide'
9 10 local transcript = require 'bugabinga.pivi.transcript'
10 11 local fake = dofile 'tests/helpers/pivi_fake.lua'
30 31 described[mapping.keys] = mapping.what
33 - for _, keys in ipairs { '?', '<CR>', ']m', '[m', ']t', '[t', 'gf', 'gD', 'K', } do
34 + -- The keys are the configuration's choice, so the guide reports those.
35 + for _, keys in ipairs { '<CR>', 'K', ']]', '[[', } do
34 36 assert.is_string( described[keys], keys .. ' is missing from the guide' )
36 38 end )
53 55 it( 'explains what each key does, not only that it exists', function()
54 56 local written = text_of( guide.lines( bufnr ) )
56 - assert.is_true( written:find( 'collapse or expand', 1, true ) ~= nil )
57 - assert.is_true( written:find( 'next entry', 1, true ) ~= nil )
58 - assert.is_true( written:find( 'compare', 1, true ) ~= nil )
58 + assert.is_true( written:lower():find( 'next entry', 1, true ) ~= nil )
59 + assert.is_true( written:lower():find( 'compare', 1, true ) ~= nil )
60 + assert.is_true( written:find( 'za', 1, true ) ~= nil )
59 61 end )
61 63 it( 'shows itself without leaving the transcript', function()
74 76 end )
76 - it( 'leaves a backwards search alone outside pivi', function()
77 - vim.cmd 'enew'
78 - local ordinary = vim.api.nvim_get_current_buf()
80 - for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( ordinary, 'n' ) ) do
78 + it( 'leaves a backwards search alone everywhere', function()
79 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
81 80 assert.is_not.equal( '?', mapping.lhs )
83 82 for _, mapping in ipairs( vim.api.nvim_get_keymap 'n' ) do

neovim/tests/bugabinga/pivi/editor/navigate_spec.lua

Modified
163 163 assert.is_nil( navigate.locate( bufnr, answer.first ) )
166 - it( 'binds going to a location in its own buffers only', function()
167 - local bound = {}
166 + it( 'publishes going to a location as an intent', function()
167 + local published
168 168 for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
169 - bound[mapping.lhs] = mapping.desc or ''
169 + if mapping.lhs == '<Plug>(pivi-goto-location)' then published = mapping end
172 - assert.is_string( bound.gf )
173 - assert.is_true( bound.gf:find( 'pivi', 1, true ) ~= nil )
172 + assert.is_table( published )
232 231 assert.is_nil( navigate.before( empty, 1 ) )
235 - it( 'binds motions in its own buffers without claiming global keys', function()
236 - local bound = {}
234 + it( 'publishes each motion as an intent', function()
235 + local published = {}
237 236 for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
238 - bound[mapping.lhs] = true
237 + published[mapping.lhs] = true
241 - for _, keys in ipairs { ']m', '[m', ']t', '[t', } do
242 - assert.is_true( bound[keys], keys .. ' is not bound in the transcript' )
243 - end
245 - -- Neovim ships its own bracket motions; pivi adds none of its own globally.
246 - for _, mapping in ipairs( vim.api.nvim_get_keymap 'n' ) do
247 - assert.is_nil( ( mapping.desc or '' ):find( 'pivi', 1, true ) )
240 + for _, intent in ipairs {
241 + '<Plug>(pivi-next-entry)', '<Plug>(pivi-previous-entry)',
242 + '<Plug>(pivi-next-tool)', '<Plug>(pivi-previous-tool)',
243 + } do
244 + assert.is_true( published[intent], intent .. ' is not published' )

neovim/tests/bugabinga/pivi/keys_spec.lua

Added
new file mode 100644
1 +---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch, redundant-parameter
2 +require 'bugabinga.pivi.fold'
3 +require 'bugabinga.pivi.navigate'
4 +require 'bugabinga.pivi.detail'
5 +require 'bugabinga.pivi.diff'
6 +require 'bugabinga.pivi.guide'
7 +local command = require 'bugabinga.pivi.command'
8 +local session = require 'bugabinga.pivi.session'
9 +local transcript = require 'bugabinga.pivi.transcript'
10 +local fake = dofile 'tests/helpers/pivi_fake.lua'
12 +local MODES = { 'n', 'i', 'x', }
14 +--- Keys pivi binds itself, rather than publishing for the configuration.
15 +--- Slash-command entry is the single exception, because the behaviour is the
16 +--- key: it only takes over at the first character of a prompt.
17 +local ALLOWED = { ['/'] = true, }
19 +local claimed = function( bufnr )
20 + local found = {}
21 + for _, mode in ipairs( MODES ) do
22 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, mode ) ) do
23 + if not mapping.lhs:find( '<Plug>', 1, true ) and not ALLOWED[mapping.lhs] then
24 + table.insert( found, mode .. ' ' .. mapping.lhs )
25 + end
26 + end
27 + end
28 + return found
29 +end
31 +describe( 'pivi keys', function()
32 + after_each( function()
33 + session.stop_all()
34 + transcript.forget_all()
35 + fake.close_extra_windows()
36 + vim.cmd 'silent! %bwipeout!'
37 + end )
39 + it( 'claims no key of its own in a transcript', function()
40 + local bufnr = transcript.ensure 'unclaimed'
42 + assert.same( {}, claimed( bufnr ) )
43 + end )
45 + it( 'claims no key of its own in a prompt, beyond slash-command entry', function()
46 + local channel = fake.new()
47 + local current = session.create {
48 + spawn = channel.spawn,
49 + name = 'spec',
50 + extension = false,
51 + skill = false,
52 + executable = 'pi',
53 + env = {},
54 + }
55 + local bufnr = command.open_prompt( current )
56 + vim.cmd.stopinsert()
58 + assert.same( {}, claimed( bufnr ) )
59 + end )
61 + it( 'publishes every behavior a key could want', function()
62 + local bufnr = transcript.ensure 'published'
63 + local published = {}
64 + for _, mode in ipairs( MODES ) do
65 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, mode ) ) do
66 + published[mapping.lhs] = true
67 + end
68 + end
70 + for _, intent in ipairs {
71 + '<Plug>(pivi-toggle-entry)',
72 + '<Plug>(pivi-next-entry)',
73 + '<Plug>(pivi-previous-entry)',
74 + '<Plug>(pivi-next-tool)',
75 + '<Plug>(pivi-previous-tool)',
76 + '<Plug>(pivi-goto-location)',
77 + '<Plug>(pivi-inspect-entry)',
78 + '<Plug>(pivi-compare-change)',
79 + '<Plug>(pivi-guide)',
80 + } do
81 + assert.is_true( published[intent], intent .. ' is not published' )
82 + end
83 + end )
85 + it( 'never claims a global key', function()
86 + for _, mode in ipairs( MODES ) do
87 + for _, mapping in ipairs( vim.api.nvim_get_keymap( mode ) ) do
88 + if not mapping.lhs:find( '<Plug>', 1, true ) then
89 + assert.is_nil( ( mapping.desc or '' ):find( 'pivi ', 1, true ),
90 + mapping.lhs .. ' was claimed globally by pivi' )
91 + end
92 + end
93 + end
94 + end )
96 + it( 'yields to a completion popup before accepting a prompt', function()
97 + local channel = fake.new()
98 + local current = session.create {
99 + spawn = channel.spawn,
100 + name = 'accepting',
101 + extension = false,
102 + skill = false,
103 + executable = 'pi',
104 + env = {},
106 + local bufnr = command.open_prompt( current )
107 + vim.cmd.stopinsert()
109 + local accept
110 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'i' ) ) do
111 + if mapping.lhs == '<Plug>(pivi-accept)' then accept = mapping end
112 + end
114 + assert.is_table( accept )
115 + assert.equal( 1, accept.expr )
117 + -- With no popup open the prompt is what gets accepted.
118 + vim.api.nvim_buf_set_lines( bufnr, 0, -1, false, { 'a question', } )
119 + assert.equal( '', accept.callback() )
120 + end )
121 +end )