fix(lsp): check method is supported when range formatting (#21970)

`vim.lsp.buf.format()` silently did nothing if no servers supported
`textDocument/rangeFormatting` when formatting with a range.

Issue found by `@hwrd:matrix.org` in the Matrix chat.
This commit is contained in:
Sean Dewar
2023-01-24 06:15:43 +00:00
committed by GitHub
parent 7126d9b8c7
commit bcdbf77537
2 changed files with 41 additions and 9 deletions

View File

@@ -197,19 +197,20 @@ function M.format(options)
clients = vim.tbl_filter(options.filter, clients)
end
clients = vim.tbl_filter(function(client)
return client.supports_method('textDocument/formatting')
end, clients)
if #clients == 0 then
vim.notify('[LSP] Format request failed, no matching language servers.')
end
local mode = api.nvim_get_mode().mode
local range = options.range
if not range and mode == 'v' or mode == 'V' then
range = range_from_selection()
end
local method = range and 'textDocument/rangeFormatting' or 'textDocument/formatting'
clients = vim.tbl_filter(function(client)
return client.supports_method(method)
end, clients)
if #clients == 0 then
vim.notify('[LSP] Format request failed, no matching language servers.')
end
---@private
local function set_range(client, params)
@@ -221,7 +222,6 @@ function M.format(options)
return params
end
local method = range and 'textDocument/rangeFormatting' or 'textDocument/formatting'
if options.async then
local do_format
do_format = function(idx, client)