@@ -36,12 +36,12 @@ function M._calculate_lines(suggestion)
36
36
local deleted_lines_count = suggestion .range [" end" ].line - suggestion .range .start .line
37
37
local added_lines = vim .split (trim_end (suggestion .newText ), " \n " )
38
38
local added_lines_count = suggestion .newText == " " and 0 or # added_lines
39
- local same_line = 0
39
+ local same_line = false
40
40
41
41
if deleted_lines_count == 0 and added_lines_count == 1 then
42
42
--- changing within line
43
43
deleted_lines_count = 1
44
- same_line = 1
44
+ same_line = true
45
45
end
46
46
47
47
-- if
87
87
--- @param edits copilotlsp.InlineEdit[]
88
88
--- @param ns_id integer
89
89
function M ._display_next_suggestion (edits , ns_id )
90
- local state = vim .b [vim .api .nvim_get_current_buf ()].nes_state
90
+ local bufnr = vim .api .nvim_get_current_buf ()
91
+ local state = vim .b [bufnr ].nes_state
91
92
if state then
92
93
M .clear_suggestion (vim .api .nvim_get_current_buf (), ns_id )
93
94
end
@@ -101,20 +102,55 @@ function M._display_next_suggestion(edits, ns_id)
101
102
102
103
local lines = M ._calculate_lines (suggestion )
103
104
104
- if lines .deleted_lines_count > 0 then
105
- -- Deleted range red highlight
106
- vim .api .nvim_buf_set_extmark (bufnr , ns_id , lines .delete_extmark .row , 0 , {
107
- hl_group = " CopilotLspNesDelete" ,
108
- end_row = lines .delete_extmark .end_row ,
109
- })
105
+ local line_replacement = false
106
+ if lines .same_line then
107
+ local row = suggestion .range .start .line
108
+ local start_col = suggestion .range .start .character
109
+ local end_col = suggestion .range [" end" ].character
110
+
111
+ local line_text = vim .api .nvim_buf_get_lines (bufnr , row , row + 1 , false )[1 ]
112
+ if start_col == 0 and end_col == # line_text then
113
+ line_replacement = true
114
+ end
110
115
end
111
- if lines .added_lines_count > 0 then
112
- local text = trim_end (edits [1 ].text )
113
- local virt_lines = require (" copilot-lsp.util" ).hl_text_to_virt_lines (text , vim .bo [bufnr ].filetype )
114
116
115
- vim .api .nvim_buf_set_extmark (bufnr , ns_id , lines .virt_lines_extmark .row , 0 , {
116
- virt_lines = virt_lines ,
117
- })
117
+ if lines .same_line and not line_replacement then
118
+ local row = suggestion .range .start .line
119
+ local start_col = suggestion .range .start .character
120
+ local end_col = suggestion .range [" end" ].character
121
+
122
+ -- inline edit
123
+ if start_col < end_col then
124
+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , row , start_col , {
125
+ hl_group = " CopilotLspNesDelete" ,
126
+ end_col = end_col ,
127
+ })
128
+ end
129
+ if suggestion .text ~= " " then
130
+ local virt_lines =
131
+ require (" copilot-lsp.util" ).hl_text_to_virt_lines (suggestion .text , vim .bo [bufnr ].filetype )
132
+ local virt_text = virt_lines [1 ]
133
+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , row , end_col , {
134
+ virt_text = virt_text ,
135
+ virt_text_pos = " inline" ,
136
+ })
137
+ end
138
+ else
139
+ if lines .deleted_lines_count > 0 then
140
+ -- Deleted range red highlight
141
+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , lines .delete_extmark .row , 0 , {
142
+ hl_group = " CopilotLspNesDelete" ,
143
+ end_row = lines .delete_extmark .end_row ,
144
+ })
145
+ end
146
+ if lines .added_lines_count > 0 then
147
+ local text = trim_end (edits [1 ].text )
148
+ local virt_lines = require (" copilot-lsp.util" ).hl_text_to_virt_lines (text , vim .bo [bufnr ].filetype )
149
+
150
+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , lines .virt_lines_extmark .row , 0 , {
151
+ virt_lines = virt_lines ,
152
+ })
153
+ end
118
154
end
119
155
120
156
vim .b [bufnr ].nes_state = suggestion
0 commit comments