feat: use type hints, fix api method names

This commit is contained in:
əlemi 2024-08-22 22:07:00 +02:00
parent 75e763711f
commit 399aaad45f
Signed by: alemi
GPG key ID: A4895B84D311642C
8 changed files with 57 additions and 40 deletions

View file

@ -6,13 +6,13 @@ local buffer_id_map = {}
local user_buffer_name = {} local user_buffer_name = {}
local ticks = {} local ticks = {}
local function create(name, content) local function create(name)
state.client:get_workspace(state.workspace):create_buffer(name, content):await() state.workspace:create_buffer(name):await()
print(" ++ created buffer '" .. name .. "' on " .. state.workspace) print(" ++ created buffer '" .. name .. "' on " .. state.workspace.name)
end end
local function delete(name) local function delete(name)
state.client:get_workspace(state.workspace):delete_buffer(name):await() state.workspace:delete_buffer(name):await()
print(" -- deleted buffer " .. name) print(" -- deleted buffer " .. name)
end end
@ -27,7 +27,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.client:get_workspace(state.workspace):attach(name):await() local controller = state.workspace:attach_buffer(name):await()
-- TODO map name to uuid -- TODO map name to uuid
@ -36,7 +36,7 @@ local function attach(name, current, content)
ticks[buffer] = 0 ticks[buffer] = 0
if content ~= nil then if content ~= nil then
controller:send(0, 0, content) -- no need to await local _ = controller:send(0, 0, content) -- no need to await
end end
-- hook serverbound callbacks -- hook serverbound callbacks
@ -49,17 +49,17 @@ local function attach(name, current, content)
"start(row:%s, col:%s) offset:%s end(row:%s, col:%s new(row:%s, col:%s)) len(old:%s, new:%s)", "start(row:%s, col:%s) offset:%s end(row:%s, col:%s new(row:%s, col:%s)) len(old:%s, new:%s)",
start_row, start_col, start_offset, old_end_row, old_end_col, new_end_row, new_end_col, old_end_byte_len, new_byte_len start_row, start_col, start_offset, old_end_row, old_end_col, new_end_row, new_end_col, old_end_byte_len, new_byte_len
)) ))
local content local change_content
if new_byte_len == 0 then if new_byte_len == 0 then
content = "" change_content = ""
else else
content = table.concat( change_content = table.concat(
vim.api.nvim_buf_get_text(buf, start_row, start_col, start_row + new_end_row, start_col + new_end_col, {}), vim.api.nvim_buf_get_text(buf, start_row, start_col, start_row + new_end_row, start_col + new_end_col, {}),
'\n' '\n'
) )
end end
print(string.format("sending: %s %s %s %s -- '%s'", start_row, start_col, start_row + new_end_row, start_col + new_end_col, content)) print(string.format("sending: %s %s %s %s -- '%s'", start_row, start_col, start_row + new_end_row, start_col + new_end_col, change_content))
controller:send(start_offset, start_offset + old_end_byte_len, content) -- no need to await controller:send(start_offset, start_offset + old_end_byte_len, change_content):await()
end, end,
}) })
@ -96,7 +96,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.client:get_workspace(state.workspace):detach(name) state.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)
@ -106,13 +106,16 @@ 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.client:get_workspace(state.workspace):get_buffer(name) local controller = state.workspace:get_buffer(name)
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer) if controller ~= nil then
utils.buffer.set_content(buffer, controller:content():await()) ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
print(" :: synched buffer " .. name) utils.buffer.set_content(buffer, controller:content():await())
else print(" :: synched buffer " .. name)
print(" !! buffer not managed") return
end
end end
print(" !! buffer not managed")
end end

View file

