Skip to content

Commit d4c8e00

Browse files
committed
Add option to request synchronized .
Also add LspDocumentFormatSync. This can be used for autocmd before saving the file.
1 parent 43770c3 commit d4c8e00

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

autoload/lsp/client.vim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ function! s:lsp_send(id, opts, type) abort " opts = { method, params?, on_notifi
226226
call async#job#send(a:id, l:payload)
227227

228228
if (a:type == s:send_type_request)
229-
return l:request['id']
229+
let l:id = l:request['id']
230+
if get(a:opts, 'sync', 0) !=# 0
231+
call async#job#wait([l:id])
232+
endif
233+
return l:id
230234
else
231235
return 0
232236
endif

autoload/lsp/ui/vim.vim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function! lsp#ui#vim#rename() abort
136136
echo ' ... Renaming ...'
137137
endfunction
138138

139-
function! lsp#ui#vim#document_format() abort
139+
function! s:document_format(sync) abort
140140
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_document_formatting_provider(v:val)')
141141
let s:last_req_id = s:last_req_id + 1
142142

@@ -156,12 +156,21 @@ function! lsp#ui#vim#document_format() abort
156156
\ 'insertSpaces': getbufvar(bufnr('%'), '&expandtab') ? v:true : v:false,
157157
\ },
158158
\ },
159+
\ 'sync': a:sync,
159160
\ 'on_notification': function('s:handle_text_edit', [l:server, s:last_req_id, 'document format']),
160161
\ })
161162

162163
echo 'Formatting document ...'
163164
endfunction
164165

166+
function! lsp#ui#vim#document_format_sync() abort
167+
return s:document_format(1)
168+
endfunction
169+
170+
function! lsp#ui#vim#document_format() abort
171+
return s:document_format(0)
172+
endfunction
173+
165174
function! s:get_visual_selection_pos() abort
166175
" https://groups.google.com/d/msg/vim_dev/oCUQzO3y8XE/vfIMJiHCHtEJ
167176
" https://stackoverflow.com/a/6271254

plugin/lsp.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ command! LspReferences call lsp#ui#vim#references()
3333
command! LspRename call lsp#ui#vim#rename()
3434
command! LspWorkspaceSymbol call lsp#ui#vim#workspace_symbol()
3535
command! LspDocumentFormat call lsp#ui#vim#document_format()
36+
command! LspDocumentFormatSync call lsp#ui#vim#document_format_sync()
3637
command! -range LspDocumentRangeFormat call lsp#ui#vim#document_range_format()
3738
command! LspImplementation call lsp#ui#vim#implementation()
3839
command! LspTypeDefinition call lsp#ui#vim#type_definition()

0 commit comments

Comments
 (0)