diff --git a/lua/codemp/buffers.lua b/lua/codemp/buffers.lua index bfb985b..fbdb506 100644 --- a/lua/codemp/buffers.lua +++ b/lua/codemp/buffers.lua @@ -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 diff --git a/lua/codemp/init.lua b/lua/codemp/init.lua index 1496212..44935dc 100644 --- a/lua/codemp/init.lua +++ b/lua/codemp/init.lua @@ -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, diff --git a/lua/codemp/neo-tree/commands.lua b/lua/codemp/neo-tree/commands.lua index e549ee7..0a3a1d0 100644 --- a/lua/codemp/neo-tree/commands.lua +++ b/lua/codemp/neo-tree/commands.lua @@ -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 diff --git a/lua/codemp/workspace.lua b/lua/codemp/workspace.lua index 2ed9882..0d06492 100644 --- a/lua/codemp/workspace.lua +++ b/lua/codemp/workspace.lua @@ -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))