@@ -14,7 +14,12 @@ local function _store_suggestion_history(bufnr, state)
14
14
if not config .nes .enable_history then
15
15
return
16
16
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
18
23
end
19
24
20
25
--- @private
27
32
--- @param ns_id integer
28
33
function M .clear_suggestion (bufnr , ns_id )
29
34
bufnr = bufnr and bufnr > 0 and bufnr or vim .api .nvim_get_current_buf ()
30
- -- Validate buffer exists before accessing buffer-scoped variables
31
35
if not vim .api .nvim_buf_is_valid (bufnr ) then
32
36
return
33
37
end
@@ -37,16 +41,6 @@ function M.clear_suggestion(bufnr, ns_id)
37
41
end
38
42
_dismiss_suggestion (bufnr , ns_id )
39
43
--- @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
50
44
vim .b [bufnr ].nes_state = nil
51
45
vim .b [bufnr ].copilotlsp_nes_cursor_moves = nil
52
46
vim .b [bufnr ].copilotlsp_nes_last_line = nil
@@ -61,27 +55,35 @@ function M.restore_suggestion(bufnr, ns_id)
61
55
if not vim .api .nvim_buf_is_valid (bufnr ) then
62
56
return false
63
57
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
69
58
if not config .nes .enable_history then
70
59
return false
71
60
end
72
- --- @type copilotlsp.InlineEdit
73
61
local history = vim .b [bufnr ].copilotlsp_nes_history
74
- if not history then
62
+ if not history or # history == 0 then
75
63
return false
76
64
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
79
74
if start_line >= vim .api .nvim_buf_line_count (bufnr ) then
80
75
_clear_suggestion_history (bufnr )
81
76
return false
82
77
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
+
85
87
return true
86
88
end
87
89
@@ -218,15 +220,11 @@ end
218
220
--- @param ns_id integer
219
221
--- @param edits copilotlsp.InlineEdit[]
220
222
function M ._display_next_suggestion (bufnr , ns_id , edits )
221
- M .clear_suggestion (bufnr , ns_id )
222
223
if not edits or # edits == 0 then
223
224
return
224
225
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 )
230
228
231
229
local suggestion = edits [1 ]
232
230
local preview = M ._calculate_preview (bufnr , suggestion )
@@ -236,6 +234,10 @@ function M._display_next_suggestion(bufnr, ns_id, edits)
236
234
vim .b [bufnr ].copilotlsp_nes_namespace_id = ns_id
237
235
vim .b [bufnr ].copilotlsp_nes_cursor_moves = 1
238
236
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
+
239
241
vim .api .nvim_create_autocmd ({ " CursorMoved" , " CursorMovedI" }, {
240
242
buffer = bufnr ,
241
243
callback = function ()
0 commit comments