Skip to content

Commit 711b37a

Browse files
author
Bassam Data
committed
feat: Replacing manual handling with vim.ringbuf for history
1 parent 21f00df commit 711b37a

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

lua/copilot-lsp/nes/init.lua

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,12 @@ function M.clear()
135135
return false
136136
end
137137

138-
--- Check if there's a suggestion in history that can be restored
139-
---@param bufnr? integer
140-
---@return boolean
141-
function M.has_history(bufnr)
142-
bufnr = bufnr and bufnr > 0 and bufnr or vim.api.nvim_get_current_buf()
143-
local history = vim.b[bufnr].copilotlsp_nes_history
144-
return history ~= nil and #history > 0
145-
end
146-
147138
--- Restore the last suggestion from history
148139
---@param bufnr? integer
149140
---@return boolean -- true if suggestion was restored, false otherwise
150141
function M.restore_suggestion(bufnr)
151142
bufnr = bufnr and bufnr > 0 and bufnr or vim.api.nvim_get_current_buf()
152-
if not M.has_history(bufnr) then
143+
if not nes_ui.has_history(bufnr) then
153144
return false
154145
end
155146
-- Set flag to indicate this is a restoration, not a new suggestion

lua/copilot-lsp/nes/ui.lua

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local M = {}
22
local config = require("copilot-lsp.config").config
33

4+
local buffer_histories = {}
5+
46
---@param bufnr integer
57
---@param ns_id integer
68
local function _dismiss_suggestion(bufnr, ns_id)
@@ -11,18 +13,17 @@ end
1113
---@param bufnr integer
1214
---@param state copilotlsp.InlineEdit
1315
local function _store_suggestion_history(bufnr, state)
14-
local history = vim.b[bufnr].copilotlsp_nes_history or {}
15-
table.insert(history, 1, vim.deepcopy(state))
16-
if #history > 2 then
17-
table.remove(history, 3)
16+
if not buffer_histories[bufnr] then
17+
buffer_histories[bufnr] = vim.ringbuf(2)
1818
end
19-
vim.b[bufnr].copilotlsp_nes_history = history
19+
buffer_histories[bufnr]:push(vim.deepcopy(state))
2020
end
2121

2222
---@private
2323
---@param bufnr integer
2424
local function _clear_suggestion_history(bufnr)
25-
vim.b[bufnr].copilotlsp_nes_history = nil
25+
buffer_histories[bufnr] = nil
26+
vim.b[bufnr].copilotlsp_nes_restore_index = nil
2627
end
2728

2829
---@param bufnr? integer
@@ -44,6 +45,19 @@ function M.clear_suggestion(bufnr, ns_id)
4445
vim.b[bufnr].copilotlsp_nes_last_col = nil
4546
end
4647

48+
--- Check if there's history for a buffer
49+
---@param bufnr integer
50+
---@return boolean
51+
function M.has_history(bufnr)
52+
local history = buffer_histories[bufnr]
53+
if not history then
54+
return false
55+
end
56+
-- Check if ringbuf has any items
57+
local item = history:peek()
58+
return item ~= nil
59+
end
60+
4761
---@param bufnr? integer
4862
---@param ns_id integer
4963
---@return boolean -- true if suggestion was restored, false otherwise
@@ -52,32 +66,27 @@ function M.restore_suggestion(bufnr, ns_id)
5266
if not vim.api.nvim_buf_is_valid(bufnr) then
5367
return false
5468
end
55-
local history = vim.b[bufnr].copilotlsp_nes_history
56-
if not history or #history == 0 then
69+
local history = buffer_histories[bufnr]
70+
if not history then
5771
return false
5872
end
59-
local restore_index = vim.b[bufnr].copilotlsp_nes_restore_index or 0
60-
restore_index = restore_index + 1
61-
-- If we've cycled through all history, wrap around
62-
if restore_index > #history then
63-
restore_index = 1
73+
local suggestion = history:pop()
74+
if not suggestion then
75+
return false
6476
end
65-
local suggestion = history[restore_index]
66-
vim.b[bufnr].copilotlsp_nes_restore_index = restore_index
77+
-- Validate suggestion is still applicable
6778
local start_line = suggestion.range.start.line
6879
if start_line >= vim.api.nvim_buf_line_count(bufnr) then
6980
_clear_suggestion_history(bufnr)
7081
return false
7182
end
72-
-- Clear current display and show restored suggestion
7383
_dismiss_suggestion(bufnr, ns_id)
7484
local preview = M._calculate_preview(bufnr, suggestion)
7585
M._display_preview(bufnr, ns_id, preview)
76-
7786
vim.b[bufnr].nes_state = suggestion
7887
vim.b[bufnr].copilotlsp_nes_namespace_id = ns_id
7988
vim.b[bufnr].copilotlsp_nes_cursor_moves = 1
80-
89+
history:push(suggestion)
8190
return true
8291
end
8392

@@ -329,4 +338,11 @@ function M._display_next_suggestion(bufnr, ns_id, edits)
329338
})
330339
end
331340

341+
-- Clean up history when buffer is deleted
342+
vim.api.nvim_create_autocmd("BufDelete", {
343+
callback = function(ev)
344+
buffer_histories[ev.buf] = nil
345+
end,
346+
})
347+
332348
return M

0 commit comments

Comments
 (0)