2024-08-06 01:11:09 +02:00
|
|
|
local utils = require('codemp.utils')
|
2024-08-06 18:46:22 +02:00
|
|
|
local buffers = require('codemp.buffers')
|
2024-08-24 01:56:27 +02:00
|
|
|
local session = require('codemp.session')
|
2024-08-06 01:11:09 +02:00
|
|
|
|
|
|
|
local user_hl = {}
|
|
|
|
|
2024-08-25 03:50:52 +02:00
|
|
|
local function fetch_workspaces_list()
|
2024-08-24 01:58:03 +02:00
|
|
|
local new_list = {}
|
2024-09-01 03:06:27 +02:00
|
|
|
local owned = session.client:list_workspaces(true, false):await()
|
|
|
|
for _, ws in pairs(owned) do
|
|
|
|
table.insert(new_list, {
|
|
|
|
name = ws,
|
|
|
|
owned = true,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
local invited = session.client:list_workspaces(false, true):await()
|
|
|
|
for _, ws in pairs(invited) do
|
|
|
|
table.insert(new_list, {
|
|
|
|
name = ws,
|
|
|
|
owned = false,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
session.available = new_list
|
2024-09-14 14:24:48 +02:00
|
|
|
require('codemp.window').update()
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
|
|
|
|
2024-08-24 01:56:27 +02:00
|
|
|
---@param ws Workspace
|
|
|
|
local function register_cursor_callback(ws)
|
|
|
|
local controller = ws.cursor
|
2024-08-06 01:11:09 +02:00
|
|
|
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, {
|
2024-08-24 01:56:27 +02:00
|
|
|
group = vim.api.nvim_create_augroup("codemp-workspace-" .. ws.name, { clear = true }),
|
2024-08-06 01:11:09 +02:00
|
|
|
callback = function (_)
|
|
|
|
local cur = utils.cursor.position()
|
2024-08-08 04:34:23 +02:00
|
|
|
local buf = vim.api.nvim_get_current_buf()
|
2024-08-06 01:11:09 +02:00
|
|
|
if buffers.map[buf] ~= nil then
|
2024-08-23 00:59:13 +02:00
|
|
|
local _ = controller:send(buffers.map[buf], cur[1][1], cur[1][2], cur[2][1], cur[2][2]) -- no need to await here
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2024-08-24 01:56:27 +02:00
|
|
|
---@param ws Workspace
|
|
|
|
local function register_cursor_handler(ws)
|
|
|
|
local controller = ws.cursor
|
2024-08-16 03:49:24 +02:00
|
|
|
local async = vim.loop.new_async(vim.schedule_wrap(function ()
|
2024-08-15 03:41:28 +02:00
|
|
|
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
|
|
|
|
if user_hl[event.user] == nil then
|
|
|
|
user_hl[event.user] = {
|
|
|
|
ns = vim.api.nvim_create_namespace("codemp-cursor-" .. event.user),
|
2024-08-17 04:26:34 +02:00
|
|
|
hi = utils.color(event.user),
|
2024-08-16 03:49:24 +02:00
|
|
|
}
|
|
|
|
end
|
2024-08-17 03:28:08 +02:00
|
|
|
local old_buffer = buffers.users[event.user]
|
2024-08-17 03:15:57 +02:00
|
|
|
if old_buffer ~= nil then
|
2024-08-17 04:26:49 +02:00
|
|
|
local old_buffer_id = buffers.map_rev[old_buffer]
|
|
|
|
if old_buffer_id ~= nil then
|
|
|
|
vim.api.nvim_buf_clear_namespace(old_buffer_id, user_hl[event.user].ns, 0, -1)
|
|
|
|
end
|
2024-08-17 03:15:57 +02:00
|
|
|
end
|
2024-08-17 03:28:08 +02:00
|
|
|
buffers.users[event.user] = event.buffer
|
2024-08-17 04:26:49 +02:00
|
|
|
local buffer_id = buffers.map_rev[event.buffer]
|
|
|
|
if buffer_id ~= nil then
|
2024-08-16 03:49:24 +02:00
|
|
|
utils.multiline_highlight(
|
2024-08-17 04:26:49 +02:00
|
|
|
buffer_id,
|
2024-08-16 03:49:24 +02:00
|
|
|
user_hl[event.user].ns,
|
|
|
|
user_hl[event.user].hi,
|
|
|
|
event.start,
|
|
|
|
event.finish
|
|
|
|
)
|
|
|
|
end
|
2024-08-17 04:26:49 +02:00
|
|
|
if old_buffer ~= event.buffer then
|
2024-09-14 14:24:48 +02:00
|
|
|
require('codemp.window').update() -- redraw user positions
|
2024-08-17 03:28:34 +02:00
|
|
|
end
|
2024-08-15 03:41:28 +02:00
|
|
|
end
|
2024-08-16 03:49:24 +02:00
|
|
|
end))
|
|
|
|
controller:callback(function (_controller) async:send() end)
|
|
|
|
end
|
|
|
|
|
2024-08-23 00:59:13 +02:00
|
|
|
---@param workspace string workspace name to join
|
|
|
|
---@return Workspace
|
|
|
|
---join a workspace and register event handlers
|
2024-08-16 03:49:24 +02:00
|
|
|
local function join(workspace)
|
2024-08-24 01:56:27 +02:00
|
|
|
local ws = session.client:join_workspace(workspace):await()
|
|
|
|
print(" >< joined workspace " .. ws.name)
|
|
|
|
register_cursor_callback(ws)
|
|
|
|
register_cursor_handler(ws)
|
2024-08-15 03:41:28 +02:00
|
|
|
|
2024-08-22 22:07:00 +02:00
|
|
|
-- TODO this is temporary and ad-hoc
|
2024-09-05 04:38:12 +02:00
|
|
|
-- ws:callback(function (event)
|
|
|
|
-- if event.type == "leave" then
|
|
|
|
-- if buffers.users[event.value] ~= nil then
|
|
|
|
-- vim.schedule(function ()
|
|
|
|
-- vim.api.nvim_buf_clear_namespace(buffers.users[event.value], user_hl[event.value].ns, 0, -1)
|
|
|
|
-- buffers.users[event.value] = nil
|
|
|
|
-- user_hl[event.value] = nil
|
|
|
|
-- end)
|
|
|
|
-- end
|
|
|
|
-- end
|
2024-09-14 14:24:48 +02:00
|
|
|
-- vim.schedule(function() require('codemp.window').update() end)
|
2024-09-05 04:38:12 +02:00
|
|
|
-- end)
|
2024-08-24 01:56:27 +02:00
|
|
|
|
|
|
|
session.workspace = ws
|
2024-09-14 14:24:48 +02:00
|
|
|
require('codemp.window').update()
|
2024-08-22 22:07:00 +02:00
|
|
|
|
|
|
|
return ws
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function leave()
|
2024-08-24 01:56:27 +02:00
|
|
|
session.client:leave_workspace(session.workspace.name)
|
2024-08-06 01:11:09 +02:00
|
|
|
print(" -- left workspace")
|
2024-08-24 01:56:27 +02:00
|
|
|
session.workspace = nil
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
join = join,
|
|
|
|
leave = leave,
|
|
|
|
map = user_hl,
|
2024-08-24 01:58:03 +02:00
|
|
|
list = fetch_workspaces_list,
|
2024-08-06 01:11:09 +02:00
|
|
|
}
|