Skip to content

Commit b58841b

Browse files
authored
Merge pull request #215 from prabirshrestha/sync-request
Add option to request synchronized .
2 parents b1083bf + 7cc8a70 commit b58841b

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-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

doc/vim-lsp.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CONTENTS *vim-lsp-contents*
2222
LspDocumentDiagnostics |LspDocumentDiagnostics|
2323
LspDefinition |LspDefinition|
2424
LspDocumentFormat |LspDocumentFormat|
25+
LspDocumentFormatSync |LspDocumentFormatSync|
2526
LspDocumentRangeFormat |LspDocumentRangeFormat|
2627
LspDocumentSymbol |LspDocumentSymbol|
2728
LspHover |LspHover|
@@ -304,6 +305,16 @@ LspDocumentFormat *LspDocumentFormat*
304305

305306
Format the entire document.
306307

308+
LspDocumentFormatSync *LspDocumentFormatSync*
309+
310+
Same as |LspDocumentFormat| but synchronous. Useful when running |autocmd|
311+
commands such as formatting before save.
312+
313+
Example:
314+
autocmd BufWritePre <buffer> LspDocumentFormatSync
315+
316+
Note that this may slow down vim.
317+
307318
LspDocumentRangeFormat *LspDocumentRangeFormat*
308319

309320
Format the current document selection.

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)