From 43ef7df22d58a72e8b155265620f6c030900812e Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Thu, 13 Jan 2022 18:28:13 +0900 Subject: fix(lsp): fix applying multiple out-of-range TextEdits (#17037) --- test/functional/plugin/lsp_spec.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 7ad1a52fe3..6a4730c5ac 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -1375,6 +1375,20 @@ describe('LSP', function() 'foobar'; }, buf_lines(1)) end) + it('applies multiple text edits at the end of the document', function() + local edits = { + make_edit(4, 0, 5, 0, ""); + make_edit(5, 0, 5, 0, "foobar"); + } + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + eq({ + 'First line of text'; + 'Second line of text'; + 'Third line of text'; + 'Fourth line of text'; + 'foobar'; + }, buf_lines(1)) + end) describe('cursor position', function() it('don\'t fix the cursor if the range contains the cursor', function() -- cgit From e7cd81156755c2f588752d469bceee9e48377b4e Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Thu, 13 Jan 2022 10:47:36 +0100 Subject: fix(lsp): handle negative activeSignature in signatureHelp (#17064) omnisharp-roslyn can send negative values: { activeParameter = 0, activeSignature = -1, signatures = { { documentation = "", label = "TestEntity.TestEntity()", parameters = {} } } } In 3.16 of the specification `activeSignature` is defined as `uinteger` and therefore negative values shouldn't be allowed, but within 3.15 it was defined as `number` which makes me think we can be a bit lenient in this case and handle them. The expected behavior is quite clear: The active signature. If omitted or the value lies outside the range of `signatures` the value defaults to zero or is ignored if the `SignatureHelp` has no signatures. Fixes an error: util.lua:1685: attempt to get length of local 'lines' (a nil value) util.lua:1685: in function 'trim_empty_lines' handlers.lua:334: in function 'textDocument/signatureHelp' --- test/functional/plugin/lsp_spec.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 6a4730c5ac..91d80990c9 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2333,6 +2333,27 @@ describe('LSP', function() end) end) + describe('lsp.util.convert_signature_help_to_markdown_lines', function() + it('can handle negative activeSignature', function() + local result = exec_lua[[ + local signature_help = { + activeParameter = 0, + activeSignature = -1, + signatures = { + { + documentation = "", + label = "TestEntity.TestEntity()", + parameters = {} + }, + } + } + return vim.lsp.util.convert_signature_help_to_markdown_lines(signature_help, 'cs', {','}) + ]] + local expected = {'```cs', 'TestEntity.TestEntity()', '```', ''} + eq(expected, result) + end) + end) + describe('lsp.util.get_effective_tabstop', function() local function test_tabstop(tabsize, softtabstop) exec_lua(string.format([[ -- cgit From bc722c8a74766e14aff3a8e2fc46db72ed864053 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Thu, 13 Jan 2022 02:34:04 -0800 Subject: fix(lsp): strictly enforce passing offset encoding (#17049) This removes the "fallback" to utf-16 in many of our helper functions. We should always explicitly pass these around when possible except in two locations: * generating params with help utilities called by buf.lua functions * the buf.lua functions themselves Anything that is called by the handler should be passed the offset encoding. --- test/functional/plugin/lsp_spec.lua | 93 ++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 37 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 91d80990c9..eab520948f 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -1291,7 +1291,7 @@ describe('LSP', function() make_edit(2, 0, 2, 2, {"3"}); make_edit(3, 2, 3, 4, {""}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ '123First line of text'; '2econd line of text'; @@ -1311,7 +1311,7 @@ describe('LSP', function() make_edit(3, #'', 3, #"Fourth", {"another line of text", "before this"}); make_edit(3, #'Fourth', 3, #"Fourth line of text", {"!"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ ''; '123'; @@ -1335,7 +1335,7 @@ describe('LSP', function() make_edit(3, #"Fourth", 3, #'', {"another line of text", "before this"}); make_edit(3, #"Fourth line of text", 3, #'Fourth', {"!"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ ''; '123'; @@ -1352,7 +1352,7 @@ describe('LSP', function() local edits = { make_edit(4, 3, 4, 4, {"ä"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second line of text'; @@ -1365,7 +1365,7 @@ describe('LSP', function() local edits = { make_edit(5, 0, 5, 0, "foobar"); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second line of text'; @@ -1380,7 +1380,7 @@ describe('LSP', function() make_edit(4, 0, 5, 0, ""); make_edit(5, 0, 5, 0, "foobar"); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second line of text'; @@ -1396,7 +1396,7 @@ describe('LSP', function() local edits = { make_edit(1, 0, 1, 19, 'Second line of text') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second line of text'; @@ -1413,7 +1413,7 @@ describe('LSP', function() make_edit(1, 0, 1, 6, ''), make_edit(1, 6, 1, 19, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; ''; @@ -1430,7 +1430,7 @@ describe('LSP', function() make_edit(1, 0, 1, 6, ''), make_edit(0, 18, 5, 0, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; }, buf_lines(1)) @@ -1442,7 +1442,7 @@ describe('LSP', function() local edits = { make_edit(1, 0, 2, 0, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Third line of text'; @@ -1457,7 +1457,7 @@ describe('LSP', function() local edits = { make_edit(1, 7, 1, 11, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second of text'; @@ -1473,7 +1473,7 @@ describe('LSP', function() local edits = { make_edit(0, 11, 1, 12, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Third line of text'; @@ -1489,21 +1489,21 @@ describe('LSP', function() local edits = { make_edit(0, 0, 5, 0, {"All replaced"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({'All replaced'}, buf_lines(1)) end) it('applies edits when the end line is 2 larger than vim\'s', function() local edits = { make_edit(0, 0, 6, 0, {"All replaced"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({'All replaced'}, buf_lines(1)) end) it('applies edits with a column offset', function() local edits = { make_edit(0, 0, 5, 2, {"All replaced"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({'All replaced'}, buf_lines(1)) end) end) @@ -1531,7 +1531,7 @@ describe('LSP', function() ]] end) it('correctly goes ahead with the edit if all is normal', function() - exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit(5)) + exec_lua("vim.lsp.util.apply_text_document_edit(..., nil, 'utf-16')", text_document_edit(5)) eq({ 'First ↥ 🤦 🦄 line of text'; '2nd line of 语text'; @@ -1543,7 +1543,7 @@ describe('LSP', function() local bufnr = select(1, ...) local text_edit = select(2, ...) vim.lsp.util.buf_versions[bufnr] = 10 - vim.lsp.util.apply_text_document_edit(text_edit) + vim.lsp.util.apply_text_document_edit(text_edit, nil, 'utf-16') ]], target_bufnr, text_document_edit(0)) eq({ 'First ↥ 🤦 🦄 line of text'; @@ -1556,7 +1556,7 @@ describe('LSP', function() local args = {...} local versionedBuf = args[2] vim.lsp.util.buf_versions[versionedBuf.bufnr] = versionedBuf.currentVersion - vim.lsp.util.apply_text_document_edit(args[1]) + vim.lsp.util.apply_text_document_edit(args[1], nil, 'utf-16') ]], edit, versionedBuf) end @@ -1582,17 +1582,36 @@ describe('LSP', function() describe('workspace_apply_edit', function() it('workspace/applyEdit returns ApplyWorkspaceEditResponse', function() - local expected = { - applied = true; - failureReason = nil; + local expected_handlers = { + {NIL, {}, {method="test", client_id=1}}; + } + test_rpc_server { + test_name = "basic_init"; + on_init = function(client, _) + client.stop() + end; + -- If the program timed out, then code will be nil. + on_exit = function(code, signal) + eq(0, code, "exit code", fake_lsp_logfile) + eq(0, signal, "exit signal", fake_lsp_logfile) + end; + -- Note that NIL must be used here. + -- on_handler(err, method, result, client_id) + on_handler = function(...) + local expected = { + applied = true; + failureReason = nil; + } + eq(expected, exec_lua [[ + local apply_edit = { + label = nil; + edit = {}; + } + return vim.lsp.handlers['workspace/applyEdit'](nil, apply_edit, {client_id = TEST_RPC_CLIENT_ID}) + ]]) + eq(table.remove(expected_handlers), {...}) + end; } - eq(expected, exec_lua [[ - local apply_edit = { - label = nil; - edit = {}; - } - return vim.lsp.handlers['workspace/applyEdit'](nil, apply_edit) - ]]) end) end) @@ -1666,7 +1685,7 @@ describe('LSP', function() local workspace_edits = args[1] local target_bufnr = args[2] - vim.lsp.util.apply_workspace_edit(workspace_edits) + vim.lsp.util.apply_workspace_edit(workspace_edits, 'utf-16') return vim.api.nvim_buf_get_lines(target_bufnr, 0, -1, false) ]], make_workspace_edit(edits), target_bufnr)) @@ -1688,7 +1707,7 @@ describe('LSP', function() local workspace_edits = args[1] local target_bufnr = args[2] - vim.lsp.util.apply_workspace_edit(workspace_edits) + vim.lsp.util.apply_workspace_edit(workspace_edits, 'utf-16') return vim.api.nvim_buf_get_lines(target_bufnr, 0, -1, false) ]], make_workspace_edit(edits), target_bufnr)) @@ -1705,7 +1724,7 @@ describe('LSP', function() }, } } - exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit) + exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16') eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) end) it('createFile does not touch file if it exists and ignoreIfExists is set', function() @@ -1723,7 +1742,7 @@ describe('LSP', function() }, } } - exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit) + exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16') eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) eq('Dummy content', read_file(tmpfile)) end) @@ -1743,7 +1762,7 @@ describe('LSP', function() }, } } - exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit) + exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16') eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) eq('', read_file(tmpfile)) end) @@ -1764,7 +1783,7 @@ describe('LSP', function() } } } - eq(true, pcall(exec_lua, 'vim.lsp.util.apply_workspace_edit(...)', edit)) + eq(true, pcall(exec_lua, 'vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16')) eq(false, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) eq(false, exec_lua('return vim.api.nvim_buf_is_loaded(vim.fn.bufadd(...))', tmpfile)) end) @@ -1925,7 +1944,7 @@ describe('LSP', function() } }, } - return vim.lsp.util.locations_to_items(locations) + return vim.lsp.util.locations_to_items(locations, 'utf-16') ]] eq(expected, actual) end) @@ -1955,7 +1974,7 @@ describe('LSP', function() } }, } - return vim.lsp.util.locations_to_items(locations) + return vim.lsp.util.locations_to_items(locations, 'utf-16') ]] eq(expected, actual) end) @@ -2259,7 +2278,7 @@ describe('LSP', function() end local jump = function(msg) - eq(true, exec_lua('return vim.lsp.util.jump_to_location(...)', msg)) + eq(true, exec_lua('return vim.lsp.util.jump_to_location(...)', msg, "utf-16")) eq(target_bufnr, exec_lua[[return vim.fn.bufnr('%')]]) return { line = exec_lua[[return vim.fn.line('.')]], -- cgit From a87ecf5d086c9a93be4c5f331a684301b2c1bc12 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 3 Feb 2022 17:19:51 +0800 Subject: fix(health): do not run external processes in a shell --- test/functional/plugin/health_spec.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index 37de5d0ce6..f7c2dbdb43 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -230,3 +230,14 @@ describe('health.vim', function() end) end) end) + +describe(':checkhealth provider', function() + it("works correctly with a wrongly configured 'shell'", function() + clear() + command([[set shell=echo\ WRONG!!!]]) + command('let g:loaded_perl_provider = 0') + command('let g:loaded_python3_provider = 0') + command('checkhealth provider') + eq(nil, string.match(curbuf_contents(), 'WRONG!!!')) + end) +end) -- cgit From 300b009f47bc617faa1c445966e2085c455e0c45 Mon Sep 17 00:00:00 2001 From: Edmund Cape Date: Wed, 19 Jan 2022 13:30:20 -0500 Subject: fix(healthcheck): handle empty reports --- test/functional/plugin/health_spec.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index f7c2dbdb43..a9bd76ce24 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -153,6 +153,10 @@ describe('health.vim', function() ## report 2 - OK: nothing to see here + test_plug.submodule_empty: require("test_plug.submodule_empty.health").check() + ======================================================================== + - ERROR: The healthcheck report for "test_plug.submodule_empty" plugin is empty. + test_plug.submodule_failed: require("test_plug.submodule_failed.health").check() ======================================================================== - ERROR: Failed to run healthcheck for "test_plug.submodule_failed" plugin. Exception: @@ -172,6 +176,16 @@ describe('health.vim', function() ]]) end) + it("... including empty reports", function() + command("checkhealth test_plug.submodule_empty") + helpers.expect([[ + + test_plug.submodule_empty: require("test_plug.submodule_empty.health").check() + ======================================================================== + - ERROR: The healthcheck report for "test_plug.submodule_empty" plugin is empty. + ]]) + end) + it("gracefully handles broken lua healthcheck", function() command("checkhealth test_plug.submodule_failed") local buf_lines = helpers.curbuf('get_lines', 0, -1, true) -- cgit From 991e472881bf29805982b402c1a010cde051ded3 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Fri, 28 May 2021 15:45:34 -0400 Subject: feat(lua): add api and lua autocmds --- test/functional/plugin/shada_spec.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/shada_spec.lua b/test/functional/plugin/shada_spec.lua index a4d78682ad..6f22f865e6 100644 --- a/test/functional/plugin/shada_spec.lua +++ b/test/functional/plugin/shada_spec.lua @@ -1,7 +1,7 @@ local helpers = require('test.functional.helpers')(after_each) local clear = helpers.clear -local eq, nvim_eval, nvim_command, nvim, exc_exec, funcs, nvim_feed, curbuf = - helpers.eq, helpers.eval, helpers.command, helpers.nvim, helpers.exc_exec, +local eq, meths, nvim_eval, nvim_command, nvim, exc_exec, funcs, nvim_feed, curbuf = + helpers.eq, helpers.meths, helpers.eval, helpers.command, helpers.nvim, helpers.exc_exec, helpers.funcs, helpers.feed, helpers.curbuf local neq = helpers.neq local read_file = helpers.read_file @@ -2162,6 +2162,10 @@ describe('plugin/shada.vim', function() wshada('\004\000\009\147\000\196\002ab\196\001a') wshada_tmp('\004\000\009\147\000\196\002ab\196\001b') + + local bufread_commands = meths.get_autocmds({ group = "ShaDaCommands", event = "BufReadCmd" }) + eq(2, #bufread_commands--[[, vim.inspect(bufread_commands) ]]) + -- Need to set nohidden so that the buffer containing 'fname' is not unloaded -- after loading 'fname_tmp', otherwise the '++opt not supported' test below -- won't work since the BufReadCmd autocmd won't be triggered. -- cgit From 5d6006f9bfc2f1f064adbcfa974da6976e867450 Mon Sep 17 00:00:00 2001 From: David Shen <17462095+mxteries@users.noreply.github.com> Date: Wed, 2 Mar 2022 20:42:27 -0500 Subject: feat(diagnostic): add "code" to the diagnostic structure (#17510) --- test/functional/plugin/lsp/diagnostic_spec.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp/diagnostic_spec.lua b/test/functional/plugin/lsp/diagnostic_spec.lua index 1269a2350c..83d794b620 100644 --- a/test/functional/plugin/lsp/diagnostic_spec.lua +++ b/test/functional/plugin/lsp/diagnostic_spec.lua @@ -110,6 +110,7 @@ describe('vim.lsp.diagnostic', function() } ]] eq({code = 42, tags = {"foo", "bar"}, data = "Hello world"}, result[1].user_data.lsp) + eq(42, result[1].code) eq(42, result[2].code) eq({"foo", "bar"}, result[2].tags) eq("Hello world", result[2].data) -- cgit From a5e475fcc269b32a8a487f787af20802cbfabe28 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sat, 5 Mar 2022 09:17:56 -0800 Subject: fix(lsp): start incremental sync range at previous newline character (#17610) This change forces the start of an incremental sync range to begin always on an existing line. --- .../plugin/lsp/incremental_sync_spec.lua | 33 +++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp/incremental_sync_spec.lua b/test/functional/plugin/lsp/incremental_sync_spec.lua index 4e3eddb960..da54f3c649 100644 --- a/test/functional/plugin/lsp/incremental_sync_spec.lua +++ b/test/functional/plugin/lsp/incremental_sync_spec.lua @@ -207,16 +207,16 @@ describe('incremental synchronization', function() { range = { ['start'] = { - character = 0, - line = 1 + character = 11, + line = 0, }, ['end'] = { character = 0, line = 1 } }, - rangeLength = 0, - text = 'hello world\n' + rangeLength = 1, + text = '\nhello world\n' } } test_edit({"hello world"}, {"yyp"}, expected_text_changes, 'utf-16', '\n') @@ -226,19 +226,38 @@ describe('incremental synchronization', function() { range = { ['start'] = { + character = 11, + line = 0 + }, + ['end'] = { character = 0, line = 1 + } + }, + rangeLength = 1, + text = '\n\n' + } + } + test_edit({"hello world"}, {"o"}, expected_text_changes, 'utf-16', '\n') + end) + it('adding a line to an empty buffer', function() + local expected_text_changes = { + { + range = { + ['start'] = { + character = 0, + line = 0 }, ['end'] = { character = 0, line = 1 } }, - rangeLength = 0, - text = '\n' + rangeLength = 1, + text = '\n\n' } } - test_edit({"hello world"}, {"o"}, expected_text_changes, 'utf-16', '\n') + test_edit({""}, {"o"}, expected_text_changes, 'utf-16', '\n') end) end) describe('multi line edit', function() -- cgit From 3800615da9eaf9e8b26d9040c882c74084d688e4 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sun, 6 Mar 2022 07:52:11 -0800 Subject: fix(lsp): handle insertion of previous line (#17618) --- test/functional/plugin/lsp/incremental_sync_spec.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp/incremental_sync_spec.lua b/test/functional/plugin/lsp/incremental_sync_spec.lua index da54f3c649..4985da9cd7 100644 --- a/test/functional/plugin/lsp/incremental_sync_spec.lua +++ b/test/functional/plugin/lsp/incremental_sync_spec.lua @@ -259,6 +259,25 @@ describe('incremental synchronization', function() } test_edit({""}, {"o"}, expected_text_changes, 'utf-16', '\n') end) + it('insert a line above the current line', function() + local expected_text_changes = { + { + range = { + ['start'] = { + character = 0, + line = 0 + }, + ['end'] = { + character = 0, + line = 0 + } + }, + rangeLength = 0, + text = '\n' + } + } + test_edit({""}, {"O"}, expected_text_changes, 'utf-16', '\n') + end) end) describe('multi line edit', function() it('deletion and insertion', function() -- cgit From af427dedf663b1987fa54c5f885409ad51824a20 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sun, 20 Mar 2022 13:41:46 -0400 Subject: fix(lsp): set tabSize from 'shiftwidth', not 'softtabstop' (#17787) The use of 'softtabstop' to set tabSize was introduced in 5d5b068, replacing 'tabstop'. If we look past the name tabSize and at the actual purpose of the field, it's the indentation width used when formatting. This corresponds to the Vim option 'shiftwidth', not 'softtabstop'. The latter has the comparatively mundane purpose of controlling what happens when you hit the tab key (and even this is incomplete, as it fails to account for 'smarttab'). --- test/functional/plugin/lsp_spec.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index eab520948f..6cda9af0f4 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2374,18 +2374,16 @@ describe('LSP', function() end) describe('lsp.util.get_effective_tabstop', function() - local function test_tabstop(tabsize, softtabstop) + local function test_tabstop(tabsize, shiftwidth) exec_lua(string.format([[ - vim.api.nvim_buf_set_option(0, 'softtabstop', %d) + vim.api.nvim_buf_set_option(0, 'shiftwidth', %d) vim.api.nvim_buf_set_option(0, 'tabstop', 2) - vim.api.nvim_buf_set_option(0, 'shiftwidth', 3) - ]], softtabstop)) + ]], shiftwidth)) eq(tabsize, exec_lua('return vim.lsp.util.get_effective_tabstop()')) end - it('with softtabstop = 1', function() test_tabstop(1, 1) end) - it('with softtabstop = 0', function() test_tabstop(2, 0) end) - it('with softtabstop = -1', function() test_tabstop(3, -1) end) + it('with shiftwidth = 1', function() test_tabstop(1, 1) end) + it('with shiftwidth = 0', function() test_tabstop(2, 0) end) end) describe('vim.lsp.buf.outgoing_calls', function() -- cgit From 61205c1defb64ac5466496b5451e4a7f3171e21e Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Fri, 25 Mar 2022 19:57:59 +0100 Subject: chore: fix typos (#17755) Co-authored-by: Jordan Haine --- test/functional/plugin/matchparen_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/matchparen_spec.lua b/test/functional/plugin/matchparen_spec.lua index 13e1283e2c..2670734c1a 100644 --- a/test/functional/plugin/matchparen_spec.lua +++ b/test/functional/plugin/matchparen_spec.lua @@ -27,7 +27,7 @@ describe('matchparen', function() feed('{') feed('}') - -- critical part: up + cr should result in an empty line inbetween the + -- critical part: up + cr should result in an empty line in between the -- brackets... if the bug is there, the empty line will be before the '{' feed('') feed('') -- cgit From 6160973f36b532b6f9ff2cd7c20958fd791f2e2a Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 15 Apr 2022 11:12:41 +0200 Subject: fix(lsp): fix lookup of boolean values in workspace/configuration (#18026) --- test/functional/plugin/lsp_spec.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 6cda9af0f4..436b431e38 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -347,6 +347,8 @@ describe('LSP', function() {NIL, { items = { { section = "testSetting1" }; { section = "testSetting2" }; + { section = "test.Setting3" }; + { section = "test.Setting4" }; }}, { method="workspace/configuration", client_id=1}}; {NIL, {}, {method="start", client_id=1}}; } @@ -368,6 +370,7 @@ describe('LSP', function() client.config.settings = { testSetting1 = true; testSetting2 = false; + test = {Setting3 = 'nested' }; }]=]) end if ctx.method == 'workspace/configuration' then -- cgit From 55135cea619cd1b8b8d7563c14436c092fa749ab Mon Sep 17 00:00:00 2001 From: ii14 <59243201+ii14@users.noreply.github.com> Date: Tue, 26 Apr 2022 19:00:28 +0200 Subject: fix(lsp): fix unnecessary buffers being added on empty diagnostics (#18275) Some language servers send empty `textDocument/publishDiagnostics` messages after indexing the project with URIs corresponding to unopened buffers. This commit guards against opening buffers corresponding to empty diagnostics. --- test/functional/plugin/lsp/diagnostic_spec.lua | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp/diagnostic_spec.lua b/test/functional/plugin/lsp/diagnostic_spec.lua index 83d794b620..19b01edb29 100644 --- a/test/functional/plugin/lsp/diagnostic_spec.lua +++ b/test/functional/plugin/lsp/diagnostic_spec.lua @@ -3,6 +3,7 @@ local helpers = require('test.functional.helpers')(after_each) local clear = helpers.clear local exec_lua = helpers.exec_lua local eq = helpers.eq +local neq = require('test.helpers').neq describe('vim.lsp.diagnostic', function() local fake_uri @@ -227,5 +228,43 @@ describe('vim.lsp.diagnostic', function() eq(exec_lua([[return vim.str_byteindex(..., 7, true)]], line), result[1].col) eq(exec_lua([[return vim.str_byteindex(..., 8, true)]], line), result[1].end_col) end) + + it('does not create buffer on empty diagnostics', function() + local bufnr + + -- No buffer is created without diagnostics + bufnr = exec_lua [[ + vim.lsp.diagnostic.on_publish_diagnostics(nil, { + uri = "file:///fake/uri2", + diagnostics = {}, + }, {client_id=client_id}) + return vim.fn.bufnr(vim.uri_to_fname("file:///fake/uri2")) + ]] + eq(bufnr, -1) + + -- Create buffer on diagnostics + bufnr = exec_lua [[ + vim.lsp.diagnostic.on_publish_diagnostics(nil, { + uri = "file:///fake/uri2", + diagnostics = { + make_error('Diagnostic', 0, 0, 0, 0), + }, + }, {client_id=client_id}) + return vim.fn.bufnr(vim.uri_to_fname("file:///fake/uri2")) + ]] + neq(bufnr, -1) + eq(exec_lua([[return #vim.diagnostic.get(...)]], bufnr), 1) + + -- Clear diagnostics after buffer was created + bufnr = exec_lua [[ + vim.lsp.diagnostic.on_publish_diagnostics(nil, { + uri = "file:///fake/uri2", + diagnostics = {}, + }, {client_id=client_id}) + return vim.fn.bufnr(vim.uri_to_fname("file:///fake/uri2")) + ]] + neq(bufnr, -1) + eq(exec_lua([[return #vim.diagnostic.get(...)]], bufnr), 0) + end) end) end) -- cgit From df09e03cf74337675751c3240069a26aec75fa3b Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sat, 30 Apr 2022 10:14:31 +0200 Subject: feat(lsp): options to filter and auto-apply code actions (#18221) Implement two new options to vim.lsp.buf.code_action(): - filter (function): predicate taking an Action as input, and returning a boolean. - apply (boolean): when set to true, and there is just one remaining action (after filtering), the action is applied without user query. These options can, for example, be used to filter out, and automatically apply, the action indicated by the server to be preferred: vim.lsp.buf.code_action({ filter = function(action) return action.isPreferred end, apply = true, }) Fix #17514. --- test/functional/plugin/lsp_spec.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 436b431e38..03ed167bac 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2665,6 +2665,42 @@ describe('LSP', function() end } end) + it('Filters and automatically applies action if requested', function() + local client + local expected_handlers = { + {NIL, {}, {method="shutdown", client_id=1}}; + {NIL, {}, {method="start", client_id=1}}; + } + test_rpc_server { + test_name = 'code_action_filter', + on_init = function(client_) + client = client_ + end, + on_setup = function() + 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}) + if ctx.method == 'start' then + exec_lua([[ + vim.lsp.commands['preferred_command'] = function(cmd) + vim.lsp.commands['executed_preferred'] = function() + end + end + local bufnr = vim.api.nvim_get_current_buf() + vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.lsp.buf.code_action({ filter = function(a) return a.isPreferred end, apply = true, }) + ]]) + elseif ctx.method == 'shutdown' then + eq('function', exec_lua[[return type(vim.lsp.commands['executed_preferred'])]]) + client.stop() + end + end + } + end) end) describe('vim.lsp.commands', function() it('Accepts only string keys', function() -- cgit From c618b314c6a266806edf692122b16ba9ff7a8e10 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sat, 30 Apr 2022 02:22:30 -0700 Subject: chore(lsp): remove capabilities sanitization (#17814) * feat(lsp)!: remove capabilities sanitization Users must now access client.server_capabilities which matches the same structure as the protocol. https://microsoft.github.io/language-server-protocol/specification client.resolved_capabilities is no longer used to gate capabilities, and will be removed in a future release. BREAKING CHANGE Co-authored-by: Mathias Fussenegger --- test/functional/plugin/lsp_spec.lua | 72 +++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 34 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 03ed167bac..6cde96ceaf 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -289,7 +289,7 @@ describe('LSP', function() test_rpc_server { test_name = "basic_init"; on_init = function(client) - eq(0, client.resolved_capabilities().text_document_did_change) + eq(0, client.server_capabilities().textDocumentSync.change) client.request('shutdown') client.notify('exit') client.stop() @@ -407,10 +407,9 @@ describe('LSP', function() on_init = function(client) client.stop() local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(full_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_save) - eq(false, client.resolved_capabilities().code_lens) - eq(false, client.resolved_capabilities().code_lens_resolve) + eq(full_kind, client.server_capabilities().textDocumentSync.change) + eq({includeText = false}, client.server_capabilities().textDocumentSync.save) + eq(false, client.server_capabilities().codeLensProvider) end; on_exit = function(code, signal) eq(0, code, "exit code", fake_lsp_logfile) @@ -430,14 +429,19 @@ describe('LSP', function() test_name = "capabilities_for_client_supports_method"; on_init = function(client) client.stop() - local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(full_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().completion) - eq(true, client.resolved_capabilities().hover) - eq(false, client.resolved_capabilities().goto_definition) - eq(false, client.resolved_capabilities().rename) - eq(true, client.resolved_capabilities().code_lens) - eq(true, client.resolved_capabilities().code_lens_resolve) + local expected_sync_capabilities = { + change = 1, + openClose = true, + save = { includeText = false }, + willSave = false, + willSaveWaitUntil = false, + } + eq(expected_sync_capabilities, client.server_capabilities().textDocumentSync) + eq(true, client.server_capabilities().completionProvider) + eq(true, client.server_capabilities().hoverProvider) + eq(false, client.server_capabilities().definitionProvider) + eq(false, client.server_capabilities().renameProvider) + eq(true, client.server_capabilities().codeLensProvider.resolveProvider) -- known methods for resolved capabilities eq(true, client.supports_method("textDocument/hover")) @@ -720,8 +724,8 @@ describe('LSP', function() on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(full_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(full_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) client.notify('finish') end; on_exit = function(code, signal) @@ -761,8 +765,8 @@ describe('LSP', function() on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(full_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(full_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(not lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID), "Shouldn't attach twice") ]] @@ -804,8 +808,8 @@ describe('LSP', function() on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(full_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(full_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) ]] @@ -847,8 +851,8 @@ describe('LSP', function() on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(full_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(full_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) ]] @@ -896,8 +900,8 @@ describe('LSP', function() on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(full_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(full_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) ]] @@ -947,8 +951,8 @@ describe('LSP', function() on_init = function(_client) client = _client local sync_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Incremental") - eq(sync_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(sync_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) ]] @@ -998,8 +1002,8 @@ describe('LSP', function() on_init = function(_client) client = _client local sync_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Incremental") - eq(sync_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(sync_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) ]] @@ -1047,8 +1051,8 @@ describe('LSP', function() on_init = function(_client) client = _client local sync_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Incremental") - eq(sync_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(sync_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) ]] @@ -1091,8 +1095,8 @@ describe('LSP', function() on_init = function(_client) client = _client local sync_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(sync_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(sync_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) ]] @@ -1142,8 +1146,8 @@ describe('LSP', function() on_init = function(_client) client = _client local sync_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") - eq(sync_kind, client.resolved_capabilities().text_document_did_change) - eq(true, client.resolved_capabilities().text_document_open_close) + eq(sync_kind, client.server_capabilities().textDocumentSync.change) + eq(true, client.server_capabilities().textDocumentSync.openClose) exec_lua [[ assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) ]] @@ -2583,7 +2587,7 @@ describe('LSP', function() test_name = test.name; on_init = function(_client) client = _client - eq(true, client.resolved_capabilities().rename) + eq(true, client.server_capabilities().renameProvider.prepareProvider) end; on_setup = function() exec_lua([=[ -- cgit From 5b04e46d23b65413d934d812d61d8720b815eb1c Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sat, 30 Apr 2022 06:36:40 -0700 Subject: feat(lsp): add vim.lsp.buf.format (#18193) --- test/functional/plugin/lsp_spec.lua | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 6cde96ceaf..6e28946cc4 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2773,4 +2773,65 @@ describe('LSP', function() } end) end) + + describe("vim.lsp.buf.format", function() + it("Aborts with notify if no client matches filter", function() + local client + test_rpc_server { + test_name = "basic_init", + on_init = function(c) + client = c + end, + on_handler = function() + local notify_msg = exec_lua([[ + local bufnr = vim.api.nvim_get_current_buf() + 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({ name = 'does-not-exist' }) + vim.notify = notify + return notify_msg + ]]) + eq("[LSP] Format request failed, no matching language servers.", notify_msg) + client.stop() + end, + } + end) + it("Sends textDocument/formatting request to format buffer", function() + local expected_handlers = { + {NIL, {}, {method="shutdown", client_id=1}}; + {NIL, {}, {method="start", client_id=1}}; + } + local client + test_rpc_server { + test_name = "basic_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.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 }) + vim.notify = notify + return notify_msg + ]]) + eq(NIL, notify_msg) + elseif ctx.method == "shutdown" then + client.stop() + end + end, + } + end) + end) end) -- cgit From 88411613e23bd829088f48983f0253f1b7e5c3fd Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Sat, 30 Apr 2022 17:23:50 +0200 Subject: feat(lsp): add async option to vim.lsp.buf.format (#18322) Deprecates the existing `vim.lsp.buf.formatting` function. With this, `vim.lsp.buf.format` will replace all three: - vim.lsp.buf.formatting - vim.lsp.buf.formatting_sync - vim.lsp.buf.formatting_seq_sync --- test/functional/plugin/lsp_spec.lua | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 6e28946cc4..be717cf724 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2833,5 +2833,49 @@ describe('LSP', function() end, } end) + it('Can format async', function() + local expected_handlers = { + {NIL, {}, {method="shutdown", client_id=1}}; + {NIL, {}, {method="start", client_id=1}}; + } + local client + test_rpc_server { + test_name = "basic_formatting", + on_init = function(c) + client = c + end, + on_handler = function(_, _, ctx) + table.remove(expected_handlers) + if ctx.method == "start" then + local result = exec_lua([[ + local bufnr = vim.api.nvim_get_current_buf() + 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 + + local handler = vim.lsp.handlers['textDocument/formatting'] + local handler_called = false + vim.lsp.handlers['textDocument/formatting'] = function(...) + handler_called = true + end + + vim.lsp.buf.format({ bufnr = bufnr, async = true }) + vim.wait(1000, function() return handler_called end) + + vim.notify = notify + vim.lsp.handlers['textDocument/formatting'] = handler + return {notify = notify_msg, handler_called = handler_called} + ]]) + eq({handler_called=true}, result) + elseif ctx.method == "shutdown" then + client.stop() + end + end, + } + end) end) end) -- cgit From 0344736aa698dc205f8f9f80609b7033308d39ca Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Sat, 30 Apr 2022 22:13:26 +0200 Subject: fix(lsp): handle textDocumentSync.save bool capability (#18332) Follow up to https://github.com/neovim/neovim/pull/17814 --- test/functional/plugin/lsp_spec.lua | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'test/functional/plugin') 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}}; -- cgit From 94eb72cc44fee4cae7a41cb1ff5fb21f81976658 Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 5 May 2022 18:50:12 +0200 Subject: fix(lsp): make sure to always reset active codelens refreshes (#18331) This fixes issues where subsequent calls to vim.lsp.codelens.refresh() would have no effect due to the buffer not getting cleared from the active_refresh table. Examples of how such scenarios would occur are: - A textDocument/codeLens result yielded an error. - The 'textDocument/codeLens' handler was overriden in such a way that it no longer called vim.lsp.codelens.on_codelens(). --- test/functional/plugin/lsp_spec.lua | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 3ee293db66..530d414334 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2833,6 +2833,85 @@ describe('LSP', function() end } end) + + it('releases buffer refresh lock', function() + local client + local expected_handlers = { + {NIL, {}, {method="shutdown", client_id=1}}; + {NIL, {}, {method="start", client_id=1}}; + } + test_rpc_server { + test_name = 'codelens_refresh_lock', + on_init = function(client_) + client = client_ + end, + on_setup = function() + exec_lua([=[ + local bufnr = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {'One line'}) + vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + + CALLED = false + RESPONSE = nil + local on_codelens = vim.lsp.codelens.on_codelens + vim.lsp.codelens.on_codelens = function (err, result, ...) + CALLED = true + RESPONSE = { err = err, result = result } + return on_codelens(err, result, ...) + end + ]=]) + 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}) + if ctx.method == 'start' then + -- 1. first codelens request errors + local response = exec_lua([=[ + CALLED = false + vim.lsp.codelens.refresh() + vim.wait(100, function () return CALLED end) + return RESPONSE + ]=]) + eq( { err = { code = -32002, message = "ServerNotInitialized" } }, response) + + -- 2. second codelens request runs + response = exec_lua([=[ + CALLED = false + local cmd_called = nil + vim.lsp.commands["Dummy"] = function (command) + cmd_called = command + end + vim.lsp.codelens.refresh() + vim.wait(100, function () return CALLED end) + vim.lsp.codelens.run() + vim.wait(100, function () return cmd_called end) + return cmd_called + ]=]) + eq( { command = "Dummy", title = "Lens1" }, response) + + -- 3. third codelens request runs + response = exec_lua([=[ + CALLED = false + local cmd_called = nil + vim.lsp.commands["Dummy"] = function (command) + cmd_called = command + end + vim.lsp.codelens.refresh() + vim.wait(100, function () return CALLED end) + vim.lsp.codelens.run() + vim.wait(100, function () return cmd_called end) + return cmd_called + ]=]) + eq( { command = "Dummy", title = "Lens2" }, response) + elseif ctx.method == 'shutdown' then + client.stop() + end + end + } + end) end) describe("vim.lsp.buf.format", function() -- cgit From 55187de1157e05ea71c7c0404345dee0e27e963e Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Thu, 5 May 2022 23:56:00 +0200 Subject: fix(lsp): fix rename capability checks and multi client support (#18441) Adds filter and id options to filter the client to use for rename. Similar to the recently added `format` function. rename will use all matching clients one after another and can handle a mix of prepareRename/rename support. Also ensures the right `offset_encoding` is used for the `make_position_params` calls --- test/functional/plugin/lsp_spec.lua | 2 -- 1 file changed, 2 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 530d414334..33a8976b79 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2636,10 +2636,8 @@ describe('LSP', function() name = "prepare_rename_error", expected_handlers = { {NIL, {}, {method="shutdown", client_id=1}}; - {NIL, NIL, {method="textDocument/rename", client_id=1, bufnr=1}}; {NIL, {}, {method="start", client_id=1}}; }, - expected_text = "two", -- see test case }, }) do it(test.it, function() -- cgit From a9d25e94725d5cfc41c2fabff22b2284e109fa0c Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 12 May 2022 18:48:02 +0200 Subject: fix(lsp): perform client side filtering of code actions (#18392) Implement filtering of actions based on the kind when passing the 'only' parameter to code_action(). Action kinds are hierachical with a '.' as the separator, and the filter thus allows, for example, both 'quickfix' and 'quickfix.foo' when requestiong only 'quickfix'. Fix https://github.com/neovim/neovim/pull/18221#issuecomment-1110179121 --- test/functional/plugin/lsp_spec.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 33a8976b79..4cb7636825 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2753,12 +2753,33 @@ describe('LSP', function() vim.lsp.commands['executed_preferred'] = function() end end + vim.lsp.commands['quickfix_command'] = function(cmd) + vim.lsp.commands['executed_quickfix'] = function() + end + end local bufnr = vim.api.nvim_get_current_buf() vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) vim.lsp.buf.code_action({ filter = function(a) return a.isPreferred end, apply = true, }) + vim.lsp.buf.code_action({ + -- expect to be returned actions 'quickfix' and 'quickfix.foo' + context = { only = {'quickfix'}, }, + apply = true, + filter = function(a) + if a.kind == 'quickfix.foo' then + vim.lsp.commands['filtered_quickfix_foo'] = function() end + return false + elseif a.kind == 'quickfix' then + return true + else + assert(nil, 'unreachable') + end + end, + }) ]]) elseif ctx.method == 'shutdown' then eq('function', exec_lua[[return type(vim.lsp.commands['executed_preferred'])]]) + eq('function', exec_lua[[return type(vim.lsp.commands['filtered_quickfix_foo'])]]) + eq('function', exec_lua[[return type(vim.lsp.commands['executed_quickfix'])]]) client.stop() end end -- cgit From f6ba7d69be2c57f80c9ee95276733e949aceb009 Mon Sep 17 00:00:00 2001 From: Arsham Shirvani Date: Fri, 13 May 2022 15:49:08 +0100 Subject: fix(man.vim): q in "$MANPAGER mode" does not quit #18443 Problem: q in "$MANPAGER mode" does not quit Nvim. This is because ftplugin/man.vim creates its own mapping: nnoremap q :lclosec which overrides the one set by the autoload file when using :Man! ("$MANPAGER mode") Solution: Set b:pager during "$MANPAGER mode" so that ftplugin/man.vim can set the mapping correctly. Fixes #18281 Ref #17791 Helped-by: Gregory Anders <8965202+gpanders@users.noreply.github.com> --- test/functional/plugin/man_spec.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/man_spec.lua b/test/functional/plugin/man_spec.lua index e5b2e7dc1f..c8da5a711f 100644 --- a/test/functional/plugin/man_spec.lua +++ b/test/functional/plugin/man_spec.lua @@ -2,13 +2,19 @@ local helpers = require('test.functional.helpers')(after_each) local Screen = require('test.functional.ui.screen') local command, eval, rawfeed = helpers.command, helpers.eval, helpers.rawfeed local clear = helpers.clear +local funcs = helpers.funcs +local nvim_prog = helpers.nvim_prog +local matches = helpers.matches describe(':Man', function() + before_each(function() + clear() + end) + describe('man.lua: highlight_line()', function() local screen before_each(function() - clear() command('syntax on') command('set filetype=man') command('syntax off') -- Ignore syntax groups @@ -137,4 +143,10 @@ describe(':Man', function() ]]) end) end) + + it('q quits in "$MANPAGER mode" (:Man!) #18281', function() + -- This will hang if #18281 regresses. + local args = {nvim_prog, '--headless', '+autocmd VimLeave * echo "quit works!!"', '+Man!', '+call nvim_input("q")'} + matches('quit works!!', funcs.system(args, {'manpage contents'})) + end) end) -- cgit From 2ffafc7aa91fb1d9a71fff12051e40961a7b7f69 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Mon, 9 May 2022 12:00:27 -0600 Subject: feat(lsp): add LspAttach and LspDetach autocommands The current approach of using `on_attach` callbacks for configuring buffers for LSP is suboptimal: 1. It does not use the standard Nvim interface for driving and hooking into events (i.e. autocommands) 2. There is no way for "third parties" (e.g. plugins) to hook into the event. This means that *all* buffer configuration must go into the user-supplied on_attach callback. This also makes it impossible for these configurations to be modular, since it all must happen in the same place. 3. There is currently no way to do something when a client detaches from a buffer (there is no `on_detach` callback). The solution is to use the traditional method of event handling in Nvim: autocommands. When a LSP client is attached to a buffer, fire a `LspAttach`. Likewise, when a client detaches from a buffer fire a `LspDetach` event. This enables plugins to easily add LSP-specific configuration to buffers as well as enabling users to make their own configurations more modular (e.g. by creating multiple LspAttach autocommands that each do something unique). --- test/functional/plugin/lsp_spec.lua | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 4cb7636825..6c961eff7d 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -18,6 +18,7 @@ local NIL = helpers.NIL local read_file = require('test.helpers').read_file local write_file = require('test.helpers').write_file local isCI = helpers.isCI +local meths = helpers.meths -- Use these to get access to a coroutine so that I can run async tests and use -- yield. @@ -341,6 +342,43 @@ describe('LSP', function() } end) + it('should fire autocommands on attach and detach', function() + local client + test_rpc_server { + test_name = "basic_init"; + on_setup = function() + exec_lua [[ + BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_create_autocmd('LspAttach', { + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + vim.g.lsp_attached = client.name + end, + }) + vim.api.nvim_create_autocmd('LspDetach', { + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + vim.g.lsp_detached = client.name + end, + }) + ]] + end; + on_init = function(_client) + client = _client + eq(true, exec_lua("return lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)")) + client.notify('finish') + end; + on_handler = function(_, _, ctx) + if ctx.method == 'finish' then + eq('basic_init', meths.get_var('lsp_attached')) + exec_lua("return lsp.buf_detach_client(BUFFER, TEST_RPC_CLIENT_ID)") + eq('basic_init', meths.get_var('lsp_detached')) + client.stop() + end + end; + } + end) + it('client should return settings via workspace/configuration handler', function() local expected_handlers = { {NIL, {}, {method="shutdown", client_id=1}}; -- cgit From 58b9b7e7c48e5996e041cdbfa526e6cad5b37b97 Mon Sep 17 00:00:00 2001 From: bfredl Date: Sun, 22 May 2022 14:06:21 +0200 Subject: fix(ci): remove 2000ms blocking wait in many plugin/lsp_spec.lua tests Cuts down typical run time for `plugin/lsp_spec.lua` from 70 secs to 12 secs in ASAN CI build. This happens in ASAN/EXIT_FREE builds where nvim waits 2000ms due to unclosed handled. I wasn't able to pin-point the exact cause. But these tests ran in nested context where two server/client pairs were setup for no good reason. Moving these tests out so only one client is being setup fixed the exit hang. --- test/functional/plugin/lsp_spec.lua | 70 +++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 30 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 6c961eff7d..f8d4552330 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -222,10 +222,32 @@ describe('LSP', function() end) end) + describe('lsp._cmd_parts test', function() + local function _cmd_parts(input) + return exec_lua([[ + lsp = require('vim.lsp') + return lsp._cmd_parts(...) + ]], input) + end + it('should valid cmd argument', function() + eq(true, pcall(_cmd_parts, {"nvim"})) + eq(true, pcall(_cmd_parts, {"nvim", "--head"})) + end) + + it('should invalid cmd argument', function() + eq('Error executing lua: .../lsp.lua:0: cmd: expected list, got nvim', + pcall_err(_cmd_parts, 'nvim')) + eq('Error executing lua: .../lsp.lua:0: cmd argument: expected string, got number', + pcall_err(_cmd_parts, {'nvim', 1})) + end) + end) +end) + +describe('LSP', function() describe('basic_init test', function() after_each(function() stop() - exec_lua("lsp.stop_client(lsp.get_active_clients())") + exec_lua("lsp.stop_client(lsp.get_active_clients(), true)") exec_lua("lsp._vim_exit_handler()") end) @@ -424,16 +446,23 @@ describe('LSP', function() } end) it('workspace/configuration returns NIL per section if client was started without config.settings', function() - fake_lsp_server_setup('workspace/configuration no settings') - eq({ NIL, NIL, }, exec_lua [[ - local result = { - items = { - {section = 'foo'}, - {section = 'bar'}, - } - } - return vim.lsp.handlers['workspace/configuration'](nil, result, {client_id=TEST_RPC_CLIENT_ID}) - ]]) + local result = nil + test_rpc_server { + test_name = 'basic_init'; + on_init = function(c) c.stop() end, + on_setup = function() + result = exec_lua [[ + local result = { + items = { + {section = 'foo'}, + {section = 'bar'}, + } + } + return vim.lsp.handlers['workspace/configuration'](nil, result, {client_id=TEST_RPC_CLIENT_ID}) + ]] + end + } + eq({ NIL, NIL }, result) end) it('should verify capabilities sent', function() @@ -1345,25 +1374,6 @@ describe('LSP', function() } end) end) - describe('lsp._cmd_parts test', function() - local function _cmd_parts(input) - return exec_lua([[ - lsp = require('vim.lsp') - return lsp._cmd_parts(...) - ]], input) - end - it('should valid cmd argument', function() - eq(true, pcall(_cmd_parts, {"nvim"})) - eq(true, pcall(_cmd_parts, {"nvim", "--head"})) - end) - - it('should invalid cmd argument', function() - eq('Error executing lua: .../lsp.lua:0: cmd: expected list, got nvim', - pcall_err(_cmd_parts, 'nvim')) - eq('Error executing lua: .../lsp.lua:0: cmd argument: expected string, got number', - pcall_err(_cmd_parts, {'nvim', 1})) - end) - end) end) describe('LSP', function() -- cgit From d837b6d50cbb7e90ac8f77a9e1ac2a69acae02c1 Mon Sep 17 00:00:00 2001 From: Javier Lopez Date: Wed, 1 Jun 2022 09:10:10 -0500 Subject: fix(checkhealth): skip vim.health #18816 Problem: https://github.com/neovim/neovim/pull/18720#issuecomment-1142614996 The vim.health module is detected as a healthcheck, which produces spurious errors: vim: require("vim.health").check() ======================================================================== - ERROR: Failed to run healthcheck for "vim" plugin. Exception: function health#check, line 20 Vim(eval):E5108: Error executing lua [string "luaeval()"]:1: attempt to call field 'check' (a nil value) stack traceback: [string "luaeval()"]:1: in main chunk Solution: Skip vim.health when discovering healthchecks. --- test/functional/plugin/health_spec.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index a9bd76ce24..ba66117fb1 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -5,7 +5,7 @@ local Screen = require('test.functional.ui.screen') local clear = helpers.clear local curbuf_contents = helpers.curbuf_contents local command = helpers.command -local eq = helpers.eq +local eq, neq = helpers.eq, helpers.neq local getcompletion = helpers.funcs.getcompletion describe(':checkhealth', function() @@ -37,6 +37,7 @@ describe(':checkhealth', function() eq('nvim', getcompletion('nvim', 'checkhealth')[1]) eq('provider', getcompletion('prov', 'checkhealth')[1]) eq('vim.lsp', getcompletion('vim.ls', 'checkhealth')[1]) + neq('vim', getcompletion('^vim', 'checkhealth')[1]) -- should not complete vim.health end) end) @@ -242,6 +243,13 @@ describe('health.vim', function() - ERROR: No healthcheck found for "non_existent_healthcheck" plugin. ]]) end) + + it("does not use vim.health as a healtcheck", function() + -- vim.health is not a healthcheck + command("checkhealth vim") + helpers.expect([[ + ERROR: No healthchecks found.]]) + end) end) end) -- cgit From c6d747e6a5227e17556c62e16ed054398eb1a89a Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Fri, 3 Jun 2022 18:16:11 +0200 Subject: feat(lsp): send didChangeConfiguration after init (#18847) Most LSP servers require the notification to correctly load the settings and for those who don't it doesn't cause any harm. So far this is done in lspconfig, but with the addition of vim.lsp.start it should be part of core. --- test/functional/plugin/lsp_spec.lua | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index f8d4552330..05ea6f7523 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -45,10 +45,10 @@ local function clear_notrace() end -local function fake_lsp_server_setup(test_name, timeout_ms, options) +local function fake_lsp_server_setup(test_name, timeout_ms, options, settings) exec_lua([=[ lsp = require('vim.lsp') - local test_name, fixture_filename, logfile, timeout, options = ... + local test_name, fixture_filename, logfile, timeout, options, settings = ... TEST_RPC_CLIENT_ID = lsp.start_client { cmd_env = { NVIM_LOG_FILE = logfile; @@ -79,17 +79,18 @@ local function fake_lsp_server_setup(test_name, timeout_ms, options) allow_incremental_sync = options.allow_incremental_sync or false; debounce_text_changes = options.debounce_text_changes or 0; }; + settings = settings; on_exit = function(...) vim.rpcnotify(1, "exit", ...) end; } - ]=], test_name, fake_lsp_code, fake_lsp_logfile, timeout_ms or 1e3, options or {}) + ]=], test_name, fake_lsp_code, fake_lsp_logfile, timeout_ms or 1e3, options or {}, settings or {}) end local function test_rpc_server(config) if config.test_name then clear_notrace() - fake_lsp_server_setup(config.test_name, config.timeout_ms or 1e3, config.options) + fake_lsp_server_setup(config.test_name, config.timeout_ms or 1e3, config.options, config.settings) end local client = setmetatable({}, { __index = function(_, name) @@ -298,6 +299,22 @@ describe('LSP', function() } end) + it('should send didChangeConfiguration after initialize if there are settings', function() + test_rpc_server({ + test_name = 'basic_init_did_change_configuration', + on_init = function(client, _) + client.stop() + end, + on_exit = function(code, signal) + eq(0, code, 'exit code', fake_lsp_logfile) + eq(0, signal, 'exit signal', fake_lsp_logfile) + end, + settings = { + dummy = 1, + }, + }) + end) + it('should succeed with manual shutdown', function() if isCI() then pending('hangs the build on CI #14028, re-enable with freeze timeout #14204') -- cgit From e4df1c9b9e61e79234684d30ca700b42f82bc34a Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Sun, 5 Jun 2022 16:43:32 +0200 Subject: fix(lsp): fix multi client handling in code action (#18869) Fixes https://github.com/neovim/neovim/issues/18860 --- test/functional/plugin/lsp_spec.lua | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 05ea6f7523..22e2354723 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2793,6 +2793,45 @@ describe('LSP', function() end } end) + it('Calls workspace/executeCommand if no client side command', function() + local client + local expected_handlers = { + { NIL, {}, { method = 'shutdown', client_id = 1 } }, + { + NIL, + { command = 'dummy1', title = 'Command 1' }, + { bufnr = 1, method = 'workspace/executeCommand', client_id = 1 }, + }, + { NIL, {}, { method = 'start', client_id = 1 } }, + } + test_rpc_server({ + test_name = 'code_action_server_side_command', + on_init = function(client_) + client = client_ + end, + on_setup = function() 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) + ctx.params = nil -- don't compare in assert + eq(table.remove(expected_handlers), { err, result, ctx }) + if ctx.method == 'start' then + exec_lua([[ + local bufnr = vim.api.nvim_get_current_buf() + vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.fn.inputlist = function() + return 1 + end + vim.lsp.buf.code_action() + ]]) + elseif ctx.method == 'shutdown' then + client.stop() + end + end, + }) + end) it('Filters and automatically applies action if requested', function() local client local expected_handlers = { -- cgit From b6467dfc23dab476e256490b8014bbb488684e6b Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 30 May 2022 02:40:17 -0700 Subject: test(report): formatting, drop dumplog() Don't need to dumplog() on each failed test because we now have test-ids that associate log messages with tests. --- test/functional/plugin/lsp_spec.lua | 140 ++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 70 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 22e2354723..6db969693e 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -266,8 +266,8 @@ describe('LSP', function() end; -- If the program timed out, then code will be nil. on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; -- Note that NIL must be used here. -- on_handler(err, method, result, client_id) @@ -288,8 +288,8 @@ describe('LSP', function() client.stop() end; on_exit = function(code, signal) - eq(101, code, "exit code", fake_lsp_logfile) -- See fake-lsp-server.lua - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(101, code, "exit code") -- See fake-lsp-server.lua + eq(0, signal, "exit signal") assert_log(pesc([[assert_eq failed: left == "\"shutdown\"", right == "\"test\""]]), fake_lsp_logfile) end; @@ -335,8 +335,8 @@ describe('LSP', function() client.stop() end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(...) eq(table.remove(expected_handlers), {...}, "expected handler") @@ -367,8 +367,8 @@ describe('LSP', function() client.notify('finish') end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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") @@ -436,8 +436,8 @@ describe('LSP', function() client = _client end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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") @@ -496,8 +496,8 @@ describe('LSP', function() eq(false, client.server_capabilities().codeLensProvider) end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(...) eq(table.remove(expected_handlers), {...}, "expected handler") @@ -517,8 +517,8 @@ describe('LSP', function() client = c end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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") @@ -547,8 +547,8 @@ describe('LSP', function() client = c end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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") @@ -596,8 +596,8 @@ describe('LSP', function() eq(true, client.supports_method("unknown-method")) end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(...) eq(table.remove(expected_handlers), {...}, "expected handler") @@ -626,8 +626,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(...) eq(table.remove(expected_handlers), {...}, "expected handler") @@ -651,8 +651,8 @@ describe('LSP', function() exec_lua("vim.lsp.buf.type_definition()") end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(...) eq(table.remove(expected_handlers), {...}, "expected handler") @@ -672,8 +672,8 @@ describe('LSP', function() client = _client end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") eq(0, #expected_handlers, "did not call expected handler") end; on_handler = function(err, _, ctx) @@ -696,8 +696,8 @@ describe('LSP', function() client = _client end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") eq(0, #expected_handlers, "did not call expected handler") end; on_handler = function(err, _, ctx) @@ -726,8 +726,8 @@ describe('LSP', function() client.notify("release") end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") eq(0, #expected_handlers, "did not call expected handler") end; on_handler = function(err, _, ctx) @@ -759,8 +759,8 @@ describe('LSP', function() client.notify("release") end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") eq(0, #expected_handlers, "did not call expected handler") end; on_handler = function(err, _, ctx) @@ -793,8 +793,8 @@ describe('LSP', function() client.notify("release") end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") eq(0, #expected_handlers, "did not call expected handler") end; on_handler = function(err, _, ctx) @@ -828,8 +828,8 @@ describe('LSP', function() client.notify("release") end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") eq(0, #expected_handlers, "did not call expected handler") eq(3, eval('g:requests')) end; @@ -874,8 +874,8 @@ describe('LSP', function() client.notify('finish') end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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") @@ -917,8 +917,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) if ctx.method == 'start' then @@ -960,8 +960,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) if ctx.method == 'start' then @@ -1003,8 +1003,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) if ctx.method == 'start' then @@ -1052,8 +1052,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) if ctx.method == 'start' then @@ -1103,8 +1103,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) if ctx.method == 'start' then @@ -1154,8 +1154,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) if ctx.method == 'start' then @@ -1203,8 +1203,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) if ctx.method == 'start' then @@ -1247,8 +1247,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) if ctx.method == 'start' then @@ -1298,8 +1298,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result,ctx) if ctx.method == 'start' then @@ -1340,8 +1340,8 @@ describe('LSP', function() client.stop(true) end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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") @@ -1379,8 +1379,8 @@ describe('LSP', function() ]] end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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") @@ -1725,8 +1725,8 @@ describe('LSP', function() end; -- If the program timed out, then code will be nil. on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; -- Note that NIL must be used here. -- on_handler(err, method, result, client_id) @@ -2728,8 +2728,8 @@ describe('LSP', function() ]=]) end; on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + eq(0, code, "exit code") + eq(0, signal, "exit signal") end; on_handler = function(err, result, ctx) -- Don't compare & assert params, they're not relevant for the testcase @@ -2768,8 +2768,8 @@ describe('LSP', function() on_setup = function() end, on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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}) @@ -2846,8 +2846,8 @@ describe('LSP', function() on_setup = function() end, on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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}) @@ -2919,8 +2919,8 @@ describe('LSP', function() on_setup = function() end, on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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}) @@ -2985,8 +2985,8 @@ describe('LSP', function() ]=]) end, on_exit = function(code, signal) - eq(0, code, "exit code", fake_lsp_logfile) - eq(0, signal, "exit signal", fake_lsp_logfile) + 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}) -- cgit From 6f6286e4f90da25a7d1b6bcc96b79b0ccbaf5c26 Mon Sep 17 00:00:00 2001 From: L3MON4D3 <41961280+L3MON4D3@users.noreply.github.com> Date: Wed, 29 Jun 2022 18:53:49 +0200 Subject: fix(lsp): small bugs in snippet-parser #18998 This fixes the following bugs: `${1:else_text}` -> format with if_text: "else_text" `${1:-else_text}` -> format with if_text: "else_text" `${1:}` in `format` (eg. empty else_text) -> error. `${1:}` (eg. empty placeholder) -> error. Thanks hrsh7th :) --- test/functional/plugin/lsp/snippet_spec.lua | 134 ++++++++++++++++++++++------ 1 file changed, 109 insertions(+), 25 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp/snippet_spec.lua b/test/functional/plugin/lsp/snippet_spec.lua index 4e127743eb..7903885420 100644 --- a/test/functional/plugin/lsp/snippet_spec.lua +++ b/test/functional/plugin/lsp/snippet_spec.lua @@ -19,9 +19,9 @@ describe('vim.lsp._snippet', function() { type = snippet.NodeType.TEXT, raw = 'TE\\$\\}XT', - esc = 'TE$}XT' - } - } + esc = 'TE$}XT', + }, + }, }, parse('TE\\$\\}XT')) end) @@ -36,8 +36,8 @@ describe('vim.lsp._snippet', function() { type = snippet.NodeType.TABSTOP, tabstop = 2, - } - } + }, + }, }, parse('$1${2}')) end) @@ -56,7 +56,7 @@ describe('vim.lsp._snippet', function() { type = snippet.NodeType.TEXT, raw = 'TE\\$\\}XT', - esc = 'TE$}XT' + esc = 'TE$}XT', }, { type = snippet.NodeType.TABSTOP, @@ -73,21 +73,21 @@ describe('vim.lsp._snippet', function() { type = snippet.NodeType.FORMAT, capture_index = 1, - modifier = 'upcase' - } - } + modifier = 'upcase', + }, + }, }, }, { type = snippet.NodeType.TEXT, raw = 'TE\\$\\}XT', - esc = 'TE$}XT' + esc = 'TE$}XT', }, - } - } - } + }, + }, + }, }, - } + }, }, parse('${1:${2:TE\\$\\}XT$3${1/regex/${1:/upcase}/i}TE\\$\\}XT}}')) end) @@ -110,8 +110,8 @@ describe('vim.lsp._snippet', function() { type = snippet.NodeType.TABSTOP, tabstop = 1, - } - } + }, + }, }, { type = snippet.NodeType.VARIABLE, @@ -124,11 +124,11 @@ describe('vim.lsp._snippet', function() type = snippet.NodeType.FORMAT, capture_index = 1, modifier = 'upcase', - } - } - } + }, + }, + }, }, - } + }, }, parse('$VAR${VAR}${VAR:$1}${VAR/regex/${1:/upcase}/}')) end) @@ -141,12 +141,96 @@ describe('vim.lsp._snippet', function() tabstop = 1, items = { ',', - '|' - } - } - } + '|', + }, + }, + }, }, parse('${1|\\,,\\||}')) end) -end) + it('should parse format', function() + eq({ + type = snippet.NodeType.SNIPPET, + children = { + { + type = snippet.NodeType.VARIABLE, + name = 'VAR', + transform = { + type = snippet.NodeType.TRANSFORM, + pattern = 'regex', + format = { + { + type = snippet.NodeType.FORMAT, + capture_index = 1, + modifier = 'upcase', + }, + { + type = snippet.NodeType.FORMAT, + capture_index = 1, + if_text = 'if_text', + else_text = '', + }, + { + type = snippet.NodeType.FORMAT, + capture_index = 1, + if_text = '', + else_text = 'else_text', + }, + { + type = snippet.NodeType.FORMAT, + capture_index = 1, + else_text = 'else_text', + if_text = 'if_text', + }, + { + type = snippet.NodeType.FORMAT, + capture_index = 1, + if_text = '', + else_text = 'else_text', + }, + }, + }, + }, + }, + }, parse('${VAR/regex/${1:/upcase}${1:+if_text}${1:-else_text}${1:?if_text:else_text}${1:else_text}/}')) + end) + it('should parse empty strings', function() + eq({ + children = { + { + children = { { + esc = '', + raw = '', + type = 7, + } }, + tabstop = 1, + type = 2, + }, + { + esc = ' ', + raw = ' ', + type = 7, + }, + { + name = 'VAR', + transform = { + format = { + { + capture_index = 1, + else_text = '', + if_text = '', + type = 6, + }, + }, + option = 'g', + pattern = 'erg', + type = 5, + }, + type = 3, + }, + }, + type = 0, + }, parse('${1:} ${VAR/erg/${1:?:}/g}')) + end) +end) -- cgit From 13abe20b5f855779456c84b9583ed614223a69c8 Mon Sep 17 00:00:00 2001 From: ii14 <59243201+ii14@users.noreply.github.com> Date: Sun, 17 Jul 2022 19:13:33 +0200 Subject: refactor(lsp): use autocmd api (#19407) * refactor(lsp): use autocmd api * refactor(lsp): inline BufWritePost and VimLeavePre callbacks --- test/functional/plugin/lsp/diagnostic_spec.lua | 2 +- test/functional/plugin/lsp_spec.lua | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp/diagnostic_spec.lua b/test/functional/plugin/lsp/diagnostic_spec.lua index 19b01edb29..f73ffc29b0 100644 --- a/test/functional/plugin/lsp/diagnostic_spec.lua +++ b/test/functional/plugin/lsp/diagnostic_spec.lua @@ -221,7 +221,7 @@ describe('vim.lsp.diagnostic', function() local diags = vim.diagnostic.get(diagnostic_bufnr) vim.lsp.stop_client(client_id) - vim.lsp._vim_exit_handler() + vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false }) return diags ]], line) eq(1, #result) diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 6db969693e..c166982052 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -137,7 +137,7 @@ local function test_rpc_server(config) end stop() if config.test_name then - exec_lua("lsp._vim_exit_handler()") + exec_lua("vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false })") end end @@ -172,7 +172,7 @@ describe('LSP', function() end) after_each(function() - exec_lua("lsp._vim_exit_handler()") + exec_lua("vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false })") -- exec_lua("lsp.stop_all_clients(true)") end) @@ -249,7 +249,7 @@ describe('LSP', function() after_each(function() stop() exec_lua("lsp.stop_client(lsp.get_active_clients(), true)") - exec_lua("lsp._vim_exit_handler()") + exec_lua("vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false })") end) it('should run correctly', function() @@ -505,7 +505,7 @@ describe('LSP', function() } end) - it('_text_document_did_save_handler sends didSave with bool textDocumentSync.save', function() + it('BufWritePost sends didSave with bool textDocumentSync.save', function() local expected_handlers = { {NIL, {}, {method="shutdown", client_id=1}}; {NIL, {}, {method="start", client_id=1}}; @@ -526,7 +526,7 @@ describe('LSP', function() 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) + vim.api.nvim_exec_autocmds('BufWritePost', { buffer = BUFFER, modeline = false }) ]=]) else client.stop() @@ -535,7 +535,7 @@ describe('LSP', function() } end) - it('_text_document_did_save_handler sends didSave including text if server capability is set', function() + it('BufWritePost 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}}; @@ -557,7 +557,7 @@ describe('LSP', function() 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) + vim.api.nvim_exec_autocmds('BufWritePost', { buffer = BUFFER, modeline = false }) ]=]) else client.stop() -- cgit