From 48a59f8f4f3eced84b21a473527b00ef1b9b5bd2 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Fri, 30 Apr 2021 13:40:20 +0200 Subject: Add formatting_seq_sync, change formatting and formatting_sync --- runtime/lua/vim/lsp/buf.lua | 94 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 7 deletions(-) (limited to 'runtime/lua/vim/lsp') diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 31116985e2..d7f3353ad9 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -111,6 +111,39 @@ function M.completion(context) return request('textDocument/completion', params) end +--@private +--- If there is more than one client with formatting capability, asks the user +--- which one to use. +-- +--@returns The client to use for formatting +local function get_formatting_client() + local clients = vim.tbl_values(vim.lsp.buf_get_clients()); + clients = vim.tbl_filter(function (client) + return client.resolved_capabilities.document_formatting + end, clients) + -- better UX when choices are always in the same order (between restarts) + table.sort(clients, function (a, b) return a.name < b.name end) + + if #clients > 1 then + local choices = {} + for k,v in ipairs(clients) do + table.insert(choices, string.format("%d %s", k, v.name)) + end + local user_choice = vim.fn.confirm( + "Select a language server for formatting:", + table.concat(choices, "\n"), + 0, + "Question" + ) + if user_choice == 0 then return nil end + return clients[user_choice] + elseif #clients < 1 then + return nil + else + return clients[1] + end +end + --- Formats the current buffer. --- --@param options (optional, table) Can be used to specify FormattingOptions. @@ -119,8 +152,11 @@ end -- --@see https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting function M.formatting(options) + local client = get_formatting_client() + if client == nil then return end + local params = util.make_formatting_params(options) - return request('textDocument/formatting', params) + return client.request("textDocument/formatting", params) end --- Performs |vim.lsp.buf.formatting()| synchronously. @@ -134,14 +170,58 @@ end --- --@param options Table with valid `FormattingOptions` entries --@param timeout_ms (number) Request timeout +--@see |vim.lsp.buf.formatting_seq_sync| function M.formatting_sync(options, timeout_ms) + local client = get_formatting_client() + if client == nil then return end + local params = util.make_formatting_params(options) - local result = vim.lsp.buf_request_sync(0, "textDocument/formatting", params, timeout_ms) - if not result or vim.tbl_isempty(result) then return end - local _, formatting_result = next(result) - result = formatting_result.result - if not result then return end - vim.lsp.util.apply_text_edits(result) + local result = client.request_sync("textDocument/formatting", params, timeout_ms) + if result and result.result then + util.apply_text_edits(result.result) + end +end + +--- Formats the current buffer by sequentially requesting formatting from attached clients. +--- +--- Useful when multiple clients with formatting capability are attached. +--- +--- Since it's synchronous, can be used for running on save, to make sure buffer is formatted +--- prior to being saved. {timeout_ms} is passed on to the |vim.lsp.client| `request_sync` method. +--- Example: +---
+--- vim.api.nvim_command[[autocmd BufWritePre  lua vim.lsp.buf.formatting_seq_sync()]]
+--- 
+--- +--@param options (optional, table) `FormattingOptions` entries +--@param timeout_ms (optional, number) Request timeout +--@param order (optional, table) List of client names. Formatting is requested from clients +---in the following order: first all clients that are not in the `order` list, then +---the remaining clients in the order as they occur in the `order` list. +function M.formatting_seq_sync(options, timeout_ms, order) + local clients = vim.tbl_values(vim.lsp.buf_get_clients()); + + -- sort the clients according to `order` + for _, client_name in ipairs(order or {}) do + -- if the client exists, move to the end of the list + for i, client in ipairs(clients) do + if client.name == client_name then + table.insert(clients, table.remove(clients, i)) + break + end + end + end + + -- loop through the clients and make synchronous formatting requests + for _, client in ipairs(clients) do + if client.resolved_capabilities.document_formatting then + local params = util.make_formatting_params(options) + local result = client.request_sync("textDocument/formatting", params, timeout_ms) + if result and result.result then + util.apply_text_edits(result.result) + end + end + end end --- Formats a given range. -- cgit From dc9c6ea2194526d829599fe17768a7a09bbc1f56 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Sun, 2 May 2021 15:37:31 +0200 Subject: Support multiple range formatting clients --- runtime/lua/vim/lsp/buf.lua | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'runtime/lua/vim/lsp') diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index d7f3353ad9..8d4aa50370 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -112,14 +112,14 @@ function M.completion(context) end --@private ---- If there is more than one client with formatting capability, asks the user ---- which one to use. +--- If there is more than one client that supports the given method, +--- asks the user to select one. -- ---@returns The client to use for formatting -local function get_formatting_client() +--@returns The client that the user selected or nil +local function select_client(method) local clients = vim.tbl_values(vim.lsp.buf_get_clients()); clients = vim.tbl_filter(function (client) - return client.resolved_capabilities.document_formatting + return client.supports_method(method) end, clients) -- better UX when choices are always in the same order (between restarts) table.sort(clients, function (a, b) return a.name < b.name end) @@ -130,7 +130,7 @@ local function get_formatting_client() table.insert(choices, string.format("%d %s", k, v.name)) end local user_choice = vim.fn.confirm( - "Select a language server for formatting:", + "Select a language server:", table.concat(choices, "\n"), 0, "Question" @@ -152,7 +152,7 @@ end -- --@see https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting function M.formatting(options) - local client = get_formatting_client() + local client = select_client("textDocument/formatting") if client == nil then return end local params = util.make_formatting_params(options) @@ -172,7 +172,7 @@ end --@param timeout_ms (number) Request timeout --@see |vim.lsp.buf.formatting_seq_sync| function M.formatting_sync(options, timeout_ms) - local client = get_formatting_client() + local client = select_client("textDocument/formatting") if client == nil then return end local params = util.make_formatting_params(options) @@ -232,15 +232,12 @@ end --@param end_pos ({number, number}, optional) mark-indexed position. ---Defaults to the end of the last visual selection. function M.range_formatting(options, start_pos, end_pos) - 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 client = select_client("textDocument/rangeFormatting") + if client == nil then return end + local params = util.make_given_range_params(start_pos, end_pos) - params.options = options - return request('textDocument/rangeFormatting', params) + params.options = util.make_formatting_params(options).options + return client.request("textDocument/rangeFormatting", params) end --- Renames all references to the symbol under the cursor. -- cgit From f0f3fddcddf9c01f89a8afaa32bced52632373ee Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Sun, 2 May 2021 16:16:49 +0200 Subject: Synchronous formatting methods notify the user on timeout and interrupted --- runtime/lua/vim/lsp/buf.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/lsp') diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 8d4aa50370..341a3e82fc 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -176,9 +176,11 @@ function M.formatting_sync(options, timeout_ms) if client == nil then return end local params = util.make_formatting_params(options) - local result = client.request_sync("textDocument/formatting", params, timeout_ms) + local result, err = client.request_sync("textDocument/formatting", params, timeout_ms) if result and result.result then util.apply_text_edits(result.result) + elseif err then + vim.notify("vim.lsp.buf.formatting_sync: " .. err, vim.log.levels.WARN) end end @@ -216,9 +218,11 @@ function M.formatting_seq_sync(options, timeout_ms, order) for _, client in ipairs(clients) do if client.resolved_capabilities.document_formatting then local params = util.make_formatting_params(options) - local result = client.request_sync("textDocument/formatting", params, timeout_ms) + local result, err = client.request_sync("textDocument/formatting", params, timeout_ms) if result and result.result then util.apply_text_edits(result.result) + elseif err then + vim.notify(string.format("vim.lsp.buf.formatting_seq_sync: (%s) %s", client.name, err), vim.log.levels.WARN) end end end -- cgit