mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-21 23:14:54 +01:00
fix: invoke attach callback on main thread
This commit is contained in:
parent
14286f1699
commit
d0d1fb73ae
1 changed files with 120 additions and 117 deletions
|
@ -57,137 +57,140 @@ local function attach(name, opts)
|
||||||
|
|
||||||
vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer })
|
vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer })
|
||||||
vim.api.nvim_buf_set_name(buffer, name)
|
vim.api.nvim_buf_set_name(buffer, name)
|
||||||
|
|
||||||
CODEMP.workspace:attach(name):and_then(function (controller)
|
CODEMP.workspace:attach(name):and_then(function (controller)
|
||||||
-- TODO disgusting! but poll blocks forever on empty buffers...
|
vim.schedule(function()
|
||||||
if not opts.nowait then
|
-- TODO disgusting! but poll blocks forever on empty buffers...
|
||||||
local promise = controller:poll()
|
if not opts.nowait then
|
||||||
for i=1, 20, 1 do
|
local promise = controller:poll()
|
||||||
if promise.ready then break end
|
for i=1, 20, 1 do
|
||||||
vim.uv.sleep(100)
|
if promise.ready then break end
|
||||||
|
vim.uv.sleep(100)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if opts.window ~= nil then
|
if opts.window ~= nil then
|
||||||
vim.api.nvim_win_set_buf(opts.window, buffer)
|
vim.api.nvim_win_set_buf(opts.window, buffer)
|
||||||
vim.api.nvim_set_current_win(opts.window)
|
vim.api.nvim_set_current_win(opts.window)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO map name to uuid
|
-- TODO map name to uuid
|
||||||
|
|
||||||
id_buffer_map[buffer] = name
|
id_buffer_map[buffer] = name
|
||||||
buffer_id_map[name] = buffer
|
buffer_id_map[name] = buffer
|
||||||
ticks[buffer] = 0
|
ticks[buffer] = 0
|
||||||
|
|
||||||
-- hook serverbound callbacks
|
-- hook serverbound callbacks
|
||||||
-- TODO breaks when deleting whole lines at buffer end
|
-- TODO breaks when deleting whole lines at buffer end
|
||||||
vim.api.nvim_buf_attach(buffer, false, {
|
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)
|
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 tick <= ticks[buf] then return end
|
||||||
if id_buffer_map[buf] == nil then return true end -- unregister callback handler
|
if id_buffer_map[buf] == nil then return true end -- unregister callback handler
|
||||||
if CODEMP.config.debug then print(string.format(
|
if CODEMP.config.debug then print(string.format(
|
||||||
"start(row:%s, col:%s) offset:%s end(row:%s, col:%s new(row:%s, col:%s)) len(old:%s, new:%s)",
|
"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
|
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
|
||||||
)) end
|
)) end
|
||||||
local end_offset = start_offset + old_end_byte_len
|
local end_offset = start_offset + old_end_byte_len
|
||||||
local change_content = ""
|
local change_content = ""
|
||||||
local len = utils.buffer.len(buf)
|
local len = utils.buffer.len(buf)
|
||||||
if start_offset + new_byte_len + 1 > len then
|
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??
|
-- 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
|
local delta = (start_offset + new_byte_len + 1) - len
|
||||||
if CODEMP.config.debug then print("/!\\ bytes out of bounds by " .. delta .. ", adjusting") end
|
if CODEMP.config.debug then print("/!\\ bytes out of bounds by " .. delta .. ", adjusting") end
|
||||||
end_offset = end_offset - delta
|
end_offset = end_offset - delta
|
||||||
start_offset = start_offset - delta
|
start_offset = start_offset - delta
|
||||||
end
|
end
|
||||||
if new_byte_len > 0 then
|
if new_byte_len > 0 then
|
||||||
local actual_end_col = new_end_col
|
local actual_end_col = new_end_col
|
||||||
if new_end_row == 0 then actual_end_col = new_end_col + start_col end
|
if new_end_row == 0 then actual_end_col = new_end_col + start_col end
|
||||||
local actual_end_row = start_row + new_end_row
|
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?
|
-- -- 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..
|
-- -- 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
|
-- -- 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
|
-- 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
|
-- -- 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)
|
-- local row_count = vim.api.nvim_buf_line_count(buf)
|
||||||
-- if actual_end_row + 1 > row_count then
|
-- if actual_end_row + 1 > row_count then
|
||||||
-- local delta = (actual_end_row + 1) - row_count
|
-- local delta = (actual_end_row + 1) - row_count
|
||||||
-- if CODEMP.config.debug then print("/!\\ row out of bounds by " .. delta .. ", adjusting") end
|
-- if CODEMP.config.debug then print("/!\\ row out of bounds by " .. delta .. ", adjusting") end
|
||||||
-- actual_end_row = actual_end_row - delta
|
-- actual_end_row = actual_end_row - delta
|
||||||
-- actual_end_col = len - vim.api.nvim_buf_get_offset(buf, row_count)
|
-- actual_end_col = len - vim.api.nvim_buf_get_offset(buf, row_count)
|
||||||
-- end
|
-- end
|
||||||
-- end
|
-- end
|
||||||
local lines = vim.api.nvim_buf_get_text(buf, start_row, start_col, actual_end_row, actual_end_col, {})
|
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')
|
change_content = table.concat(lines, '\n')
|
||||||
end
|
end
|
||||||
if CODEMP.config.debug then
|
if CODEMP.config.debug then
|
||||||
print(string.format("sending: %s..%s '%s'", start_offset, start_offset + old_end_byte_len, change_content))
|
print(string.format("sending: %s..%s '%s'", start_offset, start_offset + old_end_byte_len, change_content))
|
||||||
end
|
end
|
||||||
controller:send({
|
controller:send({
|
||||||
start = start_offset, finish = end_offset, content = change_content
|
start = start_offset, finish = end_offset, content = change_content
|
||||||
}):await()
|
}):await()
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
local lock = false
|
local lock = false
|
||||||
local async = vim.loop.new_async(vim.schedule_wrap(function ()
|
local async = vim.loop.new_async(vim.schedule_wrap(function ()
|
||||||
if lock then return end
|
if lock then return end
|
||||||
lock = true
|
lock = true
|
||||||
while true do
|
while true do
|
||||||
local event = controller:try_recv():await()
|
local event = controller:try_recv():await()
|
||||||
if event == nil then break end
|
if event == nil then break end
|
||||||
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
||||||
CODEMP.ignore_following_action = true
|
CODEMP.ignore_following_action = true
|
||||||
if CODEMP.config.debug then
|
if CODEMP.config.debug then
|
||||||
print(" ~~ applying change ~~ " .. event.start .. ".." .. event.finish .. "::[" .. event.content .. "]")
|
print(" ~~ applying change ~~ " .. event.start .. ".." .. event.finish .. "::[" .. event.content .. "]")
|
||||||
end
|
end
|
||||||
utils.buffer.set_content(buffer, event.content, event.start, event.finish)
|
utils.buffer.set_content(buffer, event.content, event.start, event.finish)
|
||||||
if event.hash ~= nil then
|
if event.hash ~= nil then
|
||||||
if CODEMP.native.hash(utils.buffer.get_content(buffer)) ~= event.hash then
|
if CODEMP.native.hash(utils.buffer.get_content(buffer)) ~= event.hash then
|
||||||
if CODEMP.config.auto_sync then
|
if CODEMP.config.auto_sync then
|
||||||
print(" /!\\ out of sync, resynching...")
|
print(" /!\\ out of sync, resynching...")
|
||||||
utils.buffer.set_content(buffer, controller:content():await())
|
utils.buffer.set_content(buffer, controller:content():await())
|
||||||
else
|
else
|
||||||
vim.ui.select(
|
vim.ui.select(
|
||||||
{ "sync", "detach" },
|
{ "sync", "detach" },
|
||||||
{ prompt = "out of sync! force resync or detach?" },
|
{ prompt = "out of sync! force resync or detach?" },
|
||||||
function (choice)
|
function (choice)
|
||||||
if not choice then return end
|
if not choice then return end
|
||||||
if choice == "sync" then
|
if choice == "sync" then
|
||||||
utils.buffer.set_content(buffer, controller:content():await())
|
utils.buffer.set_content(buffer, controller:content():await())
|
||||||
|
end
|
||||||
|
if choice == "detach" then
|
||||||
|
detach(name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if choice == "detach" then
|
)
|
||||||
detach(name)
|
end
|
||||||
end
|
return
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
return
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
lock = false
|
||||||
|
end))
|
||||||
|
|
||||||
|
local remote_content = controller:content():await()
|
||||||
|
if opts.content ~= nil then
|
||||||
|
-- TODO this may happen too soon!!
|
||||||
|
local _ = controller:send({
|
||||||
|
start = 0, finish = #remote_content, content = opts.content
|
||||||
|
}) -- no need to await
|
||||||
|
else
|
||||||
|
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
|
||||||
end
|
end
|
||||||
lock = false
|
|
||||||
end))
|
|
||||||
|
|
||||||
local remote_content = controller:content():await()
|
controller:callback(function (_) async:send() end)
|
||||||
if opts.content ~= nil then
|
vim.defer_fn(function() async:send() end, 500) -- force a try_recv after 500ms
|
||||||
-- TODO this may happen too soon!!
|
|
||||||
local _ = controller:send({
|
|
||||||
start = 0, finish = #remote_content, content = opts.content
|
|
||||||
}) -- no need to await
|
|
||||||
else
|
|
||||||
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
|
|
||||||
end
|
|
||||||
|
|
||||||
controller:callback(function (_) async:send() end)
|
local filetype = vim.filetype.match({ buf = buffer })
|
||||||
vim.defer_fn(function() async:send() end, 500) -- force a try_recv after 500ms
|
vim.api.nvim_set_option_value("filetype", filetype, { buf = buffer })
|
||||||
|
print(" ++ attached to buffer " .. name)
|
||||||
local filetype = vim.filetype.match({ buf = buffer })
|
require('codemp.window').update()
|
||||||
vim.api.nvim_set_option_value("filetype", filetype, { buf = buffer })
|
end)
|
||||||
print(" ++ attached to buffer " .. name)
|
|
||||||
require('codemp.window').update()
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue