repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
neovim/lua/bugabinga/pivi/edit.lua
Rawlocal activity = require 'bugabinga.pivi.activity'
local is_file_buffer = function( bufnr )
return vim.api.nvim_buf_is_valid( bufnr )
and vim.bo[bufnr].buftype == ''
and vim.api.nvim_buf_get_name( bufnr ) ~= ''
end
--- Finds the loaded buffer for a path, if any.
local buffer_for = function( path )
local wanted = vim.fs.normalize( vim.fn.fnamemodify( path, ':p' ) )
for _, bufnr in ipairs( vim.api.nvim_list_bufs() ) do
if vim.api.nvim_buf_is_loaded( bufnr ) then
local name = vim.api.nvim_buf_get_name( bufnr )
if name ~= '' and vim.fs.normalize( name ) == wanted then return bufnr end
end
end
return nil
end
--- Reads authoritative content: the loaded buffer wins over disk.
local read = function( path )
local bufnr = buffer_for( path )
if bufnr then
return table.concat( vim.api.nvim_buf_get_lines( bufnr, 0, -1, false ), '\n' ), 'buffer'
end
local ok, lines = pcall( vim.fn.readfile, path )
if not ok then return nil, 'missing' end
return table.concat( lines, '\n' ), 'disk'
end
--- Writes a loaded buffer to disk so later filesystem readers observe agent edits.
--- pivi starts no filesystem watcher; visibility is part of the edit itself.
local persist = function( bufnr )
if not is_file_buffer( bufnr ) then return false end
if not vim.bo[bufnr].modified then return true end
local ok = pcall( vim.api.nvim_buf_call, bufnr, function()
vim.cmd 'silent! noautocmd write'
end )
return ok and not vim.bo[bufnr].modified
end
--- Applies line replacements as one coherent undoable change.
--- Replacements are zero based, end exclusive, and are applied bottom up.
--- When Neovim refuses to join undo blocks the edit still lands.
--- @return boolean applied
--- @return boolean joined whether the change stayed a single undo block
local apply = function( bufnr, replacements )
vim.validate( 'bufnr', bufnr, 'number' )
vim.validate( 'replacements', replacements, 'table' )
if not vim.api.nvim_buf_is_valid( bufnr ) then return false, false end
local ordered = vim.deepcopy( replacements )
table.sort( ordered, function( left, right ) return left.first > right.first end )
local joined = true
local touched = {}
for index, replacement in ipairs( ordered ) do
if index > 1 then
local ok = pcall( vim.api.nvim_buf_call, bufnr, function() vim.cmd.undojoin() end )
if not ok then joined = false end
end
vim.api.nvim_buf_set_lines(
bufnr,
replacement.first,
replacement.last,
false,
replacement.lines
)
table.insert( touched, {
first = replacement.first,
last = replacement.first + #replacement.lines,
} )
end
activity.mark_edit( bufnr, touched )
return true, joined
end
local find_text = function( content, needle )
local first = content:find( needle, 1, true )
if not first then return nil end
local second = content:find( needle, first + 1, true )
if second then return nil, 'text is not unique' end
return first
end
--- Replaces one unique text occurrence in a loaded buffer.
local replace = function( bufnr, old_text, new_text )
vim.validate( 'old_text', old_text, 'string' )
vim.validate( 'new_text', new_text, 'string' )
if not vim.api.nvim_buf_is_valid( bufnr ) then return false, 'invalid buffer' end
local lines = vim.api.nvim_buf_get_lines( bufnr, 0, -1, false )
local content = table.concat( lines, '\n' )
local first, failure = find_text( content, old_text )
if not first then return false, failure or 'text not found' end
local replaced = content:sub( 1, first - 1 ) .. new_text .. content:sub( first + #old_text )
local before = #vim.split( content:sub( 1, first - 1 ), '\n', { plain = true, } )
local after = before + #vim.split( new_text, '\n', { plain = true, } ) - 1
local applied, joined = apply( bufnr, { {
first = 0,
last = -1,
lines = vim.split( replaced, '\n', { plain = true, } ),
}, } )
if applied then
activity.mark_edit( bufnr, { { first = before - 1, last = after, }, } )
end
return applied, joined
end
return {
buffer_for = buffer_for,
is_file_buffer = is_file_buffer,
read = read,
persist = persist,
apply = apply,
replace = replace,
}