repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(neovim): let pivi run commands, name files, and reach pi capabilities

0634d896386563498c8ee4250e34d872f8e70c71

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

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (8)

+743 / −9 text lines

neovim/lua/bugabinga/pivi/AGENTS.md

Modified

+9 / −1 text lines

104 104 29. **A picker filters the text it is shown.** Formatting an item as `name description` makes every description compete with the name; show the name alone.
106 -30. **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.
106 +30. **A written prompt is not always a prompt.** `bugabinga.pivi.compose` decides: a leading bang is a shell command through pi's `bash`, which accepts `excludeFromContext` even though `rpc.md` does not document it, and `@name` is expanded client-side because pi has no server-side `@` over this channel.
107 + Expansion reuses `edit.read`, so a named file is sent as its buffer content when it is open and unsaved; anything else would contradict every other pivi read.
108 + The wire shape is pi's own: `<file name="ABSOLUTE">
109 +content
110 +</file>`, images as `images[]`.
112 +31. **A local in Lua is not visible before its declaration.** Two functions have now been used above their definition and resolved to `nil` at runtime; if a helper is called from an earlier function, define it earlier.
114 +32. **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.
108 116 ## Commands

neovim/lua/bugabinga/pivi/command.lua

Modified

+261 / −0 text lines

8 8 local comparison = require 'bugabinga.pivi.diff'
9 9 local guide = require 'bugabinga.pivi.guide'
10 10 local layout = require 'bugabinga.pivi.layout'
11 +local compose = require 'bugabinga.pivi.compose'
12 13 local INPUT_FILETYPE = 'pivi-input'
139 140 --- Shows a session's prompt, reusing the one it already has.
140 141 --- A draft survives, because reopening a prompt is asking to continue writing,
141 142 --- not to start over.
143 +--- What `@` can refer to: what is open first, then what is nearby.
144 +--- A buffer is what the user is actually looking at, so it outranks a path
145 +--- that merely exists.
146 +local references = function( cwd )
147 + local found, seen = {}, {}
149 + for _, bufnr in ipairs( vim.api.nvim_list_bufs() ) do
150 + if vim.api.nvim_buf_is_loaded( bufnr ) and vim.bo[bufnr].buftype == '' then
151 + local name = vim.api.nvim_buf_get_name( bufnr )
152 + if name ~= '' then
153 + local shown = vim.fn.fnamemodify( name, ':.' )
154 + if not seen[shown] then
155 + seen[shown] = true
156 + table.insert( found, { path = shown, open = true, modified = vim.bo[bufnr].modified, } )
157 + end
158 + end
159 + end
160 + end
162 + local ok, listed = pcall( vim.fn.systemlist, { 'git', '-C', cwd, 'ls-files', } )
163 + if ok and vim.v.shell_error == 0 then
164 + for _, path in ipairs( listed ) do
165 + if not seen[path] then
166 + seen[path] = true
167 + table.insert( found, { path = path, open = false, } )
168 + end
169 + end
170 + end
172 + return found
173 +end
175 +local pick_reference = function( current, on_pick )
176 + local choices = references( current.cwd or vim.uv.cwd() )
177 + if #choices == 0 then
178 + notify( 'nothing to refer to', vim.log.levels.WARN )
179 + return
180 + end
182 + vim.ui.select( choices, {
183 + prompt = 'refer to',
184 + format_item = function( choice ) return choice.path end,
185 + }, function( choice )
186 + if choice then on_pick( choice.path ) end
187 + end )
188 +end
142 190 local open_prompt = function( current )
143 191 local existing = prompts[current.name]
144 192 if existing and vim.api.nvim_buf_is_valid( existing ) then
173 221 return ''
174 222 end, { buffer = bufnr, expr = true, desc = 'pivi slash command entry', } )
224 + -- `@` names a file, as it does in pi itself. It only takes over when it
225 + -- starts a word, so an address or a decorator can still be typed.
226 + vim.keymap.set( 'i', '@', function()
227 + local line = vim.api.nvim_get_current_line()
228 + local column = vim.api.nvim_win_get_cursor( 0 )[2]
229 + local before = column > 0 and line:sub( column, column ) or ''
230 + if before ~= '' and not before:match '%s' then return '@' end
232 + vim.schedule( function()
233 + pick_reference( current, function( path )
234 + vim.api.nvim_put( { '@' .. path .. ' ', }, 'c', true, true )
235 + vim.cmd.startinsert { bang = true, }
236 + end )
237 + end )
238 + return ''
239 + end, { buffer = bufnr, expr = true, desc = 'pivi refer to a file', } )
176 241 guide.bind( bufnr )
178 243 local send = function()
500 +--- Runs a shell command through pi, so its output joins the conversation.
501 +subcommands.bang = function( arguments )
502 + local current = require_session()
503 + if not current then return end
504 + if arguments == '' then
505 + notify( 'bang needs a command', vim.log.levels.WARN )
506 + return
507 + end
509 + local accepted, rejection = current.bash( arguments, false )
510 + if not accepted then
511 + notify( rejection, vim.log.levels.WARN )
512 + return
513 + end
514 + transcript.show( current.buffer )
515 +end
517 +--- The same, kept out of the conversation.
518 +subcommands['bang!'] = function( arguments )
519 + local current = require_session()
520 + if not current then return end
521 + if arguments == '' then
522 + notify( 'bang needs a command', vim.log.levels.WARN )
523 + return
524 + end
526 + local accepted, rejection = current.bash( arguments, true )
527 + if not accepted then
528 + notify( rejection, vim.log.levels.WARN )
529 + return
530 + end
531 + transcript.show( current.buffer )
532 +end
534 +--- Switches thinking level, the other capability `/thinking` cannot reach here.
535 +subcommands.thinking = function( arguments )
536 + local current = require_session()
537 + if not current then return end
539 + current.request( { type = 'get_available_thinking_levels', }, function( response )
540 + local levels = response.success and response.data and response.data.levels or {}
541 + if #levels == 0 then
542 + notify( 'this model offers no thinking levels', vim.log.levels.WARN )
543 + return
544 + end
546 + local choose = function( level )
547 + current.request( { type = 'set_thinking_level', level = level, }, function( answer )
548 + if answer.success then
549 + notify( 'thinking ' .. level )
550 + else
551 + notify( answer.error or 'pi refused the level', vim.log.levels.ERROR )
552 + end
553 + end )
554 + end
556 + if arguments ~= '' then
557 + if vim.list_contains( levels, arguments ) then
558 + choose( arguments )
559 + else
560 + notify( 'no such level: ' .. arguments, vim.log.levels.WARN )
561 + end
562 + return
563 + end
565 + vim.ui.select( levels, { prompt = 'thinking', }, function( level )
566 + if level then choose( level ) end
567 + end )
568 + end )
569 +end
571 +--- Compacts the conversation, optionally saying what to keep.
572 +subcommands.compact = function( arguments )
573 + local current = require_session()
574 + if not current then return end
576 + local request = { type = 'compact', }
577 + if arguments ~= '' then request.customInstructions = arguments end
579 + notify 'compacting'
580 + current.request( request, function( response )
581 + if not response.success then
582 + notify( response.error or 'compaction failed', vim.log.levels.ERROR )
583 + return
584 + end
585 + local data = response.data or {}
586 + notify( ( 'compacted %s to about %s tokens' ):format(
587 + tostring( data.tokensBefore or '?' ), tostring( data.estimatedTokensAfter or '?' )
588 + ) )
589 + end )
590 +end
592 +--- Turns automatic compaction on or off.
593 +subcommands.autocompact = function( arguments )
594 + local current = require_session()
595 + if not current then return end
597 + local wanted = arguments ~= 'off'
598 + current.request( { type = 'set_auto_compaction', enabled = wanted, }, function( response )
599 + if response.success then
600 + notify( 'automatic compaction ' .. ( wanted and 'on' or 'off' ) )
601 + else
602 + notify( response.error or 'pi refused', vim.log.levels.ERROR )
603 + end
604 + end )
605 +end
607 +--- What this session has cost so far.
608 +subcommands.session = function()
609 + local current = require_session()
610 + if not current then return end
612 + current.request( { type = 'get_session_stats', }, function( response )
613 + if not response.success or not response.data then
614 + notify( 'pi did not report session statistics', vim.log.levels.WARN )
615 + return
616 + end
618 + local data = response.data
619 + local tokens = data.tokens or {}
620 + local usage = data.contextUsage or {}
621 + local told = {
622 + 'session ' .. tostring( data.sessionId or current.name ),
623 + ( 'messages %s, tool calls %s' ):format(
624 + tostring( data.totalMessages or '?' ), tostring( data.toolCalls or '?' )
625 + ),
626 + ( 'tokens %s in, %s out, %s total' ):format(
627 + tostring( tokens.input or 0 ), tostring( tokens.output or 0 ), tostring( tokens.total or 0 )
628 + ),
629 + ( 'cost %s' ):format( tostring( data.cost or 0 ) ),
631 + if usage.percent then
632 + table.insert( told, ( 'context %s%% of %s' ):format(
633 + tostring( usage.percent ), tostring( usage.contextWindow )
634 + ) )
635 + end
636 + notify( table.concat( told, '\n' ) )
637 + end )
638 +end
640 +--- Names the session, so it can be recognised when resumed.
641 +subcommands.name = function( arguments )
642 + local current = require_session()
643 + if not current then return end
644 + if arguments == '' then
645 + notify( 'name needs a name', vim.log.levels.WARN )
646 + return
647 + end
649 + current.request( { type = 'set_session_name', name = arguments, }, function( response )
650 + if response.success then
651 + notify( 'named ' .. arguments )
652 + else
653 + notify( response.error or 'pi refused the name', vim.log.levels.ERROR )
654 + end
655 + end )
656 +end
658 +--- Starts a fresh conversation in the process that is already running.
659 +--- `start` spawns another pi; this one does not.
660 +subcommands.new = function()
661 + local current = require_session()
662 + if not current then return end
664 + current.request( { type = 'new_session', }, function( response )
665 + if not response.success then
666 + notify( response.error or 'pi refused a new session', vim.log.levels.ERROR )
667 + return
668 + end
669 + if response.data and response.data.cancelled then
670 + notify( 'an extension cancelled the new session', vim.log.levels.WARN )
671 + return
672 + end
673 + transcript.clear( current.buffer )
674 + notify 'new conversation'
675 + end )
676 +end
678 +--- Writes the conversation out as it is.
679 +subcommands.export = function( arguments )
680 + local current = require_session()
681 + if not current then return end
683 + local request = { type = 'export_html', }
684 + if arguments ~= '' then request.outputPath = vim.fn.expand( arguments ) end
686 + current.request( request, function( response )
687 + if response.success and response.data then
688 + notify( 'exported to ' .. tostring( response.data.path ) )
689 + else
690 + notify( response.error or 'export failed', vim.log.levels.ERROR )
691 + end
692 + end )
693 +end
435 695 subcommands.guide = function()
436 696 guide.show( vim.api.nvim_get_current_buf() )
483 743 if subcommand == 'command' then return matching( command_names( session.active() ) ) end
484 744 if subcommand == 'stop' or subcommand == 'sessions' then return matching( session.list() ) end
485 745 if subcommand == 'follow' then return matching { 'window', 'tab', 'current', } end
746 + if subcommand == 'autocompact' then return matching { 'on', 'off', } end
487 748 if line:match '^%s*Pi%s+%S*$' == nil then return {} end
488 749 return matching( names() )

neovim/lua/bugabinga/pivi/compose.lua

Added
new file mode 100644

+124 / −0 text lines

1 +local edit = require 'bugabinga.pivi.edit'
3 +local IMAGES = {
4 + png = 'image/png',
5 + jpg = 'image/jpeg',
6 + jpeg = 'image/jpeg',
7 + gif = 'image/gif',
8 + webp = 'image/webp',
9 +}
11 +--- What a written prompt turns out to mean.
12 +--- Pi reads a leading bang as a shell command rather than something said, and
13 +--- doubles it to keep the output out of the conversation.
14 +--- @param text string
15 +--- @return table action kind, and what that kind needs
16 +local parse = function( text )
17 + vim.validate( 'text', text, 'string' )
18 + local written = vim.trim( text )
20 + local quiet = written:match '^!!%s*(.*)$'
21 + if quiet then
22 + return { kind = 'bash', command = vim.trim( quiet ), exclude = true, }
23 + end
25 + local loud = written:match '^!%s*(.*)$'
26 + if loud then
27 + return { kind = 'bash', command = vim.trim( loud ), exclude = false, }
28 + end
30 + return { kind = 'prompt', message = written, }
31 +end
33 +local image_type = function( path )
34 + local suffix = path:match '%.([%a%d]+)$'
35 + if not suffix then return nil end
36 + return IMAGES[suffix:lower()]
37 +end
39 +local absolute = function( path, cwd )
40 + local expanded = vim.fn.expand( path )
41 + if expanded:match '^/' or expanded:match '^%a:' then return vim.fs.normalize( expanded ) end
42 + return vim.fs.normalize( ( cwd or vim.uv.cwd() ) .. '/' .. expanded )
43 +end
45 +--- Reads a file for the model.
46 +--- A loaded buffer wins over what is on disk, because that is what the user is
47 +--- looking at and what every other pivi read already answers with.
48 +local contents = function( path )
49 + local text = edit.read( path )
50 + return text
51 +end
53 +local encode_image = function( path )
54 + local read = io.open( path, 'rb' )
55 + if not read then return nil end
56 + local bytes = read:read '*a'
57 + read:close()
58 + if not bytes or bytes == '' then return nil end
60 + local ok, encoded = pcall( vim.base64.encode, bytes )
61 + if not ok then return nil end
62 + return encoded
63 +end
65 +--- Replaces every file reference with the file itself.
66 +--- The shape matches what Pi builds for the same reference elsewhere, so a
67 +--- model sees one convention rather than two.
68 +--- @param text string
69 +--- @param options table? cwd
70 +--- @return string message
71 +--- @return table images
72 +--- @return string[] missing references that could not be read
73 +local expand = function( text, options )
74 + vim.validate( 'text', text, 'string' )
75 + options = options or {}
77 + local images = {}
78 + local missing = {}
79 + local attached = {}
81 + local message = text:gsub( '@([%w%._%-~/\\:]+)', function( reference )
82 + local path = absolute( reference, options.cwd )
84 + local mime = image_type( path )
85 + if mime then
86 + local data = encode_image( path )
87 + if not data then
88 + table.insert( missing, reference )
89 + return '@' .. reference
90 + end
91 + table.insert( images, { type = 'image', data = data, mimeType = mime, } )
92 + return ( '<file name="%s"></file>' ):format( path )
93 + end
95 + local body = contents( path )
96 + if not body then
97 + table.insert( missing, reference )
98 + return '@' .. reference
99 + end
101 + if attached[path] then return ( '<file name="%s"></file>' ):format( path ) end
102 + attached[path] = true
103 + return ( '<file name="%s">\n%s\n</file>' ):format( path, body )
104 + end )
106 + return message, images, missing
107 +end
109 +--- Every reference a written prompt names.
110 +local references = function( text )
111 + local found = {}
112 + for reference in ( text or '' ):gmatch '@([%w%._%-~/\\:]+)' do
113 + table.insert( found, reference )
114 + end
115 + return found
116 +end
118 +return {
119 + image_types = IMAGES,
120 + parse = parse,
121 + expand = expand,
122 + references = references,
123 + absolute = absolute,

neovim/lua/bugabinga/pivi/session.lua

Modified

+53 / −2 text lines

10 10 local follow = require 'bugabinga.pivi.follow'
11 11 local edit = require 'bugabinga.pivi.edit'
12 12 local runtime = require 'bugabinga.pivi.runtime'
13 +local compose = require 'bugabinga.pivi.compose'
13 14 local entry = require 'bugabinga.pivi.entry'
14 15 local render = require 'bugabinga.pivi.render'
15 16 local fold = require 'bugabinga.pivi.fold'
290 291 return true
294 +--- Runs a shell command through Pi.
295 +--- Pi keeps the output in the conversation unless asked not to, which is what
296 +--- doubling the bang means.
297 +local run_bash = function( self, command, exclude, callback )
298 + vim.validate( 'command', command, 'string' )
299 + if command == '' then return false, 'nothing to run' end
301 + local id = begin( self, 'tool', {
302 + tool = exclude and 'bang!' or 'bang',
303 + args = { command = command, },
304 + cwd = self.cwd,
305 + } )
306 + entry.line( self.buffer, id, tool.headline( entry.meta( self.buffer, id ) ) )
308 + self.client.request(
309 + { type = 'bash', command = command, excludeFromContext = exclude == true, },
310 + function( response )
311 + local data = response.data or {}
312 + entry.annotate( self.buffer, id, {
313 + result = { content = { { type = 'text', text = data.output or '', }, }, },
314 + failed = response.success ~= true or ( data.exitCode ~= nil and data.exitCode ~= 0 ),
315 + exit = data.exitCode,
316 + } )
317 + entry.rewrite( self.buffer, id, tool.body( entry.meta( self.buffer, id ) ) )
318 + seal( self )
319 + if callback then callback( response ) end
320 + end
323 + return true, nil
324 +end
293 326 --- Submits text to the session.
294 327 --- A submission during an active run must choose its delivery explicitly;
295 328 --- rejection is returned, never silently swallowed.
314 347 return false, 'unknown delivery "' .. tostring( delivery ) .. '"; use steer or follow_up'
350 + -- A written prompt may not be a prompt at all.
351 + if options.compose ~= false then
352 + local intent = compose.parse( text )
353 + if intent.kind == 'bash' then
354 + return run_bash( self, intent.command, intent.exclude )
355 + end
356 + end
317 358 local snapshot = options.snapshot
318 359 if snapshot == nil and options.capture ~= false then
319 360 snapshot = context.snapshot( options.source )
322 - local message = text
363 + local expanded, images, missing = text, nil, {}
364 + if options.compose ~= false then
365 + expanded, images, missing = compose.expand( text, { cwd = self.cwd, } )
366 + for _, reference in ipairs( missing ) do
367 + vim.notify( 'pivi: cannot read @' .. reference, vim.log.levels.WARN )
368 + end
369 + end
371 + local message = expanded
323 372 if snapshot then
324 - message = context.render( snapshot ) .. text
373 + message = context.render( snapshot ) .. expanded
327 376 render_submission( self, text )
335 384 command = { type = 'prompt', message = message, }
386 + if images and #images > 0 then command.images = images end
338 388 table.insert( self.submissions, { text = text, delivery = delivery, snapshot = snapshot, } )
410 460 self.submissions = self.submissions or {}
411 461 self.submit = function( text, options ) return submit( self, text, options ) end
412 462 self.request = function( command, callback ) return request( self, command, callback ) end
463 + self.bash = function( command, exclude, callback ) return run_bash( self, command, exclude, callback ) end
413 464 self.abort = function( callback ) return abort( self, callback ) end
414 465 self.clear_queue = function( callback ) return clear_queue( self, callback ) end
415 466 self.refresh = function( callback ) return refresh( self, callback ) end

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

Modified

+13 / −1 text lines

61 61 In the prompt, `<c-y>` accepts: the completion when one is offered, the prompt otherwise. `<CR>` in normal mode sends it too, so both work. The prompt opens in insert mode, so keys behind a leader need `<Esc>` first; `vim.g.pivi_start_insert = false` opens it in normal mode instead.
63 -`/model` and other terminal-only commands are not offered over this channel and are refused rather than said to the model; `:Pi model` switches model natively.
63 +`/model` and other terminal-only commands are not offered over this channel and are refused rather than said to the model; `:Pi model` and `:Pi thinking` reach the same capability natively.
65 +In a prompt, a leading `!` runs the rest as a shell command through pi and keeps the output in the conversation; `!!` runs it and keeps the output out. `@` names a file: it opens a picker when it starts a word, and stays an ordinary character inside one. A named file is sent as its buffer content when it is open and unsaved, because that is what every other pivi read answers with; an image is attached rather than inlined.
65 67 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.
80 82 | `:Pi abort` / `queue` / `clear` | run and queue control |
81 83 | `:Pi diff` | compare the change of the entry under the cursor |
82 84 | `:Pi guide` | show the guide of keys and commands |
85 +| `:Pi bang {cmd}` | run a shell command through pi, output joins the conversation |
86 +| `:Pi bang! {cmd}` | the same, kept out of the conversation |
87 +| `:Pi model [name]` | switch model |
88 +| `:Pi thinking [level]` | switch thinking level |
89 +| `:Pi compact [instructions]` | compact the conversation |
90 +| `:Pi autocompact on\|off` | automatic compaction |
91 +| `:Pi session` | tokens, cost and context so far |
92 +| `:Pi name {name}` | name the session |
93 +| `:Pi new` | fresh conversation in the running process |
94 +| `:Pi export [path]` | write the conversation out |
83 95 | `:Pi follow [window\|tab\|current]` | toggle follow mode |
85 97 `<Plug>(pivi-show)`, `<Plug>(pivi-prompt)`, `<Plug>(pivi-abort)`, and `<Plug>(pivi-follow)` exist; pivi sets no global mappings.

neovim/tests/bugabinga/pivi/compose_spec.lua

Added
new file mode 100644

+129 / −0 text lines

1 +---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch, redundant-parameter
2 +local compose = require 'bugabinga.pivi.compose'
4 +local temp_file = function( lines, suffix )
5 + local path = vim.fs.normalize( vim.fn.tempname() .. ( suffix or '.lua' ) )
6 + vim.fn.writefile( lines, path )
7 + return path
8 +end
10 +describe( 'pivi compose', function()
11 + after_each( function()
12 + vim.cmd 'silent! %bwipeout!'
13 + end )
15 + describe( 'what was written', function()
16 + it( 'reads a leading bang as a command to run', function()
17 + local intent = compose.parse '!just test'
19 + assert.equal( 'bash', intent.kind )
20 + assert.equal( 'just test', intent.command )
21 + assert.is_false( intent.exclude )
22 + end )
24 + it( 'reads a doubled bang as a command kept out of the conversation', function()
25 + local intent = compose.parse '!!git status'
27 + assert.equal( 'bash', intent.kind )
28 + assert.equal( 'git status', intent.command )
29 + assert.is_true( intent.exclude )
30 + end )
32 + it( 'tolerates space after the bang', function()
33 + assert.equal( 'ls -la', compose.parse( '! ls -la' ).command )
34 + assert.equal( 'ls', compose.parse( ' !! ls ' ).command )
35 + end )
37 + it( 'reads anything else as something said', function()
38 + local intent = compose.parse 'rename greet to hail'
40 + assert.equal( 'prompt', intent.kind )
41 + assert.equal( 'rename greet to hail', intent.message )
42 + end )
44 + it( 'does not mistake a bang inside a sentence for a command', function()
45 + assert.equal( 'prompt', compose.parse( 'fix this!' ).kind )
46 + assert.equal( 'prompt', compose.parse( 'why not use !! here' ).kind )
47 + end )
48 + end )
50 + describe( 'what was referred to', function()
51 + it( 'names every reference', function()
52 + assert.same( { 'a.lua', 'src/b.lua', }, compose.references 'compare @a.lua with @src/b.lua' )
53 + assert.same( {}, compose.references 'nothing here' )
54 + end )
56 + it( 'replaces a reference with the file itself', function()
57 + local path = temp_file { 'local value = 1', }
59 + local message, images, missing = compose.expand( 'look at @' .. path )
61 + assert.same( {}, missing )
62 + assert.same( {}, images )
63 + assert.is_true( message:find( '<file name="' .. path .. '">', 1, true ) ~= nil )
64 + assert.is_true( message:find( 'local value = 1', 1, true ) ~= nil )
65 + assert.is_true( message:find( '</file>', 1, true ) ~= nil )
66 + end )
68 + it( 'prefers what the buffer holds over what the file holds', function()
69 + local path = temp_file { 'on disk', }
70 + vim.cmd( 'edit! ' .. vim.fn.fnameescape( path ) )
71 + vim.api.nvim_buf_set_lines( 0, 0, -1, false, { 'not saved yet', } )
73 + local message = compose.expand( 'read @' .. path )
75 + assert.is_true( message:find( 'not saved yet', 1, true ) ~= nil )
76 + assert.is_nil( message:find( 'on disk', 1, true ) )
77 + end )
79 + it( 'resolves a relative reference against the session directory', function()
80 + local path = temp_file { 'relative', }
81 + local directory = vim.fs.dirname( path )
83 + local message = compose.expand( '@' .. vim.fs.basename( path ), { cwd = directory, } )
85 + assert.is_true( message:find( 'relative', 1, true ) ~= nil )
86 + end )
88 + it( 'attaches an image rather than inlining it', function()
89 + local path = vim.fs.normalize( vim.fn.tempname() .. '.png' )
90 + local write = io.open( path, 'wb' )
91 + write:write '\137PNG\r\n\26\n fake but binary'
92 + write:close()
94 + local message, images = compose.expand( 'see @' .. path )
96 + assert.equal( 1, #images )
97 + assert.equal( 'image/png', images[1].mimeType )
98 + assert.equal( 'image', images[1].type )
99 + assert.is_true( #images[1].data > 0 )
100 + assert.is_true( message:find( '<file name="' .. path .. '"></file>', 1, true ) ~= nil )
101 + end )
103 + it( 'reports a reference it cannot read and leaves it written', function()
104 + local absent = vim.fn.tempname() .. '-absent.lua'
106 + local message, _, missing = compose.expand( 'read @' .. absent )
108 + assert.equal( 1, #missing )
109 + assert.is_true( message:find( '@' .. absent, 1, true ) ~= nil )
110 + end )
112 + it( 'includes a file once however often it is named', function()
113 + local path = temp_file { 'once', }
115 + local message = compose.expand( ( '@%s and @%s' ):format( path, path ) )
117 + local count = select( 2, message:gsub( 'once', '' ) )
118 + assert.equal( 1, count )
119 + end )
121 + it( 'leaves text without references alone', function()
122 + local message, images, missing = compose.expand 'just words'
124 + assert.equal( 'just words', message )
125 + assert.same( {}, images )
126 + assert.same( {}, missing )
127 + end )
128 + end )
129 +end )

neovim/tests/bugabinga/pivi/keys_spec.lua

Modified

+51 / −5 text lines

12 12 local MODES = { 'n', 'i', 'x', }
14 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, }
15 +--- These are the exceptions where the behaviour *is* the key, mirroring pi:
16 +--- `/` only takes over at the first character, and `@` only when it starts a
17 +--- word, so both remain typable everywhere else.
18 +local ALLOWED = { ['/'] = true, ['@'] = true, }
19 20 local claimed = function( bufnr )
20 21 local found = {}
42 43 assert.same( {}, claimed( bufnr ) )
43 44 end )
45 - it( 'claims no key of its own in a prompt, beyond slash-command entry', function()
46 + it( 'claims no key of its own in a prompt, beyond naming a command or a file', function()
46 47 local channel = fake.new()
47 48 local current = session.create {
48 49 spawn = channel.spawn,
58 59 assert.same( {}, claimed( bufnr ) )
59 60 end )
62 + it( 'still types a literal @ inside a word', function()
63 + -- Naming a file opens a picker, which this test is not exercising.
64 + local original = vim.ui.select
65 + vim.ui.select = function( _, _, on_choice ) on_choice( nil ) end
67 + local channel = fake.new()
68 + local current = session.create {
69 + spawn = channel.spawn,
70 + name = 'typing',
71 + extension = false,
72 + skill = false,
73 + executable = 'pi',
74 + env = {},
75 + }
76 + local bufnr = command.open_prompt( current )
77 + vim.cmd.stopinsert()
79 + local at
80 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'i' ) ) do
81 + if mapping.lhs == '@' then at = mapping end
82 + end
83 + assert.is_table( at )
85 + local win = vim.fn.bufwinid( bufnr )
86 + assert.is_true( win > 0 )
87 + vim.api.nvim_set_current_win( win )
89 + -- Normal mode cannot place the cursor past the last character, so both
90 + -- positions below sit inside the line.
91 + -- Inside a word it is just a character.
92 + vim.api.nvim_buf_set_lines( bufnr, 0, -1, false, { 'mail me', } )
93 + vim.api.nvim_win_set_cursor( win, { 1, 4, } )
94 + assert.equal( '@', at.callback() )
96 + -- Starting a word it names a file.
97 + vim.api.nvim_buf_set_lines( bufnr, 0, -1, false, { 'look at x', } )
98 + vim.api.nvim_win_set_cursor( win, { 1, 8, } )
99 + assert.equal( '', at.callback() )
101 + vim.wait( 50, function() return false end )
102 + vim.ui.select = original
103 + end )
61 105 it( 'publishes every behavior a key could want', function()
62 106 local bufnr = transcript.ensure 'published'
63 107 local published = {}
85 129 it( 'never claims a global key', function()
130 + -- pivi describes its own mappings as `pivi ...`; the configuration
131 + -- describes its choices as `pi: ...`, and those are allowed to be global.
86 132 for _, mode in ipairs( MODES ) do
87 133 for _, mapping in ipairs( vim.api.nvim_get_keymap( mode ) ) do
88 134 if not mapping.lhs:find( '<Plug>', 1, true ) then
89 - assert.is_nil( ( mapping.desc or '' ):find( 'pivi ', 1, true ),
135 + assert.is_nil( ( mapping.desc or '' ):match '^pivi ',
90 136 mapping.lhs .. ' was claimed globally by pivi' )

neovim/tests/bugabinga/pivi/session_spec.lua

Modified

+103 / −0 text lines

591 591 assert.equal( 'write new.lua', transcript.lines( current.buffer )[item.first] )
594 + it( 'runs a banged prompt as a command instead of saying it', function()
595 + local channel = fake.new()
596 + local current = create( channel )
598 + assert.is_true( current.submit( '!just test', { capture = false, } ) )
600 + local ran
601 + for _, sent in ipairs( channel.commands ) do
602 + if sent.type == 'bash' then ran = sent end
603 + assert.is_not.equal( 'prompt', sent.type )
604 + end
606 + assert.is_table( ran )
607 + assert.equal( 'just test', ran.command )
608 + assert.is_false( ran.excludeFromContext )
609 + end )
611 + it( 'keeps a doubly banged command out of the conversation', function()
612 + local channel = fake.new()
613 + local current = create( channel )
615 + current.submit( '!!git status', { capture = false, } )
617 + local ran
618 + for _, sent in ipairs( channel.commands ) do
619 + if sent.type == 'bash' then ran = sent end
620 + end
622 + assert.is_table( ran )
623 + assert.equal( 'git status', ran.command )
624 + assert.is_true( ran.excludeFromContext )
625 + end )
627 + it( 'shows what a command produced inside its own entry', function()
628 + local channel = fake.new()
629 + local current = create( channel )
631 + current.submit( '!echo hi', { capture = false, } )
633 + local ran
634 + for _, sent in ipairs( channel.commands ) do
635 + if sent.type == 'bash' then ran = sent end
636 + end
637 + channel.frame {
638 + id = ran.id,
639 + type = 'response',
640 + command = 'bash',
641 + success = true,
642 + data = { output = 'hi', exitCode = 0, },
644 + channel.flush()
646 + local item = entry.list( current.buffer )[1]
647 + local lines = transcript.lines( current.buffer )
649 + assert.equal( 'tool', item.kind )
650 + assert.equal( 'bang', item.meta.tool )
651 + assert.is_true( vim.list_contains( lines, 'hi' ) )
652 + assert.is_false( item.meta.failed )
653 + end )
655 + it( 'marks a command that exited badly', function()
656 + local channel = fake.new()
657 + local current = create( channel )
659 + current.submit( '!false', { capture = false, } )
661 + local ran
662 + for _, sent in ipairs( channel.commands ) do
663 + if sent.type == 'bash' then ran = sent end
664 + end
665 + channel.frame {
666 + id = ran.id,
667 + type = 'response',
668 + command = 'bash',
669 + success = true,
670 + data = { output = '', exitCode = 1, },
672 + channel.flush()
674 + assert.is_true( entry.list( current.buffer )[1].meta.failed )
675 + end )
677 + it( 'sends a referenced file with what was said', function()
678 + local path = vim.fs.normalize( vim.fn.tempname() .. '.lua' )
679 + vim.fn.writefile( { 'local referenced = true', }, path )
681 + local channel = fake.new()
682 + local current = create( channel )
684 + current.submit( 'explain @' .. path, { capture = false, } )
686 + local sent = commands_of( channel, 'prompt' )[1]
687 + assert.is_table( sent )
688 + assert.is_true( sent.message:find( 'local referenced = true', 1, true ) ~= nil )
689 + assert.is_true( sent.message:find( '<file name=', 1, true ) ~= nil )
691 + -- The transcript still shows what the user actually wrote.
692 + local prompt = entry.list( current.buffer )[1]
693 + local lines = transcript.lines( current.buffer )
694 + assert.equal( 'explain @' .. path, lines[prompt.first] )
695 + end )
594 697 it( 'records a rejected submission as its own entry', function()
595 698 local channel = fake.new()
596 699 local current = create( channel )