mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 07:24:52 +01:00
feat: non-blocking API
This commit is contained in:
parent
646760f3cb
commit
9dea608f04
5 changed files with 179 additions and 170 deletions
|
@ -13,7 +13,6 @@ local ticks = {}
|
|||
---@param buffer? integer buffer to use for attaching (will clear content)
|
||||
---@param content? string if provided, set this content after attaching
|
||||
---@param nowait? boolean skip waiting for initial content sync
|
||||
---@return BufferController
|
||||
local function attach(name, buffer, content, nowait)
|
||||
if buffer_id_map[name] ~= nil then
|
||||
error("already attached to buffer " .. name)
|
||||
|
@ -25,8 +24,7 @@ local function attach(name, buffer, content, nowait)
|
|||
|
||||
vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer })
|
||||
vim.api.nvim_buf_set_name(buffer, name)
|
||||
local controller = session.workspace:attach(name):await()
|
||||
|
||||
session.workspace:attach(name):and_then(function (controller)
|
||||
if not nowait then
|
||||
local promise = controller:poll()
|
||||
for i=1, 20, 1 do
|
||||
|
@ -125,14 +123,14 @@ local function attach(name, buffer, content, nowait)
|
|||
end
|
||||
end
|
||||
|
||||
controller:callback(function (_controller) async:send() end)
|
||||
controller:callback(function (_) async:send() end)
|
||||
vim.defer_fn(function() async:send() end, 500) -- force a try_recv after 500ms
|
||||
|
||||
local filetype = vim.filetype.match({ buf = buffer })
|
||||
vim.api.nvim_set_option_value("filetype", filetype, { buf = buffer })
|
||||
print(" ++ attached to buffer " .. name)
|
||||
require('codemp.window').update()
|
||||
return controller
|
||||
end)
|
||||
end
|
||||
|
||||
---@param name string
|
||||
|
@ -180,9 +178,10 @@ local function create(buffer)
|
|||
if session.workspace == nil then
|
||||
error("join a workspace first")
|
||||
end
|
||||
session.workspace:create(buffer):await()
|
||||
session.workspace:create(buffer):and_then(function ()
|
||||
print(" ++ created buffer " .. buffer)
|
||||
require('codemp.window').update()
|
||||
end)
|
||||
end
|
||||
|
||||
return {
|
||||
|
|
|
@ -8,9 +8,11 @@ local function connect()
|
|||
if CODEMP.config.password == nil then
|
||||
CODEMP.config.password = vim.g.codemp_password or vim.fn.input("password > ", "")
|
||||
end
|
||||
session.client = CODEMP.native.connect(CODEMP.config):await()
|
||||
CODEMP.native.connect(CODEMP.config):and_then(function (client)
|
||||
session.client = client
|
||||
require('codemp.window').update()
|
||||
vim.schedule(function () workspace.list() end)
|
||||
workspace.list()
|
||||
end)
|
||||
end
|
||||
|
||||
return {
|
||||
|
|
|
@ -50,9 +50,10 @@ local connected_actions = {
|
|||
|
||||
start = function(ws)
|
||||
if ws == nil then error("missing workspace name") end
|
||||
session.client:create_workspace(ws):await()
|
||||
vim.schedule(function () workspace.list() end)
|
||||
session.client:create_workspace(ws):and_then(function ()
|
||||
print(" <> created workspace " .. ws)
|
||||
workspace.list()
|
||||
end)
|
||||
end,
|
||||
|
||||
available = function()
|
||||
|
@ -71,8 +72,9 @@ local connected_actions = {
|
|||
else
|
||||
ws = vim.fn.input("workspace > ", "")
|
||||
end
|
||||
session.client:invite_to_workspace(ws, user):await()
|
||||
print(" ][ invited " .. user .. " to workspace " .. ws)
|
||||
session.client:invite_to_workspace(ws, user):and_then(function ()
|
||||
print(" :: invited " .. user .. " to workspace " .. ws)
|
||||
end)
|
||||
end,
|
||||
|
||||
disconnect = function()
|
||||
|
@ -108,8 +110,9 @@ local joined_actions = {
|
|||
|
||||
delete = function(path)
|
||||
if path == nil then error("missing buffer name") end
|
||||
session.workspace:delete(path):await()
|
||||
session.workspace:delete(path):and_then(function()
|
||||
print(" xx deleted buffer " .. path)
|
||||
end)
|
||||
end,
|
||||
|
||||
buffers = function()
|
||||
|
|
|
@ -116,19 +116,21 @@ M.delete = function(state, path, extra)
|
|||
vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input)
|
||||
if input == nil then return end
|
||||
if not vim.startswith("y", string.lower(input)) then return end
|
||||
session.workspace:delete(selected.name):await()
|
||||
session.workspace:delete(selected.name):and_then(function ()
|
||||
print("deleted buffer " .. selected.name)
|
||||
manager.refresh("codemp")
|
||||
end)
|
||||
end)
|
||||
elseif selected.type == "workspace" then
|
||||
if session.client == nil then error("connect to server first") end
|
||||
vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input)
|
||||
if input == nil then return end
|
||||
if not vim.startswith("y", string.lower(input)) then return end
|
||||
session.client:delete_workspace(selected.name):await()
|
||||
session.client:delete_workspace(selected.name):and_then(function ()
|
||||
print("deleted workspace " .. selected.name)
|
||||
manager.refresh("codemp")
|
||||
end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -138,22 +140,25 @@ M.add = function(state, path, extra)
|
|||
if vim.startswith(selected.name, "#") then
|
||||
vim.ui.input({ prompt = "new buffer path" }, function(input)
|
||||
if input == nil or input == "" then return end
|
||||
session.workspace:create(input):await()
|
||||
session.workspace:create(input):and_then(function ()
|
||||
manager.refresh("codemp")
|
||||
end)
|
||||
end)
|
||||
elseif selected.name == "workspaces" then
|
||||
vim.ui.input({ prompt = "new workspace name" }, function(input)
|
||||
if input == nil or input == "" then return end
|
||||
session.client:create_workspace(input):await()
|
||||
vim.schedule(function () require('codemp.workspace').list() end)
|
||||
session.client:create_workspace(input):and_then(function ()
|
||||
require('codemp.workspace').list()
|
||||
end)
|
||||
end)
|
||||
end
|
||||
elseif selected.type == "workspace" then
|
||||
vim.ui.input({ prompt = "user name to invite" }, function(input)
|
||||
if input == nil or input == "" then return end
|
||||
session.client:invite_to_workspace(selected.name, input):await()
|
||||
session.client:invite_to_workspace(selected.name, input):and_then(function ()
|
||||
print("invited user " .. input .. " to workspace " .. selected.name)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
manager.refresh("codemp")
|
||||
end
|
||||
|
|
|
@ -12,14 +12,14 @@ local user_hl = {}
|
|||
|
||||
local function fetch_workspaces_list()
|
||||
local new_list = {}
|
||||
local owned = session.client:list_workspaces(true, false):await()
|
||||
session.client:list_workspaces(true, false):and_then(function (owned)
|
||||
for _, ws in pairs(owned) do
|
||||
table.insert(new_list, {
|
||||
name = ws,
|
||||
owned = true,
|
||||
})
|
||||
end
|
||||
local invited = session.client:list_workspaces(false, true):await()
|
||||
session.client:list_workspaces(false, true):and_then(function (invited)
|
||||
for _, ws in pairs(invited) do
|
||||
table.insert(new_list, {
|
||||
name = ws,
|
||||
|
@ -28,6 +28,8 @@ local function fetch_workspaces_list()
|
|||
end
|
||||
session.available = new_list
|
||||
require('codemp.window').update()
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
---@param ws Workspace
|
||||
|
@ -105,10 +107,9 @@ local function register_cursor_handler(ws)
|
|||
end
|
||||
|
||||
---@param workspace string workspace name to join
|
||||
---@return Workspace
|
||||
---join a workspace and register event handlers
|
||||
local function join(workspace)
|
||||
local ws = session.client:join_workspace(workspace):await()
|
||||
session.client:join_workspace(workspace):and_then(function (ws)
|
||||
print(" >< joined workspace " .. ws.name)
|
||||
register_cursor_callback(ws)
|
||||
register_cursor_handler(ws)
|
||||
|
@ -139,8 +140,7 @@ local function join(workspace)
|
|||
|
||||
session.workspace = ws
|
||||
require('codemp.window').update()
|
||||
|
||||
return ws
|
||||
end)
|
||||
end
|
||||
|
||||
local function leave()
|
||||
|
|
Loading…
Reference in a new issue