mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 15:34:53 +01:00
feat: use new :await() api
This commit is contained in:
parent
3e9647ab39
commit
a134f18ab1
3 changed files with 18 additions and 25 deletions
|
@ -6,12 +6,12 @@ local buffer_id_map = {}
|
||||||
local ticks = {}
|
local ticks = {}
|
||||||
|
|
||||||
local function create(name, content)
|
local function create(name, content)
|
||||||
state.client:get_workspace(state.workspace):create_buffer(name, content)
|
state.client:get_workspace(state.workspace):create_buffer(name, content):await()
|
||||||
print(" ++ created buffer '" .. name .. "' on " .. state.workspace)
|
print(" ++ created buffer '" .. name .. "' on " .. state.workspace)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function delete(name)
|
local function delete(name)
|
||||||
state.client:get_workspace(state.workspace):delete_buffer(name)
|
state.client:get_workspace(state.workspace):delete_buffer(name):await()
|
||||||
print(" -- deleted buffer " .. name)
|
print(" -- deleted buffer " .. name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ local function attach(name, current, content)
|
||||||
vim.api.nvim_buf_set_name(buffer, "codemp::" .. name)
|
vim.api.nvim_buf_set_name(buffer, "codemp::" .. name)
|
||||||
vim.api.nvim_set_current_buf(buffer)
|
vim.api.nvim_set_current_buf(buffer)
|
||||||
end
|
end
|
||||||
local controller = state.client:get_workspace(state.workspace):attach(name)
|
local controller = state.client:get_workspace(state.workspace):attach(name):await()
|
||||||
|
|
||||||
-- TODO map name to uuid
|
-- TODO map name to uuid
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ local function attach(name, current, content)
|
||||||
ticks[buffer] = 0
|
ticks[buffer] = 0
|
||||||
|
|
||||||
if content ~= nil then
|
if content ~= nil then
|
||||||
controller:send(0, 0, content)
|
controller:send(0, 0, content) -- no need to await
|
||||||
end
|
end
|
||||||
|
|
||||||
-- hook serverbound callbacks
|
-- hook serverbound callbacks
|
||||||
|
@ -58,36 +58,34 @@ local function attach(name, current, content)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
print(string.format("sending: %s %s %s %s -- '%s'", start_row, start_col, start_row + new_end_row, start_col + new_end_col, content))
|
print(string.format("sending: %s %s %s %s -- '%s'", start_row, start_col, start_row + new_end_row, start_col + new_end_col, content))
|
||||||
controller:send(start_offset, start_offset + old_end_byte_len, content)
|
controller:send(start_offset, start_offset + old_end_byte_len, content) -- no need to await
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
local async = vim.loop.new_async(vim.schedule_wrap(function ()
|
local async = vim.loop.new_async(vim.schedule_wrap(function ()
|
||||||
while true do
|
while true do
|
||||||
local success, event = pcall(controller.try_recv, controller)
|
local event = controller:try_recv():await()
|
||||||
if not success then
|
|
||||||
print("error in buffer async handler: " .. tostring(event))
|
|
||||||
break
|
|
||||||
end
|
|
||||||
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)
|
||||||
-- print(" ~~ applying change ~~ " .. event.first .. ".." .. event.last .. "::[" .. event.content .. "]")
|
print(" ~~ applying change ~~ " .. event.first .. ".." .. event.last .. "::[" .. event.content .. "]")
|
||||||
utils.buffer.set_content(buffer, event.content, event.first, event.last)
|
utils.buffer.set_content(buffer, event.content, event.first, event.last)
|
||||||
if event.hash ~= nil then
|
if event.hash ~= nil then
|
||||||
if utils.hash(utils.buffer.get_content(buffer)) ~= event.hash then
|
if utils.hash(utils.buffer.get_content(buffer)) ~= event.hash then
|
||||||
-- OUT OF SYNC!
|
-- OUT OF SYNC!
|
||||||
-- TODO this may be destructive! we should probably prompt the user before doing this
|
-- TODO this may be destructive! we should probably prompt the user before doing this
|
||||||
print(" /!\\ out of sync, resynching...")
|
print(" /!\\ out of sync, resynching...")
|
||||||
utils.buffer.set_content(buffer, controller:content())
|
utils.buffer.set_content(buffer, controller:content():await())
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end))
|
end))
|
||||||
controller:callback(function (_controller) async:send() end)
|
controller:callback(function (_controller) async:send() end)
|
||||||
vim.schedule(function ()
|
vim.defer_fn(function() async:send() end, 500) -- force a try_recv after 500ms
|
||||||
async:send() -- run once to try_recv anything we synched in the meantime
|
|
||||||
end)
|
local remote_content = controller:content():await()
|
||||||
|
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
||||||
|
utils.buffer.set_content(buffer, remote_content)
|
||||||
|
|
||||||
print(" ++ attached to buffer " .. name)
|
print(" ++ attached to buffer " .. name)
|
||||||
return controller
|
return controller
|
||||||
|
@ -109,7 +107,7 @@ local function sync()
|
||||||
if name ~= nil then
|
if name ~= nil then
|
||||||
local controller = state.client:get_workspace(state.workspace):get_buffer(name)
|
local controller = state.client:get_workspace(state.workspace):get_buffer(name)
|
||||||
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
||||||
utils.buffer.set_content(buffer, controller:content())
|
utils.buffer.set_content(buffer, controller:content():await())
|
||||||
print(" :: synched buffer " .. name)
|
print(" :: synched buffer " .. name)
|
||||||
else
|
else
|
||||||
print(" !! buffer not managed")
|
print(" !! buffer not managed")
|
||||||
|
|
|
@ -22,7 +22,7 @@ local base_actions = {
|
||||||
if host == nil then host = 'http://codemp.alemi.dev:50053' end
|
if host == nil then host = 'http://codemp.alemi.dev:50053' end
|
||||||
local user = vim.g.codemp_username or vim.fn.input("username > ", "user-" .. vim.fn.rand() % 1024)
|
local user = vim.g.codemp_username or vim.fn.input("username > ", "user-" .. vim.fn.rand() % 1024)
|
||||||
local password = vim.g.codemp_password or vim.fn.input("password > ", "lmaodefaultpassword")
|
local password = vim.g.codemp_password or vim.fn.input("password > ", "lmaodefaultpassword")
|
||||||
state.client = native.connect(host, user, password)
|
state.client = native.connect(host, user, password):await()
|
||||||
print(" ++ connected to " .. host .. " as " .. user)
|
print(" ++ connected to " .. host .. " as " .. user)
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
@ -46,8 +46,7 @@ local connected_actions = {
|
||||||
|
|
||||||
disconnect = function()
|
disconnect = function()
|
||||||
print(" xx disconnecting client " .. state.client.id)
|
print(" xx disconnecting client " .. state.client.id)
|
||||||
native.close_client(state.client.id)
|
state.client = nil -- should drop and thus close everything
|
||||||
state.client = nil
|
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,11 +34,7 @@ end
|
||||||
local function register_cursor_handler(controller)
|
local function register_cursor_handler(controller)
|
||||||
local async = vim.loop.new_async(vim.schedule_wrap(function ()
|
local async = vim.loop.new_async(vim.schedule_wrap(function ()
|
||||||
while true do
|
while true do
|
||||||
local success, event = pcall(controller.try_recv, controller)
|
local event = controller:try_recv():await()
|
||||||
if not success then
|
|
||||||
print("error in cursor callback: " .. tostring(event))
|
|
||||||
break
|
|
||||||
end
|
|
||||||
if event == nil then break end
|
if event == nil then break end
|
||||||
if user_hl[event.user] == nil then
|
if user_hl[event.user] == nil then
|
||||||
user_hl[event.user] = {
|
user_hl[event.user] = {
|
||||||
|
@ -64,7 +60,7 @@ local function register_cursor_handler(controller)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function join(workspace)
|
local function join(workspace)
|
||||||
local ws = state.client:join_workspace(workspace)
|
local ws = state.client:join_workspace(workspace):await()
|
||||||
register_cursor_callback(ws.cursor)
|
register_cursor_callback(ws.cursor)
|
||||||
register_cursor_handler(ws.cursor)
|
register_cursor_handler(ws.cursor)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue