Skip to content

Commit

Permalink
fix(text_edit): discarded change from the initial buffer (#1552)
Browse files Browse the repository at this point in the history
* fix(text_edit): discarded change from the initial buffer

When multiple calls to apply_text_edits, the changes made to the current
buffer are discarded if the following text_edit concerns a file that
is not in the buffer list.

The problem comes from the _switch function, which executes `edit!` when
it detects that the target is not in the buffer list. If a change was
made to the current buffer before that, that change is discarded
(definition of `edit!`).

The fix consists of a different logic: when _switch detects that the
target is not in the buffer list, it calls `badd` prior to switching to
the buffer that has just been added. No more `edit!`, no more discarded
changes.

The added test fails without the patch in the _switch function.

* use better way to have a named buffer
  • Loading branch information
tbruyelle authored Jul 20, 2024
1 parent 0094b5e commit e05c42f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
11 changes: 5 additions & 6 deletions autoload/lsp/utils/text_edit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,17 @@ function! s:_compare(text_edit1, text_edit2) abort
return a:text_edit1.range.start.character - a:text_edit2.range.start.character
endif
return l:diff
endfunction
endfunction

"
" _switch
"
function! s:_switch(path) abort
if bufnr(a:path) >= 0
execute printf('keepalt keepjumps %sbuffer!', bufnr(a:path))
else
execute printf('keepalt keepjumps edit! %s', fnameescape(a:path))
if bufnr(a:path) == -1
execute printf('badd %s', fnameescape(a:path))
endif
endfunction
execute printf('keepalt keepjumps %sbuffer!', bufnr(a:path))
endfunction

"
" delete
Expand Down
40 changes: 40 additions & 0 deletions test/lsp/utils/text_edit.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function! s:set_text(lines)
% delete _
put =a:lines
execute 'normal ggdd'
execute 'file my-file'
endfunction

function! s:get_text()
Expand Down Expand Up @@ -642,6 +643,45 @@ Describe lsp#utils#text_edit

Assert Equals(getbufline(l:target, 1), ['aiueo'])
End

It should apply edits to buffer and unloaded file
let l:text = ['plop']
call s:set_text(l:text)
let l:buffer_text = s:get_text()
Assert Equals(l:buffer_text, ['plop', ''])
let l:target = globpath(&runtimepath, 'test/lsp/utils/text_edit.vimspec')
call lsp#utils#text_edit#apply_text_edits(
\ lsp#utils#path_to_uri(expand('%')),
\ [{
\ 'range': {
\ 'start': {
\ 'line': 0,
\ 'character': 0,
\ },
\ 'end': {
\ 'line': 1,
\ 'character': 0,
\ }
\ },
\ 'newText': "buffer\n"
\ }])
call lsp#utils#text_edit#apply_text_edits(lsp#utils#path_to_uri(l:target), [{
\ 'range': {
\ 'start': {
\ 'line': 0,
\ 'character': 0,
\ },
\ 'end': {
\ 'line': 1,
\ 'character': 0,
\ }
\ },
\ 'newText': "unloaded\n"
\ }])

Assert Equals(getbufline(l:target, 1), ['unloaded'])
Assert Equals(getbufline(expand('%'), 1), ['buffer'])
End
End
End

0 comments on commit e05c42f

Please sign in to comment.