chore: state -> session, small fixes

This commit is contained in:
əlemi 2024-08-24 01:56:27 +02:00
parent 13e6182ce0
commit 380257945e
Signed by: alemi
GPG key ID: A4895B84D311642C
7 changed files with 65 additions and 53 deletions

View file

@ -1,5 +1,5 @@
local utils = require('codemp.utils') local utils = require('codemp.utils')
local state = require('codemp.state') local session = require('codemp.session')
local id_buffer_map = {} local id_buffer_map = {}
local buffer_id_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_buf_set_name(buffer, "codemp::" .. name)
vim.api.nvim_set_current_buf(buffer) vim.api.nvim_set_current_buf(buffer)
end end
local controller = state.workspace:attach_buffer(name):await() local controller = session.workspace:attach_buffer(name):await()
-- TODO map name to uuid -- TODO map name to uuid
@ -102,7 +102,7 @@ local function detach(name)
local buffer = buffer_id_map[name] local buffer = buffer_id_map[name]
id_buffer_map[buffer] = nil id_buffer_map[buffer] = nil
buffer_id_map[name] = nil buffer_id_map[name] = nil
state.workspace:detach_buffer(name) session.workspace:detach_buffer(name)
vim.api.nvim_buf_delete(buffer, {}) vim.api.nvim_buf_delete(buffer, {})
print(" -- detached from buffer " .. name) print(" -- detached from buffer " .. name)
@ -112,7 +112,7 @@ local function sync()
local buffer = vim.api.nvim_get_current_buf() local buffer = vim.api.nvim_get_current_buf()
local name = id_buffer_map[buffer] local name = id_buffer_map[buffer]
if name ~= nil then if name ~= nil then
local controller = state.workspace:get_buffer(name) local controller = session.workspace:get_buffer(name)
if controller ~= nil then if controller ~= nil then
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer) ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
utils.buffer.set_content(buffer, controller:content():await()) utils.buffer.set_content(buffer, controller:content():await())

View file

