@@ -580,6 +580,32 @@ function M.prev()
580580 end , ctx )
581581end
582582
583+ local function apply_edit (range , new_lines , bufnr )
584+ local start_line = range .start .line
585+ local start_char = range .start .character
586+ local end_line = range [" end" ].line
587+ local end_char = range [" end" ].character
588+
589+ local current_lines = vim .api .nvim_buf_get_lines (bufnr , start_line , end_line + 1 , false )
590+ if # current_lines == 0 then return end
591+
592+ local before = current_lines [1 ]:sub (1 , start_char )
593+ local after = current_lines [# current_lines ]:sub (end_char + 1 )
594+
595+ local result_lines = {}
596+ if # new_lines == 1 then
597+ table.insert (result_lines , before .. new_lines [1 ] .. after )
598+ else
599+ table.insert (result_lines , before .. new_lines [1 ])
600+ for i = 2 , # new_lines - 1 do
601+ table.insert (result_lines , new_lines [i ])
602+ end
603+ table.insert (result_lines , new_lines [# new_lines ] .. after )
604+ end
605+
606+ vim .api .nvim_buf_set_lines (bufnr , start_line , end_line + 1 , false , result_lines )
607+ end
608+
583609--- @param modifier ? (fun ( suggestion : copilot_get_completions_data_completion ): copilot_get_completions_data_completion )
584610function M .accept (modifier )
585611 local ctx = get_ctx ()
@@ -639,8 +665,7 @@ function M.accept(modifier)
639665 vim .schedule_wrap (function ()
640666 -- Create an undo breakpoint
641667 vim .cmd (" let &undolevels=&undolevels" )
642- -- Hack for 'autoindent', makes the indent persist. Check `:help 'autoindent'`.
643- vim .api .nvim_feedkeys (vim .api .nvim_replace_termcodes (" <Space><Left><Del>" , true , false , true ), " n" , false )
668+
644669 local bufnr = vim .api .nvim_get_current_buf ()
645670
646671 -- only utf encodings are supported
@@ -657,13 +682,7 @@ function M.accept(modifier)
657682 local lines_count = # lines
658683 local last_col = # lines [lines_count ]
659684
660- -- apply_text_edits will remove the last \n if the last line is empty,
661- -- so we trick it by adding an extra one
662- if last_col == 0 then
663- newText = newText .. " \n "
664- end
665-
666- vim .lsp .util .apply_text_edits ({ { range = range , newText = newText } }, bufnr , encoding )
685+ apply_edit (range , lines , bufnr )
667686
668687 -- Position cursor at the end of the last inserted line
669688 local new_cursor_line = range [" start" ].line + # lines
0 commit comments