@ -18,10 +18,16 @@ end
-- always available -- always available
local base_actions = { local base_actions = {
connect = function(host) connect = function(host, bang)
if host == nil then host = 'http://codemp.alemi.dev:50053' end if host == nil then host = 'http://codemp.alemi.dev:50053' end
local user = vim.g.codemp_username or vim.fn.input("username > ", "") local user, password
local password = vim.g.codemp_password or vim.fn.input("password > ", "") if bang then -- ignore configured values
user = vim.fn.input("username > ", "")
password = vim.fn.input("password > ", "")
else
user = vim.g.codemp_username or vim.fn.input("username > ", "")
password = vim.g.codemp_password or vim.fn.input("password > ", "")
end
state.client = native.connect(host, user, password):await() state.client = native.connect(host, user, password):await()
print(" ++ connected to " .. host .. " as " .. user) print(" ++ connected to " .. host .. " as " .. user)
end, end,
@ -39,8 +45,7 @@ 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 = ws state.workspace = workspace.join(ws)
workspace.join(ws)
print(" >< joined workspace " .. ws) print(" >< joined workspace " .. ws)
end, end,
@ -87,7 +92,7 @@ local joined_actions = {
if path == nil then if path == nil then
local cwd = vim.fn.getcwd() local cwd = vim.fn.getcwd()
local full_path = vim.fn.expand("%:p") local full_path = vim.fn.expand("%:p")
path = string.gsub(full_path, cwd .. "/", "") path = string.gsub(full_path, cwd .. "/", "")
end end
if #path > 0 then if #path > 0 then
local buf = vim.api.nvim_get_current_buf() local buf = vim.api.nvim_get_current_buf()
@ -186,9 +191,8 @@ vim.api.nvim_create_user_command(
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 state.client ~= nil and state.workspace ~= nil then
local ws = state.client:get_workspace(state.workspace) if state.workspace ~= nil then
if ws ~= nil then return filter(lead, state.workspace:filetree())
return filter(lead, ws:filetree())
end end
end end
end end

View file

@ -25,7 +25,7 @@ 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 state = require('codemp.state')
local rt = native.runtime_drive_forever() -- 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)
--end, true) --end, true)
@ -33,10 +33,9 @@ local rt = native.runtime_drive_forever() -- spawn thread to drive tokio runtime
vim.api.nvim_create_autocmd( vim.api.nvim_create_autocmd(
{"ExitPre"}, {"ExitPre"},
{ {
callback = function (ev) callback = function (_ev)
if state.client ~= nil then if state.client ~= nil then
print(" xx disconnecting codemp client") print(" xx disconnecting codemp client")
native.close_client(state.client.id)
state.client = nil state.client = nil
end end
end end

View file

@ -1,5 +1,11 @@
---@module 'annotations'
---@return Codemp
local function load()
local native, _ = require("codemp.native")
return native
end
return { return {
load = function () load = load,
return require("codemp.lua")
end
} }

View file

@ -1,4 +1,7 @@
---@type Workspace
local workspace = nil local workspace = nil
---@type Client
local client = nil local client = nil
return { return {

View file

@ -70,6 +70,8 @@ end
-- - [x] delete line at end of buffer -- - [x] delete line at end of buffer
-- - [x] delete multiline at end of buffer -- - [x] delete multiline at end of buffer
-- - [x] autocomplete -- - [x] autocomplete
-- - [ ] delete whole buffer
-- - [ ] enter insert in newline with `o`
local function buffer_set_content(buf, content, first, last) local function buffer_set_content(buf, content, first, last)
if first == nil and last == nil then if first == nil and last == nil then
local lines = vim.split(content, "\n", {trimempty=false}) local lines = vim.split(content, "\n", {trimempty=false})

View file

@ -2,9 +2,9 @@ local state = require('codemp.state')
local utils = require('codemp.utils') local utils = require('codemp.utils')
local buffers = require('codemp.buffers') local buffers = require('codemp.buffers')
local buffer_id
local prev_window = nil local prev_window = nil
local window_id = nil local window_id = nil
local buffer_id = nil
local ns = vim.api.nvim_create_namespace("codemp-window") local ns = vim.api.nvim_create_namespace("codemp-window")
vim.api.nvim_create_autocmd({"WinLeave"}, { vim.api.nvim_create_autocmd({"WinLeave"}, {
@ -23,7 +23,7 @@ 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.client:get_workspace(state.workspace):filetree() local tree = state.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 .. " |: " .. state.workspace .. "\n"

View file

@ -1,5 +1,3 @@
local native = require('codemp.loader').load()
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 state = require('codemp.state')
@ -63,6 +61,7 @@ local function join(workspace)
register_cursor_callback(ws.cursor) register_cursor_callback(ws.cursor)
register_cursor_handler(ws.cursor) register_cursor_handler(ws.cursor)
-- TODO this is temporary and ad-hoc
ws:callback(function (event) ws:callback(function (event)
if event.type == "leave" then if event.type == "leave" then
if buffers.users[event.value] ~= nil then if buffers.users[event.value] ~= nil then
@ -76,15 +75,17 @@ local function join(workspace)
vim.schedule(function() window.update() end) vim.schedule(function() window.update() end)
end) end)
window.update() window.update()
return ws
end end
local function leave() local function leave()
native.leave_workspace() state.client:leave_workspace(state.workspace.name)
print(" -- left workspace") print(" -- left workspace")
end end
local function open_buffer_tree() local function open_buffer_tree()
local tree = state.client:get_workspace(state.workspace):filetree() local tree = state.workspace:filetree()
if tree_buf == nil then if tree_buf == nil then
tree_buf = vim.api.nvim_create_buf(false, true) tree_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(tree_buf, "codemp::" .. state.workspace) vim.api.nvim_buf_set_name(tree_buf, "codemp::" .. state.workspace)
@ -107,7 +108,6 @@ return {
join = join, join = join,
leave = leave, leave = leave,
map = user_hl, map = user_hl,
positions = user_buffer,
open_buffer_tree = open_buffer_tree, open_buffer_tree = open_buffer_tree,
buffer_tree = tree_buf, buffer_tree = tree_buf,
} }