@@ -4,6 +4,8 @@ local util = require("copilot.util")
44
55local M = {
66 id = nil ,
7+ augroup = " copilot.client" ,
8+ disabled = false ,
79}
810
911local function store_client_id (id )
5355
5456--- @param force ? boolean
5557function M .buf_attach (force )
58+ if M .disabled then
59+ print (" [Copilot] Offline" )
60+ return
61+ end
62+
5663 if not force and not util .should_attach () then
5764 return
5865 end
@@ -67,28 +74,30 @@ function M.buf_detach()
6774 end
6875end
6976
70- --- @param should_start ? boolean
71- function M .get (should_start )
72- if not M .config then
73- error (" copilot.setup is not called yet" )
77+ function M .get ()
78+ return vim .lsp .get_client_by_id (M .id )
79+ end
80+
81+ --- @param callback fun ( client : table ): nil
82+ function M .use_client (callback )
83+ if M .disabled then
84+ print (" [Copilot] Offline" )
85+ return
7486 end
7587
76- local client = M .id and vim .lsp .get_client_by_id (M .id ) or nil
88+ local client = M .get () --[[ @as table]]
89+
90+ if not client then
91+ if not M .config then
92+ error (" copilot.setup is not called yet" )
93+ end
7794
78- if should_start and not (M .id and client ) then
7995 local client_id = vim .lsp .start_client (M .config )
8096 store_client_id (client_id )
8197
82- client = vim . lsp . get_client_by_id ( M . id )
98+ client = M . get ( )
8399 end
84100
85- return client
86- end
87-
88- --- @param callback fun ( client : table ): nil
89- function M .use_client (callback )
90- local client = M .get (true ) --[[ @as table]]
91-
92101 if client .initialized then
93102 callback (client )
94103 return
@@ -136,21 +145,24 @@ M.merge_server_opts = function(params)
136145 }, params .server_opts_overrides or {})
137146end
138147
139- M .setup = function (params )
140- if vim .fn .executable (params .copilot_node_command ) ~= 1 then
148+ function M .setup ()
149+ M .disabled = false
150+
151+ M .config = M .merge_server_opts (config .get ())
152+
153+ if vim .fn .executable (M .config .cmd [1 ]) ~= 1 then
154+ is_disabled = true
141155 vim .notify (
142- string.format (" [copilot] copilot_node_command(%s) is not executable" , params . copilot_node_command ),
156+ string.format (" [copilot] copilot_node_command(%s) is not executable" , M . config . cmd [ 1 ] ),
143157 vim .log .levels .ERROR
144158 )
145159 return
146160 end
147161
148- M .config = M .merge_server_opts (params )
149-
150- local augroup = vim .api .nvim_create_augroup (" copilot.client" , { clear = true })
162+ vim .api .nvim_create_augroup (M .augroup , { clear = true })
151163
152164 vim .api .nvim_create_autocmd (" FileType" , {
153- group = augroup ,
165+ group = M . augroup ,
154166 callback = vim .schedule_wrap (function ()
155167 M .buf_attach ()
156168 end ),
@@ -161,4 +173,15 @@ M.setup = function(params)
161173 end )
162174end
163175
176+ function M .teardown ()
177+ M .disabled = true
178+
179+ vim .api .nvim_clear_autocmds ({ group = M .augroup })
180+
181+ if M .id then
182+ vim .lsp .stop_client (M .id )
183+ M .id = nil
184+ end
185+ end
186+
164187return M
0 commit comments