repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(neovim): size pivi windows for the editor they are opened in

e5dbbcf13af7b5b6a37f9e2195b58bdfe0bda109

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

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (7)

neovim/lua/bugabinga/pivi/AGENTS.md

Modified
97 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.
99 +27. **A session may be a stand-in.** The drive harness passes a stub session, so code reached from a command must not assume `session.buffer` is a live buffer.
100 + This is exactly the class of regression the drive harness exists to catch; it caught it.
102 +28. **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.
101 104 ## Commands
140 143 | Output language | parsed as a standalone string and applied as highlight extmarks, never as a fence, so transcript text stays what the tool produced |
141 144 | 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 |
142 145 | Guide | `?` reads the mappings that exist in the buffer rather than a list written beside them, so it cannot drift |
146 +| Layout | `bugabinga.pivi.layout` decides beside or below from the editor size, follows the user's `splitright` and `splitbelow`, and never carves a new window out of a pivi window. Headless neovim is a fixed 80x24 grid, so the decision is unit-tested as a pure function and the windows are verified in a real terminal |
144 148 ## Gotchas

neovim/lua/bugabinga/pivi/command.lua

Modified
7 7 local runtime = require 'bugabinga.pivi.runtime'
8 8 local comparison = require 'bugabinga.pivi.diff'
9 9 local guide = require 'bugabinga.pivi.guide'
10 +local layout = require 'bugabinga.pivi.layout'
11 12 local INPUT_FILETYPE = 'pivi-input'
93 94 --- Opens a modifiable prompt buffer bound to a session.
94 95 local prompts = {}
97 +--- The window showing a session's transcript, when there is one.
98 +--- A session is not always a live one: a caller may hold a stand-in, so the
99 +--- buffer is only trusted once it looks like a buffer.
100 +local transcript_window = function( current )
101 + if type( current.buffer ) ~= 'number' then return nil end
102 + if not vim.api.nvim_buf_is_valid( current.buffer ) then return nil end
103 + local win = vim.fn.bufwinid( current.buffer )
104 + if win == -1 then return nil end
105 + return win
106 +end
96 108 --- Shows a session's prompt, reusing the one it already has.
97 109 --- A draft survives, because reopening a prompt is asking to continue writing,
98 110 --- not to start over.
100 112 local existing = prompts[current.name]
101 113 if existing and vim.api.nvim_buf_is_valid( existing ) then
102 114 local win = vim.fn.bufwinid( existing )
103 - if win == -1 and pcall( vim.cmd.split ) then
104 - win = vim.api.nvim_get_current_win()
105 - vim.api.nvim_win_set_buf( win, existing )
106 - pcall( vim.api.nvim_win_set_height, win, 6 )
115 + if win == -1 then
116 + win = layout.open_prompt( existing, transcript_window( current ) ) or -1
108 118 if win ~= -1 then
109 119 vim.api.nvim_set_current_win( win )
159 169 return ''
160 170 end, { buffer = bufnr, expr = true, desc = 'pivi accept the completion, else the prompt', } )
162 - if pcall( vim.cmd.split ) then
163 - local win = vim.api.nvim_get_current_win()
164 - vim.api.nvim_win_set_buf( win, bufnr )
165 - pcall( vim.api.nvim_win_set_height, win, 6 )
172 + local win = layout.open_prompt( bufnr, transcript_window( current ) )
173 + if win then
166 174 transcript.decorate( win, current.name )
167 175 vim.wo[win].winbar = '%#Title#π ' .. current.name ..
168 176 '%* %#Comment#' .. ( vim.g.pivi_submit_hint or 'send' ) .. ' / commands%*'

neovim/lua/bugabinga/pivi/init.lua

