@@ -260,17 +260,27 @@ local context_source = {
260260 context .remove_selection (item .data .additional_data --[[ @as OpencodeContextSelection]] )
261261 end
262262
263+ -- Remove the inserted completion text from the input buffer
263264 vim .schedule (function ()
264265 vim .fn .feedkeys (vim .api .nvim_replace_termcodes (' <Esc>' , true , false , true ), ' n' )
265266
266267 local _ , col = unpack (vim .api .nvim_win_get_cursor (0 ))
267268 local line = vim .api .nvim_get_current_line ()
268269 local key = config .get_key_for_function (' input_window' , ' context_items' )
269- if col > 0 and line :sub (col , col ) == key then
270+
271+ local completion_text = key .. (item .label or item .insert_text or ' ' )
272+ local text_start = col - # completion_text
273+
274+ if text_start >= 0 and line :sub (text_start + 1 , col ) == completion_text then
275+ line = line :sub (1 , text_start ) .. line :sub (col + 1 )
276+ input_win .set_current_line (line )
277+ vim .api .nvim_win_set_cursor (0 , { vim .api .nvim_win_get_cursor (0 )[1 ], text_start })
278+ elseif col > 0 and line :sub (col , col ) == key then
270279 line = line :sub (1 , col - 1 ) .. line :sub (col + 1 )
271280 input_win .set_current_line (line )
272- vim .fn .feedkeys (vim .api .nvim_replace_termcodes (' a' , true , false , true ), ' n' )
273281 end
282+
283+ vim .fn .feedkeys (vim .api .nvim_replace_termcodes (' a' , true , false , true ), ' n' )
274284 end )
275285 end ,
276286}
0 commit comments