]> git.madduck.net Git - etc/vim.git/blob - autoload/lsp/internal/document_formatting.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/vim-lsp/' content from commit 04428c92
[etc/vim.git] / autoload / lsp / internal / document_formatting.vim
1 " options - {
2 "   bufnr: bufnr('%')       " required
3 "   server - 'server_name'  " optional
4 "   sync: 0                 " optional, defaults to 0 (async)
5 " }
6 function! lsp#internal#document_formatting#format(options) abort
7     let l:mode = mode()
8     if l:mode =~# '[vV]' || l:mode ==# "\<C-V>"
9         return lsp#internal#document_range_formatting#format(a:options)
10     endif
11
12     if has_key(a:options, 'server')
13         let l:servers = [a:options['server']]
14     else
15         let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_document_formatting_provider(v:val)')
16     endif
17
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)
21         return
22     endif
23
24     " TODO: ask user to select server for formatting if there are multiple servers
25     let l:server = l:servers[0]
26
27     redraw | echo 'Formatting Document ...'
28
29     call lsp#_new_command()
30
31     let l:request = {
32         \ 'method': 'textDocument/formatting',
33         \ 'params': {
34         \   'textDocument': lsp#get_text_document_identifier(a:options['bufnr']),
35         \   'options': {
36         \       'tabSize': lsp#utils#buffer#get_indent_size(a:options['bufnr']),
37         \       'insertSpaces': getbufvar(a:options['bufnr'], '&expandtab') ? v:true : v:false,
38         \   }
39         \ },
40         \ 'bufnr': a:options['bufnr'],
41         \ }
42
43     if get(a:options, 'sync', 0) == 1
44         try
45             let l:x = lsp#callbag#pipe(
46                 \ lsp#request(l:server, l:request),
47                 \ lsp#callbag#takeUntil(lsp#callbag#pipe(
48                 \   lsp#stream(),
49                 \   lsp#callbag#filter({x->has_key(x, 'command')}),
50                 \ )),
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()
55         catch
56             call s:format_error(v:exception . ' ' . v:throwpoint)
57         endtry
58     else
59         return lsp#callbag#pipe(
60             \ lsp#request(l:server, l:request),
61             \ lsp#callbag#takeUntil(lsp#callbag#pipe(
62             \   lsp#stream(),
63             \   lsp#callbag#filter({x->has_key(x, 'command')}),
64             \ )),
65             \ lsp#callbag#subscribe({
66             \   'next':{x->s:format_next(x)},
67             \   'error': {x->s:format_error(e)},
68             \   'complete': {->s:format_complete()},
69             \ }),
70             \ )
71     endif
72 endfunction
73
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', ''))
77 endfunction
78
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 : ''))
82 endfunction
83
84 function! s:format_complete() abort
85     redraw | echo 'Formatting Document complete'
86 endfunction