diff --git a/src/buffers.lua b/src/buffers.lua index 5fa8e48..2104b3a 100644 --- a/src/buffers.lua +++ b/src/buffers.lua @@ -1,5 +1,5 @@ local utils = require('codemp.utils') -local state = require('codemp.state') +local session = require('codemp.session') local id_buffer_map = {} local buffer_id_map = {} @@ -32,7 +32,7 @@ local function attach(name, current, content) vim.api.nvim_buf_set_name(buffer, "codemp::" .. name) vim.api.nvim_set_current_buf(buffer) end - local controller = state.workspace:attach_buffer(name):await() + local controller = session.workspace:attach_buffer(name):await() -- TODO map name to uuid @@ -102,7 +102,7 @@ local function detach(name) local buffer = buffer_id_map[name] id_buffer_map[buffer] = nil buffer_id_map[name] = nil - state.workspace:detach_buffer(name) + session.workspace:detach_buffer(name) vim.api.nvim_buf_delete(buffer, {}) print(" -- detached from buffer " .. name) @@ -112,7 +112,7 @@ local function sync() local buffer = vim.api.nvim_get_current_buf() local name = id_buffer_map[buffer] if name ~= nil then - local controller = state.workspace:get_buffer(name) + local controller = session.workspace:get_buffer(name) if controller ~= nil then ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer) utils.buffer.set_content(buffer, controller:content():await()) diff --git a/src/command.lua b/src/command.lua index 4c61a90..8fff8da 100644 --- a/src/command.lua +++ b/src/command.lua @@ -1,4 +1,4 @@ -local state = require('codemp.state') +local session = require('codemp.session') local buffers = require('codemp.buffers') local workspace = require('codemp.workspace') local utils = require('codemp.utils') @@ -36,7 +36,7 @@ local base_actions = { -- only available if state.client is not nil local connected_actions = { id = function() - print("> codemp::" .. state.client.id) + print("> codemp::" .. session.client.id) end, toggle = function() @@ -45,39 +45,38 @@ local connected_actions = { join = function(ws) if ws == nil then error("missing workspace name") end - state.workspace = workspace.join(ws) - print(" >< joined workspace " .. ws) + workspace.join(ws) end, start = function(ws) if ws == nil then error("missing workspace name") end - state.client:create_workspace(ws):await() + session.client:create_workspace(ws):await() print(" <> created workspace " .. ws) end, available = function() - for _, ws in ipairs(state.client:list_workspaces(true, false):await()) do + for _, ws in ipairs(session.client:list_workspaces(true, false):await()) do print(" ++ " .. ws) end - for _, ws in ipairs(state.client:list_workspaces(false, true):await()) do + for _, ws in ipairs(session.client:list_workspaces(false, true):await()) do print(" -- " .. ws) end end, invite = function(user) local ws - if state.workspace ~= nil then - ws = state.workspace + if session.workspace ~= nil then + ws = session.workspace else ws = vim.fn.input("workspace > ", "") end - state.client:invite_to_workspace(ws, user):await() + session.client:invite_to_workspace(ws, user):await() print(" ][ invited " .. user .. " to workspace " .. ws) end, disconnect = function() - print(" xx disconnecting client " .. state.client.id) - state.client = nil -- should drop and thus close everything + print(" xx disconnecting client " .. session.client.id) + session.client = nil -- should drop and thus close everything end, } @@ -120,7 +119,7 @@ local joined_actions = { attach = function(path, bang) if path == nil then error("missing buffer name") end - buffers.attach(path, bang) + buffers.attach(path) end, detach = function(path) @@ -131,7 +130,7 @@ local joined_actions = { leave = function(ws) if ws == nil then error("missing workspace to leave") end - state.client:leave_workspace(ws) + workspace.leave() end, } @@ -145,11 +144,11 @@ vim.api.nvim_create_user_command( fn = base_actions[action] end - if state.client ~= nil and connected_actions[action] ~= nil then + if session.client ~= nil and connected_actions[action] ~= nil then fn = connected_actions[action] end - if state.workspace ~= nil and joined_actions[action] ~= nil then + if session.workspace ~= nil and joined_actions[action] ~= nil then fn = joined_actions[action] end @@ -175,13 +174,13 @@ vim.api.nvim_create_user_command( n = n + 1 suggestions[n] = sugg end - if state.client ~= nil then + if session.client ~= nil then for sugg, _ in pairs(connected_actions) do n = n + 1 suggestions[n] = sugg end end - if state.workspace ~= nil then + if session.workspace ~= nil then for sugg, _ in pairs(joined_actions) do n = n + 1 suggestions[n] = sugg @@ -190,9 +189,9 @@ vim.api.nvim_create_user_command( return filter(lead, suggestions) elseif stage == 3 then if args[#args-1] == 'attach' or args[#args-1] == 'detach' then - if state.client ~= nil and state.workspace ~= nil then - if state.workspace ~= nil then - return filter(lead, state.workspace:filetree()) + if session.client ~= nil and session.workspace ~= nil then + if session.workspace ~= nil then + return filter(lead, session.workspace:filetree()) end end end diff --git a/src/init.lua b/src/init.lua index 118c27c..3deb4e7 100644 --- a/src/init.lua +++ b/src/init.lua @@ -24,7 +24,7 @@ end -- end local native = require('codemp.loader').load() -- make sure we can load the native library correctly, otherwise no point going forward -local state = require('codemp.state') +local session = require('codemp.session') local rt = native.spawn_runtime_driver() -- spawn thread to drive tokio runtime --native.logger(function (msg) -- vim.schedule(function () print(msg) end) @@ -34,9 +34,9 @@ vim.api.nvim_create_autocmd( {"ExitPre"}, { callback = function (_ev) - if state.client ~= nil then + if session.client ~= nil then print(" xx disconnecting codemp client") - state.client = nil + session.client = nil end end } @@ -46,7 +46,7 @@ require('codemp.command') return { native = native, - state = require('codemp.state'), + session = session, buffers = require('codemp.buffers'), workspace = require('codemp.workspace'), window = require('codemp.window'), diff --git a/src/session.lua b/src/session.lua new file mode 100644 index 0000000..caab786 --- /dev/null +++ b/src/session.lua @@ -0,0 +1,18 @@ +---@type Workspace +local workspace + +---@type Client +local client + +---@class WorkspaceReference +---@field name string +---@field owned boolean + +---@type WorkspaceReference[] +local available_workspaces = {} + +return { + workspace = workspace, + client = client, + available = available_workspaces, +} diff --git a/src/state.lua b/src/state.lua deleted file mode 100644 index fe374a1..0000000 --- a/src/state.lua +++ /dev/null @@ -1,10 +0,0 @@ ----@type Workspace -local workspace = nil - ----@type Client -local client = nil - -return { - workspace = workspace, - client = client, -} diff --git a/src/window.lua b/src/window.lua index 1780332..f305574 100644 --- a/src/window.lua +++ b/src/window.lua @@ -1,4 +1,4 @@ -local state = require('codemp.state') +local session = require('codemp.session') local utils = require('codemp.utils') local buffers = require('codemp.buffers') @@ -30,10 +30,10 @@ local function update_window() local buffer_to_row = {} local user_to_row = {} local off = {} - local tree = state.workspace:filetree() + local tree = session.workspace:filetree() vim.api.nvim_set_option_value('modifiable', true, { buf = buffer_id }) local tmp = ">| codemp\n" - tmp = tmp .. " |: " .. state.workspace .. "\n" + tmp = tmp .. " |: " .. session.workspace.name .. "\n" tmp = tmp .. " |\n" local base_row = 3 for n, path in pairs(tree) do diff --git a/src/workspace.lua b/src/workspace.lua index da5a554..9f79479 100644 --- a/src/workspace.lua +++ b/src/workspace.lua @@ -1,14 +1,15 @@ local utils = require('codemp.utils') local buffers = require('codemp.buffers') -local state = require('codemp.state') +local session = require('codemp.session') local window = require('codemp.window') local user_hl = {} ----@param controller CursorController -local function register_cursor_callback(controller) +---@param ws Workspace +local function register_cursor_callback(ws) + local controller = ws.cursor vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, { - group = vim.api.nvim_create_augroup("codemp-workspace-" .. state.workspace, { clear = true }), + group = vim.api.nvim_create_augroup("codemp-workspace-" .. ws.name, { clear = true }), callback = function (_) local cur = utils.cursor.position() local buf = vim.api.nvim_get_current_buf() @@ -19,8 +20,9 @@ local function register_cursor_callback(controller) }) end ----@param controller CursorController -local function register_cursor_handler(controller) +---@param ws Workspace +local function register_cursor_handler(ws) + local controller = ws.cursor local async = vim.loop.new_async(vim.schedule_wrap(function () while true do local event = controller:try_recv():await() @@ -61,9 +63,10 @@ end ---@return Workspace ---join a workspace and register event handlers local function join(workspace) - local ws = state.client:join_workspace(workspace):await() - register_cursor_callback(ws.cursor) - register_cursor_handler(ws.cursor) + local ws = session.client:join_workspace(workspace):await() + print(" >< joined workspace " .. ws.name) + register_cursor_callback(ws) + register_cursor_handler(ws) -- TODO this is temporary and ad-hoc ws:callback(function (event) @@ -78,14 +81,16 @@ local function join(workspace) end vim.schedule(function() window.update() end) end) - window.update() + + session.workspace = ws return ws end local function leave() - state.client:leave_workspace(state.workspace.name) + session.client:leave_workspace(session.workspace.name) print(" -- left workspace") + session.workspace = nil end return {