Skip to content

Commit d921204

Browse files
authored
fix: multiple buffers opened cause incorrect should_attach logic (#521)
The first buffer would be taken as whether or not the LSP should attach to other buffers
1 parent 4958fb9 commit d921204

12 files changed

+387
-44
lines changed

lua/copilot/client/init.lua

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ 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")
4244
if lsp.initialization_failed() then
4345
logger.error("copilot-language-server failed to initialize")
4446
M.startup_error = "initialization of copilot-language-server failed"
@@ -55,6 +57,10 @@ function M.buf_attach(force)
5557
return
5658
end
5759

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

6672
logger.trace("attaching to buffer")
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)")
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)
7778
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+
)
7887
logger.trace("buffer attached")
7988
end
8089

@@ -93,39 +102,36 @@ function M.is_disabled()
93102
return is_disabled
94103
end
95104

96-
---@param callback fun(client:table):nil
97-
function M.use_client(callback)
105+
function M.ensure_client_started()
98106
if is_disabled then
99107
logger.notify("copilot is offline")
100108
return
101109
end
102110

103-
local client = M.get()
104-
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)
111+
if not M.config then
112+
M.config = client_config.create(config)
113+
end
112114

113-
if not util.should_attach() then
114-
logger.debug("not attaching to buffer based on should_attach criteria")
115-
return
116-
end
115+
if not M.config then
116+
logger.error("copilot.setup is not called yet")
117+
return
118+
end
117119

120+
if not M.id then
118121
local client_id, err = vim.lsp.start(M.config)
119-
120122
if not client_id then
121123
logger.error(string.format("error starting LSP client: %s", err))
122124
return
123125
end
124-
125126
store_client_id(client_id)
126-
elseif not client.initialized then
127-
client_config.add_callback(callback)
128-
else
127+
end
128+
end
129+
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
129135
callback(client)
130136
end
131137
end

lua/copilot/util.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ 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+
)
4851

4952
if not conf_attach then
5053
return false, "copilot is disabled"

tests/child_helper.lua

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,23 @@ 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-
script = string.format(
76-
[[%s%s = {
77-
%s
78-
},
79-
]],
80-
script,
81-
k,
82-
v
83-
)
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
8492
end
8593
end
8694
end
@@ -93,6 +101,16 @@ function M.new_child_neovim(test_name)
93101
script
94102
)
95103

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+
96114
child.lua(script)
97115

98116
child.lua([[
@@ -101,13 +119,13 @@ function M.new_child_neovim(test_name)
101119
return client.initialized
102120
end
103121
104-
vim.wait(30000, copilot_is_initialized, 10)
122+
vim.wait(5000, copilot_is_initialized, 10)
105123
]])
106124
end
107125

108126
function child.wait_for_suggestion()
109127
child.lua([[
110-
vim.wait(30000, function()
128+
vim.wait(5000, function()
111129
return M.suggested
112130
end, 10)
113131
]])
@@ -120,7 +138,7 @@ function M.new_child_neovim(test_name)
120138
return lines[1] and lines[1] ~= ""
121139
end
122140
123-
vim.wait(30000, function()
141+
vim.wait(5000, function()
124142
return suggestion_is_visible()
125143
end, 50)
126144
]])

tests/files/file1.txt

Whitespace-only changes.

tests/files/file2.txt

Whitespace-only changes.

tests/files/file3.txt

