Skip to content

Commit a74101a

Browse files
author
Bassam Data
committed
feat: adding suggestion_history for last NES
1 parent 041385e commit a74101a

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

lua/copilot-lsp/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
---@field clear_on_large_distance boolean Whether to clear suggestion when cursor is far away
55
---@field count_horizontal_moves boolean Whether to count horizontal cursor movements
66
---@field reset_on_approaching boolean Whether to reset counter when approaching suggestion
7+
---@field enable_history boolean Whether to enable suggestion history for restoration
78

89
local M = {}
910

@@ -16,6 +17,7 @@ M.defaults = {
1617
clear_on_large_distance = true,
1718
count_horizontal_moves = true,
1819
reset_on_approaching = true,
20+
enable_history = true,
1921
},
2022
}
2123

lua/copilot-lsp/nes/init.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,24 @@ 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+
return vim.b[bufnr].copilotlsp_nes_history ~= nil
144+
end
145+
146+
--- Restore the last suggestion from history
147+
---@param bufnr? integer
148+
---@return boolean -- true if suggestion was restored, false otherwise
149+
function M.restore_suggestion(bufnr)
150+
if not M.has_history(bufnr) then
151+
return false
152+
end
153+
-- Set flag to indicate this is a restoration, not a new suggestion
154+
vim.b[bufnr].copilotlsp_nes_restoring = true
155+
return nes_ui.restore_suggestion(bufnr, nes_ns)
156+
end
157+
138158
return M

lua/copilot-lsp/nes/ui.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ local function _dismiss_suggestion(bufnr, ns_id)
77
pcall(vim.api.nvim_buf_clear_namespace, bufnr, ns_id, 0, -1)
88
end
99

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+
1026
---@param bufnr? integer
1127
---@param ns_id integer
1228
function M.clear_suggestion(bufnr, ns_id)
@@ -22,6 +38,10 @@ function M.clear_suggestion(bufnr, ns_id)
2238
_dismiss_suggestion(bufnr, ns_id)
2339
---@type copilotlsp.InlineEdit
2440
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)
2545
if not state then
2646
return
2747
end
@@ -33,6 +53,38 @@ function M.clear_suggestion(bufnr, ns_id)
3353
vim.b[bufnr].copilotlsp_nes_last_col = nil
3454
end
3555

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+
3688
---@private
3789
---@param bufnr integer
3890
---@param edit lsp.TextEdit
@@ -170,6 +222,11 @@ function M._display_next_suggestion(bufnr, ns_id, edits)
170222
if not edits or #edits == 0 then
171223
return
172224
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
173230

174231
local suggestion = edits[1]
175232
local preview = M._calculate_preview(bufnr, suggestion)

0 commit comments

Comments
 (0)