From 152b641a7e338158430dbc6c034e44be8d7382be Mon Sep 17 00:00:00 2001 From: Antoine Gaudreau Simard Date: Mon, 28 Jul 2025 10:47:32 -0400 Subject: [PATCH 1/5] WIP --- lua/copilot/client/init.lua | 2 +- lua/copilot/util.lua | 3 ++ tests/child_helper.lua | 42 ++++++++++----- tests/files/file1.txt | 0 tests/files/file2.txt | 0 tests/files/file3.txt | 0 ...ient()---correct-buffer-when-multiple-open | 51 +++++++++++++++++++ tests/test_client.lua | 47 ++++++++++++++++- 8 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 tests/files/file1.txt create mode 100644 tests/files/file2.txt create mode 100644 tests/files/file3.txt create mode 100644 tests/screenshots/tests-test_client.lua---client()---correct-buffer-when-multiple-open diff --git a/lua/copilot/client/init.lua b/lua/copilot/client/init.lua index 2d80f0fc..ef9e001d 100644 --- a/lua/copilot/client/init.lua +++ b/lua/copilot/client/init.lua @@ -111,7 +111,7 @@ function M.use_client(callback) client_config.add_callback(callback) if not util.should_attach() then - logger.debug("not attaching to buffer based on should_attach criteria") + logger.debug("use_client: not attaching to buffer based on should_attach criteria") return end diff --git a/lua/copilot/util.lua b/lua/copilot/util.lua index 41b94e33..87cda987 100644 --- a/lua/copilot/util.lua +++ b/lua/copilot/util.lua @@ -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" diff --git a/tests/child_helper.lua b/tests/child_helper.lua index 0b6b0cb4..928242c5 100644 --- a/tests/child_helper.lua +++ b/tests/child_helper.lua @@ -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 @@ -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.lua") + 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([[ @@ -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(50000, function() return M.suggested end, 10) ]]) @@ -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) ]]) diff --git a/tests/files/file1.txt b/tests/files/file1.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/files/file2.txt b/tests/files/file2.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/files/file3.txt b/tests/files/file3.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/screenshots/tests-test_client.lua---client()---correct-buffer-when-multiple-open b/tests/screenshots/tests-test_client.lua---client()---correct-buffer-when-multiple-open new file mode 100644 index 00000000..071dc9bb --- /dev/null +++ b/tests/screenshots/tests-test_client.lua---client()---correct-buffer-when-multiple-open @@ -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/file2.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 diff --git a/tests/test_client.lua b/tests/test_client.lua index 9e8f453f..a6f248d9 100644 --- a/tests/test_client.lua +++ b/tests/test_client.lua @@ -1,3 +1,4 @@ +local reference_screenshot = MiniTest.expect.reference_screenshot local child_helper = require("tests.child_helper") local child = child_helper.new_child_neovim("test_client") local u = require("tests.utils") @@ -39,7 +40,7 @@ T["client()"]["status info"] = function() end end - vim.wait(30000, function() + vim.wait(5000, function() return has_passed() end, 50) @@ -49,4 +50,48 @@ T["client()"]["status info"] = function() u.expect_match(messages, ".*Online.*Enabled.*") end +T["client()"] = MiniTest.new_set() + +-- need a hook in attach() to see if it attaches to the correct buffer +T["client()"]["suggestions work when multiple files open with should_attach logic"] = function() + child.config.should_attach = [[function(bufnr, bufname) + buffername = bufname:match("([^/\\]+)$") + + if not _G.buffername then + _G.buffername = '' + end + + _G.buffername = _G.buffername .. ' ; ' .. buffername.. ' (' .. tostring(bufnr) .. ')' + + if (bufnr == 2) then + return true + else + return false, "incorrect buffer" + end + end]] + child.config.suggestion = child.config.suggestion .. "auto_trigger = true," + + child.configure_copilot() + child.cmd("e tests/files/file1.txt") + child.cmd("e tests/files/file2.txt") + child.type_keys("i") + -- child.cmd("e tests/files/file3.txt") + child.type_keys("123", "", "o456", "", "o7") + child.wait_for_suggestion() + -- local buffername = child.lua("return tostring(_G.buffername)") + + -- local messages = child.lua([[ + -- return vim.api.nvim_exec("messages", { output = true }) or "" + -- ]]) + + -- convert buffername to only the file name + -- buffername = buffername:match("([^/\\]+)$") + + -- u.expect_match(buffername, "file3.txt") + -- print("Buffername: " .. buffername) + -- print("Messages: " .. messages) + + reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } }) +end + return T From a8871658094c62538e58d4b7c2b6954cb98abcf7 Mon Sep 17 00:00:00 2001 From: Antoine Gaudreau Simard Date: Mon, 28 Jul 2025 11:04:41 -0400 Subject: [PATCH 2/5] WIP --- ...ltiple-files-open-with-should_attach-logic} | 0 tests/test_client.lua | 18 +++++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) rename tests/screenshots/{tests-test_client.lua---client()---correct-buffer-when-multiple-open => tests-test_client.lua---client()---suggestions-work-when-multiple-files-open-with-should_attach-logic} (100%) diff --git a/tests/screenshots/tests-test_client.lua---client()---correct-buffer-when-multiple-open b/tests/screenshots/tests-test_client.lua---client()---suggestions-work-when-multiple-files-open-with-should_attach-logic similarity index 100% rename from tests/screenshots/tests-test_client.lua---client()---correct-buffer-when-multiple-open rename to tests/screenshots/tests-test_client.lua---client()---suggestions-work-when-multiple-files-open-with-should_attach-logic diff --git a/tests/test_client.lua b/tests/test_client.lua index a6f248d9..e131a849 100644 --- a/tests/test_client.lua +++ b/tests/test_client.lua @@ -57,17 +57,17 @@ T["client()"]["suggestions work when multiple files open with should_attach logi child.config.should_attach = [[function(bufnr, bufname) buffername = bufname:match("([^/\\]+)$") - if not _G.buffername then - _G.buffername = '' + if not _G.bufnames then + _G.bufnames = '' end - _G.buffername = _G.buffername .. ' ; ' .. buffername.. ' (' .. tostring(bufnr) .. ')' + _G.bufnames = _G.bufnames .. ' ; ' .. buffername.. ' (' .. tostring(bufnr) .. ')' - if (bufnr == 2) then + -- if (buffername == "file1.txt") or (buffername == "file2.txt") then return true - else - return false, "incorrect buffer" - end + -- else + -- return false, "incorrect buffer" + -- end end]] child.config.suggestion = child.config.suggestion .. "auto_trigger = true," @@ -78,7 +78,7 @@ T["client()"]["suggestions work when multiple files open with should_attach logi -- child.cmd("e tests/files/file3.txt") child.type_keys("123", "", "o456", "", "o7") child.wait_for_suggestion() - -- local buffername = child.lua("return tostring(_G.buffername)") + local bufnames = child.lua("return tostring(_G.bufnames)") -- local messages = child.lua([[ -- return vim.api.nvim_exec("messages", { output = true }) or "" @@ -88,7 +88,7 @@ T["client()"]["suggestions work when multiple files open with should_attach logi -- buffername = buffername:match("([^/\\]+)$") -- u.expect_match(buffername, "file3.txt") - -- print("Buffername: " .. buffername) + print("bufnames: " .. bufnames) -- print("Messages: " .. messages) reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } }) From 5368aebc2e3da874d7a424e7c4f972b048c2a2a4 Mon Sep 17 00:00:00 2001 From: Antoine Gaudreau Simard Date: Mon, 28 Jul 2025 11:05:42 -0400 Subject: [PATCH 3/5] wip --- tests/child_helper.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/child_helper.lua b/tests/child_helper.lua index 928242c5..595c3d33 100644 --- a/tests/child_helper.lua +++ b/tests/child_helper.lua @@ -125,7 +125,7 @@ function M.new_child_neovim(test_name) function child.wait_for_suggestion() child.lua([[ - vim.wait(50000, function() + vim.wait(5000, function() return M.suggested end, 10) ]]) From f5af9aca66deb3bb1bb43aa01f215bd1e62af4a3 Mon Sep 17 00:00:00 2001 From: Antoine Gaudreau Simard Date: Mon, 28 Jul 2025 15:34:43 -0400 Subject: [PATCH 4/5] fix: when multiple buffers are opened the first incorrectly dictates if the others will be attached --- lua/copilot/client/init.lua | 66 +++++++------- ...-off-when-previous-file-only-should_attach | 51 +++++++++++ ...-when-previous-file-only-should_attach---2 | 51 +++++++++++ ...lready-in-insert-mode-and-opening-file---3 | 51 +++++++++++ tests/test_client.lua | 90 ++++++++++++++++--- 5 files changed, 268 insertions(+), 41 deletions(-) create mode 100644 tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach create mode 100644 tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach---2 create mode 100644 tests/screenshots/tests-test_client.lua---client()---suggestions-work-when-already-in-insert-mode-and-opening-file---3 diff --git a/lua/copilot/client/init.lua b/lua/copilot/client/init.lua index ef9e001d..db8d92d2 100644 --- a/lua/copilot/client/init.lua +++ b/lua/copilot/client/init.lua @@ -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" @@ -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 @@ -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 @@ -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("use_client: 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 diff --git a/tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach b/tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach new file mode 100644 index 00000000..2a5c8b21 --- /dev/null +++ b/tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach @@ -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 diff --git a/tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach---2 b/tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach---2 new file mode 100644 index 00000000..2c2c8226 --- /dev/null +++ b/tests/screenshots/tests-test_client.lua---client()---suggestions-off-when-previous-file-only-should_attach---2 @@ -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 diff --git a/tests/screenshots/tests-test_client.lua---client()---suggestions-work-when-already-in-insert-mode-and-opening-file---3 b/tests/screenshots/tests-test_client.lua---client()---suggestions-work-when-already-in-insert-mode-and-opening-file---3 new file mode 100644 index 00000000..5558cee4 --- /dev/null +++ b/tests/screenshots/tests-test_client.lua---client()---suggestions-work-when-already-in-insert-mode-and-opening-file---3 @@ -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 diff --git a/tests/test_client.lua b/tests/test_client.lua index e131a849..1147ed6e 100644 --- a/tests/test_client.lua +++ b/tests/test_client.lua @@ -52,22 +52,21 @@ end T["client()"] = MiniTest.new_set() --- need a hook in attach() to see if it attaches to the correct buffer T["client()"]["suggestions work when multiple files open with should_attach logic"] = function() child.config.should_attach = [[function(bufnr, bufname) - buffername = bufname:match("([^/\\]+)$") + local buffername = bufname:match("([^/\\]+)$") or "" if not _G.bufnames then _G.bufnames = '' end _G.bufnames = _G.bufnames .. ' ; ' .. buffername.. ' (' .. tostring(bufnr) .. ')' - - -- if (buffername == "file1.txt") or (buffername == "file2.txt") then + + if buffername == "file2.txt" then return true - -- else - -- return false, "incorrect buffer" - -- end + end + + return false end]] child.config.suggestion = child.config.suggestion .. "auto_trigger = true," @@ -80,16 +79,85 @@ T["client()"]["suggestions work when multiple files open with should_attach logi child.wait_for_suggestion() local bufnames = child.lua("return tostring(_G.bufnames)") - -- local messages = child.lua([[ - -- return vim.api.nvim_exec("messages", { output = true }) or "" - -- ]]) + local messages = child.lua([[ + return vim.api.nvim_exec("messages", { output = true }) or "" + ]]) -- convert buffername to only the file name -- buffername = buffername:match("([^/\\]+)$") -- u.expect_match(buffername, "file3.txt") + print("Messages: " .. messages) print("bufnames: " .. bufnames) - -- print("Messages: " .. messages) + + reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } }) +end + +T["client()"]["suggestions off when previous file only should_attach"] = function() + child.config.should_attach = [[function(bufnr, bufname) + local buffername = bufname:match("([^/\\]+)$") or "" + + if buffername == "file1.txt" then + return true + end + + return false + end]] + child.config.suggestion = child.config.suggestion .. "auto_trigger = true," + + child.configure_copilot() + child.cmd("e tests/files/file1.txt") + child.cmd("e tests/files/file2.txt") + child.type_keys("i") + -- child.cmd("e tests/files/file3.txt") + child.type_keys("123", "", "o456", "", "o7") + child.wait_for_suggestion() + + reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } }) +end + +T["client()"]["suggestions off when previous file only should_attach - 2"] = function() + child.config.should_attach = [[function(bufnr, bufname) + local buffername = bufname:match("([^/\\]+)$") or "" + + if buffername == "file2.txt" then + return true + end + + return false + end]] + child.config.suggestion = child.config.suggestion .. "auto_trigger = true," + + child.configure_copilot() + child.cmd("e tests/files/file1.txt") + child.cmd("e tests/files/file2.txt") + child.type_keys("i") + child.cmd("e tests/files/file3.txt") + child.type_keys("123", "", "o456", "", "o7") + child.wait_for_suggestion() + + reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } }) +end + +T["client()"]["suggestions work when already in insert mode and opening file - 3"] = function() + child.config.should_attach = [[function(bufnr, bufname) + local buffername = bufname:match("([^/\\]+)$") or "" + + if buffername == "file3.txt" then + return true + end + + return false + end]] + child.config.suggestion = child.config.suggestion .. "auto_trigger = true," + + child.configure_copilot() + child.cmd("e tests/files/file1.txt") + child.cmd("e tests/files/file2.txt") + child.type_keys("i") + child.cmd("e tests/files/file3.txt") + child.type_keys("123", "", "o456", "", "o7") + child.wait_for_suggestion() reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } }) end From f59ed8907420ec6a813cee2436b2ed3a7c60605c Mon Sep 17 00:00:00 2001 From: Antoine Gaudreau Simard Date: Mon, 28 Jul 2025 15:36:06 -0400 Subject: [PATCH 5/5] WIP on fix_should_attach --- tests/child_helper.lua | 2 +- tests/test_panel.lua | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/child_helper.lua b/tests/child_helper.lua index 595c3d33..73afb2df 100644 --- a/tests/child_helper.lua +++ b/tests/child_helper.lua @@ -102,7 +102,7 @@ function M.new_child_neovim(test_name) ) -- write to temporary file for debugging purposes - local tmpfile = string.format("./tests/logs/test_config.lua") + local tmpfile = string.format("./tests/logs/test_config.txt") local file = io.open(tmpfile, "w") if file then file:write(script) diff --git a/tests/test_panel.lua b/tests/test_panel.lua index 20f87f7e..94988f47 100644 --- a/tests/test_panel.lua +++ b/tests/test_panel.lua @@ -1,7 +1,6 @@ local eq = MiniTest.expect.equality local child_helper = require("tests.child_helper") local child = child_helper.new_child_neovim("test_panel") --- local reference_screenshot = MiniTest.expect.reference_screenshot local utils = require("copilot.panel.utils") local T = MiniTest.new_set({