@ -1,4 +1,4 @@
local state = require('codemp.state') local session = require('codemp.session')
local buffers = require('codemp.buffers') local buffers = require('codemp.buffers')
local workspace = require('codemp.workspace') local workspace = require('codemp.workspace')
local utils = require('codemp.utils') local utils = require('codemp.utils')
@ -36,7 +36,7 @@ local base_actions = {
-- only available if state.client is not nil -- only available if state.client is not nil
local connected_actions = { local connected_actions = {
id = function() id = function()
print("> codemp::" .. state.client.id) print("> codemp::" .. session.client.id)
end, end,
toggle = function() toggle = function()
@ -45,39 +45,38 @@ local connected_actions = {
join = function(ws) join = function(ws)
if ws == nil then error("missing workspace name") end if ws == nil then error("missing workspace name") end
state.workspace = workspace.join(ws) workspace.join(ws)
print(" >< joined workspace " .. ws)
end, end,
start = function(ws) start = function(ws)
if ws == nil then error("missing workspace name") end 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) print(" <> created workspace " .. ws)
end, end,
available = function() 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) print(" ++ " .. ws)
end 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) print(" -- " .. ws)
end end
end, end,
invite = function(user) invite = function(user)
local ws local ws
if state.workspace ~= nil then if session.workspace ~= nil then
ws = state.workspace ws = session.workspace
else else
ws = vim.fn.input("workspace > ", "") ws = vim.fn.input("workspace > ", "")
end end
state.client:invite_to_workspace(ws, user):await() session.client:invite_to_workspace(ws, user):await()
print(" ][ invited " .. user .. " to workspace " .. ws) print(" ][ invited " .. user .. " to workspace " .. ws)
end, end,
disconnect = function() disconnect = function()
print(" xx disconnecting client " .. state.client.id) print(" xx disconnecting client " .. session.client.id)
state.client = nil -- should drop and thus close everything session.client = nil -- should drop and thus close everything
end, end,
} }
@ -120,7 +119,7 @@ local joined_actions = {
attach = function(path, bang) attach = function(path, bang)
if path == nil then error("missing buffer name") end if path == nil then error("missing buffer name") end
buffers.attach(path, bang) buffers.attach(path)
end, end,
detach = function(path) detach = function(path)
@ -131,7 +130,7 @@ local joined_actions = {
leave = function(ws) leave = function(ws)
if ws == nil then error("missing workspace to leave") end if ws == nil then error("missing workspace to leave") end
state.client:leave_workspace(ws) workspace.leave()
end, end,
} }
@ -145,11 +144,11 @@ vim.api.nvim_create_user_command(
fn = base_actions[action] fn = base_actions[action]
end 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] fn = connected_actions[action]
end 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] fn = joined_actions[action]
end end
@ -175,13 +174,13 @@ vim.api.nvim_create_user_command(
n = n + 1 n = n + 1
suggestions[n] = sugg suggestions[n] = sugg
end end
if state.client ~= nil then if session.client ~= nil then
for sugg, _ in pairs(connected_actions) do for sugg, _ in pairs(connected_actions) do
n = n + 1 n = n + 1
suggestions[n] = sugg suggestions[n] = sugg
end end
end end
if state.workspace ~= nil then if session.workspace ~= nil then
for sugg, _ in pairs(joined_actions) do for sugg, _ in pairs(joined_actions) do
n = n + 1 n = n + 1
suggestions[n] = sugg suggestions[n] = sugg
@ -190,9 +189,9 @@ vim.api.nvim_create_user_command(
return filter(lead, suggestions) return filter(lead, suggestions)
elseif stage == 3 then elseif stage == 3 then
if args[#args-1] == 'attach' or args[#args-1] == 'detach' then if args[#args-1] == 'attach' or args[#args-1] == 'detach' then
if state.client ~= nil and state.workspace ~= nil then if session.client ~= nil and session.workspace ~= nil then
if state.workspace ~= nil then if session.workspace ~= nil then
return filter(lead, state.workspace:filetree()) return filter(lead, session.workspace:filetree())
end end
end end
end end

View file

@ -24,7 +24,7 @@ end
-- end -- end
local native = require('codemp.loader').load() -- make sure we can load the native library correctly, otherwise no point going forward 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 local rt = native.spawn_runtime_driver() -- spawn thread to drive tokio runtime
--native.logger(function (msg) --native.logger(function (msg)
-- vim.schedule(function () print(msg) end) -- vim.schedule(function () print(msg) end)
@ -34,9 +34,9 @@ vim.api.nvim_create_autocmd(
{"ExitPre"}, {"ExitPre"},
{ {
callback = function (_ev) callback = function (_ev)
if state.client ~= nil then if session.client ~= nil then
print(" xx disconnecting codemp client") print(" xx disconnecting codemp client")
state.client = nil session.client = nil
end end
end end
} }
@ -46,7 +46,7 @@ require('codemp.command')
return { return {
native = native, native = native,
state = require('codemp.state'), session = session,
buffers = require('codemp.buffers'), buffers = require('codemp.buffers'),
workspace = require('codemp.workspace'), workspace = require('codemp.workspace'),
window = require('codemp.window'), window = require('codemp.window'),

18
src/session.lua Normal file
View file

@ -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,
}

View file

@ -1,10 +0,0 @@
---@type Workspace
local workspace = nil
---@type Client
local client = nil
return {
workspace = workspace,
client = client,
}

View file

@ -1,4 +1,4 @@
local state = require('codemp.state') local session = require('codemp.session')
local utils = require('codemp.utils') local utils = require('codemp.utils')
local buffers = require('codemp.buffers') local buffers = require('codemp.buffers')
@ -30,10 +30,10 @@ local function update_window()
local buffer_to_row = {} local buffer_to_row = {}
local user_to_row = {} local user_to_row = {}
local off = {} local off = {}
local tree = state.workspace:filetree() local tree = session.workspace:filetree()
vim.api.nvim_set_option_value('modifiable', true, { buf = buffer_id }) vim.api.nvim_set_option_value('modifiable', true, { buf = buffer_id })
local tmp = ">| codemp\n" local tmp = ">| codemp\n"
tmp = tmp .. " |: " .. state.workspace .. "\n" tmp = tmp .. " |: " .. session.workspace.name .. "\n"
tmp = tmp .. " |\n" tmp = tmp .. " |\n"
local base_row = 3 local base_row = 3
for n, path in pairs(tree) do for n, path in pairs(tree) do

View file

@ -1,14 +1,15 @@
local utils = require('codemp.utils') local utils = require('codemp.utils')
local buffers = require('codemp.buffers') local buffers = require('codemp.buffers')
local state = require('codemp.state') local session = require('codemp.session')
local window = require('codemp.window') local window = require('codemp.window')
local user_hl = {} local user_hl = {}
---@param controller CursorController ---@param ws Workspace
local function register_cursor_callback(controller) local function register_cursor_callback(ws)
local controller = ws.cursor
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, { 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 (_) callback = function (_)
local cur = utils.cursor.position() local cur = utils.cursor.position()
local buf = vim.api.nvim_get_current_buf() local buf = vim.api.nvim_get_current_buf()
@ -19,8 +20,9 @@ local function register_cursor_callback(controller)
}) })
end end
---@param controller CursorController ---@param ws Workspace
local function register_cursor_handler(controller) local function register_cursor_handler(ws)
local controller = ws.cursor
local async = vim.loop.new_async(vim.schedule_wrap(function () local async = vim.loop.new_async(vim.schedule_wrap(function ()
while true do while true do
local event = controller:try_recv():await() local event = controller:try_recv():await()
@ -61,9 +63,10 @@ end
---@return Workspace ---@return Workspace
---join a workspace and register event handlers ---join a workspace and register event handlers
local function join(workspace) local function join(workspace)
local ws = state.client:join_workspace(workspace):await() local ws = session.client:join_workspace(workspace):await()
register_cursor_callback(ws.cursor) print(" >< joined workspace " .. ws.name)
register_cursor_handler(ws.cursor) register_cursor_callback(ws)
register_cursor_handler(ws)
-- TODO this is temporary and ad-hoc -- TODO this is temporary and ad-hoc
ws:callback(function (event) ws:callback(function (event)
@ -78,14 +81,16 @@ local function join(workspace)
end end
vim.schedule(function() window.update() end) vim.schedule(function() window.update() end)
end) end)
window.update()
session.workspace = ws
return ws return ws
end end
local function leave() local function leave()
state.client:leave_workspace(state.workspace.name) session.client:leave_workspace(session.workspace.name)
print(" -- left workspace") print(" -- left workspace")
session.workspace = nil
end end
return { return {