Skip to content

Commit 7ba514e

Browse files
committed
feat(client): improved start and attach
1 parent b6e94db commit 7ba514e

File tree

7 files changed

+148
-113
lines changed

7 files changed

+148
-113
lines changed

lua/copilot/auth.lua

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local api = require("copilot.api")
2-
local u = require("copilot.util")
2+
local c = require("copilot.client")
33

44
local M = {}
55

@@ -102,39 +102,33 @@ function M.setup(client)
102102
end
103103

104104
function M.signin()
105-
local client = u.get_copilot_client()
106-
if not client then
107-
return
108-
end
109-
110-
M.setup(client)
105+
c.use_client(function(client)
106+
M.setup(client)
107+
end)
111108
end
112109

113110
function M.signout()
114-
local client = u.get_copilot_client()
115-
if not client then
116-
return
117-
end
118-
119-
api.check_status(
120-
client,
121-
{ options = { localChecksOnly = true } },
122-
---@param status copilot_check_status_data
123-
function(err, status)
124-
if err then
125-
echo(err)
126-
return
111+
c.use_client(function(client)
112+
api.check_status(
113+
client,
114+
{ options = { localChecksOnly = true } },
115+
---@param status copilot_check_status_data
116+
function(err, status)
117+
if err then
118+
echo(err)
119+
return
120+
end
121+
122+
if status.user then
123+
echo("Signed out as GitHub user " .. status.user)
124+
else
125+
echo("Not signed in")
126+
end
127+
128+
api.sign_out(client, function() end)
127129
end
128-
129-
if status.user then
130-
echo("Signed out as GitHub user " .. status.user)
131-
else
132-
echo("Not signed in")
133-
end
134-
135-
api.sign_out(client, function() end)
136-
end
137-
)
130+
)
131+
end)
138132
end
139133

140134
local function find_config_path()

lua/copilot/client.lua

Lines changed: 82 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,90 @@ local api = require("copilot.api")
22
local config = require("copilot.config")
33
local util = require("copilot.util")
44

5-
local M = {}
5+
local M = {
6+
id = nil,
7+
}
8+
9+
local function store_client_id(id)
10+
if M.id and M.id ~= id then
11+
if vim.lsp.get_client_by_id(M.id) then
12+
error("unexpectedly started multiple copilot server")
13+
end
14+
end
15+
16+
M.id = id
17+
end
618

719
local copilot_node_version = nil
820
function M.get_node_version()
921
if not copilot_node_version then
10-
copilot_node_version = string.match(table.concat(vim.fn.systemlist(config.get("copilot_node_command") .. " --version", nil, false)), "v(%S+)")
22+
copilot_node_version = string.match(
23+
table.concat(vim.fn.systemlist(config.get("copilot_node_command") .. " --version", nil, false)),
24+
"v(%S+)"
25+
)
1126
end
1227
return copilot_node_version
1328
end
1429

15-
local register_autocmd = function()
16-
vim.api.nvim_create_autocmd({ "BufEnter" }, {
17-
callback = vim.schedule_wrap(M.buf_attach_copilot),
18-
})
30+
function M.buf_is_attached(bufnr)
31+
return M.id and vim.lsp.buf_is_attached(bufnr or 0, M.id)
1932
end
2033

2134
---@param force? boolean
22-
function M.buf_attach(client, force)
35+
function M.buf_attach(force)
2336
if not force and not util.should_attach() then
2437
return
2538
end
2639

27-
client = client or util.get_copilot_client()
28-
if client and not util.is_attached(client) then
29-
vim.lsp.buf_attach_client(0, client.id)
40+
local client_id = vim.lsp.start(M.config)
41+
store_client_id(client_id)
42+
end
43+
44+
function M.buf_detach()
45+
if M.buf_is_attached(0) then
46+
vim.lsp.buf_detach_client(0, M.id)
3047
end
3148
end
3249

33-
function M.buf_detach(client)
34-
client = client or util.get_copilot_client()
35-
if client and util.is_attached(client) then
36-
vim.lsp.buf_detach_client(0, client.id)
50+
---@param should_start? boolean
51+
function M.get(should_start)
52+
if not M.config then
53+
error("copilot.setup is not called yet")
54+
end
55+
56+
local client = M.id and vim.lsp.get_client_by_id(M.id) or nil
57+
58+
if should_start and not (M.id and client) then
59+
local client_id = vim.lsp.start_client(M.config)
60+
store_client_id(client_id)
61+
62+
client = vim.lsp.get_client_by_id(M.id)
3763
end
64+
65+
return client
3866
end
3967

