diff --git a/autoload/lsp/utils/text_edit.vim b/autoload/lsp/utils/text_edit.vim index 6f64b47d0..e6e0c835e 100644 --- a/autoload/lsp/utils/text_edit.vim +++ b/autoload/lsp/utils/text_edit.vim @@ -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 diff --git a/test/lsp/utils/text_edit.vimspec b/test/lsp/utils/text_edit.vimspec index 67776d307..494c07086 100644 --- a/test/lsp/utils/text_edit.vimspec +++ b/test/lsp/utils/text_edit.vimspec @@ -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() @@ -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