Modified
20 20 local tool = require 'bugabinga.pivi.tool'
21 21 local comparison = require 'bugabinga.pivi.diff'
22 22 local guide = require 'bugabinga.pivi.guide'
23 +local layout = require 'bugabinga.pivi.layout'
24 25 --- Reads cached status only. Safe to call from a statusline.
25 26 local summary = function( name ) return publish.summary( name ) end
55 56 tool = tool,
56 57 comparison = comparison,
57 58 guide = guide,
59 + layout = layout,
58 60 summary = summary,
59 61 reset = reset,
62 64 command.setup()
66 +vim.api.nvim_create_autocmd( 'VimResized', {
67 + group = vim.api.nvim_create_augroup( 'pivi_layout', { clear = true, } ),
68 + desc = 'Resize pivi windows to suit the new editor size.',
69 + callback = function() pcall( layout.apply ) end,
70 +} )
64 72 vim.api.nvim_create_autocmd( 'VimLeavePre', {
65 73 group = vim.api.nvim_create_augroup( 'pivi_shutdown', { clear = true, } ),
66 74 desc = 'End every Pi process when Neovim exits.',

neovim/lua/bugabinga/pivi/layout.lua

Added
new file mode 100644
1 +--- How pivi sizes its own windows.
2 +--- Every number is a readability judgement rather than a taste: prose needs a
3 +--- certain width before it stops being a column of fragments, and code needs
4 +--- one before it starts wrapping.
5 +local DEFAULTS = {
6 + transcript_share = 0.4,
7 + transcript_min = 60,
8 + transcript_max = 100,
9 + source_min = 50,
10 + stacked_share = 0.45,
11 + stacked_min = 12,
12 + prompt_share = 0.15,
13 + prompt_min = 3,
14 + prompt_max = 10,
17 +local settings = function()
18 + local chosen = vim.g.pivi_layout
19 + if type( chosen ) ~= 'table' then return DEFAULTS end
20 + return vim.tbl_extend( 'force', DEFAULTS, chosen )
21 +end
23 +local clamp = function( value, low, high )
24 + return math.max( low, math.min( high, value ) )
25 +end
27 +--- Decides where the transcript goes and how large it is.
28 +--- Beside the source while both still fit, stacked under it when they do not.
29 +--- @param columns number editor width
30 +--- @param lines number editor height
31 +--- @return table plan orientation, size, and prompt height
32 +local plan = function( columns, lines, chosen )
33 + chosen = chosen or settings()
35 + local wanted = clamp(
36 + math.floor( columns * chosen.transcript_share + 0.5 ),
37 + chosen.transcript_min,
38 + chosen.transcript_max
39 + )
41 + local prompt = clamp(
42 + math.floor( lines * chosen.prompt_share + 0.5 ),
43 + chosen.prompt_min,
44 + math.min( chosen.prompt_max, math.max( chosen.prompt_min, lines - 4 ) )
45 + )
47 + -- One column is spent on the separator between two side-by-side windows.
48 + if columns - wanted - 1 >= chosen.source_min then
49 + return { orientation = 'beside', width = wanted, prompt = prompt, }
50 + end
52 + local height = clamp(
53 + math.floor( lines * chosen.stacked_share + 0.5 ),
54 + math.min( chosen.stacked_min, math.max( 1, lines - 4 ) ),
55 + math.max( 1, lines - 4 )
56 + )
58 + return { orientation = 'below', height = height, prompt = prompt, }
59 +end
61 +local current = function()
62 + return plan( vim.o.columns, vim.o.lines )
63 +end
65 +local size = function( win, chosen )
66 + if not vim.api.nvim_win_is_valid( win ) then return end
67 + if chosen.orientation == 'beside' then
68 + pcall( vim.api.nvim_win_set_width, win, chosen.width )
69 + vim.wo[win].winfixwidth = true
70 + else
71 + pcall( vim.api.nvim_win_set_height, win, chosen.height )
72 + vim.wo[win].winfixheight = true
73 + end
74 +end
76 +local owned = function( win )
77 + if not vim.api.nvim_win_is_valid( win ) then return false end
78 + local filetype = vim.bo[vim.api.nvim_win_get_buf( win )].filetype
79 + return filetype == 'pivi' or filetype == 'pivi-input'
80 +end
82 +--- The window a new pivi window should be carved out of.
83 +--- Splitting a pivi window would stack pivi on pivi, so the widest window the
84 +--- user actually works in is preferred.
85 +local host = function()
86 + local win = vim.api.nvim_get_current_win()
87 + if not owned( win ) then return win end
89 + local widest, area = nil, -1
90 + for _, candidate in ipairs( vim.api.nvim_tabpage_list_wins( 0 ) ) do
91 + if not owned( candidate ) and vim.api.nvim_win_get_config( candidate ).relative == '' then
92 + local measure = vim.api.nvim_win_get_width( candidate ) * vim.api.nvim_win_get_height( candidate )
93 + if measure > area then widest, area = candidate, measure end
94 + end
95 + end
96 + return widest or win
97 +end
99 +--- Opens a window for the transcript, leaving the working window focused.
100 +--- The split direction follows the user's own `splitright` and `splitbelow`.
101 +--- @return number? win
102 +local open = function( bufnr )
103 + if not vim.api.nvim_buf_is_valid( bufnr ) then return nil end
105 + local working = vim.api.nvim_get_current_win()
106 + local chosen = current()
107 + local into = host()
109 + local win
110 + local opened = pcall( vim.api.nvim_win_call, into, function()
111 + vim.cmd( chosen.orientation == 'beside' and 'vsplit' or 'split' )
112 + win = vim.api.nvim_get_current_win()
113 + end )
114 + if not opened or not win or not vim.api.nvim_win_is_valid( win ) then return nil end
116 + vim.api.nvim_win_set_buf( win, bufnr )
117 + size( win, chosen )
119 + if vim.api.nvim_win_is_valid( working ) then
120 + vim.api.nvim_set_current_win( working )
121 + end
122 + return win
123 +end
125 +--- Opens the prompt under the transcript when there is one, so the two stay
126 +--- in the same column instead of carving up the source window.
127 +--- @return number? win
128 +local open_prompt = function( bufnr, beside )
129 + if not vim.api.nvim_buf_is_valid( bufnr ) then return nil end
131 + local chosen = current()
133 + local into = beside and vim.api.nvim_win_is_valid( beside ) and beside or host()
135 + local win
136 + local opened = pcall( vim.api.nvim_win_call, into, function()
137 + vim.cmd 'split'
138 + win = vim.api.nvim_get_current_win()
139 + end )
141 + if not opened or not win or not vim.api.nvim_win_is_valid( win ) then return nil end
143 + vim.api.nvim_win_set_buf( win, bufnr )
144 + pcall( vim.api.nvim_win_set_height, win, chosen.prompt )
145 + vim.wo[win].winfixheight = true
146 + vim.api.nvim_set_current_win( win )
147 + return win
148 +end
150 +local windows = function()
151 + local found = { transcript = {}, prompt = {}, }
152 + for _, win in ipairs( vim.api.nvim_list_wins() ) do
153 + local bufnr = vim.api.nvim_win_get_buf( win )
154 + local filetype = vim.bo[bufnr].filetype
155 + if filetype == 'pivi' then
156 + table.insert( found.transcript, win )
157 + elseif filetype == 'pivi-input' then
158 + table.insert( found.prompt, win )
159 + end
160 + end
161 + return found
162 +end
164 +--- Brings existing pivi windows back to the size the editor now deserves.
165 +local apply = function()
166 + local chosen = current()
167 + local found = windows()
169 + for _, win in ipairs( found.transcript ) do
170 + size( win, chosen )
171 + end
172 + for _, win in ipairs( found.prompt ) do
173 + pcall( vim.api.nvim_win_set_height, win, chosen.prompt )
174 + end
175 +end
177 +return {
178 + defaults = DEFAULTS,
179 + settings = settings,
180 + plan = plan,
181 + current = current,
182 + host = host,
183 + open = open,
184 + open_prompt = open_prompt,
185 + windows = windows,
186 + apply = apply,

neovim/lua/bugabinga/pivi/transcript.lua

Modified
166 166 for _, win in ipairs( vim.api.nvim_list_wins() ) do
167 167 if vim.api.nvim_win_get_buf( win ) == bufnr then return win end
169 - local current = vim.api.nvim_get_current_win()
170 - local split = pcall( vim.cmd.vsplit )
171 - if not split then return nil end
172 - local win = vim.api.nvim_get_current_win()
173 - vim.api.nvim_win_set_buf( win, bufnr )
169 + local win = require( 'bugabinga.pivi.layout' ).open( bufnr )
170 + if not win then return nil end
174 171 decorate( win, name_of( bufnr ) )
175 - vim.api.nvim_set_current_win( current )
176 172 return win

neovim/tests/bugabinga/pivi/layout_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 +local layout = require 'bugabinga.pivi.layout'
4 +-- The editor size a headless test runs at is fixed, so the decision is tested
5 +-- as a decision and the windows it produces are verified in a real terminal.
6 +local at = function( columns, lines ) return layout.plan( columns, lines ) end
8 +describe( 'pivi layout', function()
9 + local original
11 + before_each( function()
12 + original = vim.g.pivi_layout
13 + end )
15 + after_each( function()
16 + vim.g.pivi_layout = original
17 + end )
19 + describe( 'where the transcript goes', function()
20 + it( 'puts it beside the source on a wide editor', function()
21 + local chosen = at( 192, 43 )
23 + assert.equal( 'beside', chosen.orientation )
24 + assert.is_number( chosen.width )
25 + assert.is_nil( chosen.height )
26 + end )
28 + it( 'stacks it under the source when the source would be squeezed', function()
29 + local chosen = at( 100, 30 )
31 + assert.equal( 'below', chosen.orientation )
32 + assert.is_number( chosen.height )
33 + assert.is_nil( chosen.width )
34 + end )
36 + it( 'stacks it on a narrow editor rather than splitting into columns', function()
37 + assert.equal( 'below', at( 80, 24 ).orientation )
38 + assert.equal( 'below', at( 60, 20 ).orientation )
39 + end )
41 + it( 'switches to beside exactly where the source still fits', function()
42 + local settings = layout.settings()
43 + local edge = settings.transcript_min + settings.source_min + 1
45 + assert.equal( 'below', at( edge - 1, 40 ).orientation )
46 + assert.equal( 'beside', at( edge, 40 ).orientation )
47 + end )
48 + end )
50 + describe( 'how wide the transcript is', function()
51 + it( 'keeps prose readable rather than proportional on a narrow editor', function()
52 + local settings = layout.settings()
53 + assert.equal( settings.transcript_min, at( 120, 40 ).width )
54 + end )
56 + it( 'stops growing once prose is wide enough', function()
57 + local settings = layout.settings()
58 + assert.equal( settings.transcript_max, at( 400, 60 ).width )
59 + end )
61 + it( 'takes a share in between', function()
62 + local chosen = at( 192, 43 )
63 + local settings = layout.settings()
65 + assert.is_true( chosen.width > settings.transcript_min )
66 + assert.is_true( chosen.width < settings.transcript_max )
67 + end )
69 + it( 'always leaves the source enough room to be code', function()
70 + local settings = layout.settings()
71 + for columns = 60, 400, 7 do
72 + local chosen = at( columns, 40 )
73 + if chosen.orientation == 'beside' then
74 + assert.is_true( columns - chosen.width - 1 >= settings.source_min,
75 + ( 'source squeezed at %d columns' ):format( columns ) )
76 + end
77 + end
78 + end )
79 + end )
81 + describe( 'how tall the prompt is', function()
82 + it( 'grows with the editor, within reason', function()
83 + local settings = layout.settings()
85 + assert.equal( settings.prompt_min, at( 200, 12 ).prompt )
86 + assert.is_true( at( 200, 43 ).prompt > settings.prompt_min )
87 + assert.equal( settings.prompt_max, at( 200, 200 ).prompt )
88 + end )
90 + it( 'never asks for more room than the editor has', function()
91 + for lines = 6, 60, 3 do
92 + local chosen = at( 200, lines )
93 + assert.is_true( chosen.prompt < lines, ( 'prompt too tall at %d lines' ):format( lines ) )
94 + end
95 + end )
96 + end )
98 + describe( 'stacked height', function()
99 + it( 'leaves the source visible', function()
100 + for lines = 10, 60, 5 do
101 + local chosen = at( 80, lines )
102 + assert.equal( 'below', chosen.orientation )
103 + assert.is_true( chosen.height < lines, ( 'transcript too tall at %d lines' ):format( lines ) )
104 + end
105 + end )
106 + end )
108 + it( 'lets the configuration override every judgement', function()
109 + vim.g.pivi_layout = { transcript_min = 20, transcript_max = 30, source_min = 10, }
111 + local chosen = at( 100, 40 )
113 + assert.equal( 'beside', chosen.orientation )
114 + assert.equal( 30, chosen.width )
115 + end )
117 + it( 'never carves a new window out of one of its own', function()
118 + local transcript = require 'bugabinga.pivi.transcript'
119 + local working = vim.api.nvim_get_current_win()
121 + local bufnr = transcript.ensure 'hosting'
122 + local shown = transcript.show( bufnr )
123 + assert.is_number( shown )
125 + -- With the cursor inside the transcript, the next window still comes out
126 + -- of the window the user works in.
127 + vim.api.nvim_set_current_win( shown )
128 + assert.equal( working, layout.host() )
130 + vim.api.nvim_set_current_win( working )
131 + transcript.forget_all()
132 + end )
134 + it( 'reports the windows it owns and no others', function()
135 + vim.cmd 'enew'
136 + local ordinary = vim.api.nvim_get_current_buf()
138 + local found = layout.windows()
140 + for _, win in ipairs( found.transcript ) do
141 + assert.is_not.equal( ordinary, vim.api.nvim_win_get_buf( win ) )
142 + end
143 + for _, win in ipairs( found.prompt ) do
144 + assert.is_not.equal( ordinary, vim.api.nvim_win_get_buf( win ) )
145 + end
146 + end )
147 +end )

neovim/tests/drive/run.lua

Modified
88 88 local result = remote [[
89 89 local session = require 'bugabinga.pivi.session'
90 90 local command = require 'bugabinga.pivi.command'
91 - local current = setmetatable({ name = 'drive' }, { __index = function() return function() end end })
91 + local current = setmetatable(
92 + { name = 'drive', buffer = vim.api.nvim_create_buf(false, true) },
93 + { __index = function() return function() end end }
94 + )
92 95 local bufnr = command.open_prompt(current)
93 96 vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'hello' })
94 97 return {