@@ -8,6 +8,7 @@ local client_config = require("copilot.client.config")
8
8
local is_disabled = false
9
9
10
10
--- @class CopilotClient
11
+ --- @field augroup string | nil
11
12
--- @field id integer | nil
12
13
--- @field capabilities lsp.ClientCapabilities | nil
13
14
--- @field config vim.lsp.ClientConfig | nil
@@ -16,6 +17,7 @@ local is_disabled = false
16
17
-- Only for informational purposes, use Vim API to check actual status
17
18
--- @field buffer_statuses table<integer , string>
18
19
local M = {
20
+ augroup = nil ,
19
21
id = nil ,
20
22
capabilities = nil ,
21
23
config = nil ,
@@ -166,12 +168,37 @@ function M.setup()
166
168
167
169
is_disabled = false
168
170
M .id = nil
171
+
172
+ -- nvim_clear_autocmds throws an error if the group does not exist
173
+ local augroup = " copilot.client"
174
+ vim .api .nvim_create_augroup (augroup , { clear = true })
175
+ M .augroup = augroup
176
+
177
+ vim .api .nvim_create_autocmd (" FileType" , {
178
+ group = M .augroup ,
179
+ callback = function ()
180
+ logger .trace (" filetype autocmd called" )
181
+ vim .schedule (function ()
182
+ -- todo: when we do lazy/late attaching this needs changing
183
+ M .buf_attach ()
184
+ end )
185
+ end ,
186
+ desc = " [copilot] (suggestion) file type" ,
187
+ })
188
+
169
189
vim .schedule (M .ensure_client_started )
190
+ -- FileType is likely already triggered for shown buffer
191
+ vim .schedule (M .buf_attach )
170
192
end
171
193
172
194
function M .teardown ()
173
195
is_disabled = true
174
196
197
+ -- nvim_clear_autocmds throws an error if the group does not exist
198
+ if M .augroup then
199
+ vim .api .nvim_clear_autocmds ({ group = M .augroup })
200
+ end
201
+
175
202
if M .id then
176
203
vim .lsp .stop_client (M .id )
177
204
end
0 commit comments