diff --git a/lua/codemp/init.lua b/lua/codemp/init.lua index 8e38257..1fb8159 100644 --- a/lua/codemp/init.lua +++ b/lua/codemp/init.lua @@ -10,7 +10,7 @@ if CODEMP == nil then ---@field available WorkspaceReference[] available workspaces to connect to ---@field timer? any libuv timer ---@field config Config codemp configuration - ---@field setup fun(opts: Config): nil update codemp configuration + ---@field setup fun(opts: Config): nil update config and setup plugin CODEMP = { rt = nil, native = nil, @@ -25,6 +25,33 @@ if CODEMP == nil then }, setup = function (opts) CODEMP.config = vim.tbl_extend('force', CODEMP.config, opts) + -- register logger + CODEMP.native.logger(print, CODEMP.config.debug) + -- start background runtime, with stop event + CODEMP.rt = CODEMP.native.spawn_runtime_driver() -- spawn thread to drive tokio runtime + vim.api.nvim_create_autocmd( + {"ExitPre"}, + { + callback = function (_ev) + if CODEMP.client ~= nil then + print(" xx disconnecting codemp client") + CODEMP.client = nil -- drop reference so it gets garbage collected + end + CODEMP.rt:stop() + end + } + ) + + CODEMP.timer = vim.loop.new_timer() + CODEMP.timer:start(CODEMP.config.timer_interval, CODEMP.config.timer_interval, function() + while true do + local cb, arg = CODEMP.native.poll_callback() + if cb == nil then break end + vim.schedule(function() cb(arg) end) + end + end) + + require('codemp.command') -- not really related but should only happen once end } end @@ -35,37 +62,6 @@ if CODEMP.native == nil then print(" !! could not load native bindings, try reloading") return CODEMP end - CODEMP.native.logger(print, CODEMP.config.debug) end -if CODEMP.rt == nil then - CODEMP.rt = CODEMP.native.spawn_runtime_driver() -- spawn thread to drive tokio runtime - vim.api.nvim_create_autocmd( - {"ExitPre"}, - { - callback = function (_ev) - if CODEMP.client ~= nil then - print(" xx disconnecting codemp client") - CODEMP.client = nil - end - CODEMP.rt:stop() - end - } - ) -end - -if CODEMP.timer == nil then - CODEMP.timer = vim.loop.new_timer() - CODEMP.timer:start(CODEMP.config.timer_interval, CODEMP.config.timer_interval, function() - while true do - local cb, arg = CODEMP.native.poll_callback() - if cb == nil then break end - vim.schedule(function() cb(arg) end) - end - end) - - require('codemp.command') -- not really related but should only happen once -end - - return CODEMP