Skip to content

Commit e7bfbfb

Browse files
committed
fix: buffer attachment logic in various conditions
fixes 547
1 parent 31c87a0 commit e7bfbfb

File tree

3 files changed

+85
-33
lines changed

3 files changed

+85
-33
lines changed

lua/copilot/client/init.lua

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ local client_config = require("copilot.client.config")
88
local is_disabled = false
99

1010
---@class CopilotClient
11-
---@field augroup string|nil
1211
---@field id integer|nil
1312
---@field capabilities lsp.ClientCapabilities | nil
1413
---@field config vim.lsp.ClientConfig | nil
1514
---@field startup_error string | nil
1615
---@field initialized boolean
1716
local M = {
18-
augroup = nil,
1917
id = nil,
2018
capabilities = nil,
2119
config = nil,
@@ -42,6 +40,11 @@ end
4240
function M.buf_attach(force)
4341
local bufnr = vim.api.nvim_get_current_buf()
4442

43+
if M.buf_is_attached(bufnr) then
44+
logger.trace("buffer already attached")
45+
return
46+
end
47+
4548
if lsp.initialization_failed() then
4649
logger.error("copilot-language-server failed to initialize")
4750
M.startup_error = "initialization of copilot-language-server failed"
@@ -153,32 +156,13 @@ function M.setup()
153156
end
154157

155158
is_disabled = false
156-
157159
M.id = nil
158-
159-
-- nvim_clear_autocmds throws an error if the group does not exist
160-
local augroup = "copilot.client"
161-
vim.api.nvim_create_augroup(augroup, { clear = true })
162-
M.augroup = augroup
163-
164-
vim.api.nvim_create_autocmd("FileType", {
165-
group = M.augroup,
166-
callback = vim.schedule_wrap(function()
167-
M.buf_attach()
168-
end),
169-
})
170-
171160
vim.schedule(M.ensure_client_started)
172161
end
173162

174163
function M.teardown()
175164
is_disabled = true
176165

177-
-- nvim_clear_autocmds throws an error if the group does not exist
178-
if M.augroup then
179-
vim.api.nvim_clear_autocmds({ group = M.augroup })
180-
end
181-
182166
if M.id then
183167
vim.lsp.stop_client(M.id)
184168
end

lua/copilot/suggestion/init.lua

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,20 @@ local function schedule(ctx)
505505
end)
506506
end
507507

508+
---@param ctx copilot_suggestion_context
509+
---@param caller_context string
510+
---@return boolean
511+
function M.first_request_scheduled(ctx, caller_context)
512+
if not ctx.first then
513+
logger.trace("suggestion " .. caller_context .. ", no first request", ctx)
514+
c.buf_attach()
515+
schedule(ctx)
516+
return true
517+
end
518+
519+
return false
520+
end
521+
508522
function M.next()
509523
local ctx = get_ctx()
510524
logger.trace("suggestion next", ctx)
@@ -513,10 +527,7 @@ function M.next()
513527
reset_ctx(ctx)
514528
end
515529

516-
-- no suggestion request yet
517-
if not ctx.first then
518-
logger.trace("suggestion next, no first request")
519-
schedule(ctx)
530+
if M.first_request_scheduled(ctx, "next") then
520531
return
521532
end
522533

@@ -533,10 +544,7 @@ function M.prev()
533544
reset_ctx(ctx)
534545
end
535546

536-
-- no suggestion request yet
537-
if not ctx.first then
538-
logger.trace("suggestion prev, no first request", ctx)
539-
schedule(ctx)
547+
if M.first_request_scheduled(ctx, "prev") then
540548
return
541549
end
542550

@@ -550,10 +558,7 @@ function M.accept(modifier)
550558
local ctx = get_ctx()
551559
logger.trace("suggestion accept", ctx)
552560

553-
-- no suggestion request yet
554-
if (not ctx.first) and config.suggestion.trigger_on_accept then
555-
logger.trace("suggestion accept, not first request", ctx)
556-
schedule(ctx)
561+
if config.suggestion.trigger_on_accept and M.first_request_scheduled(ctx, "suggestion accept") then
557562
return
558563
end
559564

@@ -722,6 +727,11 @@ local function on_insert_enter()
722727
end
723728
end
724729

730+
local function on_filetype()
731+
logger.trace("suggestion on file type")
732+
on_insert_enter()
733+
end
734+
725735
local function on_buf_enter()
726736
if vim.fn.mode():match("^[iR]") then
727737
logger.trace("suggestion on buf enter")
@@ -738,6 +748,7 @@ local function on_cursor_moved_i()
738748
local ctx = get_ctx()
739749
if copilot._copilot_timer or ctx.params or should_auto_trigger() then
740750
logger.trace("suggestion on cursor moved insert")
751+
c.buf_attach()
741752
schedule(ctx)
742753
end
743754
end
@@ -746,6 +757,7 @@ local function on_text_changed_p()
746757
local ctx = get_ctx()
747758
if not copilot.hide_during_completion and (copilot._copilot_timer or ctx.params or should_auto_trigger()) then
748759
logger.trace("suggestion on text changed pum")
760+
c.buf_attach()
749761
schedule(ctx)
750762
end
751763
end
@@ -785,6 +797,12 @@ local function create_autocmds()
785797
desc = "[copilot] (suggestion) insert enter",
786798
})
787799

800+
vim.api.nvim_create_autocmd("FileType", {
801+
group = M.augroup,
802+
callback = on_filetype,
803+
desc = "[copilot] (suggestion) file type",
804+
})
805+
788806
vim.api.nvim_create_autocmd("BufEnter", {
789807
group = copilot.augroup,
790808
callback = on_buf_enter,

tests/test_client.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,54 @@ T["client()"]["will not attach to buffer due to filetype exclusion"] = function(
208208
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
209209
end
210210

211+
T["client()"]["auto_trigger off - will not attach automatically"] = function()
212+
child.configure_copilot()
213+
child.cmd("e test.txt")
214+
child.type_keys("i", "<Esc>")
215+
child.cmd("Copilot status")
216+
217+
local messages = child.lua([[
218+
local messages = ""
219+
local function has_passed()
220+
messages = vim.api.nvim_exec("messages", { output = true }) or ""
221+
if messages:find(".*Online.*Enabled.*") then
222+
return true
223+
end
224+
end
225+
226+
vim.wait(1000, function()
227+
return has_passed()
228+
end, 50)
229+
230+
return messages
231+
]])
232+
233+
u.expect_no_match(messages, ".*Online.*Enabled.*")
234+
end
235+
236+
T["client()"]["auto_trigger off - will attach when requesting suggestion"] = function()
237+
child.configure_copilot()
238+
child.cmd("e test.txt")
239+
child.type_keys("i", "<M-l>", "<Esc>")
240+
child.cmd("Copilot status")
241+
242+
local messages = child.lua([[
243+
local messages = ""
244+
local function has_passed()
245+
messages = vim.api.nvim_exec("messages", { output = true }) or ""
246+
if messages:find(".*Online.*Enabled.*") then
247+
return true
248+
end
249+
end
250+
251+
vim.wait(1000, function()
252+
return has_passed()
253+
end, 50)
254+
255+
return messages
256+
]])
257+
258+
u.expect_match(messages, ".*Online.*Enabled.*")
259+
end
260+
211261
return T

0 commit comments

Comments
 (0)