lsp: Add sync variant of LSP formatting

Also, factor out a `vim.lsp.util.get_effective_tabstop()` helper and add
tests for it.
This commit is contained in:
David Lukes
2020-05-05 17:23:45 +02:00
parent 4496628c18
commit ebee9ebe2b
4 changed files with 59 additions and 10 deletions

View File

@@ -65,19 +65,18 @@ function M.completion(context)
end
function M.formatting(options)
validate { options = {options, 't', true} }
local sts = vim.bo.softtabstop;
options = vim.tbl_extend('keep', options or {}, {
tabSize = (sts > 0 and sts) or (sts < 0 and vim.bo.shiftwidth) or vim.bo.tabstop;
insertSpaces = vim.bo.expandtab;
})
local params = {
textDocument = { uri = vim.uri_from_bufnr(0) };
options = options;
}
local params = util.make_formatting_params(options)
return request('textDocument/formatting', params)
end
function M.formatting_sync(options, timeout_ms)
local params = util.make_formatting_params(options)
local result = vim.lsp.buf_request_sync(0, "textDocument/formatting", params, timeout_ms)
if not result then return end
result = result[1].result
vim.lsp.util.apply_text_edits(result)
end
function M.range_formatting(options, start_pos, end_pos)
validate {
options = {options, 't', true};