feat: follow others

This commit is contained in:
əlemi 2024-09-25 04:00:46 +02:00
parent c5dc023bc7
commit 8a93fb5dc2
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 34 additions and 3 deletions

View file

@ -92,6 +92,7 @@ local function attach(name, buffer, content, nowait)
local event = controller:try_recv():await()
if event == nil then break end
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
CODEMP.ignore_following_action = true
if CODEMP.config.debug then
print(" ~~ applying change ~~ " .. event.start .. ".." .. event.finish .. "::[" .. event.content .. "]")
end

View file

@ -10,12 +10,16 @@ if CODEMP == nil then
---@field available WorkspaceReference[] available workspaces to connect to
---@field timer? any libuv timer
---@field config Config codemp configuration
---@field following string | nil
---@field ignore_following_action boolean TODO a more elegant solution?
---@field setup fun(opts: Config): nil update config and setup plugin
CODEMP = {
rt = nil,
native = nil,
timer = nil,
available = {},
following = nil,
ignore_following_action = false,
config = {
neo_tree = false,
timer_interval = 20,

View file

@ -53,17 +53,24 @@ M.open = function(state, path, extra)
return
end
if selected.type == "user" then
if CODEMP.workspace == nil then error("cannot follow while not in a workspace") end
local usr = ws_manager.map[selected.name]
print(" /\\/ following " .. selected.name)
CODEMP.following = selected.name
local _ = CODEMP.workspace.cursor:send({
buffer = "",
start = { 0, 0 },
finish = { 0, 0 },
}) -- clear current cursor
if usr ~= nil then
local buf_name = buf_manager.users[selected.name]
local buf_id = buf_manager.map_rev[buf_name]
if buf_id ~= nil then
local win = utils.get_appropriate_window(state)
CODEMP.ignore_following_action = true
vim.api.nvim_set_current_win(win)
vim.api.nvim_win_set_buf(win, buf_id)
vim.api.nvim_win_set_cursor(win, { usr.pos[1] + 1, usr.pos[2] })
else
print(" /!\\ not attached to buffer '" .. buf_name .. "'")
end
end
return

View file

@ -38,7 +38,14 @@ local function register_cursor_callback(ws)
local once = true
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, {
group = vim.api.nvim_create_augroup("codemp-workspace-" .. ws.name, { clear = true }),
callback = function (_)
callback = function (ev)
if CODEMP.ignore_following_action then
CODEMP.ignore_following_action = false
return
elseif CODEMP.following ~= nil then
print(" / / unfollowing " .. CODEMP.following)
CODEMP.following = nil
end
local cur = utils.cursor.position()
local buf = vim.api.nvim_get_current_buf()
if buffers.map[buf] ~= nil then
@ -117,6 +124,18 @@ local function register_cursor_handler(ws)
if old_buffer ~= event.buffer then
require('codemp.window').update() -- redraw user positions
end
if CODEMP.following ~= nil and CODEMP.following == event.user then
local buf_id = buffers.map_rev[event.buffer]
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
vim.api.nvim_win_set_cursor(win, { event.start[1] + 1, event.start[2] })
end
end
end
end
end))