Skip to content

Commit 394b5cb

Browse files
committed
fix(panel): go back to correct place after accept
1 parent bfae743 commit 394b5cb

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

lua/copilot/panel.lua

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ local panel = {
3737
},
3838
layout = {
3939
position = "bottom",
40-
ratio = 0.4
40+
ratio = 0.4,
4141
},
4242

4343
auto_refresh = false,
@@ -211,8 +211,9 @@ function panel:accept()
211211
end
212212

213213
local bufnr = vim.uri_to_bufnr(panel_uri_to_doc_uri(self.panel_uri))
214+
local winid = vim.fn.bufwinid(bufnr)
214215

215-
if not vim.api.nvim_buf_is_loaded(bufnr) then
216+
if not vim.api.nvim_buf_is_loaded(bufnr) or winid == -1 then
216217
vim.cmd("echoerr 'Buffer was closed'")
217218
return
218219
end
@@ -222,17 +223,19 @@ function panel:accept()
222223
return
223224
end
224225

225-
vim.api.nvim_buf_call(bufnr, function()
226-
vim.lsp.util.apply_text_edits({
227-
{ range = entry.range, newText = entry.completionText },
228-
}, 0, "utf-16")
226+
vim.api.nvim_set_current_win(winid)
229227

230-
if self.state.was_insert then
231-
vim.cmd("startinsert!")
232-
else
233-
vim.cmd("normal! $")
234-
end
235-
end)
228+
if self.state.was_insert then
229+
vim.cmd("startinsert!")
230+
else
231+
vim.cmd("normal! $")
232+
end
233+
234+
vim.lsp.util.apply_text_edits({
235+
{ range = entry.range, newText = entry.completionText },
236+
}, 0, "utf-16")
237+
-- Put cursor at the end of current line.
238+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<End>", true, false, true), "n", false)
236239

237240
self:unlock():clear():lock()
238241

@@ -322,14 +325,14 @@ function panel:ensure_winid()
322325

323326
local split_info = split_map[position]
324327
if not split_info then
325-
print('Error: ' .. position .. ' is not a valid position')
328+
print("Error: " .. position .. " is not a valid position")
326329
return
327330
end
328331

329332
local function resolve_splitcmd()
330333
local size = math.floor(split_info.winsize_fn(0) * ratio)
331334
local cmd_prefix = split_info.cmd_prefix
332-
return "silent noswapfile " .. cmd_prefix .. tostring(size) .. ' split'
335+
return "silent noswapfile " .. cmd_prefix .. tostring(size) .. " split"
333336
end
334337

335338
self.winid = vim.api.nvim_win_call(0, function()
@@ -536,7 +539,7 @@ function mod.setup(config)
536539
panel.auto_refresh = config.auto_refresh or false
537540

538541
panel.keymap = config.keymap or {}
539-
panel.layout = vim.tbl_deep_extend('force', panel.layout, config.layout or {})
542+
panel.layout = vim.tbl_deep_extend("force", panel.layout, config.layout or {})
540543

541544
if panel.keymap.open then
542545
vim.keymap.set("i", panel.keymap.open, mod.open, {

0 commit comments

Comments
 (0)