Skip to content

Commit 2d7261d

Browse files
committed
Revert "fix: multiple buffers opened cause incorrect should_attach logic (#521)"
This reverts commit d921204.
1 parent 0e1348e commit 2d7261d

12 files changed

+44
-387
lines changed

lua/copilot/client/init.lua

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ end
3939

4040
---@param force? boolean
4141
function M.buf_attach(force)
42-
local bufnr = vim.api.nvim_get_current_buf()
43-
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
4442
if lsp.initialization_failed() then
4543
logger.error("copilot-language-server failed to initialize")
4644
M.startup_error = "initialization of copilot-language-server failed"
@@ -57,10 +55,6 @@ function M.buf_attach(force)
5755
return
5856
end
5957

60-
if not M.config then
61-
logger.debug("initializing config for attachable buffer")
62-
M.config = client_config.create(config)
63-
end
6458
if not M.config then
6559
logger.error("cannot attach: configuration not initialized")
6660
return
@@ -70,20 +64,17 @@ function M.buf_attach(force)
7064
M.config.root_dir = utils.get_root_dir(config.root_dir)
7165

7266
logger.trace("attaching to buffer")
73-
-- Only attach client to buffer, do not start client here
74-
local client = M.get()
75-
if client and not vim.lsp.buf_is_attached(bufnr, client.id) then
76-
vim.lsp.buf_attach_client(bufnr, client.id)
77-
logger.trace("explicitly attached client to buffer", bufnr, client.id)
67+
local ok, client_id_or_err = pcall(vim.lsp.start, M.config)
68+
if not ok then
69+
logger.error(string.format("failed to start LSP client: %s", client_id_or_err))
70+
return
71+
end
72+
73+
if client_id_or_err then
74+
store_client_id(client_id_or_err)
75+
else
76+
logger.error("LSP client failed to start (no client ID returned)")
7877
end
79-
logger.debug(
80-
string.format(
81-
"[buf_attach] After attach: bufnr=%s, filetype=%s, attached=%s",
82-
tostring(bufnr),
83-
tostring(filetype),
84-
tostring(client and vim.lsp.buf_is_attached(bufnr, client.id) or false)
85-
)
86-
)
8778
logger.trace("buffer attached")
8879
end
8980

@@ -102,36 +93,39 @@ function M.is_disabled()
10293
return is_disabled
10394
end
10495

105-
function M.ensure_client_started()
96+
---@param callback fun(client:table):nil
97+
function M.use_client(callback)
10698
if is_disabled then
10799
logger.notify("copilot is offline")
108100
return
109101
end
110102

111-
if not M.config then
112-
M.config = client_config.create(config)
113-
end
103+
local client = M.get()
114104

115-
if not M.config then
116-
logger.error("copilot.setup is not called yet")
117-
return
118-
end
105+
if not client then
106+
if not M.config then
107+
logger.error("copilot.setup is not called yet")
108+
return
109+
end
110+
111+
client_config.add_callback(callback)
112+
113+
if not util.should_attach() then
114+
logger.debug("not attaching to buffer based on should_attach criteria")
115+
return
116+
end
119117

120-
if not M.id then
121118
local client_id, err = vim.lsp.start(M.config)
119+
122120
if not client_id then
123121
logger.error(string.format("error starting LSP client: %s", err))
124122
return
125123
end
126-
store_client_id(client_id)
127-
end
128-
end
129124

130-
---@param callback fun(client:table):nil
131-
function M.use_client(callback)
132-
M.ensure_client_started()
133-
local client = M.get()
134-
if client then
125+
store_client_id(client_id)
126+
elseif not client.initialized then
127+
client_config.add_callback(callback)
128+
else
135129
callback(client)
136130
end
137131
end

lua/copilot/util.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ function M.should_attach()
4545
local bufnr = vim.api.nvim_get_current_buf()
4646
local bufname = vim.api.nvim_buf_get_name(bufnr)
4747
local conf_attach = config.should_attach(bufnr, bufname)
48-
logger.debug(
49-
"should_attach: " .. tostring(conf_attach) .. " for buffer: " .. bufnr .. " and filetype:" .. vim.bo.filetype
50-
)
5148

5249
if not conf_attach then
5350
return false, "copilot is disabled"

tests/child_helper.lua

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,15 @@ function M.new_child_neovim(test_name)
7272
for k, v in pairs(child.config) do
7373
if v ~= "" and v ~= nil then
7474
if type(v) == "string" then
75-
if v:sub(1, 8) == "function" then
76-
script = string.format(
77-
[[%s
78-
%s = %s,]],
79-
script,
80-
k,
81-
v
82-
)
83-
else
84-
script = string.format(
85-
[[%s
86-
%s = { %s },]],
87-
script,
88-
k,
89-
v
90-
)
91-
end
75+
script = string.format(
76+
[[%s%s = {
77+
%s
78+
},
79+
]],
80+
script,
81+
k,
82+
v
83+
)
9284
end
9385
end
9486
end
@@ -101,16 +93,6 @@ function M.new_child_neovim(test_name)
10193
script
10294
)
10395

104-
-- write to temporary file for debugging purposes
105-
local tmpfile = string.format("./tests/logs/test_config.txt")
106-
local file = io.open(tmpfile, "w")
107-
if file then
108-
file:write(script)
109-
file:close()
110-
else
111-
error("Could not open temporary file for writing: " .. tmpfile)
112-
end
113-
11496
child.lua(script)
11597

11698
child.lua([[
@@ -119,13 +101,13 @@ function M.new_child_neovim(test_name)
119101
return client.initialized
120102
end
121103
122-
vim.wait(5000, copilot_is_initialized, 10)
104+
vim.wait(30000, copilot_is_initialized, 10)
123105
]])
124106
end
125107

126108
function child.wait_for_suggestion()
127109
child.lua([[
128-
vim.wait(5000, function()
110+
vim.wait(30000, function()
129111
return M.suggested
130112
end, 10)
131113
]])
@@ -138,7 +120,7 @@ function M.new_child_neovim(test_name)
138120
return lines[1] and lines[1] ~= ""
139121
end
140122
141-
vim.wait(5000, function()
123+
vim.wait(30000, function()
142124
return suggestion_is_visible()
143125
end, 50)
144126
]])

tests/files/file1.txt

Whitespace-only changes.

tests/files/file2.txt

Whitespace-only changes.

tests/files/file3.txt

Whitespace-only changes.

tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach

Lines changed: 0 additions & 51 deletions
This file was deleted.

tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach---2

Lines changed: 0 additions & 51 deletions
This file was deleted.

tests/screenshots/tests-test_client.lua---client()---suggestions-work-when-already-in-insert-mode-and-opening-file---3

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)