Skip to content

Commit 2f50ec4

Browse files
committed
fix: unable to accept panel suggestion (buffer was closed error)
Fixes #473
1 parent e5b65bb commit 2f50ec4

File tree

4 files changed

+75
-39
lines changed

4 files changed

+75
-39
lines changed

lua/copilot/panel/init.lua

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ local config = require("copilot.config")
44
local hl_group = require("copilot.highlight").group
55
local util = require("copilot.util")
66
local logger = require("copilot.logger")
7+
local utils = require("copilot.panel.utils")
78

89
local M = {
910
handlers = require("copilot.panel.handlers"),
1011
}
1112
local marker_prefix = "[copilot] "
12-
local panel_uri_prefix = "copilot://"
1313

1414
local panel = {
1515
---@type vim.lsp.Client
@@ -66,23 +66,6 @@ local function get_display_lines(text)
6666
return lines
6767
end
6868

69-
---@return string panelUri
70-
local function panel_uri_from_doc_uri(doc_uri)
71-
local uri = panel_uri_prefix .. vim.uri_to_fname(doc_uri)
72-
return uri
73-
end
74-
75-
---@return string doc_uri
76-
local function panel_uri_to_doc_uri(panel_uri)
77-
local uri = vim.uri_from_fname(vim.uri_to_fname(panel_uri))
78-
return uri
79-
end
80-
81-
---@param bufname string
82-
local function is_panel_uri(bufname)
83-
return bufname:sub(1, #panel_uri_prefix) == panel_uri_prefix
84-
end
85-
8669
function panel:lock()
8770
vim.api.nvim_set_option_value("modifiable", false, { buf = self.bufnr })
8871
vim.api.nvim_set_option_value("readonly", true, { buf = self.bufnr })
@@ -217,7 +200,8 @@ function panel:accept()
217200
return
218201
end
219202

220-
local bufnr = vim.uri_to_bufnr(panel_uri_to_doc_uri(self.panel_uri))
203+
local doc_uri = utils.panel_uri_to_doc_uri(self.panel_uri)
204+
local bufnr = vim.uri_to_bufnr(doc_uri)
221205
local winid = vim.fn.bufwinid(bufnr)
222206

223207
if not vim.api.nvim_buf_is_loaded(bufnr) or winid == -1 then
@@ -375,7 +359,7 @@ function panel:ensure_winid()
375359
if self.auto_refresh then
376360
vim.api.nvim_create_autocmd({ "TextChangedI", "TextChangedP" }, {
377361
group = self.augroup,
378-
buffer = vim.uri_to_bufnr(panel_uri_to_doc_uri(self.panel_uri)),
362+
buffer = vim.uri_to_bufnr(utils.panel_uri_to_doc_uri(self.panel_uri)),
379363
callback = function()
380364
self.state.auto_refreshing = true
381365
self:refresh()
@@ -502,7 +486,7 @@ end
502486
function panel:init()
503487
local doc = util.get_doc()
504488

505-
if is_panel_uri(doc.uri) then
489+
if utils.is_panel_uri(doc.uri) then
506490
-- currently inside the panel itself
507491
M.refresh()
508492
return
@@ -514,15 +498,11 @@ function panel:init()
514498
return
515499
end
516500

517-
self.panel_uri = panel_uri_from_doc_uri(doc.uri)
501+
self.panel_uri = utils.panel_uri_from_doc_uri(doc.uri)
518502
self.filetype = vim.bo.filetype
519-
520503
self:ensure_bufnr()
521-
522504
self:ensure_winid()
523-
524505
self:refresh()
525-
526506
vim.api.nvim_set_current_win(self.winid)
527507
end
528508

@@ -547,7 +527,7 @@ function M.toggle()
547527
end
548528

549529
function M.refresh()
550-
vim.api.nvim_buf_call(vim.uri_to_bufnr(panel_uri_to_doc_uri(panel.panel_uri)), function()
530+
vim.api.nvim_buf_call(vim.uri_to_bufnr(utils.panel_uri_to_doc_uri(panel.panel_uri)), function()
551531
panel:refresh()
552532
end)
553533
end

lua/copilot/panel/utils.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local M = {}
2+
3+
local panel_uri_prefix = "copilot:///"
4+
5+
---@return string panelUri
6+
function M.panel_uri_from_doc_uri(doc_uri)
7+
return panel_uri_prefix .. vim.fs.normalize(vim.uri_to_fname(doc_uri))
8+
end
9+
10+
---@return string doc_uri
11+
function M.panel_uri_to_doc_uri(panel_uri)
12+
return panel_uri:gsub("^" .. panel_uri_prefix, "file:///")
13+
end
14+
15+
---@param bufname string
16+
function M.is_panel_uri(bufname)
17+
return bufname:sub(1, #panel_uri_prefix) == panel_uri_prefix
18+
end
19+
20+
return M

tests/child_helper.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ function M.new_child_neovim(test_name)
113113
]])
114114
end
115115

116+
function child.wait_for_panel_suggestion()
117+
child.lua([[
118+
local function suggestion_is_visible()
119+
lines = vim.api.nvim_buf_get_lines(2, 4, 5, false)
120+
return lines[1] and lines[1] ~= ""
121+
end
122+
123+
vim.wait(30000, function()
124+
return suggestion_is_visible()
125+
end, 50)
126+
]])
127+
end
128+
116129
return child
117130
end
118131

tests/test_panel.lua

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local eq = MiniTest.expect.equality
22
local child_helper = require("tests.child_helper")
3-
local child = child_helper.new_child_neovim("test_client")
3+
local child = child_helper.new_child_neovim("test_panel")
4+
local reference_screenshot = MiniTest.expect.reference_screenshot
5+
local utils = require("copilot.panel.utils")
46

57
local T = MiniTest.new_set({
68
hooks = {
@@ -23,19 +25,10 @@ T["panel()"]["panel suggestions works"] = function()
2325
child.configure_copilot()
2426
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
2527
child.lua("p.toggle()")
28+
child.wait_for_panel_suggestion()
2629

2730
local lines = child.lua([[
28-
local messages = ""
29-
local function suggestion_is_visible()
30-
lines = vim.api.nvim_buf_get_lines(2, 4, 5, false)
31-
return lines[1] == "789" or lines[1] == "789\r"
32-
end
33-
34-
vim.wait(30000, function()
35-
return suggestion_is_visible()
36-
end, 50)
37-
38-
return lines
31+
return vim.api.nvim_buf_get_lines(2, 4, 5, false)
3932
]])
4033

4134
-- For Windows, on some shells not all
@@ -46,4 +39,34 @@ T["panel()"]["panel suggestions works"] = function()
4639
eq(lines[1], "789")
4740
end
4841

42+
-- Disabled for now as unnamed buffers have issues with not having a URI
43+
-- T["panel()"]["panel suggestion accept works"] = function()
44+
-- child.o.lines, child.o.columns = 30, 100
45+
-- child.config.panel = child.config.panel .. "auto_refresh = true,"
46+
-- child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
47+
-- child.configure_copilot()
48+
-- child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
49+
-- child.lua("p.toggle()")
50+
-- child.wait_for_panel_suggestion()
51+
-- child.cmd("buffer 2")
52+
-- child.type_keys("4gg")
53+
-- child.lua("p.accept()")
54+
-- child.cmd("buffer 1")
55+
-- reference_screenshot(child.get_screenshot())
56+
-- end
57+
58+
T["panel.utils()"] = MiniTest.new_set()
59+
60+
T["panel.utils()"]["panel_uri_from_doc_uri works"] = function()
61+
local panel_uri = "copilot:///C:/Users/antoi/AppData/Local/nvim-data/lazy/copilot.lua/lua/copilot/suggestion/init.lua"
62+
local doc_uri = utils.panel_uri_to_doc_uri(panel_uri)
63+
eq(doc_uri, "file:///C:/Users/antoi/AppData/Local/nvim-data/lazy/copilot.lua/lua/copilot/suggestion/init.lua")
64+
end
65+
66+
T["panel.utils()"]["panel_uri_to_doc_uri"] = function()
67+
local doc_uri = "file:///C:/Users/antoi/AppData/Local/nvim-data/lazy/copilot.lua/lua/copilot/suggestion/init.lua"
68+
local panel_uri = utils.panel_uri_from_doc_uri(doc_uri)
69+
eq(panel_uri, "copilot:///C:/Users/antoi/AppData/Local/nvim-data/lazy/copilot.lua/lua/copilot/suggestion/init.lua")
70+
end
71+
4972
return T

0 commit comments

Comments
 (0)