mirror of
https://github.com/neovim/neovim.git
synced 2025-10-26 12:27:24 +00:00
Support multiple range formatting clients
This commit is contained in:
@@ -112,14 +112,14 @@ function M.completion(context)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--@private
|
--@private
|
||||||
--- If there is more than one client with formatting capability, asks the user
|
--- If there is more than one client that supports the given method,
|
||||||
--- which one to use.
|
--- asks the user to select one.
|
||||||
--
|
--
|
||||||
--@returns The client to use for formatting
|
--@returns The client that the user selected or nil
|
||||||
local function get_formatting_client()
|
local function select_client(method)
|
||||||
local clients = vim.tbl_values(vim.lsp.buf_get_clients());
|
local clients = vim.tbl_values(vim.lsp.buf_get_clients());
|
||||||
clients = vim.tbl_filter(function (client)
|
clients = vim.tbl_filter(function (client)
|
||||||
return client.resolved_capabilities.document_formatting
|
return client.supports_method(method)
|
||||||
end, clients)
|
end, clients)
|
||||||
-- better UX when choices are always in the same order (between restarts)
|
-- better UX when choices are always in the same order (between restarts)
|
||||||
table.sort(clients, function (a, b) return a.name < b.name end)
|
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))
|
table.insert(choices, string.format("%d %s", k, v.name))
|
||||||
end
|
end
|
||||||
local user_choice = vim.fn.confirm(
|
local user_choice = vim.fn.confirm(
|
||||||
"Select a language server for formatting:",
|
"Select a language server:",
|
||||||
table.concat(choices, "\n"),
|
table.concat(choices, "\n"),
|
||||||
0,
|
0,
|
||||||
"Question"
|
"Question"
|
||||||
@@ -152,7 +152,7 @@ end
|
|||||||
--
|
--
|
||||||
--@see https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting
|
--@see https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting
|
||||||
function M.formatting(options)
|
function M.formatting(options)
|
||||||
local client = get_formatting_client()
|
local client = select_client("textDocument/formatting")
|
||||||
if client == nil then return end
|
if client == nil then return end
|
||||||
|
|
||||||
local params = util.make_formatting_params(options)
|
local params = util.make_formatting_params(options)
|
||||||
@@ -172,7 +172,7 @@ end
|
|||||||
--@param timeout_ms (number) Request timeout
|
--@param timeout_ms (number) Request timeout
|
||||||
--@see |vim.lsp.buf.formatting_seq_sync|
|
--@see |vim.lsp.buf.formatting_seq_sync|
|
||||||
function M.formatting_sync(options, timeout_ms)
|
function M.formatting_sync(options, timeout_ms)
|
||||||
local client = get_formatting_client()
|
local client = select_client("textDocument/formatting")
|
||||||
if client == nil then return end
|
if client == nil then return end
|
||||||
|
|
||||||
local params = util.make_formatting_params(options)
|
local params = util.make_formatting_params(options)
|
||||||
@@ -232,15 +232,12 @@ end
|
|||||||
--@param end_pos ({number, number}, optional) mark-indexed position.
|
--@param end_pos ({number, number}, optional) mark-indexed position.
|
||||||
---Defaults to the end of the last visual selection.
|
---Defaults to the end of the last visual selection.
|
||||||
function M.range_formatting(options, start_pos, end_pos)
|
function M.range_formatting(options, start_pos, end_pos)
|
||||||
validate { options = {options, 't', true} }
|
local client = select_client("textDocument/rangeFormatting")
|
||||||
local sts = vim.bo.softtabstop;
|
if client == nil then return end
|
||||||
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 = util.make_given_range_params(start_pos, end_pos)
|
local params = util.make_given_range_params(start_pos, end_pos)
|
||||||
params.options = options
|
params.options = util.make_formatting_params(options).options
|
||||||
return request('textDocument/rangeFormatting', params)
|
return client.request("textDocument/rangeFormatting", params)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Renames all references to the symbol under the cursor.
|
--- Renames all references to the symbol under the cursor.
|
||||||
|
|||||||
Reference in New Issue
Block a user