All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 " bufnr: bufnr('%') " required
3 " server - 'server_name' " optional
4 " sync: 0 " optional, defaults to 0 (async)
6 function! lsp#internal#document_formatting#format(options) abort
8 if l:mode =~# '[vV]' || l:mode ==# "\<C-V>"
9 return lsp#internal#document_range_formatting#format(a:options)
12 if has_key(a:options, 'server')
13 let l:servers = [a:options['server']]
15 let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_document_formatting_provider(v:val)')
18 if len(l:servers) == 0
19 let l:filetype = getbufvar(a:options['bufnr'], '&filetype')
20 call lsp#utils#error('textDocument/formatting not supported for ' . l:filetype)
24 " TODO: ask user to select server for formatting if there are multiple servers
25 let l:server = l:servers[0]
27 redraw | echo 'Formatting Document ...'
29 call lsp#_new_command()
32 \ 'method': 'textDocument/formatting',
34 \ 'textDocument': lsp#get_text_document_identifier(a:options['bufnr']),
36 \ 'tabSize': lsp#utils#buffer#get_indent_size(a:options['bufnr']),
37 \ 'insertSpaces': getbufvar(a:options['bufnr'], '&expandtab') ? v:true : v:false,
40 \ 'bufnr': a:options['bufnr'],
43 if get(a:options, 'sync', 0) == 1
45 let l:x = lsp#callbag#pipe(
46 \ lsp#request(l:server, l:request),
47 \ lsp#callbag#takeUntil(lsp#callbag#pipe(
49 \ lsp#callbag#filter({x->has_key(x, 'command')}),
51 \ lsp#callbag#toList(),
52 \ ).wait({ 'sleep': get(a:options, 'sleep', 1), 'timeout': get(a:options, 'timeout', g:lsp_format_sync_timeout) })
53 call s:format_next(l:x[0])
54 call s:format_complete()
56 call s:format_error(v:exception . ' ' . v:throwpoint)
59 return lsp#callbag#pipe(
60 \ lsp#request(l:server, l:request),
61 \ lsp#callbag#takeUntil(lsp#callbag#pipe(
63 \ lsp#callbag#filter({x->has_key(x, 'command')}),
65 \ lsp#callbag#subscribe({
66 \ 'next':{x->s:format_next(x)},
67 \ 'error': {x->s:format_error(e)},
68 \ 'complete': {->s:format_complete()},
74 function! s:format_next(x) abort
75 if lsp#client#is_error(a:x['response']) | return | endif
76 call lsp#utils#text_edit#apply_text_edits(a:x['request']['params']['textDocument']['uri'], get(a:x['response'], 'result', ''))
79 function! s:format_error(e) abort
80 call lsp#log('Formatting Document Failed', a:e)
81 call lsp#utils#error('Formatting Document Failed.' . (type(a:e) == type('') ? a:e : ''))
84 function! s:format_complete() abort
85 redraw | echo 'Formatting Document complete'