From c92b1799b62d5015afc15cc272d377df48f337c3 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 26 Oct 2024 18:15:30 +0200 Subject: [PATCH] chore: updated to codemp 0.8 --- build.lua | 4 ++-- lua/codemp/buffers.lua | 14 +++++++------- lua/codemp/command.lua | 20 ++++++++++---------- lua/codemp/init.lua | 2 +- lua/codemp/neo-tree/bridge.lua | 12 ++++++------ lua/codemp/neo-tree/commands.lua | 14 ++++++++------ lua/codemp/utils.lua | 2 +- lua/codemp/window.lua | 4 ++-- lua/codemp/workspace.lua | 24 ++++++++++++------------ 9 files changed, 49 insertions(+), 47 deletions(-) diff --git a/build.lua b/build.lua index 5695aff..50ffcc4 100644 --- a/build.lua +++ b/build.lua @@ -8,6 +8,8 @@ -- `native.(so|dll|dylib)` file in this plugin folder, next to -- the `loader.lua` file. +local version = "v0.8.0" + local plugin_dir = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h") -- got this from https://lazy.folke.io/developers#building @@ -43,8 +45,6 @@ if os_uname.sysname == "Windows_NT" then sep = '\\' end local new_ext = ext if os_uname.sysname == "Darwin" then new_ext = "so" end -local version = "v0.7.3" - local native_path = plugin_dir..sep.."lua"..sep.."codemp"..sep.."new-native."..new_ext local replace_native_path = plugin_dir..sep.."lua"..sep.."codemp"..sep.."native."..new_ext local download_url_native = string.format("https://codemp.dev/releases/lua/codemp-lua-%s-%s-%s.%s", version, arch, platform, ext) diff --git a/lua/codemp/buffers.lua b/lua/codemp/buffers.lua index 2050bd3..05cacfb 100644 --- a/lua/codemp/buffers.lua +++ b/lua/codemp/buffers.lua @@ -15,7 +15,7 @@ local function detach(name) id_buffer_map[buffer] = nil buffer_id_map[name] = nil CODEMP.workspace:get_buffer(name):clear_callback() - if not CODEMP.workspace:detach(name) then + if not CODEMP.workspace:detach_buffer(name) then collectgarbage("collect") -- clear dangling references end @@ -58,7 +58,7 @@ local function attach(name, opts) vim.api.nvim_buf_set_name(buffer, name) - CODEMP.workspace:attach(name):and_then(function (controller) + CODEMP.workspace:attach_buffer(name):and_then(function (controller) vim.schedule(function() -- TODO disgusting! but poll blocks forever on empty buffers... if not opts.nowait then @@ -124,7 +124,7 @@ local function attach(name, opts) print(string.format("sending: %s..%s '%s'", start_offset, start_offset + old_end_byte_len, change_content)) end controller:send({ - start = start_offset, finish = end_offset, content = change_content + start_idx = start_offset, end_idx = end_offset, content = change_content }):await() end, }) @@ -139,9 +139,9 @@ local function attach(name, opts) ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer) CODEMP.ignore_following_action = true if CODEMP.config.debug then - print(" ~~ applying change ~~ " .. event.change.start .. ".." .. event.change.finish .. "::[" .. event.change.content .. "]") + print(" ~~ applying change ~~ " .. event.change.start_idx .. ".." .. event.change.end_idx .. "::[" .. event.change.content .. "]") end - utils.buffer.set_content(buffer, event.change.content, event.change.start, event.change.finish) + utils.buffer.set_content(buffer, event.change.content, event.change.start_idx, event.change.end_idx) if event.hash ~= nil then if CODEMP.native.hash(utils.buffer.get_content(buffer)) ~= event.hash then if CODEMP.config.auto_sync then @@ -173,7 +173,7 @@ local function attach(name, opts) if opts.content ~= nil then -- TODO this may happen too soon!! local _ = controller:send({ - start = 0, finish = #remote_content, content = opts.content + start_idx = 0, end_idx = #remote_content, content = opts.content }) -- no need to await else local current_content = utils.buffer.get_content(buffer) @@ -227,7 +227,7 @@ local function create(buffer) if CODEMP.workspace == nil then error("join a workspace first") end - CODEMP.workspace:create(buffer):and_then(function () + CODEMP.workspace:create_buffer(buffer):and_then(function () print(" ++ created buffer " .. buffer) require('codemp.window').update() end) diff --git a/lua/codemp/command.lua b/lua/codemp/command.lua index aa90596..53762a6 100644 --- a/lua/codemp/command.lua +++ b/lua/codemp/command.lua @@ -31,7 +31,7 @@ local base_actions = { -- only available if state.client is not nil local connected_actions = { id = function() - print("> codemp::" .. CODEMP.client.id) + print("> codemp::" .. CODEMP.client:current_user().id) end, join = function(ws) @@ -56,11 +56,11 @@ local connected_actions = { available = function() CODEMP.available = {} - for _, ws in ipairs(CODEMP.client:list_workspaces(true, false):await()) do + for _, ws in ipairs(CODEMP.client:fetch_owned_workspaces():await()) do print(" ++ " .. ws) table.insert(CODEMP.available, ws) end - for _, ws in ipairs(CODEMP.client:list_workspaces(false, true):await()) do + for _, ws in ipairs(CODEMP.client:fetch_joined_workspaces():await()) do print(" -- " .. ws) table.insert(CODEMP.available, ws) end @@ -70,7 +70,7 @@ local connected_actions = { invite = function(user) local ws if CODEMP.workspace ~= nil then - ws = CODEMP.workspace.name + ws = CODEMP.workspace:id() else ws = vim.fn.input("workspace > ", "") end @@ -81,10 +81,10 @@ local connected_actions = { disconnect = function() if CODEMP.workspace ~= nil then - print(" xx leaving workspace " .. CODEMP.workspace.name) + print(" xx leaving workspace " .. CODEMP.workspace:id()) workspace.leave() end - print(" xx disconnecting client " .. CODEMP.client.id) + print(" xx disconnecting client " .. CODEMP.client:current_user().id) CODEMP.client = nil -- should drop and thus close everything collectgarbage("collect") -- make sure we drop require('codemp.window').update() @@ -118,13 +118,13 @@ local joined_actions = { delete = function(path) if path == nil then error("missing buffer name") end - CODEMP.workspace:delete(path):and_then(function() + CODEMP.workspace:delete_buffer(path):and_then(function() print(" xx deleted buffer " .. path) end) end, buffers = function() - for _, buf in ipairs(CODEMP.workspace:filetree()) do + for _, buf in ipairs(CODEMP.workspace:search_buffers()) do if buffers.map_rev[buf] ~= nil then print(" +- " .. buf) else @@ -149,7 +149,7 @@ local joined_actions = { buffers.attach(p, { buffer = buffer }) end if path == nil then - local filetree = CODEMP.workspace:filetree(nil, false) + local filetree = CODEMP.workspace:search_buffers() return vim.ui.select(filetree, { prompt = "Select buffer to attach to:" }, function (choice) if choice == nil then return end -- action canceled by user doit(filetree[choice]) @@ -233,7 +233,7 @@ vim.api.nvim_create_user_command( if CODEMP.client ~= nil and CODEMP.workspace ~= nil then local choices if last_arg == "attach" then - choices = CODEMP.workspace:filetree() + choices = CODEMP.workspace:search_buffers() elseif last_arg == "detach" then choices = CODEMP.workspace.active_buffers end diff --git a/lua/codemp/init.lua b/lua/codemp/init.lua index a29d95f..bb2bba2 100644 --- a/lua/codemp/init.lua +++ b/lua/codemp/init.lua @@ -38,7 +38,7 @@ if CODEMP == nil then { callback = function (_ev) if CODEMP.workspace ~= nil then - print(" xx leaving workspace " .. CODEMP.workspace.name) + print(" xx leaving workspace " .. CODEMP.workspace:id()) require('codemp.workspace').leave() end if CODEMP.client ~= nil then diff --git a/lua/codemp/neo-tree/bridge.lua b/lua/codemp/neo-tree/bridge.lua index 2a08a4e..ee8b9f2 100644 --- a/lua/codemp/neo-tree/bridge.lua +++ b/lua/codemp/neo-tree/bridge.lua @@ -130,16 +130,16 @@ M.update_state = function(state) state.default_expanded_nodes = {} if CODEMP.workspace ~= nil then - local ws_section = new_root("#" .. CODEMP.workspace.name) + local ws_section = new_root("#" .. CODEMP.workspace:id()) table.insert(state.default_expanded_nodes, ws_section.id) - for i, path in ipairs(CODEMP.workspace:filetree()) do - table.insert(ws_section.children, new_item(CODEMP.workspace.name, path)) + for i, path in ipairs(CODEMP.workspace:search_buffers()) do + table.insert(ws_section.children, new_item(CODEMP.workspace:id(), path)) end local usr_section = new_root("users") table.insert(state.default_expanded_nodes, usr_section.id) for user, buffer in pairs(buf_manager.users) do - table.insert(usr_section.children, new_user(CODEMP.workspace.name, user)) + table.insert(usr_section.children, new_user(CODEMP.workspace:id(), user)) end table.insert(ws_section.children, spacer()) table.insert(ws_section.children, usr_section) @@ -159,8 +159,8 @@ M.update_state = function(state) local status_section = new_root("client") table.insert(state.default_expanded_nodes, status_section.id) - table.insert(status_section.children, new_entry("id", CODEMP.client.id)) - table.insert(status_section.children, new_entry("name", CODEMP.client.username)) + table.insert(status_section.children, new_entry("id", CODEMP.client:current_user().id)) + table.insert(status_section.children, new_entry("name", CODEMP.client:current_user().name)) table.insert(root, spacer()) table.insert(root, status_section) diff --git a/lua/codemp/neo-tree/commands.lua b/lua/codemp/neo-tree/commands.lua index bb6708c..2220d8c 100644 --- a/lua/codemp/neo-tree/commands.lua +++ b/lua/codemp/neo-tree/commands.lua @@ -38,7 +38,7 @@ M.open = function(state, path, extra) return end if selected.type == "workspace" then - if CODEMP.workspace ~= nil and CODEMP.workspace.name ~= selected.name then + if CODEMP.workspace ~= nil and CODEMP.workspace:id() ~= selected.name then error("must leave current workspace first") end if CODEMP.workspace == nil then @@ -62,10 +62,12 @@ M.open = function(state, path, extra) local usr = ws_manager.map[selected.name] print(" /\\/ following " .. selected.name) CODEMP.following = selected.name - local _ = CODEMP.workspace.cursor:send({ + local _ = CODEMP.workspace:cursor():send({ buffer = "", - start = { 0, 0 }, - finish = { 0, 0 }, + start_row = 0, + start_col = 0, + end_row = 0, + end_col = 0, }) -- clear current cursor if usr ~= nil then local buf_name = buf_manager.users[selected.name] @@ -133,7 +135,7 @@ M.delete = function(state, path, extra) if buf_manager.map_rev[selected.name] ~= nil then buf_manager.detach(selected.name) end - CODEMP.workspace:delete(selected.name):and_then(function () + CODEMP.workspace:delete_buffer(selected.name):and_then(function () print("deleted buffer " .. selected.name) manager.refresh("codemp") end) @@ -156,7 +158,7 @@ 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 - CODEMP.workspace:create(input):and_then(function () + CODEMP.workspace:create_buffer(input):and_then(function () manager.refresh("codemp") end) end) diff --git a/lua/codemp/utils.lua b/lua/codemp/utils.lua index 93a86ed..eff5939 100644 --- a/lua/codemp/utils.lua +++ b/lua/codemp/utils.lua @@ -225,7 +225,7 @@ return { get_content = buffer_get_content, set_content = buffer_set_content, }, - available_colors = available_colors, + available_colors = colors, color = color, poller = async_poller, sep = separator, diff --git a/lua/codemp/window.lua b/lua/codemp/window.lua index adb5dd5..3102ef9 100644 --- a/lua/codemp/window.lua +++ b/lua/codemp/window.lua @@ -40,10 +40,10 @@ local function update_window() local buffer_to_row = {} local user_to_row = {} local off = {} - local tree = CODEMP.workspace:filetree() + local tree = CODEMP.workspace:search_buffers() vim.api.nvim_set_option_value('modifiable', true, { buf = buffer_id }) local tmp = ">| codemp\n" - tmp = tmp .. " |: " .. CODEMP.workspace.name .. "\n" + tmp = tmp .. " |: " .. CODEMP.workspace:id() .. "\n" tmp = tmp .. " |\n" local base_row = 3 for n, path in pairs(tree) do diff --git a/lua/codemp/workspace.lua b/lua/codemp/workspace.lua index b565c73..fdc5a18 100644 --- a/lua/codemp/workspace.lua +++ b/lua/codemp/workspace.lua @@ -12,14 +12,14 @@ local user_hl = {} local function fetch_workspaces_list() local new_list = {} - CODEMP.client:list_workspaces(true, false):and_then(function (owned) + CODEMP.client:fetch_owned_workspaces():and_then(function (owned) for _, ws in pairs(owned) do table.insert(new_list, { name = ws, owned = true, }) end - CODEMP.client:list_workspaces(false, true):and_then(function (invited) + CODEMP.client:fetch_joined_workspaces():and_then(function (invited) for _, ws in pairs(invited) do table.insert(new_list, { name = ws, @@ -77,8 +77,8 @@ local function register_cursor_callback(controller, name) end once = false end - local oldbuf = buffers.users[CODEMP.client.username] - buffers.users[CODEMP.client.username] = bufname + local oldbuf = buffers.users[CODEMP.client:current_user().name] + buffers.users[CODEMP.client:current_user().name] = bufname if oldbuf ~= bufname then require('codemp.window').update() end @@ -175,22 +175,22 @@ local events_poller = nil ---join a workspace and register event handlers local function join(workspace) print(" <> joining workspace " .. workspace .. " ...") - CODEMP.client:join_workspace(workspace):and_then(function (ws) - print(" >< joined workspace " .. ws.name) - register_cursor_callback(ws.cursor, ws.name) - register_cursor_handler(ws.cursor) + 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()) CODEMP.workspace = ws for _, user in pairs(CODEMP.workspace:user_list()) do buffers.users[user] = "" user_hl[user] = { ns = vim.api.nvim_create_namespace("codemp-cursor-" .. user), - hi = utils.color(user), + hi = utils.color(user.name), pos = { 0, 0 }, mark = nil, } end require('codemp.window').update() - local ws_name = ws.name + local ws_name = ws:id() events_poller = utils.poller( function() if CODEMP.client == nil then return nil end @@ -225,8 +225,8 @@ local function join(workspace) end local function leave() - local ws_name = CODEMP.workspace.name - CODEMP.workspace.cursor:clear_callback() + local ws_name = CODEMP.workspace:id() + CODEMP.workspace:cursor():clear_callback() vim.api.nvim_clear_autocmds({ group = workspace_callback_group }) for id, name in pairs(buffers.map) do CODEMP.workspace:get_buffer(name):clear_callback()