Skip to content

Commit 54bf497

Browse files
author
Bassam Data
committed
feat: Adding history cycle for the last 2 suggestions
1 parent a74101a commit 54bf497

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

lua/copilot-lsp/nes/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ end
140140
---@return boolean
141141
function M.has_history(bufnr)
142142
bufnr = bufnr and bufnr > 0 and bufnr or vim.api.nvim_get_current_buf()
143-
return vim.b[bufnr].copilotlsp_nes_history ~= nil
143+
local history = vim.b[bufnr].copilotlsp_nes_history
144+
return history ~= nil and #history > 0
144145
end
145146

146147
--- Restore the last suggestion from history
147148
---@param bufnr? integer
148149
---@return boolean -- true if suggestion was restored, false otherwise
149150
function M.restore_suggestion(bufnr)
151+
bufnr = bufnr and bufnr > 0 and bufnr or vim.api.nvim_get_current_buf()
150152
if not M.has_history(bufnr) then
151153
return false
152154
end

lua/copilot-lsp/nes/ui.lua

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ local function _store_suggestion_history(bufnr, state)
1414
if not config.nes.enable_history then
1515
return
1616
end
17-
vim.b[bufnr].copilotlsp_nes_history = vim.deepcopy(state)
17+
local history = vim.b[bufnr].copilotlsp_nes_history or {}
18+
table.insert(history, 1, vim.deepcopy(state))
19+
if #history > 2 then
20+
table.remove(history, 3)
21+
end
22+
vim.b[bufnr].copilotlsp_nes_history = history
1823
end
1924

2025
---@private
@@ -27,7 +32,6 @@ end
2732
---@param ns_id integer
2833
function M.clear_suggestion(bufnr, ns_id)
2934
bufnr = bufnr and bufnr > 0 and bufnr or vim.api.nvim_get_current_buf()
30-
-- Validate buffer exists before accessing buffer-scoped variables
3135
if not vim.api.nvim_buf_is_valid(bufnr) then
3236
return
3337
end
@@ -37,16 +41,6 @@ function M.clear_suggestion(bufnr, ns_id)
3741
end
3842
_dismiss_suggestion(bufnr, ns_id)
3943
---@type copilotlsp.InlineEdit
40-
local state = vim.b[bufnr].nes_state
41-
if state then
42-
_store_suggestion_history(bufnr, state)
43-
end
44-
_dismiss_suggestion(bufnr, ns_id)
45-
if not state then
46-
return
47-
end
48-
49-
-- Clear buffer variables
5044
vim.b[bufnr].nes_state = nil
5145
vim.b[bufnr].copilotlsp_nes_cursor_moves = nil
5246
vim.b[bufnr].copilotlsp_nes_last_line = nil
@@ -61,27 +55,35 @@ function M.restore_suggestion(bufnr, ns_id)
6155
if not vim.api.nvim_buf_is_valid(bufnr) then
6256
return false
6357
end
64-
-- Don't restore if there's already an active suggestion
65-
if vim.b[bufnr].nes_state then
66-
return false
67-
end
68-
-- Don't restore if history is disabled
6958
if not config.nes.enable_history then
7059
return false
7160
end
72-
---@type copilotlsp.InlineEdit
7361
local history = vim.b[bufnr].copilotlsp_nes_history
74-
if not history then
62+
if not history or #history == 0 then
7563
return false
7664
end
77-
-- Validate that the history suggestion is still applicable
78-
local start_line = history.range.start.line
65+
local restore_index = vim.b[bufnr].copilotlsp_nes_restore_index or 0
66+
restore_index = restore_index + 1
67+
-- If we've cycled through all history, wrap around
68+
if restore_index > #history then
69+
restore_index = 1
70+
end
71+
local suggestion = history[restore_index]
72+
vim.b[bufnr].copilotlsp_nes_restore_index = restore_index
73+
local start_line = suggestion.range.start.line
7974
if start_line >= vim.api.nvim_buf_line_count(bufnr) then
8075
_clear_suggestion_history(bufnr)
8176
return false
8277
end
83-
-- Restore the suggestion
84-
M._display_next_suggestion(bufnr, ns_id, { history })
78+
-- Clear current display and show restored suggestion
79+
_dismiss_suggestion(bufnr, ns_id)
80+
local preview = M._calculate_preview(bufnr, suggestion)
81+
M._display_preview(bufnr, ns_id, preview)
82+
83+
vim.b[bufnr].nes_state = suggestion
84+
vim.b[bufnr].copilotlsp_nes_namespace_id = ns_id
85+
vim.b[bufnr].copilotlsp_nes_cursor_moves = 1
86+
8587
return true
8688
end
8789

@@ -218,15 +220,11 @@ end
218220
---@param ns_id integer
219221
---@param edits copilotlsp.InlineEdit[]
220222
function M._display_next_suggestion(bufnr, ns_id, edits)
221-
M.clear_suggestion(bufnr, ns_id)
222223
if not edits or #edits == 0 then
223224
return
224225
end
225-
-- Clear history when new suggestion arrives (not a restoration)
226-
if config.nes.enable_history and not vim.b[bufnr].copilotlsp_nes_restoring then
227-
_clear_suggestion_history(bufnr)
228-
end
229-
vim.b[bufnr].copilotlsp_nes_restoring = nil
226+
-- Clear current suggestion first
227+
M.clear_suggestion(bufnr, ns_id)
230228

231229
local suggestion = edits[1]
232230
local preview = M._calculate_preview(bufnr, suggestion)
@@ -236,6 +234,10 @@ function M._display_next_suggestion(bufnr, ns_id, edits)
236234
vim.b[bufnr].copilotlsp_nes_namespace_id = ns_id
237235
vim.b[bufnr].copilotlsp_nes_cursor_moves = 1
238236

237+
-- Store this suggestion in history immediately after displaying it
238+
_store_suggestion_history(bufnr, suggestion)
239+
vim.b[bufnr].copilotlsp_nes_restore_index = 0
240+
239241
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
240242
buffer = bufnr,
241243
callback = function()

0 commit comments

Comments
 (0)