40-
M.buf_attach_copilot = function()
41-
M.buf_attach()
68+
---@param callback fun(client:table):nil
69+
function M.use_client(callback)
70+
local client = M.get(true) --[[@as table]]
71+
72+
if client.initialized then
73+
callback(client)
74+
return
75+
end
76+
77+
local timer = vim.loop.new_timer()
78+
timer:start(
79+
0,
80+
100,
81+
vim.schedule_wrap(function()
82+
if client.initialized and not timer:is_closing() then
83+
timer:stop()
84+
timer:close()
85+
callback(client)
86+
end
87+
end)
88+
)
4289
end
4390

4491
M.merge_server_opts = function(params)
@@ -47,22 +94,19 @@ M.merge_server_opts = function(params)
4794
params.copilot_node_command,
4895
require("copilot.util").get_copilot_path(),
4996
},
50-
cmd_cwd = vim.fn.expand("~"),
5197
root_dir = vim.loop.cwd(),
5298
name = "copilot",
53-
autostart = true,
54-
single_file_support = true,
5599
on_init = function(client)
56-
vim.schedule(function ()
100+
vim.schedule(function()
57101
---@type copilot_set_editor_info_params
58102
local set_editor_info_params = util.get_editor_info()
59-
set_editor_info_params.editorInfo.version = set_editor_info_params.editorInfo.version .. ' + Node.js ' .. M.get_node_version()
103+
set_editor_info_params.editorInfo.version = set_editor_info_params.editorInfo.version
104+
.. " + Node.js "
105+
.. M.get_node_version()
60106
set_editor_info_params.editorConfiguration = util.get_editor_configuration()
61107
set_editor_info_params.networkProxy = util.get_network_proxy()
62108
api.set_editor_info(client, set_editor_info_params)
63109
end)
64-
vim.schedule(M.buf_attach_copilot)
65-
vim.schedule(register_autocmd)
66110
end,
67111
handlers = {
68112
PanelSolution = api.handlers.PanelSolution,
@@ -72,9 +116,21 @@ M.merge_server_opts = function(params)
72116
}, params.server_opts_overrides or {})
73117
end
74118

75-
M.start = function(params)
76-
local client_config = M.merge_server_opts(params)
77-
vim.lsp.start_client(client_config)
119+
M.setup = function(params)
120+
M.config = M.merge_server_opts(params)
121+
122+
local augroup = vim.api.nvim_create_augroup("copilot.client", { clear = true })
123+
124+
vim.api.nvim_create_autocmd("FileType", {
125+
group = augroup,
126+
callback = vim.schedule_wrap(function()
127+
M.buf_attach()
128+
end),
129+
})
130+
131+
vim.schedule(function()
132+
M.buf_attach()
133+
end)
78134
end
79135

80136
return M

lua/copilot/command.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function mod.toggle(opts)
109109
end
110110

111111
if u.is_attached(client) then
112-
c.buf_detach(client)
112+
c.buf_detach()
113113
return
114114
end
115115

@@ -126,7 +126,7 @@ function mod.toggle(opts)
126126
opts.force = true
127127
end
128128

129-
c.buf_attach(client, opts.force)
129+
c.buf_attach(opts.force)
130130
end
131131

132132
return mod

lua/copilot/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ local mod = {
4343
function mod.setup(opts)
4444
if mod.config then
4545
vim.notify("[copilot] config is already set", vim.log.levels.WARN)
46-
return
46+
return mod.config
4747
end
4848

4949
local config = vim.tbl_deep_extend("force", default_config, opts or {})

lua/copilot/init.lua

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,16 @@ M.setup = function(opts)
3737

3838
local conf = config.setup(opts)
3939

40-
vim.schedule(function ()
41-
client.start(conf)
42-
43-
if conf.panel.enabled then
44-
panel.setup(conf.panel)
45-
create_cmds(conf)
46-
end
47-
48-
if conf.suggestion.enabled then
49-
suggestion.setup(conf.suggestion)
50-
end
51-
end)
40+
client.setup(conf)
41+
42+
if conf.panel.enabled then
43+
panel.setup(conf.panel)
44+
create_cmds(conf)
45+
end
46+
47+
if conf.suggestion.enabled then
48+
suggestion.setup(conf.suggestion)
49+
end
5250

5351
highlight.setup()
5452

0 commit comments

Comments
 (0)