Skip to content

Commit d521eaf

Browse files
authored
fix: use post initialization callbacks when Copilot is not initialized (#438)
fixes #436
1 parent 075e845 commit d521eaf

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

lua/copilot/client/config.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ local lsp = require("copilot.lsp")
66
local utils = require("copilot.client.utils")
77
local M = {}
88

9+
---@type table<fun(client:table)>
10+
local callbacks = {}
11+
912
---@param overrides table<string, any>
1013
---@param client CopilotClient
1114
function M.prepare_client_config(overrides, client)
@@ -109,6 +112,10 @@ function M.prepare_client_config(overrides, client)
109112

110113
-- prevent requests to copilot prior to being initialized
111114
client.initialized = true
115+
116+
for _, callback in ipairs(callbacks) do
117+
callback(lsp_client)
118+
end
112119
end)
113120
end,
114121
on_exit = function(code, _, client_id)
@@ -135,4 +142,9 @@ function M.prepare_client_config(overrides, client)
135142
}, overrides)
136143
end
137144

145+
---@param callback fun(client:table)
146+
function M.add_callback(callback)
147+
table.insert(callbacks, callback)
148+
end
149+
138150
return M

lua/copilot/client/init.lua

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local util = require("copilot.util")
33
local logger = require("copilot.logger")
44
local lsp = require("copilot.lsp")
55
local utils = require("copilot.client.utils")
6+
local client_config = require("copilot.client.config")
67

78
local is_disabled = false
89

@@ -107,6 +108,7 @@ function M.use_client(callback)
107108
return
108109
end
109110

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

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

117119
store_client_id(client_id)
118-
119-
client = M.get() --[[@as table]]
120-
end
121-
122-
if client.initialized then
120+
elseif not client.initialized then
121+
client_config.add_callback(callback)
122+
else
123123
callback(client)
124-
return
125124
end
126-
127-
logger.error("client is not initialized yet")
128-
-- Following code is commented out for now because:
129-
-- 1) I am hoping it is not needed anymore and
130-
-- 2) It causes issues with testing >_<
131-
--
132-
-- local timer, err, _ = vim.uv.new_timer()
133-
--
134-
-- if not timer then
135-
-- logger.error(string.format("error creating timer: %s", err))
136-
-- return
137-
-- end
138-
--
139-
-- timer:start(
140-
-- 0,
141-
-- 100,
142-
-- vim.schedule_wrap(function()
143-
-- if client.initialized and not timer:is_closing() then
144-
-- timer:stop()
145-
-- timer:close()
146-
-- callback(client)
147-
-- else
148-
-- logger.error("client not initialized yet")
149-
-- end
150-
-- end)
151-
-- )
152125
end
153126

154127
function M.setup()

0 commit comments

Comments
 (0)