@@ -2,43 +2,90 @@ local api = require("copilot.api")
22local config = require (" copilot.config" )
33local 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
719local copilot_node_version = nil
820function 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
1328end
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 )
1932end
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
3148end
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
3866end
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+ )
4289end
4390
4491M .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 {})
73117end
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 )
78134end
79135
80136return M
0 commit comments