diff options
author | Mathias Fußenegger <mfussenegger@users.noreply.github.com> | 2022-08-01 22:32:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-01 22:32:53 +0200 |
commit | e99de3f12f00662e8131fed9912792f6d43c4975 (patch) | |
tree | 45b75fa49f1dc134d6cf84f69a66c25a425d7fba /test | |
parent | 711ef4eac9e5126d37dd4acd1384b7df372d7315 (diff) | |
download | rneovim-e99de3f12f00662e8131fed9912792f6d43c4975.tar.gz rneovim-e99de3f12f00662e8131fed9912792f6d43c4975.tar.bz2 rneovim-e99de3f12f00662e8131fed9912792f6d43c4975.zip |
fix(lsp): send didOpen if name changes on write (#19583)
`:saveas newName` changes the name of an existing buffer.
Due to the buffer re-use it skips the lsp attach phase and immediately
sends a `didSave` notification to the server.
Servers get confused about this, because they expect a `didOpen`
notification first.
Closes https://github.com/neovim/neovim/issues/18688
Diffstat (limited to 'test')
-rw-r--r-- | test/functional/fixtures/fake-lsp-server.lua | 30 | ||||
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 40 |
2 files changed, 66 insertions, 4 deletions
diff --git a/test/functional/fixtures/fake-lsp-server.lua b/test/functional/fixtures/fake-lsp-server.lua index 0dc0c8c2db..aa47198f7a 100644 --- a/test/functional/fixtures/fake-lsp-server.lua +++ b/test/functional/fixtures/fake-lsp-server.lua @@ -67,10 +67,12 @@ local function expect_notification(method, params, ...) local message = read_message() assert_eq(method, message.method, ..., "expect_notification", "method") - assert_eq(params, message.params, - ..., "expect_notification", method, "params") - assert_eq({jsonrpc = "2.0"; method=method, params=params}, message, - ..., "expect_notification", "message") + if params then + assert_eq(params, message.params, + ..., "expect_notification", method, "params") + assert_eq({jsonrpc = "2.0"; method=method, params=params}, message, + ..., "expect_notification", "message") + end end local function expect_request(method, handler, ...) @@ -257,6 +259,26 @@ function tests.basic_check_capabilities() } end +function tests.text_document_save_did_open() + skeleton { + on_init = function() + return { + capabilities = { + textDocumentSync = { + save = true + } + } + } + end; + body = function() + notify('start') + expect_notification('textDocument/didOpen') + expect_notification('textDocument/didSave') + notify('shutdown') + end; + } +end + function tests.text_document_sync_save_bool() skeleton { on_init = function() diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index c166982052..cd7415de90 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -535,6 +535,46 @@ describe('LSP', function() } end) + it('saveas sends didOpen if filename changed', 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_save_did_open', + on_init = function(c) + client = c + end, + on_exit = function(code, signal) + eq(0, code, 'exit code') + eq(0, signal, 'exit signal') + end, + on_handler = function(err, result, ctx) + eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') + if ctx.method == 'start' then + local tmpfile_old = helpers.tmpname() + local tmpfile_new = helpers.tmpname() + os.remove(tmpfile_new) + exec_lua( + [=[ + local oldname, newname = ... + BUFFER = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_name(BUFFER, oldname) + vim.api.nvim_buf_set_lines(BUFFER, 0, -1, true, {"help me"}) + lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID) + vim.api.nvim_buf_call(BUFFER, function() vim.cmd('saveas ' .. newname) end) + ]=], + tmpfile_old, + tmpfile_new + ) + else + client.stop() + end + end, + }) + end) + it('BufWritePost sends didSave including text if server capability is set', function() local expected_handlers = { {NIL, {}, {method="shutdown", client_id=1}}; |