Whitespace-only changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--|---------|---------|---------|---------|---------|---------|---------|---------|
2+
01|123
3+
02|456
4+
03|7
5+
04|~
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|~
11+
10|~
12+
11|~
13+
12|~
14+
13|~
15+
14|~
16+
15|~
17+
16|~
18+
17|~
19+
18|~
20+
19|~
21+
20|~
22+
21|~
23+
22|~
24+
23|tests/files/file2.txt [+] 3,2 All
25+
24|-- INSERT --
26+
27+
--|---------|---------|---------|---------|---------|---------|---------|---------|
28+
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
29+
02|00000000000000000000000000000000000000000000000000000000000000000000000000000000
30+
03|00000000000000000000000000000000000000000000000000000000000000000000000000000000
31+
04|11111111111111111111111111111111111111111111111111111111111111111111111111111111
32+
05|11111111111111111111111111111111111111111111111111111111111111111111111111111111
33+
06|11111111111111111111111111111111111111111111111111111111111111111111111111111111
34+
07|11111111111111111111111111111111111111111111111111111111111111111111111111111111
35+
08|11111111111111111111111111111111111111111111111111111111111111111111111111111111
36+
09|11111111111111111111111111111111111111111111111111111111111111111111111111111111
37+
10|11111111111111111111111111111111111111111111111111111111111111111111111111111111
38+
11|11111111111111111111111111111111111111111111111111111111111111111111111111111111
39+
12|11111111111111111111111111111111111111111111111111111111111111111111111111111111
40+
13|11111111111111111111111111111111111111111111111111111111111111111111111111111111
41+
14|11111111111111111111111111111111111111111111111111111111111111111111111111111111
42+
15|11111111111111111111111111111111111111111111111111111111111111111111111111111111
43+
16|11111111111111111111111111111111111111111111111111111111111111111111111111111111
44+
17|11111111111111111111111111111111111111111111111111111111111111111111111111111111
45+
18|11111111111111111111111111111111111111111111111111111111111111111111111111111111
46+
19|11111111111111111111111111111111111111111111111111111111111111111111111111111111
47+
20|11111111111111111111111111111111111111111111111111111111111111111111111111111111
48+
21|11111111111111111111111111111111111111111111111111111111111111111111111111111111
49+
22|11111111111111111111111111111111111111111111111111111111111111111111111111111111
50+
23|22222222222222222222222222222222222222222222222222222222222222222222222222222222
51+
24|33333333333344444444444444444444444444444444444444444444444444444444444444444444
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--|---------|---------|---------|---------|---------|---------|---------|---------|
2+
01|123
3+
02|456
4+
03|7
5+
04|~
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|~
11+
10|~
12+
11|~
13+
12|~
14+
13|~
15+
14|~
16+
15|~
17+
16|~
18+
17|~
19+
18|~
20+
19|~
21+
20|~
22+
21|~
23+
22|~
24+
23|tests/files/file3.txt [+] 3,2 All
25+
24|-- INSERT --
26+
27+
--|---------|---------|---------|---------|---------|---------|---------|---------|
28+
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
29+
02|00000000000000000000000000000000000000000000000000000000000000000000000000000000
30+
03|00000000000000000000000000000000000000000000000000000000000000000000000000000000
31+
04|11111111111111111111111111111111111111111111111111111111111111111111111111111111
32+
05|11111111111111111111111111111111111111111111111111111111111111111111111111111111
33+
06|11111111111111111111111111111111111111111111111111111111111111111111111111111111
34+
07|11111111111111111111111111111111111111111111111111111111111111111111111111111111
35+
08|11111111111111111111111111111111111111111111111111111111111111111111111111111111
36+
09|11111111111111111111111111111111111111111111111111111111111111111111111111111111
37+
10|11111111111111111111111111111111111111111111111111111111111111111111111111111111
38+
11|11111111111111111111111111111111111111111111111111111111111111111111111111111111
39+
12|11111111111111111111111111111111111111111111111111111111111111111111111111111111
40+
13|11111111111111111111111111111111111111111111111111111111111111111111111111111111
41+
14|11111111111111111111111111111111111111111111111111111111111111111111111111111111
42+
15|11111111111111111111111111111111111111111111111111111111111111111111111111111111
43+
16|11111111111111111111111111111111111111111111111111111111111111111111111111111111
44+
17|11111111111111111111111111111111111111111111111111111111111111111111111111111111
45+
18|11111111111111111111111111111111111111111111111111111111111111111111111111111111
46+
19|11111111111111111111111111111111111111111111111111111111111111111111111111111111
47+
20|11111111111111111111111111111111111111111111111111111111111111111111111111111111
48+
21|11111111111111111111111111111111111111111111111111111111111111111111111111111111
49+
22|11111111111111111111111111111111111111111111111111111111111111111111111111111111
50+
23|22222222222222222222222222222222222222222222222222222222222222222222222222222222
51+
24|33333333333344444444444444444444444444444444444444444444444444444444444444444444
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--|---------|---------|---------|---------|---------|---------|---------|---------|
2+
01|123
3+
02|456
4+
03|789
5+
04|~
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|~
11+
10|~
12+
11|~
13+
12|~
14+
13|~
15+
14|~
16+
15|~
17+
16|~
18+
17|~
19+
18|~
20+
19|~
21+
20|~
22+
21|~
23+
22|~
24+
23|tests/files/file3.txt [+] 3,2 All
25+
24|-- INSERT --
26+
27+
--|---------|---------|---------|---------|---------|---------|---------|---------|
28+
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
29+
02|00000000000000000000000000000000000000000000000000000000000000000000000000000000
30+
03|01100000000000000000000000000000000000000000000000000000000000000000000000000000
31+
04|22222222222222222222222222222222222222222222222222222222222222222222222222222222
32+
05|22222222222222222222222222222222222222222222222222222222222222222222222222222222
33+
06|22222222222222222222222222222222222222222222222222222222222222222222222222222222
34+
07|22222222222222222222222222222222222222222222222222222222222222222222222222222222
35+
08|22222222222222222222222222222222222222222222222222222222222222222222222222222222
36+
09|22222222222222222222222222222222222222222222222222222222222222222222222222222222
37+
10|22222222222222222222222222222222222222222222222222222222222222222222222222222222
38+
11|22222222222222222222222222222222222222222222222222222222222222222222222222222222
39+
12|22222222222222222222222222222222222222222222222222222222222222222222222222222222
40+
13|22222222222222222222222222222222222222222222222222222222222222222222222222222222
41+
14|22222222222222222222222222222222222222222222222222222222222222222222222222222222
42+
15|22222222222222222222222222222222222222222222222222222222222222222222222222222222
43+
16|22222222222222222222222222222222222222222222222222222222222222222222222222222222
44+
17|22222222222222222222222222222222222222222222222222222222222222222222222222222222
45+
18|22222222222222222222222222222222222222222222222222222222222222222222222222222222
46+
19|22222222222222222222222222222222222222222222222222222222222222222222222222222222
47+
20|22222222222222222222222222222222222222222222222222222222222222222222222222222222
48+
21|22222222222222222222222222222222222222222222222222222222222222222222222222222222
49+
22|22222222222222222222222222222222222222222222222222222222222222222222222222222222
50+
23|33333333333333333333333333333333333333333333333333333333333333333333333333333333
51+
24|44444444444455555555555555555555555555555555555555555555555555555555555555555555

0 commit comments

Comments
 (0)