Skip to content

Commit 8e8c1e8

Browse files
committed
fix: RPC Document for URI not found on ft change
fixes zbirenbaum#491
1 parent f55bf8c commit 8e8c1e8

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
tests/logs
22
tests/env.lua
33
deps/
4+
5+
tests/files/test.txt

lua/copilot/client/init.lua

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ function M.buf_attach(force, bufnr)
101101
end
102102

103103
vim.lsp.buf_attach_client(bufnr, M.id)
104+
util.set_buffer_previous_ft(bufnr, vim.bo[bufnr].filetype)
104105
if force then
105106
logger.debug("force attached to buffer")
106107
util.set_buffer_attach_status(bufnr, ATTACH_STATUS_FORCE_ATTACHED)
@@ -110,11 +111,12 @@ function M.buf_attach(force, bufnr)
110111
end
111112
end
112113

113-
function M.buf_detach()
114-
if M.buf_is_attached(0) then
115-
vim.lsp.buf_detach_client(0, M.id)
116-
logger.trace("buffer manuall detached")
117-
util.set_buffer_attach_status(vim.api.nvim_get_current_buf(), ATTACH_STATUS_MANUALLY_DETACHED)
114+
---@param bufnr? integer
115+
function M.buf_detach_if_attached(bufnr)
116+
bufnr = bufnr or vim.api.nvim_get_current_buf()
117+
if M.buf_is_attached(bufnr) then
118+
vim.lsp.buf_detach_client(bufnr, M.id)
119+
util.set_buffer_attach_status(bufnr, ATTACH_STATUS_NOT_ATTACHED_PREFIX .. "detached")
118120
end
119121
end
120122

@@ -166,6 +168,23 @@ function M.use_client(callback)
166168
end
167169
end
168170

171+
---@param bufnr integer
172+
local function on_filetype(bufnr)
173+
logger.trace("filetype autocmd called")
174+
vim.schedule(function()
175+
-- todo: when we do lazy/late attaching this needs changing
176+
177+
-- This is to handle the case where the filetype changes after the buffer is already attached,
178+
-- causing the LSP to raise an error
179+
if util.get_buffer_previous_ft(bufnr) ~= vim.bo[bufnr].filetype then
180+
logger.trace("filetype changed, detaching and re-attaching")
181+
M.buf_detach_if_attached(bufnr)
182+
end
183+
184+
M.buf_attach(false, bufnr)
185+
end)
186+
end
187+
169188
function M.setup()
170189
logger.trace("setting up client")
171190
local node_command = config.copilot_node_command
@@ -193,11 +212,7 @@ function M.setup()
193212
group = M.augroup,
194213
callback = function(args)
195214
local bufnr = (args and args.buf) or nil
196-
logger.trace("filetype autocmd called")
197-
vim.schedule(function()
198-
-- todo: when we do lazy/late attaching this needs changing
199-
M.buf_attach(false, bufnr)
200-
end)
215+
on_filetype(bufnr)
201216
end,
202217
desc = "[copilot] (suggestion) file type",
203218
})

lua/copilot/command.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ function M.attach(opts)
4949
end
5050

5151
function M.detach()
52-
if c.buf_is_attached(0) then
53-
c.buf_detach()
52+
local bufnr = vim.api.nvim_get_current_buf()
53+
if c.buf_is_attached(bufnr) then
54+
c.buf_detach_if_attached(bufnr)
55+
logger.trace("buffer manually detached")
56+
u.set_buffer_attach_status(bufnr, ATTACH_STATUS_MANUALLY_DETACHED)
5457
end
5558
end
5659

lua/copilot/util.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local logger = require("copilot.logger")
33

44
local M = {}
55
VAR_ATTACH_STATUS = "copilot_lua_attach_status"
6+
VAR_PREVIOUS_FT = "copilot_lua_previous_ft"
67
ATTACH_STATUS_MANUALLY_DETACHED = "manually detached"
78
ATTACH_STATUS_FORCE_ATTACHED = "force attached"
89
ATTACH_STATUS_ATTACHED = "attached"
@@ -172,4 +173,17 @@ function M.get_buffer_attach_status(bufnr)
172173
return (ok and result) or ATTACH_STATUS_NOT_YET_REQUESTED
173174
end
174175

176+
---@param bufnr integer
177+
---@param filetype string
178+
function M.set_buffer_previous_ft(bufnr, filetype)
179+
vim.api.nvim_buf_set_var(bufnr, VAR_PREVIOUS_FT, filetype)
180+
end
181+
182+
---@param bufnr integer
183+
---@return string
184+
function M.get_buffer_previous_ft(bufnr)
185+
local ok, result = pcall(vim.api.nvim_buf_get_var, bufnr, VAR_PREVIOUS_FT)
186+
return (ok and result) or ""
187+
end
188+
175189
return M

tests/test_client.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,19 @@ end
341341
-- u.expect_match(messages, ".*Online.*attached.*")
342342
-- end
343343

344+
T["client()"]["saving file - will not yield URI not found error"] = function()
345+
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
346+
child.configure_copilot()
347+
child.type_keys("i", "123", "<Esc>", "o456", "<Esc>", "o7")
348+
child.wait_for_suggestion()
349+
child.type_keys("<Esc>")
350+
child.lua("M.suggested = false")
351+
child.cmd("w! tests/files/test.txt")
352+
child.type_keys("a8")
353+
child.wait_for_suggestion()
354+
local messages = child.cmd_capture("messages")
355+
356+
u.expect_no_match(messages, "RPC.*Document for URI could not be found")
357+
end
358+
344359
return T

0 commit comments

Comments
 (0)