2024-08-06 01:11:09 +02:00
|
|
|
local utils = require('codemp.utils')
|
2024-08-24 01:56:27 +02:00
|
|
|
local session = require('codemp.session')
|
2024-08-06 01:11:09 +02:00
|
|
|
|
2024-08-24 01:57:42 +02:00
|
|
|
---@type table<integer, string>
|
2024-08-06 01:11:09 +02:00
|
|
|
local id_buffer_map = {}
|
2024-08-24 01:57:42 +02:00
|
|
|
---@type table<string, integer>
|
2024-08-06 01:11:09 +02:00
|
|
|
local buffer_id_map = {}
|
2024-08-24 01:57:42 +02:00
|
|
|
---@type table<string, string>
|
2024-08-17 03:28:08 +02:00
|
|
|
local user_buffer_name = {}
|
2024-08-06 01:11:09 +02:00
|
|
|
local ticks = {}
|
|
|
|
|
2024-08-24 01:57:42 +02:00
|
|
|
---@param name string name of buffer to attach to
|
2024-09-15 12:39:50 +02:00
|
|
|
---@param buffer? integer buffer to use for attaching (will clear content)
|
2024-08-24 01:57:42 +02:00
|
|
|
---@param content? string if provided, set this content after attaching
|
2024-09-06 18:24:57 +02:00
|
|
|
---@param nowait? boolean skip waiting for initial content sync
|
2024-08-24 01:57:42 +02:00
|
|
|
---@return BufferController
|
2024-09-06 18:24:57 +02:00
|
|
|
local function attach(name, buffer, content, nowait)
|
2024-08-24 01:57:42 +02:00
|
|
|
if buffer_id_map[name] ~= nil then
|
|
|
|
error("already attached to buffer " .. name)
|
|
|
|
end
|
2024-09-15 12:39:50 +02:00
|
|
|
|
|
|
|
if buffer == nil then
|
|
|
|
buffer = vim.api.nvim_get_current_buf()
|
|
|
|
end
|
|
|
|
|
2024-09-06 19:40:52 +02:00
|
|
|
vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer })
|
|
|
|
vim.api.nvim_buf_set_name(buffer, name)
|
2024-09-16 18:27:23 +02:00
|
|
|
local controller = session.workspace:attach(name):await()
|
2024-09-14 14:24:48 +02:00
|
|
|
|
2024-09-06 18:24:57 +02:00
|
|
|
if not nowait then
|
2024-09-14 14:24:48 +02:00
|
|
|
local promise = controller:poll()
|
|
|
|
for i=1, 20, 1 do
|
|
|
|
if promise.ready then break end
|
|
|
|
vim.uv.sleep(100)
|
|
|
|
end
|
2024-09-06 18:24:57 +02:00
|
|
|
end
|
2024-08-06 01:11:09 +02:00
|
|
|
|
|
|
|
-- TODO map name to uuid
|
|
|
|
|
|
|
|
id_buffer_map[buffer] = name
|
|
|
|
buffer_id_map[name] = buffer
|
|
|
|
ticks[buffer] = 0
|
|
|
|
|
|
|
|
-- hook serverbound callbacks
|
|
|
|
-- TODO breaks when deleting whole lines at buffer end
|
|
|
|
vim.api.nvim_buf_attach(buffer, false, {
|
|
|
|
on_bytes = function(_, buf, tick, start_row, start_col, start_offset, old_end_row, old_end_col, old_end_byte_len, new_end_row, new_end_col, new_byte_len)
|
|
|
|
if tick <= ticks[buf] then return end
|
|
|
|
if id_buffer_map[buf] == nil then return true end -- unregister callback handler
|
2024-09-06 03:53:52 +02:00
|
|
|
if CODEMP.config.debug then print(string.format(
|
2024-08-14 23:48:55 +02:00
|
|
|
"start(row:%s, col:%s) offset:%s end(row:%s, col:%s new(row:%s, col:%s)) len(old:%s, new:%s)",
|
|
|
|
start_row, start_col, start_offset, old_end_row, old_end_col, new_end_row, new_end_col, old_end_byte_len, new_byte_len
|
2024-09-06 03:53:52 +02:00
|
|
|
)) end
|
2024-09-09 05:14:52 +02:00
|
|
|
local end_offset = start_offset + old_end_byte_len
|
|
|
|
local change_content = ""
|
|
|
|
local len = utils.buffer.len(buf)
|
|
|
|
if start_offset + new_byte_len + 1 > len then
|
|
|
|
-- i dont know why but we may go out of bounds when doing 'dd' at the end of buffer??
|
|
|
|
local delta = (start_offset + new_byte_len + 1) - len
|
|
|
|
if CODEMP.config.debug then print("/!\\ bytes out of bounds by " .. delta .. ", adjusting") end
|
|
|
|
end_offset = end_offset - delta
|
|
|
|
start_offset = start_offset - delta
|
|
|
|
end
|
|
|
|
if new_byte_len > 0 then
|
2024-09-09 02:37:50 +02:00
|
|
|
local actual_end_col = new_end_col
|
|
|
|
if new_end_row == 0 then actual_end_col = new_end_col + start_col end
|
2024-09-09 05:14:52 +02:00
|
|
|
local actual_end_row = start_row + new_end_row
|
|
|
|
-- -- when bulk inserting at the end we go out of bounds, so we probably need to clamp?
|
|
|
|
-- -- issue: row=x+1 col=0 and row=x col=0 may be very far apart! we need to get last col of row x, ughh..
|
|
|
|
-- -- also, this doesn't work so it will stay commented out for a while longer
|
|
|
|
-- if new_end_row ~= old_end_row and new_end_col == 0 then
|
|
|
|
-- -- we may be dealing with the last line of the buffer, get_text could error because out-of-bounds
|
|
|
|
-- local row_count = vim.api.nvim_buf_line_count(buf)
|
|
|
|
-- if actual_end_row + 1 > row_count then
|
|
|
|
-- local delta = (actual_end_row + 1) - row_count
|
|
|
|
-- if CODEMP.config.debug then print("/!\\ row out of bounds by " .. delta .. ", adjusting") end
|
|
|
|
-- actual_end_row = actual_end_row - delta
|
|
|
|
-- actual_end_col = len - vim.api.nvim_buf_get_offset(buf, row_count)
|
|
|
|
-- end
|
|
|
|
-- end
|
|
|
|
local lines = vim.api.nvim_buf_get_text(buf, start_row, start_col, actual_end_row, actual_end_col, {})
|
|
|
|
change_content = table.concat(lines, '\n')
|
2024-08-14 23:48:55 +02:00
|
|
|
end
|
2024-09-06 03:53:52 +02:00
|
|
|
if CODEMP.config.debug then
|
2024-09-09 05:14:52 +02:00
|
|
|
print(string.format("sending: %s..%s '%s'", start_offset, start_offset + old_end_byte_len, change_content))
|
2024-09-06 03:53:52 +02:00
|
|
|
end
|
2024-09-16 18:27:23 +02:00
|
|
|
controller:send({
|
|
|
|
start = start_offset, finish = end_offset, content = change_content
|
|
|
|
}):await()
|
2024-08-06 01:11:09 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2024-08-16 03:49:24 +02:00
|
|
|
local async = vim.loop.new_async(vim.schedule_wrap(function ()
|
|
|
|
while true do
|
2024-08-17 01:56:09 +02:00
|
|
|
local event = controller:try_recv():await()
|
2024-08-16 03:49:24 +02:00
|
|
|
if event == nil then break end
|
|
|
|
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
2024-09-06 03:53:52 +02:00
|
|
|
if CODEMP.config.debug then
|
2024-09-16 18:27:23 +02:00
|
|
|
print(" ~~ applying change ~~ " .. event.start .. ".." .. event.finish .. "::[" .. event.content .. "]")
|
2024-09-06 03:53:52 +02:00
|
|
|
end
|
2024-09-16 18:27:23 +02:00
|
|
|
utils.buffer.set_content(buffer, event.content, event.start, event.finish)
|
2024-08-16 03:49:24 +02:00
|
|
|
if event.hash ~= nil then
|
|
|
|
if utils.hash(utils.buffer.get_content(buffer)) ~= event.hash then
|
|
|
|
-- OUT OF SYNC!
|
|
|
|
-- TODO this may be destructive! we should probably prompt the user before doing this
|
|
|
|
print(" /!\\ out of sync, resynching...")
|
2024-08-17 01:56:09 +02:00
|
|
|
utils.buffer.set_content(buffer, controller:content():await())
|
2024-08-16 03:49:24 +02:00
|
|
|
return
|
|
|
|
end
|
2024-08-14 18:12:00 +02:00
|
|
|
end
|
|
|
|
end
|
2024-08-16 03:49:24 +02:00
|
|
|
end))
|
2024-08-17 01:56:09 +02:00
|
|
|
|
|
|
|
local remote_content = controller:content():await()
|
2024-09-06 17:23:18 +02:00
|
|
|
if content ~= nil then
|
|
|
|
-- TODO this may happen too soon!!
|
2024-09-16 18:27:23 +02:00
|
|
|
local _ = controller:send({
|
|
|
|
start = 0, finish = #remote_content, content = content
|
|
|
|
}) -- no need to await
|
2024-09-06 17:23:18 +02:00
|
|
|
else
|
2024-09-14 15:18:44 +02:00
|
|
|
local current_content = utils.buffer.get_content(buffer)
|
|
|
|
if current_content ~= remote_content then
|
|
|
|
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
|
|
|
utils.buffer.set_content(buffer, remote_content)
|
|
|
|
end
|
2024-09-06 17:23:18 +02:00
|
|
|
end
|
2024-08-06 01:11:09 +02:00
|
|
|
|
2024-09-05 04:37:48 +02:00
|
|
|
controller:callback(function (_controller) async:send() end)
|
|
|
|
vim.defer_fn(function() async:send() end, 500) -- force a try_recv after 500ms
|
|
|
|
|
2024-09-06 19:58:03 +02:00
|
|
|
local filetype = vim.filetype.match({ buf = buffer })
|
|
|
|
vim.api.nvim_set_option_value("filetype", filetype, { buf = buffer })
|
2024-08-06 01:11:09 +02:00
|
|
|
print(" ++ attached to buffer " .. name)
|
2024-09-07 03:35:32 +02:00
|
|
|
require('codemp.window').update()
|
2024-08-15 01:22:48 +02:00
|
|
|
return controller
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
|
|
|
|
2024-08-23 00:59:13 +02:00
|
|
|
---@param name string
|
2024-08-24 01:57:42 +02:00
|
|
|
--TODO this should happen at the level above (Workspace) but accesses tables on this level, badly designed!
|
2024-08-08 04:34:23 +02:00
|
|
|
local function detach(name)
|
2024-08-06 01:11:09 +02:00
|
|
|
local buffer = buffer_id_map[name]
|
|
|
|
id_buffer_map[buffer] = nil
|
|
|
|
buffer_id_map[name] = nil
|
2024-09-16 18:27:23 +02:00
|
|
|
session.workspace:detach(name)
|
2024-08-06 01:11:09 +02:00
|
|
|
vim.api.nvim_buf_delete(buffer, {})
|
|
|
|
|
|
|
|
print(" -- detached from buffer " .. name)
|
2024-09-07 03:35:32 +02:00
|
|
|
require('codemp.window').update()
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
|
|
|
|
2024-08-24 01:57:42 +02:00
|
|
|
---@param buffer? integer if provided, sync given buffer id, otherwise sync current buf
|
|
|
|
local function sync(buffer)
|
|
|
|
if buffer == nil then
|
|
|
|
buffer = vim.api.nvim_get_current_buf()
|
|
|
|
end
|
2024-08-06 01:11:09 +02:00
|
|
|
local name = id_buffer_map[buffer]
|
|
|
|
if name ~= nil then
|
2024-08-24 01:56:27 +02:00
|
|
|
local controller = session.workspace:get_buffer(name)
|
2024-08-22 22:07:00 +02:00
|
|
|
if controller ~= nil then
|
2024-09-09 05:14:19 +02:00
|
|
|
local real_content = controller:content():await()
|
|
|
|
local my_content = utils.buffer.get_content(buffer)
|
|
|
|
if real_content ~= my_content then
|
|
|
|
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
|
|
|
utils.buffer.set_content(buffer, real_content)
|
|
|
|
print(" !! re-synched buffer " .. name)
|
|
|
|
else
|
|
|
|
print(" :: buffer " .. name .. " is in sync")
|
|
|
|
end
|
2024-08-22 22:07:00 +02:00
|
|
|
return
|
|
|
|
end
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
2024-08-22 22:07:00 +02:00
|
|
|
|
|
|
|
print(" !! buffer not managed")
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
|
|
|
|
2024-09-06 15:13:36 +02:00
|
|
|
local function create(buffer)
|
|
|
|
if buffer == nil then
|
|
|
|
buffer = vim.fn.expand("%p")
|
|
|
|
end
|
|
|
|
if session.workspace == nil then
|
|
|
|
error("join a workspace first")
|
|
|
|
end
|
2024-09-16 18:27:23 +02:00
|
|
|
session.workspace:create(buffer):await()
|
2024-09-07 03:35:32 +02:00
|
|
|
print(" ++ created buffer " .. buffer)
|
|
|
|
require('codemp.window').update()
|
2024-09-06 15:13:36 +02:00
|
|
|
end
|
|
|
|
|
2024-08-06 01:11:09 +02:00
|
|
|
return {
|
|
|
|
sync = sync,
|
|
|
|
attach = attach,
|
|
|
|
detach = detach,
|
2024-09-06 15:13:36 +02:00
|
|
|
create = create,
|
2024-08-06 01:11:09 +02:00
|
|
|
map = id_buffer_map,
|
|
|
|
map_rev = buffer_id_map,
|
|
|
|
ticks = ticks,
|
2024-08-17 03:28:08 +02:00
|
|
|
users = user_buffer_name,
|
2024-08-06 01:11:09 +02:00
|
|
|
}
|