Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lua/copilot/client/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ local lsp = require("copilot.lsp")
local utils = require("copilot.client.utils")
local M = {}

---@type table<fun(client:table)>
local callbacks = {}

---@param overrides table<string, any>
---@param client CopilotClient
function M.prepare_client_config(overrides, client)
Expand Down Expand Up @@ -109,6 +112,10 @@ function M.prepare_client_config(overrides, client)

-- prevent requests to copilot prior to being initialized
client.initialized = true

for _, callback in ipairs(callbacks) do
callback(lsp_client)
end
end)
end,
on_exit = function(code, _, client_id)
Expand All @@ -135,4 +142,9 @@ function M.prepare_client_config(overrides, client)
}, overrides)
end

---@param callback fun(client:table)
function M.add_callback(callback)
table.insert(callbacks, callback)
end

return M
37 changes: 5 additions & 32 deletions lua/copilot/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local util = require("copilot.util")
local logger = require("copilot.logger")
local lsp = require("copilot.lsp")
local utils = require("copilot.client.utils")
local client_config = require("copilot.client.config")

local is_disabled = false

Expand Down Expand Up @@ -107,6 +108,7 @@ function M.use_client(callback)
return
end

client_config.add_callback(callback)
local client_id, err = vim.lsp.start(M.config)

if not client_id then
Expand All @@ -115,40 +117,11 @@ function M.use_client(callback)
end

store_client_id(client_id)

client = M.get() --[[@as table]]
end

if client.initialized then
elseif not client.initialized then
client_config.add_callback(callback)
else
callback(client)
return
end

logger.error("client is not initialized yet")
-- Following code is commented out for now because:
-- 1) I am hoping it is not needed anymore and
-- 2) It causes issues with testing >_<
--
-- local timer, err, _ = vim.uv.new_timer()
--
-- if not timer then
-- logger.error(string.format("error creating timer: %s", err))
-- return
-- end
--
-- timer:start(
-- 0,
-- 100,
-- vim.schedule_wrap(function()
-- if client.initialized and not timer:is_closing() then
-- timer:stop()
-- timer:close()
-- callback(client)
-- else
-- logger.error("client not initialized yet")
-- end
-- end)
-- )
end

function M.setup()
Expand Down