Skip to content

Commit 1e37de6

Browse files
committed
feat: improve Copilot status with attach/detached reasons
1 parent 76c6617 commit 1e37de6

File tree

4 files changed

+30
-39
lines changed

4 files changed

+30
-39
lines changed

lua/copilot/client/init.lua

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ local is_disabled = false
1313
---@field config vim.lsp.ClientConfig | nil
1414
---@field startup_error string | nil
1515
---@field initialized boolean
16+
-- Only for informational purposes, use Vim API to check actual status
17+
---@field buffer_statuses table<integer, string>
1618
local M = {
1719
id = nil,
1820
capabilities = nil,
1921
config = nil,
2022
startup_error = nil,
2123
initialized = false,
24+
buffer_statuses = {},
2225
}
2326

2427
---@param id integer
@@ -56,8 +59,11 @@ function M.buf_attach(force)
5659
return
5760
end
5861

59-
if not (force or util.should_attach()) then
60-
logger.debug("not attaching to buffer based on force and should_attach criteria")
62+
local should_attach, reason = util.should_attach()
63+
64+
if not (force or should_attach) then
65+
logger.debug("not attaching to buffer based should_attach criteria: " .. reason)
66+
M.buffer_statuses[bufnr] = "not attached based on " .. reason
6167
return
6268
end
6369

@@ -73,22 +79,25 @@ function M.buf_attach(force)
7379
end
7480

7581
if not M.id then
76-
logger.trace("failed to start copilot client")
82+
logger.error("failed to start copilot client")
7783
return
7884
end
7985

80-
-- This could cause slowdowns when going into Insert mode
81-
if not vim.lsp.buf_is_attached(bufnr, M.id) then
82-
vim.lsp.buf_attach_client(bufnr, M.id)
83-
logger.trace("explicitly attached client to buffer")
86+
vim.lsp.buf_attach_client(bufnr, M.id)
87+
if force then
88+
logger.debug("force attached to buffer")
89+
M.buffer_statuses[bufnr] = "force attached"
90+
else
91+
logger.trace("buffer attached")
92+
M.buffer_statuses[bufnr] = "attached"
8493
end
85-
86-
logger.trace("buffer attached")
8794
end
8895

8996
function M.buf_detach()
9097
if M.buf_is_attached(0) then
9198
vim.lsp.buf_detach_client(0, M.id)
99+
logger.trace("buffer detached")
100+
M.buffer_statuses[vim.api.nvim_get_current_buf()] = "manually detached"
92101
end
93102
end
94103

lua/copilot/status/init.lua

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local u = require("copilot.util")
21
local logger = require("copilot.logger")
32
---@alias copilot_status_notification_data { status: ''|'Normal'|'InProgress'|'Warning', message: string }
43

@@ -98,26 +97,11 @@ function M.status()
9897
return
9998
end
10099

101-
local should_attach, no_attach_reason = u.should_attach()
102-
local is_attached = c.buf_is_attached()
103-
if is_attached then
104-
if not should_attach then
105-
add_line("Enabled manually (" .. no_attach_reason .. ")")
106-
elseif vim.bo.filetype and vim.bo.filetype ~= "" then
107-
add_line("Enabled for " .. vim.bo.filetype)
108-
else
109-
add_line("Enabled")
110-
end
111-
elseif not is_attached then
112-
if should_attach then
113-
if vim.bo.filetype and vim.bo.filetype ~= "" then
114-
add_line("Disabled manually for " .. vim.bo.filetype)
115-
else
116-
add_line("Disabled manually")
117-
end
118-
else
119-
add_line("Disabled (" .. no_attach_reason .. ")")
120-
end
100+
local buffer_status = c.buffer_statuses[vim.api.nvim_get_current_buf()]
101+
if buffer_status then
102+
add_line("Buffer status: " .. c.buffer_statuses[vim.api.nvim_get_current_buf()])
103+
else
104+
add_line("Buffer status: Attach not yet requested")
121105
end
122106

123107
if string.lower(M.data.status) == "error" then

lua/copilot/util.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function M.should_attach()
4747
local conf_attach = config.should_attach(bufnr, bufname)
4848

4949
if not conf_attach then
50-
return false, "copilot is disabled"
50+
return false, "should_attach config"
5151
end
5252

5353
return true

tests/test_client.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ T["client()"]["status info"] = function()
3535
local messages = ""
3636
local function has_passed()
3737
messages = vim.api.nvim_exec("messages", { output = true }) or ""
38-
if messages:find(".*Online.*Enabled.*") then
38+
if messages:find(".*Online.*not yet requested.*") then
3939
return true
4040
end
4141
end
@@ -47,11 +47,9 @@ T["client()"]["status info"] = function()
4747
return messages
4848
]])
4949

50-
u.expect_match(messages, ".*Online.*Enabled.*")
50+
u.expect_match(messages, ".*Online.*not yet requested.*")
5151
end
5252

53-
T["client()"] = MiniTest.new_set()
54-
5553
T["client()"]["suggestions work when multiple files open with should_attach logic"] = function()
5654
child.config.should_attach = [[function(bufnr, bufname)
5755
local buffername = bufname:match("([^/\\]+)$") or ""
@@ -218,7 +216,7 @@ T["client()"]["auto_trigger off - will not attach automatically"] = function()
218216
local messages = ""
219217
local function has_passed()
220218
messages = vim.api.nvim_exec("messages", { output = true }) or ""
221-
if messages:find(".*Online.*Enabled.*") then
219+
if messages:find(".*Online.*attached.*") then
222220
return true
223221
end
224222
end
@@ -230,7 +228,7 @@ T["client()"]["auto_trigger off - will not attach automatically"] = function()
230228
return messages
231229
]])
232230

233-
u.expect_no_match(messages, ".*Online.*Enabled.*")
231+
u.expect_no_match(messages, ".*Online.*attached.*")
234232
end
235233

236234
T["client()"]["auto_trigger off - will attach when requesting suggestion"] = function()
@@ -242,7 +240,7 @@ T["client()"]["auto_trigger off - will attach when requesting suggestion"] = fun
242240
local messages = ""
243241
local function has_passed()
244242
messages = vim.api.nvim_exec("messages", { output = true }) or ""
245-
if messages:find(".*Online.*Enabled.*") then
243+
if messages:find(".*Online.*attached.*") then
246244
return true
247245
end
248246
end
@@ -254,7 +252,7 @@ T["client()"]["auto_trigger off - will attach when requesting suggestion"] = fun
254252
return messages
255253
]])
256254

257-
u.expect_match(messages, ".*Online.*Enabled.*")
255+
u.expect_match(messages, ".*Online.*attached.*")
258256
end
259257

260258
return T

0 commit comments

Comments
 (0)