diff options
author | Tom Praschan <13141438+tom-anders@users.noreply.github.com> | 2024-06-24 16:54:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-24 07:54:56 -0700 |
commit | 5581a95534e44b8714e715c925c9de2d95ae1c21 (patch) | |
tree | 0aa6443467f9f103ff95b591c3296d4e1b394276 /test/functional/plugin | |
parent | b0e59909075a582fbcf12b4c8a3ec1bff12c4eea (diff) | |
download | rneovim-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 'test/functional/plugin')
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index b345a3288c..be303f21ce 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -4537,6 +4537,86 @@ describe('LSP', function() end, } end) + it('Sends textDocument/rangeFormatting request to format a range', function() + local expected_handlers = { + { NIL, {}, { method = 'shutdown', client_id = 1 } }, + { NIL, {}, { method = 'start', client_id = 1 } }, + } + local client + test_rpc_server { + test_name = 'range_formatting', + on_init = function(c) + client = c + end, + on_handler = function(_, _, ctx) + table.remove(expected_handlers) + if ctx.method == 'start' then + local notify_msg = exec_lua([[ + local bufnr = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, {'foo', 'bar'}) + vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + local notify_msg + local notify = vim.notify + vim.notify = function(msg, log_level) + notify_msg = msg + end + vim.lsp.buf.format({ bufnr = bufnr, range = { + start = {1, 1}, + ['end'] = {1, 1}, + }}) + vim.notify = notify + return notify_msg + ]]) + eq(NIL, notify_msg) + elseif ctx.method == 'shutdown' then + client.stop() + end + end, + } + end) + it('Sends textDocument/rangesFormatting request to format multiple ranges', function() + local expected_handlers = { + { NIL, {}, { method = 'shutdown', client_id = 1 } }, + { NIL, {}, { method = 'start', client_id = 1 } }, + } + local client + test_rpc_server { + test_name = 'ranges_formatting', + on_init = function(c) + client = c + end, + on_handler = function(_, _, ctx) + table.remove(expected_handlers) + if ctx.method == 'start' then + local notify_msg = exec_lua([[ + local bufnr = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, {'foo', 'bar', 'baz'}) + vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + local notify_msg + local notify = vim.notify + vim.notify = function(msg, log_level) + notify_msg = msg + end + vim.lsp.buf.format({ bufnr = bufnr, range = { + { + start = {1, 1}, + ['end'] = {1, 1}, + }, + { + start = {2, 2}, + ['end'] = {2, 2}, + } + }}) + vim.notify = notify + return notify_msg + ]]) + eq(NIL, notify_msg) + elseif ctx.method == 'shutdown' then + client.stop() + end + end, + } + end) it('Can format async', function() local expected_handlers = { { NIL, {}, { method = 'shutdown', client_id = 1 } }, |