@@ -83,31 +83,10 @@ function M._calculate_lines(suggestion)
83
83
}
84
84
end
85
85
86
- --- @private
87
- --- @class TextDeletion
88
- --- @field range lsp.Range
89
-
90
- --- @private
91
- --- @class InlineInsertion
92
- --- @field text string
93
- --- @field line integer
94
- --- @field character integer
95
-
96
- --- @private
97
- --- @class TextInsertion
98
- --- @field text string
99
- --- @field line integer insert lines at this line
100
-
101
- --- @private
102
- --- @class InlineEditPreview
103
- --- @field deletions ? TextDeletion[]
104
- --- @field inline_insertion ? InlineInsertion
105
- --- @field lines_insertion ? TextInsertion
106
-
107
86
--- @param bufnr integer
108
87
--- @param edit lsp.TextEdit
109
- --- @return InlineEditPreview
110
- function M .preview_inline_edit (bufnr , edit )
88
+ --- @return copilotlsp.nes. InlineEditPreview
89
+ function M .caculate_preview (bufnr , edit )
111
90
local text = edit .newText
112
91
local range = edit .range
113
92
local start_line = range .start .line
@@ -134,10 +113,8 @@ function M.preview_inline_edit(bufnr, edit)
134
113
if is_same_line and is_deletion then
135
114
-- inline deletion
136
115
return {
137
- deletions = {
138
- {
139
- range = edit .range ,
140
- },
116
+ deletion = {
117
+ range = edit .range ,
141
118
},
142
119
}
143
120
end
@@ -157,7 +134,7 @@ function M.preview_inline_edit(bufnr, edit)
157
134
if start_char == # old_lines [1 ] and new_lines [1 ] == " " then
158
135
-- insert lines after the start line
159
136
return {
160
- line_insertion = {
137
+ lines_insertion = {
161
138
text = table.concat (vim .list_slice (new_lines , 2 ), " \n " ),
162
139
line = start_line ,
163
140
},
@@ -169,7 +146,7 @@ function M.preview_inline_edit(bufnr, edit)
169
146
return {
170
147
lines_insertion = {
171
148
text = table.concat (vim .list_slice (new_lines , 1 , num_new_lines - 1 ), " \n " ),
172
- line = start_line ,
149
+ line = math.max ( start_line - 1 , 0 ) ,
173
150
},
174
151
}
175
152
end
@@ -184,12 +161,10 @@ function M.preview_inline_edit(bufnr, edit)
184
161
local insertion = table.concat (new_lines_extend , " \n " )
185
162
186
163
return {
187
- deletions = {
188
- {
189
- range = {
190
- start = { line = start_line , character = 0 },
191
- [" end" ] = { line = end_line , character = # old_lines [num_old_lines ] },
192
- },
164
+ deletion = {
165
+ range = {
166
+ start = { line = start_line , character = 0 },
167
+ [" end" ] = { line = end_line , character = # old_lines [num_old_lines ] - 1 },
193
168
},
194
169
},
195
170
lines_insertion = {
@@ -199,11 +174,44 @@ function M.preview_inline_edit(bufnr, edit)
199
174
}
200
175
end
201
176
177
+ --- @param bufnr integer
178
+ --- @param ns_id integer
179
+ --- @param preview copilotlsp.nes.InlineEditPreview
180
+ function M .display_inline_edit_preview (bufnr , ns_id , preview )
181
+ if preview .deletion then
182
+ local range = preview .deletion .range
183
+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , range .start .line , range .start .character , {
184
+ hl_group = " CopilotLspNesDelete" ,
185
+ end_row = range [" end" ].line ,
186
+ end_col = range [" end" ].character + 1 ,
187
+ })
188
+ end
189
+
190
+ local inline_insertion = preview .inline_insertion
191
+ if inline_insertion then
192
+ local virt_lines =
193
+ require (" copilot-lsp.util" ).hl_text_to_virt_lines (inline_insertion .text , vim .bo [bufnr ].filetype )
194
+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , inline_insertion .line , inline_insertion .character , {
195
+ virt_text = virt_lines [1 ],
196
+ virt_text_pos = " inline" ,
197
+ })
198
+ end
199
+
200
+ local lines_insertion = preview .lines_insertion
201
+ if lines_insertion then
202
+ local virt_lines =
203
+ require (" copilot-lsp.util" ).hl_text_to_virt_lines (lines_insertion .text , vim .bo [bufnr ].filetype )
204
+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , lines_insertion .line , 0 , {
205
+ virt_lines = virt_lines ,
206
+ })
207
+ end
208
+ end
209
+
202
210
--- @private
203
211
--- @param edits copilotlsp.InlineEdit[]
204
212
--- @param ns_id integer
205
213
function M ._display_next_suggestion (edits , ns_id )
206
- local bufnr = vim .api . nvim_get_current_buf ( )
214
+ local bufnr = vim .uri_to_bufnr ( edits [ 1 ]. textDocument . uri )
207
215
local state = vim .b [bufnr ].nes_state
208
216
if state then
209
217
M .clear_suggestion (vim .api .nvim_get_current_buf (), ns_id )
@@ -213,61 +221,10 @@ function M._display_next_suggestion(edits, ns_id)
213
221
-- vim.notify("No suggestion available", vim.log.levels.INFO)
214
222
return
215
223
end
216
- local bufnr = vim .uri_to_bufnr (edits [1 ].textDocument .uri )
217
224
local suggestion = edits [1 ]
218
225
219
- local lines = M ._calculate_lines (suggestion )
220
-
221
- local line_replacement = false
222
- -- check if the edit is a inline insert or delete but not a whole line replacement
223
- if lines .same_line then
224
- local row = suggestion .range .start .line
225
- local start_col = suggestion .range .start .character
226
- local end_col = suggestion .range [" end" ].character
227
- local line_text = vim .api .nvim_buf_get_lines (bufnr , row , row + 1 , false )[1 ]
228
- if start_col == 0 and end_col == # line_text then
229
- line_replacement = true
230
- end
231
- end
232
-
233
- if lines .same_line and not line_replacement then
234
- local row = suggestion .range .start .line
235
- local start_col = suggestion .range .start .character
236
- local end_col = suggestion .range [" end" ].character
237
-
238
- -- inline edit
239
- if start_col < end_col then
240
- vim .api .nvim_buf_set_extmark (bufnr , ns_id , row , start_col , {
241
- hl_group = " CopilotLspNesDelete" ,
242
- end_col = end_col ,
243
- })
244
- end
245
- if suggestion .text ~= " " then
246
- local virt_lines =
247
- require (" copilot-lsp.util" ).hl_text_to_virt_lines (suggestion .text , vim .bo [bufnr ].filetype )
248
- local virt_text = virt_lines [1 ]
249
- vim .api .nvim_buf_set_extmark (bufnr , ns_id , row , end_col , {
250
- virt_text = virt_text ,
251
- virt_text_pos = " inline" ,
252
- })
253
- end
254
- else
255
- if lines .deleted_lines_count > 0 then
256
- -- Deleted range red highlight
257
- vim .api .nvim_buf_set_extmark (bufnr , ns_id , lines .delete_extmark .row , 0 , {
258
- hl_group = " CopilotLspNesDelete" ,
259
- end_row = lines .delete_extmark .end_row ,
260
- })
261
- end
262
- if lines .added_lines_count > 0 then
263
- local text = trim_end (edits [1 ].text )
264
- local virt_lines = require (" copilot-lsp.util" ).hl_text_to_virt_lines (text , vim .bo [bufnr ].filetype )
265
-
266
- vim .api .nvim_buf_set_extmark (bufnr , ns_id , lines .virt_lines_extmark .row , 0 , {
267
- virt_lines = virt_lines ,
268
- })
269
- end
270
- end
226
+ local preview = M .caculate_preview (bufnr , suggestion )
227
+ M .display_inline_edit_preview (bufnr , ns_id , preview )
271
228
272
229
vim .b [bufnr ].nes_state = suggestion
273
230
0 commit comments