@@ -20,6 +20,7 @@ local function handle_nes_response(err, result)
20
20
nes_ui ._display_next_suggestion (result .edits , nes_ns )
21
21
end
22
22
23
+ --- Requests the NextEditSuggestion from the current cursor position
23
24
--- @param copilot_lss vim.lsp.Client ?
24
25
function M .request_nes (copilot_lss )
25
26
local pos_params = vim .lsp .util .make_position_params (0 , " utf-16" )
@@ -30,35 +31,80 @@ function M.request_nes(copilot_lss)
30
31
copilot_lss :request (" textDocument/copilotInlineEdit" , pos_params , handle_nes_response )
31
32
end
32
33
34
+ --- Walks the cursor to the start of the edit.
35
+ --- This function returns false if there is no edit to apply or if the cursor is already at the start position of the
36
+ --- edit.
33
37
--- @param bufnr ? integer
34
- --- @return boolean
35
- function M .apply_pending_nes (bufnr )
38
+ --- @return boolean --if the cursor walked
39
+ function M .walk_cursor_start_edit (bufnr )
36
40
bufnr = bufnr and bufnr > 0 and bufnr or vim .api .nvim_get_current_buf ()
37
-
38
41
--- @type copilotlsp.InlineEdit
39
42
local state = vim .b [bufnr ].nes_state
40
43
if not state then
41
44
return false
42
45
end
43
- vim .schedule (function ()
44
- local prev_mode = vim .api .nvim_get_mode ().mode
45
- if prev_mode == " i" then
46
- vim .cmd (" stopinsert!" )
47
- end
46
+
47
+ local cursor_row , _ = unpack (vim .api .nvim_win_get_cursor (0 ))
48
+ if cursor_row - 1 ~= state .range .start .line then
49
+ vim .b [bufnr ].nes_jump = true
48
50
--- @type lsp.Location
49
- local jump_loc = {
51
+ local jump_loc_before = {
50
52
uri = state .textDocument .uri ,
51
53
range = {
52
- start = state .range [" end " ],
53
- [" end" ] = state .range [" end " ],
54
+ start = state .range [" start " ],
55
+ [" end" ] = state .range [" start " ],
54
56
},
55
57
}
56
- vim .lsp .util .show_document (jump_loc , " utf-16" , { focus = true })
58
+ return vim .lsp .util .show_document (jump_loc_before , " utf-16" , { focus = true })
59
+ else
60
+ return false
61
+ end
62
+ end
63
+
64
+ --- Walks the cursor to the end of the edit.
65
+ --- This function returns false if there is no edit to apply or if the cursor is already at the end position of the
66
+ --- edit
67
+ --- @param bufnr ? integer
68
+ --- @return boolean --if the cursor walked
69
+ function M .walk_cursor_end_edit (bufnr )
70
+ bufnr = bufnr and bufnr > 0 and bufnr or vim .api .nvim_get_current_buf ()
71
+ --- @type copilotlsp.InlineEdit
72
+ local state = vim .b [bufnr ].nes_state
73
+ if not state then
74
+ return false
75
+ end
76
+
77
+ --- @type lsp.Location
78
+ local jump_loc_after = {
79
+ uri = state .textDocument .uri ,
80
+ range = {
81
+ start = state .range [" end" ],
82
+ [" end" ] = state .range [" end" ],
83
+ },
84
+ }
85
+ -- NOTE: If last line is deletion, then this may be outside of the buffer
86
+ vim .schedule (function ()
87
+ pcall (vim .lsp .util .show_document , jump_loc_after , " utf-16" , { focus = true })
88
+ end )
89
+ return true
90
+ end
91
+
92
+ --- This function applies the pending nes edit to the current buffer and then clears the marks for the pending
93
+ --- suggestion
94
+ --- @param bufnr ? integer
95
+ --- @return boolean --if the nes was applied
96
+ function M .apply_pending_nes (bufnr )
97
+ bufnr = bufnr and bufnr > 0 and bufnr or vim .api .nvim_get_current_buf ()
98
+
99
+ --- @type copilotlsp.InlineEdit
100
+ local state = vim .b [bufnr ].nes_state
101
+ if not state then
102
+ return false
103
+ end
104
+ vim .schedule (function ()
57
105
utils .apply_inline_edit (state )
106
+ vim .b [bufnr ].nes_jump = false
58
107
nes_ui .clear_suggestion (bufnr , nes_ns )
59
- if prev_mode == " i" then
60
- vim .cmd (" startinsert" )
61
- end
62
108
end )
63
109
return true
64
110
end
0 commit comments