@@ -199,6 +199,31 @@ local function add_cursor_data_item(ctx)
199199 )
200200end
201201
202+ --- Remove the inserted completion text from the input buffer
203+ --- @param item CompletionItem
204+ local function remove_inserted_text (item )
205+ local input_win = require (' opencode.ui.input_window' )
206+ vim .fn .feedkeys (vim .api .nvim_replace_termcodes (' <Esc>' , true , false , true ), ' n' )
207+
208+ local _ , col = unpack (vim .api .nvim_win_get_cursor (0 ))
209+ local line = vim .api .nvim_get_current_line ()
210+ local key = config .get_key_for_function (' input_window' , ' context_items' )
211+
212+ local completion_text = key .. (item .label or item .insert_text or ' ' )
213+ local text_start = col - # completion_text
214+
215+ if text_start >= 0 and line :sub (text_start + 1 , col ) == completion_text then
216+ line = line :sub (1 , text_start ) .. line :sub (col + 1 )
217+ input_win .set_current_line (line )
218+ vim .api .nvim_win_set_cursor (0 , { vim .api .nvim_win_get_cursor (0 )[1 ], text_start })
219+ elseif col > 0 and line :sub (col , col ) == key then
220+ line = line :sub (1 , col - 1 ) .. line :sub (col + 1 )
221+ input_win .set_current_line (line )
222+ end
223+
224+ vim .fn .feedkeys (vim .api .nvim_replace_termcodes (' a' , true , false , true ), ' n' )
225+ end
226+
202227--- @type CompletionSource
203228local context_source = {
204229 name = ' context' ,
@@ -260,27 +285,8 @@ local context_source = {
260285 context .remove_selection (item .data .additional_data --[[ @as OpencodeContextSelection]] )
261286 end
262287
263- -- Remove the inserted completion text from the input buffer
264288 vim .schedule (function ()
265- vim .fn .feedkeys (vim .api .nvim_replace_termcodes (' <Esc>' , true , false , true ), ' n' )
266-
267- local _ , col = unpack (vim .api .nvim_win_get_cursor (0 ))
268- local line = vim .api .nvim_get_current_line ()
269- local key = config .get_key_for_function (' input_window' , ' context_items' )
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
279- line = line :sub (1 , col - 1 ) .. line :sub (col + 1 )
280- input_win .set_current_line (line )
281- end
282-
283- vim .fn .feedkeys (vim .api .nvim_replace_termcodes (' a' , true , false , true ), ' n' )
289+ remove_inserted_text (item )
284290 end )
285291 end ,
286292}
0 commit comments