diff options
-rw-r--r-- | runtime/lua/vim/lsp.lua | 7 | ||||
-rw-r--r-- | test/functional/fixtures/fake-lsp-server.lua | 50 | ||||
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 61 |
3 files changed, 114 insertions, 4 deletions
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index 50fd26957d..5c35d3916f 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -1208,10 +1208,11 @@ function lsp._text_document_did_save_handler(bufnr) bufnr = resolve_bufnr(bufnr) local uri = vim.uri_from_bufnr(bufnr) local text = once(buf_get_full_text) - for_each_buffer_client(bufnr, function(client, _client_id) - if vim.tbl_get(client.server_capabilities, "textDocumentSync", "save") then + for_each_buffer_client(bufnr, function(client) + local save_capability = vim.tbl_get(client.server_capabilities, "textDocumentSync", "save") + if save_capability then local included_text - if vim.tbl_get(client.server_capabilities, "textDocumentSync", "save", "includeText") then + if type(save_capability) == "table" and save_capability.includeText then included_text = text(bufnr) end client.notify('textDocument/didSave', { diff --git a/test/functional/fixtures/fake-lsp-server.lua b/test/functional/fixtures/fake-lsp-server.lua index 5403405905..79a29cd8d8 100644 --- a/test/functional/fixtures/fake-lsp-server.lua +++ b/test/functional/fixtures/fake-lsp-server.lua @@ -28,7 +28,10 @@ local function assert_eq(a, b, ...) if not vim.deep_equal(a, b) then error(message_parts(": ", ..., "assert_eq failed", - string.format("left == %q, right == %q", vim.inspect(a), vim.inspect(b)) + string.format("left == %q, right == %q", + table.concat(vim.split(vim.inspect(a), "\n"), ""), + table.concat(vim.split(vim.inspect(b), "\n"), "") + ) )) end end @@ -245,6 +248,51 @@ function tests.basic_check_capabilities() } end +function tests.text_document_sync_save_bool() + skeleton { + on_init = function() + return { + capabilities = { + textDocumentSync = { + save = true + } + } + } + end; + body = function() + notify('start') + expect_notification('textDocument/didSave', {textDocument = { uri = "file://" }}) + notify('shutdown') + end; + } +end + +function tests.text_document_sync_save_includeText() + skeleton { + on_init = function() + return { + capabilities = { + textDocumentSync = { + save = { + includeText = true + } + } + } + } + end; + body = function() + notify('start') + expect_notification('textDocument/didSave', { + textDocument = { + uri = "file://" + }, + text = "help me\n" + }) + notify('shutdown') + end; + } +end + function tests.capabilities_for_client_supports_method() skeleton { on_init = function(params) diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index be717cf724..3ee293db66 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -421,6 +421,67 @@ describe('LSP', function() } end) + it('_text_document_did_save_handler sends didSave with bool textDocumentSync.save', function() + local expected_handlers = { + {NIL, {}, {method="shutdown", client_id=1}}; + {NIL, {}, {method="start", client_id=1}}; + } + local client + test_rpc_server { + test_name = "text_document_sync_save_bool"; + on_init = function(c) + client = c + end; + on_exit = function(code, signal) + eq(0, code, "exit code", fake_lsp_logfile) + eq(0, signal, "exit signal", fake_lsp_logfile) + end; + on_handler = function(err, result, ctx) + eq(table.remove(expected_handlers), {err, result, ctx}, "expected handler") + if ctx.method == "start" then + exec_lua([=[ + BUFFER = vim.api.nvim_get_current_buf() + lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID) + lsp._text_document_did_save_handler(BUFFER) + ]=]) + else + client.stop() + end + end; + } + end) + + it('_text_document_did_save_handler sends didSave including text if server capability is set', function() + local expected_handlers = { + {NIL, {}, {method="shutdown", client_id=1}}; + {NIL, {}, {method="start", client_id=1}}; + } + local client + test_rpc_server { + test_name = "text_document_sync_save_includeText"; + on_init = function(c) + client = c + end; + on_exit = function(code, signal) + eq(0, code, "exit code", fake_lsp_logfile) + eq(0, signal, "exit signal", fake_lsp_logfile) + end; + on_handler = function(err, result, ctx) + eq(table.remove(expected_handlers), {err, result, ctx}, "expected handler") + if ctx.method == "start" then + exec_lua([=[ + BUFFER = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_lines(BUFFER, 0, -1, true, {"help me"}) + lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID) + lsp._text_document_did_save_handler(BUFFER) + ]=]) + else + client.stop() + end + end; + } + end) + it('client.supports_methods() should validate capabilities', function() local expected_handlers = { {NIL, {}, {method="shutdown", client_id=1}}; |