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 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())

View file

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

View file

@ -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'),

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 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

View file

@ -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 {