Skip to content

Commit e05c42f

Browse files
authored
fix(text_edit): discarded change from the initial buffer (#1552)
* 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
1 parent 0094b5e commit e05c42f

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

autoload/lsp/utils/text_edit.vim

+5-6
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,17 @@ function! s:_compare(text_edit1, text_edit2) abort
200200
return a:text_edit1.range.start.character - a:text_edit2.range.start.character
201201
endif
202202
return l:diff
203-
endfunction
203+
endfunction
204204

205205
"
206206
" _switch
207207
"
208208
function! s:_switch(path) abort
209-
if bufnr(a:path) >= 0
210-
execute printf('keepalt keepjumps %sbuffer!', bufnr(a:path))
211-
else
212-
execute printf('keepalt keepjumps edit! %s', fnameescape(a:path))
209+
if bufnr(a:path) == -1
210+
execute printf('badd %s', fnameescape(a:path))
213211
endif
214-
endfunction
212+
execute printf('keepalt keepjumps %sbuffer!', bufnr(a:path))
213+
endfunction
215214

216215
"
217216
" delete

test/lsp/utils/text_edit.vimspec

+40
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ function! s:set_text(lines)
22
% delete _
33
put =a:lines
44
execute 'normal ggdd'
5+
execute 'file my-file'
56
endfunction
67

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

643644
Assert Equals(getbufline(l:target, 1), ['aiueo'])
644645
End
646+
647+
It should apply edits to buffer and unloaded file
648+
let l:text = ['plop']
649+
call s:set_text(l:text)
650+
let l:buffer_text = s:get_text()
651+
Assert Equals(l:buffer_text, ['plop', ''])
652+
let l:target = globpath(&runtimepath, 'test/lsp/utils/text_edit.vimspec')
653+
call lsp#utils#text_edit#apply_text_edits(
654+
\ lsp#utils#path_to_uri(expand('%')),
655+
\ [{
656+
\ 'range': {
657+
\ 'start': {
658+
\ 'line': 0,
659+
\ 'character': 0,
660+
\ },
661+
\ 'end': {
662+
\ 'line': 1,
663+
\ 'character': 0,
664+
\ }
665+
\ },
666+
\ 'newText': "buffer\n"
667+
\ }])
668+
call lsp#utils#text_edit#apply_text_edits(lsp#utils#path_to_uri(l:target), [{
669+
\ 'range': {
670+
\ 'start': {
671+
\ 'line': 0,
672+
\ 'character': 0,
673+
\ },
674+
\ 'end': {
675+
\ 'line': 1,
676+
\ 'character': 0,
677+
\ }
678+
\ },
679+
\ 'newText': "unloaded\n"
680+
\ }])
681+
682+
Assert Equals(getbufline(l:target, 1), ['unloaded'])
683+
Assert Equals(getbufline(expand('%'), 1), ['buffer'])
684+
End
645685
End
646686
End
647687

0 commit comments

Comments
 (0)