Skip to content

Commit

Permalink
added :LspDocumentRangeFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
prabirshrestha committed Aug 7, 2017
1 parent ae9d05b commit 8e3304d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions autoload/lsp/capabilities.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function! lsp#capabilities#has_document_formatting_provider(server_name) abort
return s:has_bool_provider(a:server_name, 'documentFormattingProvider')
endfunction

function! lsp#capabilities#has_document_range_formatting_provider(server_name) abort
return s:has_bool_provider(a:server_name, 'documentRangeFormattingProvider')
endfunction

function! lsp#capabilities#has_document_symbol_provider(server_name) abort
return s:has_bool_provider(a:server_name, 'documentSymbolProvider')
endfunction
Expand Down
35 changes: 34 additions & 1 deletion autoload/lsp/ui/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,45 @@ function! lsp#ui#vim#document_format() abort
\ 'insertSpaces': getbufvar(bufnr('%'), '&expandtab') ? v:true : v:false,
\ },
\ },
\ 'on_notification': function('s:handle_text_edit', [l:server, s:last_req_id, 'format']),
\ 'on_notification': function('s:handle_text_edit', [l:server, s:last_req_id, 'document format']),
\ })

echom 'Formatting document ...'
endfunction

function! lsp#ui#vim#document_range_format() abort
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_document_range_formatting_provider(v:val)')
let s:last_req_id = s:last_req_id + 1

if len(l:servers) == 0
echom 'Document range formatting not supported for ' . &filetype
return
endif

" TODO: ask user to select server for formatting
let l:server = l:servers[0]
let [l:start_lnum, l:start_col] = getpos("'<")[1:2]
let [l:end_lnum, l:end_col] = getpos("'>")[1:2]

call lsp#send_request(l:server, {
\ 'method': 'textDocument/rangeFormatting',
\ 'params': {
\ 'textDocument': lsp#get_text_document_identifier(),
\ 'range': {
\ 'start': { 'line': l:start_lnum - 1, 'character': l:start_col - 1 },
\ 'end': { 'line': l:end_lnum - 1, 'character': l:end_col - 1 },
\ },
\ 'options': {
\ 'tabSize': getbufvar(bufnr('%'), '&shiftwidth'),
\ 'insertSpaces': getbufvar(bufnr('%'), '&expandtab') ? v:true : v:false,
\ },
\ },
\ 'on_notification': function('s:handle_text_edit', [l:server, s:last_req_id, 'range format']),
\ })

echom 'Formatting document range ...'
endfunction

function! lsp#ui#vim#workspace_symbol() abort
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_workspace_symbol_provider(v:val)')
let s:last_req_id = s:last_req_id + 1
Expand Down
1 change: 1 addition & 0 deletions plugin/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ command! LspReferences call lsp#ui#vim#references()
command! LspRename call lsp#ui#vim#rename()
command! LspWorkspaceSymbol call lsp#ui#vim#workspace_symbol()
command! LspDocumentFormat call lsp#ui#vim#document_format()
command! -range LspDocumentRangeFormat call lsp#ui#vim#document_range_format()

0 comments on commit 8e3304d

Please sign in to comment.