Skip to content

Commit aa00b0b

Browse files
committed
fix: ensure attachment even when suggestion is disabled
1 parent a6af909 commit aa00b0b

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lua/copilot/client/init.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local client_config = require("copilot.client.config")
88
local 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>
1819
local 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)
170192
end
171193

172194
function 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

lua/copilot/suggestion/init.lua

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,6 @@ local function on_insert_enter()
740740
request_suggestion_when_auto_trigger("insert enter")
741741
end
742742

743-
local function on_filetype()
744-
request_suggestion_when_auto_trigger("file type")
745-
end
746-
747743
local function on_buf_enter()
748744
if vim.fn.mode():match("^[iR]") then
749745
request_suggestion_when_auto_trigger("buf enter")
@@ -805,12 +801,6 @@ local function create_autocmds()
805801
desc = "[copilot] (suggestion) insert enter",
806802
})
807803

808-
vim.api.nvim_create_autocmd("FileType", {
809-
group = M.augroup,
810-
callback = on_filetype,
811-
desc = "[copilot] (suggestion) file type",
812-
})
813-
814804
vim.api.nvim_create_autocmd("BufEnter", {
815805
group = copilot.augroup,
816806
callback = on_buf_enter,

0 commit comments

Comments
 (0)