aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorTom Praschan <13141438+tom-anders@users.noreply.github.com>2024-06-24 16:54:56 +0200
committerGitHub <noreply@github.com>2024-06-24 07:54:56 -0700
commit5581a95534e44b8714e715c925c9de2d95ae1c21 (patch)
tree0aa6443467f9f103ff95b591c3296d4e1b394276 /runtime
parentb0e59909075a582fbcf12b4c8a3ec1bff12c4eea (diff)
downloadrneovim-5581a95534e44b8714e715c925c9de2d95ae1c21.tar.gz
rneovim-5581a95534e44b8714e715c925c9de2d95ae1c21.tar.bz2
rneovim-5581a95534e44b8714e715c925c9de2d95ae1c21.zip
feat(lsp): vim.lsp.buf.format() supports textDocument/rangesFormatting #27323
While this relies on a proposed LSP 3.18 feature, it's fully backwards compatible, so IMO there's no harm in adding this already. Looks like some servers already support for this e.g. - gopls: https://go-review.googlesource.com/c/tools/+/510235 - clangd: https://github.com/llvm/llvm-project/pull/80180 Fixes #27293
Diffstat (limited to 'runtime')
-rw-r--r--runtime/doc/lsp.txt14
-rw-r--r--runtime/doc/news.txt3
-rw-r--r--runtime/lua/vim/lsp.lua1
-rw-r--r--runtime/lua/vim/lsp/buf.lua28
-rw-r--r--runtime/lua/vim/lsp/protocol.lua1
5 files changed, 36 insertions, 11 deletions
diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt
index ba826f5f7e..e987f266cc 100644
--- a/runtime/doc/lsp.txt
+++ b/runtime/doc/lsp.txt
@@ -191,6 +191,7 @@ won't run if your server doesn't support them.
- textDocument/prepareTypeHierarchy
- textDocument/publishDiagnostics
- textDocument/rangeFormatting
+- textDocument/rangesFormatting
- textDocument/references
- textDocument/rename
- textDocument/semanticTokens/full
@@ -1371,11 +1372,14 @@ format({opts}) *vim.lsp.buf.format()*
(client.id) matching this field.
• {name}? (`string`) Restrict formatting to the client with
name (client.name) matching this field.
- • {range}? (`{start:integer[],end:integer[]}`, default:
- current selection in visual mode, `nil` in other modes,
- formatting the full buffer) Range to format. Table must
- contain `start` and `end` keys with {row,col} tuples using
- (1,0) indexing.
+ • {range}?
+ (`{start:[integer,integer],end:[integer, integer]}|{start:[integer,integer],end:[integer,integer]}[]`,
+ default: current selection in visual mode, `nil` in other
+ modes, formatting the full buffer) Range to format. Table
+ must contain `start` and `end` keys with {row,col} tuples
+ using (1,0) indexing. Can also be a list of tables that
+ contain `start` and `end` keys as described above, in which
+ case `textDocument/rangesFormatting` support is required.
hover() *vim.lsp.buf.hover()*
Displays hover information about the symbol under the cursor in a floating
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt
index 81d2f2aa24..38be234b00 100644
--- a/runtime/doc/news.txt
+++ b/runtime/doc/news.txt
@@ -121,6 +121,9 @@ LSP
• Completion side effects (including snippet expansion, execution of commands
and application of additional text edits) is now built-in.
• |vim.lsp.util.locations_to_items()| sets `end_col` and `end_lnum` fields.
+• |vim.lsp.buf.format()| now supports passing a list of ranges
+ via the `range` parameter (this requires support for the
+ `textDocument/rangesFormatting` request).
LUA
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua
index 623ccdd5cd..7fe4dd8cf5 100644
--- a/runtime/lua/vim/lsp.lua
+++ b/runtime/lua/vim/lsp.lua
@@ -56,6 +56,7 @@ lsp._request_name_to_capability = {
[ms.workspace_symbol] = { 'workspaceSymbolProvider' },
[ms.textDocument_references] = { 'referencesProvider' },
[ms.textDocument_rangeFormatting] = { 'documentRangeFormattingProvider' },
+ [ms.textDocument_rangesFormatting] = { 'documentRangeFormattingProvider', 'rangesSupport' },
[ms.textDocument_formatting] = { 'documentFormattingProvider' },
[ms.textDocument_completion] = { 'completionProvider' },
[ms.textDocument_documentHighlight] = { 'documentHighlightProvider' },
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua
index 299b68e134..f20730b8e6 100644
--- a/runtime/lua/vim/lsp/buf.lua
+++ b/runtime/lua/vim/lsp/buf.lua
@@ -205,9 +205,11 @@ end
--- Range to format.
--- Table must contain `start` and `end` keys with {row,col} tuples using
--- (1,0) indexing.
+--- Can also be a list of tables that contain `start` and `end` keys as described above,
+--- in which case `textDocument/rangesFormatting` support is required.
--- (Default: current selection in visual mode, `nil` in other modes,
--- formatting the full buffer)
---- @field range? {start:integer[],end:integer[]}
+--- @field range? {start:[integer,integer],end:[integer, integer]}|{start:[integer,integer],end:[integer,integer]}[]
--- Formats a buffer using the attached (and optionally filtered) language
--- server clients.
@@ -218,10 +220,20 @@ function M.format(opts)
local bufnr = opts.bufnr or api.nvim_get_current_buf()
local mode = api.nvim_get_mode().mode
local range = opts.range
+ -- Try to use visual selection if no range is given
if not range and mode == 'v' or mode == 'V' then
range = range_from_selection(bufnr, mode)
end
- local method = range and ms.textDocument_rangeFormatting or ms.textDocument_formatting
+
+ local passed_multiple_ranges = (range and #range ~= 0 and type(range[1]) == 'table')
+ local method ---@type string
+ if passed_multiple_ranges then
+ method = ms.textDocument_rangesFormatting
+ elseif range then
+ method = ms.textDocument_rangeFormatting
+ else
+ method = ms.textDocument_formatting
+ end
local clients = vim.lsp.get_clients({
id = opts.id,
@@ -241,10 +253,14 @@ function M.format(opts)
--- @param params lsp.DocumentFormattingParams
--- @return lsp.DocumentFormattingParams
local function set_range(client, params)
- if range then
- local range_params =
- util.make_given_range_params(range.start, range['end'], bufnr, client.offset_encoding)
- params.range = range_params.range
+ local to_lsp_range = function(r) ---@return lsp.DocumentRangeFormattingParams|lsp.DocumentRangesFormattingParams
+ return util.make_given_range_params(r.start, r['end'], bufnr, client.offset_encoding).range
+ end
+
+ if passed_multiple_ranges then
+ params.ranges = vim.tbl_map(to_lsp_range, range)
+ elseif range then
+ params.range = to_lsp_range(range)
end
return params
end
diff --git a/runtime/lua/vim/lsp/protocol.lua b/runtime/lua/vim/lsp/protocol.lua
index eb18043843..656921644d 100644
--- a/runtime/lua/vim/lsp/protocol.lua
+++ b/runtime/lua/vim/lsp/protocol.lua
@@ -425,6 +425,7 @@ function protocol.make_client_capabilities()
},
rangeFormatting = {
dynamicRegistration = true,
+ rangesSupport = true,
},
completion = {
dynamicRegistration = false,