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 ticks = {}
local function create(name, content)
state.client:get_workspace(state.workspace):create_buffer(name, content):await()
print(" ++ created buffer '" .. name .. "' on " .. state.workspace)
local function create(name)
state.workspace:create_buffer(name):await()
print(" ++ created buffer '" .. name .. "' on " .. state.workspace.name)
end
local function delete(name)
state.client:get_workspace(state.workspace):delete_buffer(name):await()
state.workspace:delete_buffer(name):await()
print(" -- deleted buffer " .. name)
end
@ -27,7 +27,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.client:get_workspace(state.workspace):attach(name):await()
local controller = state.workspace:attach_buffer(name):await()
-- TODO map name to uuid
@ -36,7 +36,7 @@ local function attach(name, current, content)
ticks[buffer] = 0
if content ~= nil then
controller:send(0, 0, content) -- no need to await
local _ = controller:send(0, 0, content) -- no need to await
end
-- 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, 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
content = ""
change_content = ""
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, {}),
'\n'
)
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))
controller:send(start_offset, start_offset + old_end_byte_len, content) -- no need to await
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, change_content):await()
end,
})
@ -96,7 +96,7 @@ local function detach(name)
local buffer = buffer_id_map[name]
id_buffer_map[buffer] = 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, {})
print(" -- detached from buffer " .. name)
@ -106,13 +106,16 @@ local function sync()
local buffer = vim.api.nvim_get_current_buf()
local name = id_buffer_map[buffer]
if name ~= nil then
local controller = state.client:get_workspace(state.workspace):get_buffer(name)
ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
utils.buffer.set_content(buffer, controller:content():await())
print(" :: synched buffer " .. name)
else
print(" !! buffer not managed")
local controller = state.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())
print(" :: synched buffer " .. name)
return
end
end
print(" !! buffer not managed")
end

View file

@ -18,10 +18,16 @@ end
-- always available
local base_actions = {
connect = function(host)
connect = function(host, bang)
if host == nil then host = 'http://codemp.alemi.dev:50053' end
local user = vim.g.codemp_username or vim.fn.input("username > ", "")
local password = vim.g.codemp_password or vim.fn.input("password > ", "")
local user, 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()
print(" ++ connected to " .. host .. " as " .. user)
end,
@ -39,8 +45,7 @@ local connected_actions = {
join = function(ws)
if ws == nil then error("missing workspace name") end
state.workspace = ws
workspace.join(ws)
state.workspace = workspace.join(ws)
print(" >< joined workspace " .. ws)
end,
@ -87,7 +92,7 @@ local joined_actions = {
if path == nil then
local cwd = vim.fn.getcwd()
local full_path = vim.fn.expand("%:p")
path = string.gsub(full_path, cwd .. "/", "")
path = string.gsub(full_path, cwd .. "/", "")
end
if #path > 0 then
local buf = vim.api.nvim_get_current_buf()
@ -186,9 +191,8 @@ vim.api.nvim_create_user_command(
elseif stage == 3 then
if args[#args-1] == 'attach' or args[#args-1] == 'detach' then
if state.client ~= nil and state.workspace ~= nil then
local ws = state.client:get_workspace(state.workspace)
if ws ~= nil then
return filter(lead, ws:filetree())
if state.workspace ~= nil then
return filter(lead, state.workspace:filetree())
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 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)
-- vim.schedule(function () print(msg) end)
--end, true)
@ -33,10 +33,9 @@ local rt = native.runtime_drive_forever() -- spawn thread to drive tokio runtime
vim.api.nvim_create_autocmd(
{"ExitPre"},
{
callback = function (ev)
callback = function (_ev)
if state.client ~= nil then
print(" xx disconnecting codemp client")
native.close_client(state.client.id)
state.client = nil
end
end

View file

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

View file

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

View file

@ -70,6 +70,8 @@ end
-- - [x] delete line at end of buffer
-- - [x] delete multiline at end of buffer
-- - [x] autocomplete
-- - [ ] delete whole buffer
-- - [ ] enter insert in newline with `o`
local function buffer_set_content(buf, content, first, last)
if first == nil and last == nil then
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 buffers = require('codemp.buffers')
local buffer_id
local prev_window = nil
local window_id = nil
local buffer_id = nil
local ns = vim.api.nvim_create_namespace("codemp-window")
vim.api.nvim_create_autocmd({"WinLeave"}, {
@ -23,7 +23,7 @@ local function update_window()
local buffer_to_row = {}
local user_to_row = {}
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 })
local tmp = ">| codemp\n"
tmp = tmp .. " |: " .. state.workspace .. "\n"

View file

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