@@ -7,6 +7,22 @@ local function _dismiss_suggestion(bufnr, ns_id)
7
7
pcall (vim .api .nvim_buf_clear_namespace , bufnr , ns_id , 0 , - 1 )
8
8
end
9
9
10
+ --- @private
11
+ --- @param bufnr integer
12
+ --- @param state copilotlsp.InlineEdit
13
+ local function _store_suggestion_history (bufnr , state )
14
+ if not config .nes .enable_history then
15
+ return
16
+ end
17
+ vim .b [bufnr ].copilotlsp_nes_history = vim .deepcopy (state )
18
+ end
19
+
20
+ --- @private
21
+ --- @param bufnr integer
22
+ local function _clear_suggestion_history (bufnr )
23
+ vim .b [bufnr ].copilotlsp_nes_history = nil
24
+ end
25
+
10
26
--- @param bufnr ? integer
11
27
--- @param ns_id integer
12
28
function M .clear_suggestion (bufnr , ns_id )
@@ -22,6 +38,10 @@ function M.clear_suggestion(bufnr, ns_id)
22
38
_dismiss_suggestion (bufnr , ns_id )
23
39
--- @type copilotlsp.InlineEdit
24
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 )
25
45
if not state then
26
46
return
27
47
end
@@ -33,6 +53,38 @@ function M.clear_suggestion(bufnr, ns_id)
33
53
vim .b [bufnr ].copilotlsp_nes_last_col = nil
34
54
end
35
55
56
+ --- @param bufnr ? integer
57
+ --- @param ns_id integer
58
+ --- @return boolean -- true if suggestion was restored , false otherwise
59
+ function M .restore_suggestion (bufnr , ns_id )
60
+ bufnr = bufnr and bufnr > 0 and bufnr or vim .api .nvim_get_current_buf ()
61
+ if not vim .api .nvim_buf_is_valid (bufnr ) then
62
+ return false
63
+ 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
+ if not config .nes .enable_history then
70
+ return false
71
+ end
72
+ --- @type copilotlsp.InlineEdit
73
+ local history = vim .b [bufnr ].copilotlsp_nes_history
74
+ if not history then
75
+ return false
76
+ end
77
+ -- Validate that the history suggestion is still applicable
78
+ local start_line = history .range .start .line
79
+ if start_line >= vim .api .nvim_buf_line_count (bufnr ) then
80
+ _clear_suggestion_history (bufnr )
81
+ return false
82
+ end
83
+ -- Restore the suggestion
84
+ M ._display_next_suggestion (bufnr , ns_id , { history })
85
+ return true
86
+ end
87
+
36
88
--- @private
37
89
--- @param bufnr integer
38
90
--- @param edit lsp.TextEdit
@@ -170,6 +222,11 @@ function M._display_next_suggestion(bufnr, ns_id, edits)
170
222
if not edits or # edits == 0 then
171
223
return
172
224
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
173
230
174
231
local suggestion = edits [1 ]
175
232
local preview = M ._calculate_preview (bufnr , suggestion )
0 commit comments