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-06 01:11:09 +02:00
|
|
|
|
2024-09-16 20:01:09 +02:00
|
|
|
---@class UserHighlight
|
|
|
|
---@field ns integer namespace to use for this user
|
2024-09-25 02:11:58 +02:00
|
|
|
---@field hi HighlightPair color for user to use
|
2024-09-26 00:22:45 +02:00
|
|
|
---@field mark integer | nil extmark id
|
2024-09-16 20:01:09 +02:00
|
|
|
---@field pos [integer, integer] cursor start position of this user
|
|
|
|
|
|
|
|
---@type table<string, UserHighlight>
|
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-10-26 18:15:30 +02:00
|
|
|
CODEMP.client:fetch_owned_workspaces():and_then(function (owned)
|
2024-09-17 16:33:22 +02:00
|
|
|
for _, ws in pairs(owned) do
|
|
|
|
table.insert(new_list, {
|
|
|
|
name = ws,
|
|
|
|
owned = true,
|
|
|
|
})
|
|
|
|
end
|
2024-10-26 18:15:30 +02:00
|
|
|
CODEMP.client:fetch_joined_workspaces():and_then(function (invited)
|
2024-09-17 16:33:22 +02:00
|
|
|
for _, ws in pairs(invited) do
|
|
|
|
table.insert(new_list, {
|
|
|
|
name = ws,
|
|
|
|
owned = false,
|
|
|
|
})
|
|
|
|
end
|
2024-09-17 17:26:23 +02:00
|
|
|
CODEMP.available = new_list
|
2024-09-17 16:33:22 +02:00
|
|
|
require('codemp.window').update()
|
|
|
|
end)
|
|
|
|
end)
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
|
|
|
|
2024-09-25 04:07:18 +02:00
|
|
|
local last_jump = { 0, 0 }
|
2024-10-03 00:13:10 +02:00
|
|
|
local workspace_callback_group = nil
|
2024-09-25 04:07:18 +02:00
|
|
|
|
2024-09-26 05:37:51 +02:00
|
|
|
---@param controller CursorController
|
|
|
|
---@param name string
|
|
|
|
local function register_cursor_callback(controller, name)
|
2024-09-16 20:14:32 +02:00
|
|
|
local once = true
|
2024-10-03 00:13:10 +02:00
|
|
|
workspace_callback_group = vim.api.nvim_create_augroup("codemp-workspace-" .. name, { clear = true })
|
2024-08-06 01:11:09 +02:00
|
|
|
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, {
|
2024-10-03 00:13:10 +02:00
|
|
|
group = workspace_callback_group,
|
2024-09-26 05:37:51 +02:00
|
|
|
callback = function (_ev)
|
2024-09-25 04:00:46 +02:00
|
|
|
if CODEMP.ignore_following_action then
|
|
|
|
CODEMP.ignore_following_action = false
|
|
|
|
return
|
|
|
|
elseif CODEMP.following ~= nil then
|
|
|
|
print(" / / unfollowing " .. CODEMP.following)
|
|
|
|
CODEMP.following = nil
|
2024-09-25 04:10:48 +02:00
|
|
|
require('codemp.window').update()
|
2024-09-25 04:00:46 +02:00
|
|
|
end
|
2024-08-06 01:11:09 +02:00
|
|
|
local cur = utils.cursor.position()
|
2024-08-08 04:34:23 +02:00
|
|
|
local buf = vim.api.nvim_get_current_buf()
|
2024-10-02 23:20:52 +02:00
|
|
|
local bufname
|
2024-08-06 01:11:09 +02:00
|
|
|
if buffers.map[buf] ~= nil then
|
2024-10-02 23:20:52 +02:00
|
|
|
bufname = buffers.map[buf]
|
2024-09-16 20:14:32 +02:00
|
|
|
once = true
|
2024-09-16 18:27:23 +02:00
|
|
|
local _ = controller:send({
|
2024-10-02 23:20:52 +02:00
|
|
|
buffer = bufname,
|
2024-10-10 13:08:02 +02:00
|
|
|
start_row = cur[1][1],
|
|
|
|
start_col = cur[1][2],
|
|
|
|
end_row = cur[2][1],
|
|
|
|
end_col = cur[2][2],
|
2024-10-26 20:22:54 +02:00
|
|
|
})
|
2024-09-16 20:14:32 +02:00
|
|
|
else -- set ourselves "away" only once
|
2024-10-02 23:20:52 +02:00
|
|
|
bufname = ""
|
2024-09-16 20:14:32 +02:00
|
|
|
if once then
|
2024-10-26 20:22:54 +02:00
|
|
|
controller:send({
|
2024-10-02 23:20:52 +02:00
|
|
|
buffer = bufname,
|
2024-10-10 13:08:02 +02:00
|
|
|
start_row = 0,
|
|
|
|
start_col = 0,
|
|
|
|
end_row = 1,
|
|
|
|
end_col = 0,
|
2024-10-26 20:22:54 +02:00
|
|
|
})
|
2024-09-16 20:14:32 +02:00
|
|
|
end
|
|
|
|
once = false
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
2024-10-26 18:15:30 +02:00
|
|
|
local oldbuf = buffers.users[CODEMP.client:current_user().name]
|
|
|
|
buffers.users[CODEMP.client:current_user().name] = bufname
|
2024-10-02 23:23:53 +02:00
|
|
|
if oldbuf ~= bufname then
|
|
|
|
require('codemp.window').update()
|
|
|
|
end
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2024-09-26 05:37:51 +02:00
|
|
|
---@param controller CursorController
|
|
|
|
local function register_cursor_handler(controller)
|
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
|
2024-09-16 20:01:09 +02:00
|
|
|
local user = event.user -- do it on separate line so language server understands that it wont be nil
|
|
|
|
if user ~= nil then
|
|
|
|
if user_hl[user] == nil then
|
|
|
|
user_hl[user] = {
|
|
|
|
ns = vim.api.nvim_create_namespace("codemp-cursor-" .. event.user),
|
|
|
|
hi = utils.color(event.user),
|
2024-09-25 03:11:42 +02:00
|
|
|
mark = nil,
|
2024-09-16 20:01:09 +02:00
|
|
|
pos = { 0, 0 },
|
|
|
|
}
|
|
|
|
end
|
2024-10-10 13:08:02 +02:00
|
|
|
user_hl[user].pos = { event.sel.start_row, event.sel.start_col }
|
2024-09-16 20:01:09 +02:00
|
|
|
local old_buffer = buffers.users[event.user]
|
|
|
|
if old_buffer ~= nil then
|
|
|
|
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
|
|
|
|
end
|
2024-10-10 13:08:02 +02:00
|
|
|
buffers.users[event.user] = event.sel.buffer
|
|
|
|
local buffer_id = buffers.map_rev[event.sel.buffer]
|
2024-09-16 20:01:09 +02:00
|
|
|
if buffer_id ~= nil then
|
2024-09-25 03:05:24 +02:00
|
|
|
local hi = user_hl[event.user].hi
|
2024-10-10 13:08:02 +02:00
|
|
|
local event_finish_2 = event.sel.end_col -- TODO can't set the tuple field? need to copy out
|
|
|
|
if event.sel.start_row == event.sel.end_row and event.sel.start_col == event.sel.end_col then
|
2024-09-28 18:15:23 +02:00
|
|
|
-- vim can't draw 0-width cursors, so we always expand them to at least 1 width
|
2024-10-10 13:08:02 +02:00
|
|
|
event_finish_2 = event.sel.end_col + 1
|
2024-09-28 18:15:23 +02:00
|
|
|
end
|
2024-09-25 03:11:42 +02:00
|
|
|
user_hl[event.user].mark = vim.api.nvim_buf_set_extmark(
|
2024-09-25 00:36:08 +02:00
|
|
|
buffer_id,
|
|
|
|
user_hl[event.user].ns,
|
2024-10-10 13:08:02 +02:00
|
|
|
event.sel.start_row,
|
|
|
|
event.sel.start_col,
|
2024-09-25 00:36:08 +02:00
|
|
|
{
|
2024-09-25 03:11:42 +02:00
|
|
|
id = user_hl[event.user].mark,
|
2024-10-10 13:08:02 +02:00
|
|
|
end_row = event.sel.end_row,
|
2024-10-02 02:14:41 +02:00
|
|
|
end_col = event_finish_2,
|
2024-09-25 03:05:24 +02:00
|
|
|
hl_group = hi.bg,
|
2024-09-25 02:11:58 +02:00
|
|
|
virt_text_pos = "right_align",
|
|
|
|
sign_text = string.sub(event.user, 0, 1),
|
2024-09-25 03:05:24 +02:00
|
|
|
sign_hl_group = hi.bg,
|
|
|
|
virt_text_repeat_linebreak = true,
|
|
|
|
priority = 1000,
|
2024-09-25 02:11:58 +02:00
|
|
|
strict = false,
|
2024-09-25 00:36:08 +02:00
|
|
|
virt_text = {
|
2024-09-27 20:42:32 +02:00
|
|
|
{ " " .. event.user .. " ", hi.fg },
|
2024-09-25 03:05:24 +02:00
|
|
|
{ " ", hi.bg },
|
2024-09-25 00:36:08 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2024-09-16 20:01:09 +02:00
|
|
|
end
|
2024-10-10 13:08:02 +02:00
|
|
|
if old_buffer ~= event.sel.buffer then
|
2024-09-16 20:01:09 +02:00
|
|
|
require('codemp.window').update() -- redraw user positions
|
2024-08-17 04:26:49 +02:00
|
|
|
end
|
2024-09-25 04:00:46 +02:00
|
|
|
if CODEMP.following ~= nil and CODEMP.following == event.user then
|
2024-10-10 13:08:02 +02:00
|
|
|
local buf_id = buffers.map_rev[event.sel.buffer]
|
2024-09-25 04:00:46 +02:00
|
|
|
if buf_id ~= nil then
|
|
|
|
local win = vim.api.nvim_get_current_win()
|
|
|
|
local curr_buf = vim.api.nvim_get_current_buf()
|
|
|
|
CODEMP.ignore_following_action = true
|
|
|
|
if curr_buf ~= buf_id then
|
|
|
|
vim.api.nvim_win_set_buf(win, buf_id)
|
|
|
|
end
|
2024-09-25 04:07:18 +02:00
|
|
|
-- keep centered the cursor end that is currently being moved, but prefer start
|
2024-10-10 13:08:02 +02:00
|
|
|
if event.sel.start_row == last_jump[1] and event.sel.start_col == last_jump[2] then
|
|
|
|
vim.api.nvim_win_set_cursor(win, { event.sel.end_row + 1, event.sel.end_col })
|
2024-09-25 04:07:18 +02:00
|
|
|
else
|
2024-10-10 13:08:02 +02:00
|
|
|
vim.api.nvim_win_set_cursor(win, { event.sel.start_row + 1, event.sel.start_col })
|
2024-09-25 04:07:18 +02:00
|
|
|
end
|
2024-10-10 13:08:02 +02:00
|
|
|
last_jump = { event.sel.start_row, event.sel.start_col }
|
2024-09-25 04:00:46 +02:00
|
|
|
end
|
|
|
|
end
|
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-09-26 05:40:58 +02:00
|
|
|
local events_poller = nil
|
|
|
|
|
2024-08-23 00:59:13 +02:00
|
|
|
---@param workspace string workspace name to join
|
|
|
|
---join a workspace and register event handlers
|
2024-08-16 03:49:24 +02:00
|
|
|
local function join(workspace)
|
2024-09-17 17:58:32 +02:00
|
|
|
print(" <> joining workspace " .. workspace .. " ...")
|
2024-10-26 18:15:30 +02:00
|
|
|
CODEMP.client:attach_workspace(workspace):and_then(function (ws)
|
|
|
|
print(" >< joined workspace " .. ws:id())
|
|
|
|
register_cursor_callback(ws:cursor(), ws:id())
|
|
|
|
register_cursor_handler(ws:cursor())
|
2024-09-17 18:10:07 +02:00
|
|
|
CODEMP.workspace = ws
|
2024-09-26 00:22:45 +02:00
|
|
|
for _, user in pairs(CODEMP.workspace:user_list()) do
|
2024-10-26 20:23:57 +02:00
|
|
|
buffers.users[user.name] = ""
|
2024-10-27 11:51:50 +01:00
|
|
|
user_hl[user.name] = {
|
2024-10-26 18:30:58 +02:00
|
|
|
ns = vim.api.nvim_create_namespace("codemp-cursor-" .. user.name),
|
2024-10-26 18:15:30 +02:00
|
|
|
hi = utils.color(user.name),
|
2024-09-26 00:22:45 +02:00
|
|
|
pos = { 0, 0 },
|
|
|
|
mark = nil,
|
|
|
|
}
|
|
|
|
end
|
2024-09-17 18:10:07 +02:00
|
|
|
require('codemp.window').update()
|
2024-10-26 20:23:57 +02:00
|
|
|
local async = vim.uv.new_async(function ()
|
|
|
|
while true do
|
|
|
|
local event = ws:try_recv():await()
|
|
|
|
if event == nil then break end
|
2024-09-17 16:33:22 +02:00
|
|
|
if event.type == "leave" then
|
|
|
|
if buffers.users[event.value] ~= nil then
|
|
|
|
local buf_name = buffers.users[event.value]
|
|
|
|
local buf_id = buffers.map_rev[buf_name]
|
|
|
|
if buf_id ~= nil then
|
|
|
|
vim.api.nvim_buf_clear_namespace(buf_id, user_hl[event.value].ns, 0, -1)
|
|
|
|
end
|
|
|
|
buffers.users[event.value] = nil
|
|
|
|
user_hl[event.value] = nil
|
2024-09-17 01:55:43 +02:00
|
|
|
end
|
2024-09-17 16:33:22 +02:00
|
|
|
elseif event.type == "join" then
|
|
|
|
buffers.users[event.value] = ""
|
|
|
|
user_hl[event.value] = {
|
|
|
|
ns = vim.api.nvim_create_namespace("codemp-cursor-" .. event.value),
|
|
|
|
hi = utils.color(event.value),
|
|
|
|
pos = { 0, 0 },
|
2024-09-26 00:22:45 +02:00
|
|
|
mark = nil,
|
2024-09-17 16:33:22 +02:00
|
|
|
}
|
2024-09-16 20:01:09 +02:00
|
|
|
end
|
|
|
|
end
|
2024-10-26 20:23:57 +02:00
|
|
|
require('codemp.window').update()
|
|
|
|
end)
|
|
|
|
ws:callback(function(_) async:send() end)
|
2024-09-17 16:33:22 +02:00
|
|
|
end)
|
2024-08-06 01:11:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function leave()
|
2024-10-26 18:15:30 +02:00
|
|
|
local ws_name = CODEMP.workspace:id()
|
|
|
|
CODEMP.workspace:cursor():clear_callback()
|
2024-10-03 00:13:10 +02:00
|
|
|
vim.api.nvim_clear_autocmds({ group = workspace_callback_group })
|
|
|
|
for id, name in pairs(buffers.map) do
|
|
|
|
CODEMP.workspace:get_buffer(name):clear_callback()
|
|
|
|
buffers.map[id] = nil
|
|
|
|
buffers.map_rev[name] = nil
|
|
|
|
end
|
|
|
|
for user, _buf in pairs(buffers.users) do
|
|
|
|
buffers.users[user] = nil
|
|
|
|
end
|
2024-09-17 17:26:23 +02:00
|
|
|
CODEMP.workspace = nil
|
2024-09-26 05:40:58 +02:00
|
|
|
if events_poller ~= nil then
|
|
|
|
events_poller:stop()
|
|
|
|
events_poller = nil
|
|
|
|
end
|
2024-10-03 00:13:10 +02:00
|
|
|
if not CODEMP.client:leave_workspace(ws_name) then
|
2024-09-26 05:40:58 +02:00
|
|
|
collectgarbage("collect")
|
|
|
|
-- TODO codemp disconnects when all references to its objects are dropped. since it
|
|
|
|
-- hands out Arc<> of things, all references still not garbage collected in Lua will
|
|
|
|
-- prevent it from disconnecting. while running a full cycle may be a bit slow, this
|
|
|
|
-- only happens when manually requested, and it's not like the extra garbage collection
|
|
|
|
-- is an effort for nothing... still it would be more elegant to not need this!!
|
|
|
|
end
|
2024-10-03 00:13:10 +02:00
|
|
|
print(" -- left workspace " .. ws_name)
|
2024-09-17 18:43:45 +02:00
|
|
|
require('codemp.window').update()
|
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
|
|
|
}
|