Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 36 additions & 30 deletions lua/copilot/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ end

---@param force? boolean
function M.buf_attach(force)
local bufnr = vim.api.nvim_get_current_buf()
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
if lsp.initialization_failed() then
logger.error("copilot-language-server failed to initialize")
M.startup_error = "initialization of copilot-language-server failed"
Expand All @@ -55,6 +57,10 @@ function M.buf_attach(force)
return
end

if not M.config then
logger.debug("initializing config for attachable buffer")
M.config = client_config.create(config)
end
if not M.config then
logger.error("cannot attach: configuration not initialized")
return
Expand All @@ -64,17 +70,20 @@ function M.buf_attach(force)
M.config.root_dir = utils.get_root_dir(config.root_dir)

logger.trace("attaching to buffer")
local ok, client_id_or_err = pcall(vim.lsp.start, M.config)
if not ok then
logger.error(string.format("failed to start LSP client: %s", client_id_or_err))
return
end

if client_id_or_err then
store_client_id(client_id_or_err)
else
logger.error("LSP client failed to start (no client ID returned)")
-- Only attach client to buffer, do not start client here
local client = M.get()
if client and not vim.lsp.buf_is_attached(bufnr, client.id) then
vim.lsp.buf_attach_client(bufnr, client.id)
logger.trace("explicitly attached client to buffer", bufnr, client.id)
end
logger.debug(
string.format(
"[buf_attach] After attach: bufnr=%s, filetype=%s, attached=%s",
tostring(bufnr),
tostring(filetype),
tostring(client and vim.lsp.buf_is_attached(bufnr, client.id) or false)
)
)
logger.trace("buffer attached")
end

Expand All @@ -93,39 +102,36 @@ function M.is_disabled()
return is_disabled
end

---@param callback fun(client:table):nil
function M.use_client(callback)
function M.ensure_client_started()
if is_disabled then
logger.notify("copilot is offline")
return
end

local client = M.get()

if not client then
if not M.config then
logger.error("copilot.setup is not called yet")
return
end

client_config.add_callback(callback)
if not M.config then
M.config = client_config.create(config)
end

if not util.should_attach() then
logger.debug("not attaching to buffer based on should_attach criteria")
return
end
if not M.config then
logger.error("copilot.setup is not called yet")
return
end

if not M.id then
local client_id, err = vim.lsp.start(M.config)

if not client_id then
logger.error(string.format("error starting LSP client: %s", err))
return
end

store_client_id(client_id)
elseif not client.initialized then
client_config.add_callback(callback)
else
end
end

---@param callback fun(client:table):nil
function M.use_client(callback)
M.ensure_client_started()
local client = M.get()
if client then
callback(client)
end
end
Expand Down
3 changes: 3 additions & 0 deletions lua/copilot/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function M.should_attach()
local bufnr = vim.api.nvim_get_current_buf()
local bufname = vim.api.nvim_buf_get_name(bufnr)
local conf_attach = config.should_attach(bufnr, bufname)
logger.debug(
"should_attach: " .. tostring(conf_attach) .. " for buffer: " .. bufnr .. " and filetype:" .. vim.bo.filetype
)

if not conf_attach then
return false, "copilot is disabled"
Expand Down
42 changes: 30 additions & 12 deletions tests/child_helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,23 @@ function M.new_child_neovim(test_name)
for k, v in pairs(child.config) do
if v ~= "" and v ~= nil then
if type(v) == "string" then
script = string.format(
[[%s%s = {
%s
},
]],
script,
k,
v
)
if v:sub(1, 8) == "function" then
script = string.format(
[[%s
%s = %s,]],
script,
k,
v
)
else
script = string.format(
[[%s
%s = { %s },]],
script,
k,
v
)
end
end
end
end
Expand All @@ -93,6 +101,16 @@ function M.new_child_neovim(test_name)
script
)

-- write to temporary file for debugging purposes
local tmpfile = string.format("./tests/logs/test_config.txt")
local file = io.open(tmpfile, "w")
if file then
file:write(script)
file:close()
else
error("Could not open temporary file for writing: " .. tmpfile)
end

child.lua(script)

child.lua([[
Expand All @@ -101,13 +119,13 @@ function M.new_child_neovim(test_name)
return client.initialized
end

vim.wait(30000, copilot_is_initialized, 10)
vim.wait(5000, copilot_is_initialized, 10)
]])
end

function child.wait_for_suggestion()
child.lua([[
vim.wait(30000, function()
vim.wait(5000, function()
return M.suggested
end, 10)
]])
Expand All @@ -120,7 +138,7 @@ function M.new_child_neovim(test_name)
return lines[1] and lines[1] ~= ""
end

vim.wait(30000, function()
vim.wait(5000, function()
return suggestion_is_visible()
end, 50)
]])
Expand Down
Empty file added tests/files/file1.txt
Empty file.
Empty file added tests/files/file2.txt
Empty file.
Empty file added tests/files/file3.txt
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--|---------|---------|---------|---------|---------|---------|---------|---------|
01|123
02|456
03|7
04|~
05|~
06|~
07|~
08|~
09|~
10|~
11|~
12|~
13|~
14|~
15|~
16|~
17|~
18|~
19|~
20|~
21|~
22|~
23|tests/files/file2.txt [+] 3,2 All
24|-- INSERT --

