Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(text_edit): discarded change from the initial buffer #1552

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 'write! /tmp/xyz'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because otherwise the buffer created by this s:set_text() function will have no name. Because the _switch(path) function is called with the return value of bufname('%'), if it gets an empty path it doesn't work properly.

This wasn't discovered before because no tests before the one I added actually pass more than one text_edits to the lsp#utils#text_edit#apply_text_edits(uri, text_edits) function.

Copy link
Contributor Author

@tbruyelle tbruyelle May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said there's maybe a better way to give a name to a buffer, without involving a disk write.
For example I can replace that line with execute 'file xyz'. The tests are still green after that change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattn I have changed to file my-file which has the benefit of non affecting the file system.
7fce3b87fce3b8

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