fix: codemp now uses a global

not sure how to pass the option back from setup() to window without a
global, but i'd love to...
This commit is contained in:
əlemi 2024-09-06 03:27:03 +02:00
parent 59c850a8a9
commit 216cd2d6b1
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 58 additions and 58 deletions

View file

@ -1,48 +1,53 @@
local rt = nil if CODEMP == nil then
local session = nil ---@class CodempGlobal
local native = nil CODEMP = {
local timer = nil rt = nil,
session = nil,
native = nil,
config = {
neo_tree = false,
callback_interval = 100,
},
setup = function (opts)
CODEMP.config = vim.tbl_extend('force', CODEMP.config, opts)
local function setup(_opts)
local path = vim.fn.stdpath('data') .. '/codemp/' local path = vim.fn.stdpath('data') .. '/codemp/'
if vim.fn.isdirectory(path) == 0 then if vim.fn.isdirectory(path) == 0 then
vim.fn.mkdir(path, 'p') vim.fn.mkdir(path, 'p')
end end
if native == nil then if CODEMP.native == nil then
native = require('codemp.loader').load() -- make sure we can load the native library correctly, otherwise no point going forward CODEMP.native = require('codemp.loader').load() -- make sure we can load the native library correctly, otherwise no point going forward
--native.logger(function (msg) --native.logger(function (msg)
-- vim.schedule(function () print(msg) end) -- vim.schedule(function () print(msg) end)
--end, true) --end, true)
end end
if session == nil then if CODEMP.session == nil then
session = require('codemp.session') CODEMP.session = require('codemp.session')
end end
if rt == nil then if CODEMP.rt == nil then
rt = native.spawn_runtime_driver() -- spawn thread to drive tokio runtime CODEMP.rt = native.spawn_runtime_driver() -- 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 session.client ~= nil then if CODEMP.session.client ~= nil then
print(" xx disconnecting codemp client") print(" xx disconnecting codemp client")
session.client = nil CODEMP.session.client = nil
end end
rt:stop() CODEMP.rt:stop()
end end
} }
) )
end end
local timer_interval = vim.g.codemp_callback_interval or 100
if timer == nil then if timer == nil then
timer = vim.loop.new_timer() timer = vim.loop.new_timer()
timer:start(timer_interval, timer_interval, function() timer:start(CODEMP.config.timer_interval, CODEMP.config.timer_interval, function()
while true do while true do
local cb = native.poll_callback() local cb = CODEMP.native.poll_callback()
if cb == nil then break end if cb == nil then break end
cb() cb()
end end
@ -51,14 +56,9 @@ local function setup(_opts)
require('codemp.command') require('codemp.command')
return { return CODEMP
native = native, end,
session = session,
rt = rt,
callbacks_timer = timer,
} }
end end
return { return CODEMP
setup = setup
}

View file

@ -1,4 +1,4 @@
if vim.g.codemp_neo_tree then if CODEMP.config.neo_tree then
return { return {
update = function () require("neo-tree.sources.manager").refresh("codemp") end, update = function () require("neo-tree.sources.manager").refresh("codemp") end,
init = function () end, init = function () end,