--|---------|---------|---------|---------|---------|---------|---------|---------|
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
02|00000000000000000000000000000000000000000000000000000000000000000000000000000000
03|00000000000000000000000000000000000000000000000000000000000000000000000000000000
04|11111111111111111111111111111111111111111111111111111111111111111111111111111111
05|11111111111111111111111111111111111111111111111111111111111111111111111111111111
06|11111111111111111111111111111111111111111111111111111111111111111111111111111111
07|11111111111111111111111111111111111111111111111111111111111111111111111111111111
08|11111111111111111111111111111111111111111111111111111111111111111111111111111111
09|11111111111111111111111111111111111111111111111111111111111111111111111111111111
10|11111111111111111111111111111111111111111111111111111111111111111111111111111111
11|11111111111111111111111111111111111111111111111111111111111111111111111111111111
12|11111111111111111111111111111111111111111111111111111111111111111111111111111111
13|11111111111111111111111111111111111111111111111111111111111111111111111111111111
14|11111111111111111111111111111111111111111111111111111111111111111111111111111111
15|11111111111111111111111111111111111111111111111111111111111111111111111111111111
16|11111111111111111111111111111111111111111111111111111111111111111111111111111111
17|11111111111111111111111111111111111111111111111111111111111111111111111111111111
18|11111111111111111111111111111111111111111111111111111111111111111111111111111111
19|11111111111111111111111111111111111111111111111111111111111111111111111111111111
20|11111111111111111111111111111111111111111111111111111111111111111111111111111111
21|11111111111111111111111111111111111111111111111111111111111111111111111111111111
22|11111111111111111111111111111111111111111111111111111111111111111111111111111111
23|22222222222222222222222222222222222222222222222222222222222222222222222222222222
24|33333333333344444444444444444444444444444444444444444444444444444444444444444444
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--|---------|---------|---------|---------|---------|---------|---------|---------|
01|123
02|456
03|7
04|~
05|~
06|~
07|~
08|~
09|~
10|~
11|~
12|~
13|~
14|~
15|~
16|~
17|~
18|~
19|~
20|~
21|~
22|~
23|tests/files/file3.txt [+] 3,2 All
24|-- INSERT --

--|---------|---------|---------|---------|---------|---------|---------|---------|
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
02|00000000000000000000000000000000000000000000000000000000000000000000000000000000
03|00000000000000000000000000000000000000000000000000000000000000000000000000000000
04|11111111111111111111111111111111111111111111111111111111111111111111111111111111
05|11111111111111111111111111111111111111111111111111111111111111111111111111111111
06|11111111111111111111111111111111111111111111111111111111111111111111111111111111
07|11111111111111111111111111111111111111111111111111111111111111111111111111111111
08|11111111111111111111111111111111111111111111111111111111111111111111111111111111
09|11111111111111111111111111111111111111111111111111111111111111111111111111111111
10|11111111111111111111111111111111111111111111111111111111111111111111111111111111
11|11111111111111111111111111111111111111111111111111111111111111111111111111111111
12|11111111111111111111111111111111111111111111111111111111111111111111111111111111
13|11111111111111111111111111111111111111111111111111111111111111111111111111111111
14|11111111111111111111111111111111111111111111111111111111111111111111111111111111
15|11111111111111111111111111111111111111111111111111111111111111111111111111111111
16|11111111111111111111111111111111111111111111111111111111111111111111111111111111
17|11111111111111111111111111111111111111111111111111111111111111111111111111111111
18|11111111111111111111111111111111111111111111111111111111111111111111111111111111
19|11111111111111111111111111111111111111111111111111111111111111111111111111111111
20|11111111111111111111111111111111111111111111111111111111111111111111111111111111
21|11111111111111111111111111111111111111111111111111111111111111111111111111111111
22|11111111111111111111111111111111111111111111111111111111111111111111111111111111
23|22222222222222222222222222222222222222222222222222222222222222222222222222222222
24|33333333333344444444444444444444444444444444444444444444444444444444444444444444
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--|---------|---------|---------|---------|---------|---------|---------|---------|
01|123
02|456
03|789
04|~
05|~
06|~
07|~
08|~
09|~
10|~
11|~
12|~
13|~
14|~
15|~
16|~
17|~
18|~
19|~
20|~
21|~
22|~
23|tests/files/file3.txt [+] 3,2 All
24|-- INSERT --

--|---------|---------|---------|---------|---------|---------|---------|---------|
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
02|00000000000000000000000000000000000000000000000000000000000000000000000000000000
03|01100000000000000000000000000000000000000000000000000000000000000000000000000000
04|22222222222222222222222222222222222222222222222222222222222222222222222222222222
05|22222222222222222222222222222222222222222222222222222222222222222222222222222222
06|22222222222222222222222222222222222222222222222222222222222222222222222222222222
07|22222222222222222222222222222222222222222222222222222222222222222222222222222222
08|22222222222222222222222222222222222222222222222222222222222222222222222222222222
09|22222222222222222222222222222222222222222222222222222222222222222222222222222222
10|22222222222222222222222222222222222222222222222222222222222222222222222222222222
11|22222222222222222222222222222222222222222222222222222222222222222222222222222222
12|22222222222222222222222222222222222222222222222222222222222222222222222222222222
13|22222222222222222222222222222222222222222222222222222222222222222222222222222222
14|22222222222222222222222222222222222222222222222222222222222222222222222222222222
15|22222222222222222222222222222222222222222222222222222222222222222222222222222222
16|22222222222222222222222222222222222222222222222222222222222222222222222222222222
17|22222222222222222222222222222222222222222222222222222222222222222222222222222222
18|22222222222222222222222222222222222222222222222222222222222222222222222222222222
19|22222222222222222222222222222222222222222222222222222222222222222222222222222222
20|22222222222222222222222222222222222222222222222222222222222222222222222222222222
21|22222222222222222222222222222222222222222222222222222222222222222222222222222222
22|22222222222222222222222222222222222222222222222222222222222222222222222222222222
23|33333333333333333333333333333333333333333333333333333333333333333333333333333333
24|44444444444455555555555555555555555555555555555555555555555555555555555555555555
Loading