Skip to content

Commit ed304db

Browse files
thomasfaingnaertprabirshrestha
authored andcommitted
Fix LspDocumentFormat not working as expected (#410)
* Add test * Fix LspDocumentFormat adding newlines * Fix manually pressing enter when formatting
1 parent 1d48fc8 commit ed304db

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

autoload/lsp/utils/text_edit.vim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,16 @@ function! s:generate_sub_cmd_replace(text_edit) abort
185185

186186
let l:sub_cmd = s:preprocess_cmd(a:text_edit['range'])
187187
let l:sub_cmd .= s:generate_move_start_cmd(l:start_line, l:start_character) " move to the first position
188-
let l:sub_cmd .= 'v'
188+
189+
" If start and end position are 0, we are selecting a range of lines.
190+
" Thus, we can use linewise-visual mode, which avoids some inconsistencies
191+
" when applying text edits.
192+
if l:start_character == 0 && l:end_character == 0
193+
let l:sub_cmd .= 'V'
194+
else
195+
let l:sub_cmd .= 'v'
196+
endif
197+
189198
let l:sub_cmd .= s:generate_move_end_cmd(l:end_line, l:end_character) " move to the last position
190199

191200
if len(l:new_text) == 0

plugin/lsp.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ nnoremap <plug>(lsp-rename) :<c-u>call lsp#ui#vim#rename()<cr>
7676
nnoremap <plug>(lsp-type-definition) :<c-u>call lsp#ui#vim#type_definition()<cr>
7777
nnoremap <plug>(lsp-workspace-symbol) :<c-u>call lsp#ui#vim#workspace_symbol()<cr>
7878
nnoremap <plug>(lsp-document-format) :<c-u>call lsp#ui#vim#document_format()<cr>
79-
vnoremap <plug>(lsp-document-format) :call lsp#ui#vim#document_range_format()<cr>
79+
vnoremap <plug>(lsp-document-format) :<Home>silent <End>call lsp#ui#vim#document_range_format()<cr>
8080
nnoremap <plug>(lsp-implementation) :<c-u>call lsp#ui#vim#implementation()<cr>
8181
nnoremap <plug>(lsp-status) :<c-u>echo lsp#get_server_status()<cr>
8282
nnoremap <plug>(lsp-next-reference) :<c-u>call lsp#ui#vim#references#jump(+1)<cr>

test/lsp/utils/text_edit.vimspec

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,29 @@ Describe lsp#utils#text_edit
367367
Assert Equals(l:buffer_text, ['fr', 'baz', ''])
368368
End
369369

370+
It replaces entire buffer correctly
371+
call s:set_text(['foo', 'bar', 'baz'])
372+
373+
call lsp#utils#text_edit#apply_text_edits(
374+
\ expand('%'),
375+
\ [{
376+
\ 'range': {
377+
\ 'start': {
378+
\ 'line': 0,
379+
\ 'character': 0
380+
\ },
381+
\ 'end': {
382+
\ 'line': 3,
383+
\ 'character': 0
384+
\ }
385+
\ },
386+
\ 'newText': "x\ny\nz\n"
387+
\ }])
388+
389+
let l:buffer_text = s:get_text()
390+
Assert Equals(l:buffer_text, ['x', 'y', 'z', ''])
391+
End
392+
370393
It preserves v:completed_item
371394
" Add some text to buffer
372395
call s:set_text(['foo', 'bar'])

0 commit comments

Comments
 (0)