@@ -8,6 +8,7 @@ local client_config = require("copilot.client.config")
88local is_disabled = false
99
1010--- @class CopilotClient
11+ --- @field augroup string | nil
1112--- @field id integer | nil
1213--- @field capabilities lsp.ClientCapabilities | nil
1314--- @field config vim.lsp.ClientConfig | nil
@@ -16,6 +17,7 @@ local is_disabled = false
1617-- Only for informational purposes, use Vim API to check actual status
1718--- @field buffer_statuses table<integer , string>
1819local M = {
20+ augroup = nil ,
1921 id = nil ,
2022 capabilities = nil ,
2123 config = nil ,
@@ -166,12 +168,37 @@ function M.setup()
166168
167169 is_disabled = false
168170 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+
169189 vim .schedule (M .ensure_client_started )
190+ -- FileType is likely already triggered for shown buffer
191+ vim .schedule (M .buf_attach )
170192end
171193
172194function M .teardown ()
173195 is_disabled = true
174196
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+
175202 if M .id then
176203 vim .lsp .stop_client (M .id )
177204 end
0 commit comments