diff options
author | Lewis Russell <lewis6991@gmail.com> | 2024-07-29 11:20:15 +0100 |
---|---|---|
committer | Lewis Russell <me@lewisr.dev> | 2024-08-02 19:04:37 +0100 |
commit | 7d24c4d6b0413cd5af8d0579f0a9a694db7f775e (patch) | |
tree | d9954c2001fe512c60a76561e7a19566b9de638b | |
parent | f32557ca679cbb1d7de52ab54dc35585af9ab9d0 (diff) | |
download | rneovim-7d24c4d6b0413cd5af8d0579f0a9a694db7f775e.tar.gz rneovim-7d24c4d6b0413cd5af8d0579f0a9a694db7f775e.tar.bz2 rneovim-7d24c4d6b0413cd5af8d0579f0a9a694db7f775e.zip |
test: allow exec_lua to handle functions
Problem:
Tests have lots of exec_lua calls which input blocks of code
provided as unformatted strings.
Solution:
Teach exec_lua how to handle functions.
-rw-r--r-- | test/functional/lua/api_spec.lua | 8 | ||||
-rw-r--r-- | test/functional/lua/buffer_updates_spec.lua | 49 | ||||
-rw-r--r-- | test/functional/lua/commands_spec.lua | 14 | ||||
-rw-r--r-- | test/functional/lua/diagnostic_spec.lua | 3476 | ||||
-rw-r--r-- | test/functional/lua/filetype_spec.lua | 152 | ||||
-rw-r--r-- | test/functional/lua/fs_spec.lua | 64 | ||||
-rw-r--r-- | test/functional/lua/glob_spec.lua | 11 | ||||
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 2841 | ||||
-rw-r--r-- | test/functional/treesitter/fold_spec.lua | 44 | ||||
-rw-r--r-- | test/functional/treesitter/highlight_spec.lua | 175 | ||||
-rw-r--r-- | test/functional/treesitter/inspect_tree_spec.lua | 38 | ||||
-rw-r--r-- | test/functional/treesitter/language_spec.lua | 50 | ||||
-rw-r--r-- | test/functional/treesitter/node_spec.lua | 86 | ||||
-rw-r--r-- | test/functional/treesitter/parser_spec.lua | 486 | ||||
-rw-r--r-- | test/functional/treesitter/query_spec.lua | 505 | ||||
-rw-r--r-- | test/functional/treesitter/utils_spec.lua | 26 |
16 files changed, 4128 insertions, 3897 deletions
diff --git a/test/functional/lua/api_spec.lua b/test/functional/lua/api_spec.lua index 56969150bd..4bbb57c3a8 100644 --- a/test/functional/lua/api_spec.lua +++ b/test/functional/lua/api_spec.lua @@ -145,10 +145,10 @@ describe('luaeval(vim.api.…)', function() eq(true, fn.luaeval('vim.api.nvim__id(vim.api.nvim__id)(true)')) eq( 42, - exec_lua [[ - local f = vim.api.nvim__id({42, vim.api.nvim__id}) - return f[2](f[1]) - ]] + exec_lua(function() + local f = vim.api.nvim__id({ 42, vim.api.nvim__id }) + return f[2](f[1]) + end) ) end) diff --git a/test/functional/lua/buffer_updates_spec.lua b/test/functional/lua/buffer_updates_spec.lua index 6b575ad0ef..8ca97c8e5e 100644 --- a/test/functional/lua/buffer_updates_spec.lua +++ b/test/functional/lua/buffer_updates_spec.lua @@ -27,30 +27,35 @@ local origlines = { before_each(function() clear() - exec_lua [[ - local evname = ... + exec_lua(function() local events = {} - function test_register(bufnr, evname, id, changedtick, utf_sizes, preview) + function _G.test_register(bufnr, evname, id, changedtick, utf_sizes, preview) local function callback(...) - table.insert(events, {id, ...}) - if test_unreg == id then + table.insert(events, { id, ... }) + if _G.test_unreg == id then return true end end - local opts = {[evname]=callback, on_detach=callback, on_reload=callback, utf_sizes=utf_sizes, preview=preview} + local opts = { + [evname] = callback, + on_detach = callback, + on_reload = callback, + utf_sizes = utf_sizes, + preview = preview, + } if changedtick then opts.on_changedtick = callback end vim.api.nvim_buf_attach(bufnr, false, opts) end - function get_events() + function _G.get_events() local ret_events = events events = {} return ret_events end - ]] + end) end) describe('lua buffer event callbacks: on_lines', function() @@ -257,13 +262,13 @@ describe('lua buffer event callbacks: on_lines', function() it('has valid cursor position while shifting', function() api.nvim_buf_set_lines(0, 0, -1, true, { 'line1' }) - exec_lua([[ + exec_lua(function() vim.api.nvim_buf_attach(0, false, { on_lines = function() vim.api.nvim_set_var('listener_cursor_line', vim.api.nvim_win_get_cursor(0)[1]) end, }) - ]]) + end) feed('>>') eq(1, api.nvim_get_var('listener_cursor_line')) end) @@ -302,13 +307,13 @@ describe('lua buffer event callbacks: on_lines', function() it('#12718 lnume', function() api.nvim_buf_set_lines(0, 0, -1, true, { '1', '2', '3' }) - exec_lua([[ + exec_lua(function() vim.api.nvim_buf_attach(0, false, { on_lines = function(...) vim.api.nvim_set_var('linesev', { ... }) end, }) - ]]) + end) feed('1G0') feed('y<C-v>2j') feed('G0') @@ -326,13 +331,13 @@ describe('lua buffer event callbacks: on_lines', function() end) it('nvim_buf_call() from callback does not cause wrong Normal mode CTRL-A #16729', function() - exec_lua([[ + exec_lua(function() vim.api.nvim_buf_attach(0, false, { - on_lines = function(...) + on_lines = function() vim.api.nvim_buf_call(0, function() end) end, }) - ]]) + end) feed('itest123<Esc><C-A>') eq('test124', api.nvim_get_current_line()) end) @@ -342,19 +347,19 @@ describe('lua buffer event callbacks: on_lines', function() screen:attach() api.nvim_buf_set_lines(0, 0, -1, true, { 'aaa', 'bbb', 'ccc' }) - exec_lua([[ + exec_lua(function() local ns = vim.api.nvim_create_namespace('') vim.api.nvim_buf_attach(0, false, { on_lines = function(_, _, _, row, _, end_row) vim.api.nvim_buf_clear_namespace(0, ns, row, end_row) for i = row, end_row - 1 do - local id = vim.api.nvim_buf_set_extmark(0, ns, i, 0, { - virt_text = {{ 'NEW' .. tostring(i), 'WarningMsg' }}, + vim.api.nvim_buf_set_extmark(0, ns, i, 0, { + virt_text = { { 'NEW' .. tostring(i), 'WarningMsg' } }, }) end end, }) - ]]) + end) feed('o') screen:expect({ @@ -383,7 +388,7 @@ describe('lua buffer event callbacks: on_lines', function() it('line lengths are correct when pressing TAB with folding #29119', function() api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b' }) - exec_lua([[ + exec_lua(function() _G.res = {} vim.o.foldmethod = 'indent' vim.o.softtabstop = -1 @@ -391,9 +396,9 @@ describe('lua buffer event callbacks: on_lines', function() on_lines = function(_, bufnr, _, row, _, end_row) local lines = vim.api.nvim_buf_get_lines(bufnr, row, end_row, true) table.insert(_G.res, lines) - end + end, }) - ]]) + end) feed('i<Tab>') eq({ '\ta' }, exec_lua('return _G.res[#_G.res]')) diff --git a/test/functional/lua/commands_spec.lua b/test/functional/lua/commands_spec.lua index 57b084d3d6..456ee13da2 100644 --- a/test/functional/lua/commands_spec.lua +++ b/test/functional/lua/commands_spec.lua @@ -178,13 +178,15 @@ describe(':lua', function() eq('hello', exec_capture(':lua = x()')) exec_lua('x = {a = 1, b = 2}') eq('{\n a = 1,\n b = 2\n}', exec_capture(':lua =x')) - exec_lua([[function x(success) - if success then - return true, "Return value" - else - return false, nil, "Error message" + exec_lua(function() + function _G.x(success) + if success then + return true, 'Return value' + else + return false, nil, 'Error message' + end end - end]]) + end) eq( dedent [[ true diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index decb58dc4d..718b9469a3 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -15,10 +15,10 @@ describe('vim.diagnostic', function() before_each(function() clear() - exec_lua [[ + exec_lua(function() require('vim.diagnostic') - function make_diagnostic(msg, lnum, col, end_lnum, end_col, severity, source, code) + local function make_diagnostic(msg, lnum, col, end_lnum, end_col, severity, source, code) return { lnum = lnum, col = col, @@ -31,54 +31,97 @@ describe('vim.diagnostic', function() } end - function make_error(msg, lnum, col, end_lnum, end_col, source, code) - return make_diagnostic(msg, lnum, col, end_lnum, end_col, vim.diagnostic.severity.ERROR, source, code) + function _G.make_error(msg, lnum, col, end_lnum, end_col, source, code) + return make_diagnostic( + msg, + lnum, + col, + end_lnum, + end_col, + vim.diagnostic.severity.ERROR, + source, + code + ) end - function make_warning(msg, lnum, col, end_lnum, end_col, source, code) - return make_diagnostic(msg, lnum, col, end_lnum, end_col, vim.diagnostic.severity.WARN, source, code) + function _G.make_warning(msg, lnum, col, end_lnum, end_col, source, code) + return make_diagnostic( + msg, + lnum, + col, + end_lnum, + end_col, + vim.diagnostic.severity.WARN, + source, + code + ) end - function make_info(msg, lnum, col, end_lnum, end_col, source, code) - return make_diagnostic(msg, lnum, col, end_lnum, end_col, vim.diagnostic.severity.INFO, source, code) + function _G.make_info(msg, lnum, col, end_lnum, end_col, source, code) + return make_diagnostic( + msg, + lnum, + col, + end_lnum, + end_col, + vim.diagnostic.severity.INFO, + source, + code + ) end - function make_hint(msg, lnum, col, end_lnum, end_col, source, code) - return make_diagnostic(msg, lnum, col, end_lnum, end_col, vim.diagnostic.severity.HINT, source, code) + function _G.make_hint(msg, lnum, col, end_lnum, end_col, source, code) + return make_diagnostic( + msg, + lnum, + col, + end_lnum, + end_col, + vim.diagnostic.severity.HINT, + source, + code + ) end - function count_diagnostics(bufnr, severity, namespace) - return #vim.diagnostic.get(bufnr, {severity = severity, namespace = namespace}) + function _G.count_diagnostics(bufnr, severity, namespace) + return #vim.diagnostic.get(bufnr, { severity = severity, namespace = namespace }) end - function count_extmarks(bufnr, namespace) + function _G.count_extmarks(bufnr, namespace) local ns = vim.diagnostic.get_namespace(namespace) local extmarks = 0 if ns.user_data.virt_text_ns then - extmarks = extmarks + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.virt_text_ns, 0, -1, {}) + extmarks = extmarks + + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.virt_text_ns, 0, -1, {}) end if ns.user_data.underline_ns then - extmarks = extmarks + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.underline_ns, 0, -1, {}) + extmarks = extmarks + + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.underline_ns, 0, -1, {}) end return extmarks end - function get_virt_text_extmarks(ns) - local ns = vim.diagnostic.get_namespace(ns) + function _G.get_virt_text_extmarks(ns) + ns = vim.diagnostic.get_namespace(ns) local virt_text_ns = ns.user_data.virt_text_ns - return vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, virt_text_ns, 0, -1, {details = true}) + return vim.api.nvim_buf_get_extmarks( + _G.diagnostic_bufnr, + virt_text_ns, + 0, + -1, + { details = true } + ) end - ]] - - exec_lua([[ - diagnostic_ns = vim.api.nvim_create_namespace("diagnostic_spec") - other_ns = vim.api.nvim_create_namespace("other_namespace") - diagnostic_bufnr = vim.api.nvim_create_buf(true, false) - local lines = {"1st line of text", "2nd line of text", "wow", "cool", "more", "lines"} - vim.fn.bufload(diagnostic_bufnr) - vim.api.nvim_buf_set_lines(diagnostic_bufnr, 0, 1, false, lines) - return diagnostic_bufnr - ]]) + end) + + exec_lua(function() + _G.diagnostic_ns = vim.api.nvim_create_namespace('diagnostic_spec') + _G.other_ns = vim.api.nvim_create_namespace('other_namespace') + _G.diagnostic_bufnr = vim.api.nvim_create_buf(true, false) + local lines = { '1st line of text', '2nd line of text', 'wow', 'cool', 'more', 'lines' } + vim.fn.bufload(_G.diagnostic_bufnr) + vim.api.nvim_buf_set_lines(_G.diagnostic_bufnr, 0, 1, false, lines) + end) end) it('creates highlight groups', function() @@ -115,27 +158,28 @@ describe('vim.diagnostic', function() end) it('retrieves diagnostics from all buffers and namespaces', function() - local result = exec_lua [[ + local result = exec_lua(function() local other_bufnr = vim.api.nvim_create_buf(true, false) - local lines = vim.api.nvim_buf_get_lines(diagnostic_bufnr, 0, -1, true) + local lines = vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, -1, true) vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - make_error('Diagnostic #2', 2, 1, 2, 1), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + _G.make_error('Diagnostic #2', 2, 1, 2, 1), }) - vim.diagnostic.set(other_ns, other_bufnr, { - make_error('Diagnostic #3', 3, 1, 3, 1), + vim.diagnostic.set(_G.other_ns, other_bufnr, { + _G.make_error('Diagnostic #3', 3, 1, 3, 1), }) return vim.diagnostic.get() - ]] + end) eq(3, #result) eq( 2, - exec_lua( - [[return #vim.tbl_filter(function(d) return d.bufnr == diagnostic_bufnr end, ...)]], - result - ) + exec_lua(function(result0) + return #vim.tbl_filter(function(d) + return d.bufnr == _G.diagnostic_bufnr + end, result0) + end, result) ) eq('Diagnostic #1', result[1].message) end) @@ -143,155 +187,171 @@ describe('vim.diagnostic', function() it('removes diagnostics from the cache when a buffer is removed', function() eq( 2, - exec_lua [[ - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - local other_bufnr = vim.fn.bufadd('test | test') - local lines = vim.api.nvim_buf_get_lines(diagnostic_bufnr, 0, -1, true) - vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines) - vim.cmd('bunload! ' .. other_bufnr) - - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - make_error('Diagnostic #2', 2, 1, 2, 1), - }) - vim.diagnostic.set(diagnostic_ns, other_bufnr, { - make_error('Diagnostic #3', 3, 1, 3, 1), - }) - vim.api.nvim_set_current_buf(other_bufnr) - vim.opt_local.buflisted = true - vim.cmd('bwipeout!') - return #vim.diagnostic.get() - ]] + exec_lua(function() + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + local other_bufnr = vim.fn.bufadd('test | test') + local lines = vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, -1, true) + vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines) + vim.cmd('bunload! ' .. other_bufnr) + + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + _G.make_error('Diagnostic #2', 2, 1, 2, 1), + }) + vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, { + _G.make_error('Diagnostic #3', 3, 1, 3, 1), + }) + vim.api.nvim_set_current_buf(other_bufnr) + vim.opt_local.buflisted = true + vim.cmd('bwipeout!') + return #vim.diagnostic.get() + end) ) eq( 2, - exec_lua [[ - vim.api.nvim_set_current_buf(diagnostic_bufnr) - vim.opt_local.buflisted = false - return #vim.diagnostic.get() - ]] + exec_lua(function() + vim.api.nvim_set_current_buf(_G.diagnostic_bufnr) + vim.opt_local.buflisted = false + return #vim.diagnostic.get() + end) ) eq( 0, - exec_lua [[ - vim.cmd('bwipeout!') - return #vim.diagnostic.get() - ]] + exec_lua(function() + vim.cmd('bwipeout!') + return #vim.diagnostic.get() + end) ) end) it('removes diagnostic from stale cache on reset', function() - local diagnostics = exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - make_error('Diagnostic #2', 2, 1, 2, 1), + local diagnostics = exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + _G.make_error('Diagnostic #2', 2, 1, 2, 1), }) - local other_bufnr = vim.fn.bufadd('test | test') - vim.cmd('noautocmd bwipeout! ' .. diagnostic_bufnr) - return vim.diagnostic.get(diagnostic_bufnr) - ]] + vim.fn.bufadd('test | test') + vim.cmd('noautocmd bwipeout! ' .. _G.diagnostic_bufnr) + return vim.diagnostic.get(_G.diagnostic_bufnr) + end) eq(2, #diagnostics) - diagnostics = exec_lua [[ + diagnostics = exec_lua(function() vim.diagnostic.reset() return vim.diagnostic.get() - ]] + end) eq(0, #diagnostics) end) it('always returns a copy of diagnostic tables', function() - local result = exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), + local result = exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), }) local diag = vim.diagnostic.get() diag[1].col = 10000 return vim.diagnostic.get()[1].col == 10000 - ]] + end) eq(false, result) end) it('resolves buffer number 0 to the current buffer', function() eq( 2, - exec_lua [[ - vim.api.nvim_set_current_buf(diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - make_error('Diagnostic #2', 2, 1, 2, 1), - }) - return #vim.diagnostic.get(0) - ]] + exec_lua(function() + vim.api.nvim_set_current_buf(_G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + _G.make_error('Diagnostic #2', 2, 1, 2, 1), + }) + return #vim.diagnostic.get(0) + end) ) end) it('saves and count a single error', function() eq( 1, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - }) - return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns) - ]] + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + }) + return _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ) + end) ) end) it('saves and count multiple errors', function() eq( 2, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - make_error('Diagnostic #2', 2, 1, 2, 1), - }) - return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns) - ]] + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + _G.make_error('Diagnostic #2', 2, 1, 2, 1), + }) + return _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ) + end) ) end) it('saves and count from multiple namespaces', function() eq( { 1, 1, 2 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic From Server 1', 1, 1, 1, 1), - }) - vim.diagnostic.set(other_ns, diagnostic_bufnr, { - make_error('Diagnostic From Server 2', 1, 1, 1, 1), - }) - return { - -- First namespace - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), - -- Second namespace - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, other_ns), - -- All namespaces - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR), - } - ]] + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic From Server 1', 1, 1, 1, 1), + }) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic From Server 2', 1, 1, 1, 1), + }) + return { + -- First namespace + _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ), + -- Second namespace + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.other_ns), + -- All namespaces + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR), + } + end) ) end) it('saves and count from multiple namespaces with respect to severity', function() eq( { 3, 0, 3 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), - make_error('Diagnostic From Server 1:2', 2, 2, 2, 2), - make_error('Diagnostic From Server 1:3', 2, 3, 3, 2), - }) - vim.diagnostic.set(other_ns, diagnostic_bufnr, { - make_warning('Warning From Server 2', 3, 3, 3, 3), - }) - return { - -- Namespace 1 - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), - -- Namespace 2 - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, other_ns), - -- All namespaces - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR), - } - ]] + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), + _G.make_error('Diagnostic From Server 1:2', 2, 2, 2, 2), + _G.make_error('Diagnostic From Server 1:3', 2, 3, 3, 2), + }) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, { + _G.make_warning('Warning From Server 2', 3, 3, 3, 3), + }) + return { + -- Namespace 1 + _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ), + -- Namespace 2 + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.other_ns), + -- All namespaces + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR), + } + end) ) end) @@ -304,160 +364,190 @@ describe('vim.diagnostic', function() local all_highlights = { 1, 1, 2, 4, 2 } eq( all_highlights, - exec_lua [[ - local ns_1_diags = { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 3), - } - local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 3), - } + exec_lua(function() + local ns_1_diags = { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 2, 1, 2, 3), + } + local ns_2_diags = { + _G.make_warning('Warning 1', 2, 1, 2, 3), + } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags) - return { - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN), - count_extmarks(diagnostic_bufnr, diagnostic_ns), - count_extmarks(diagnostic_bufnr, other_ns), - } - ]] + return { + _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN), + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns), + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns), + } + end) ) -- Clear diagnostics from namespace 1, and make sure we have the right amount of stuff for namespace 2 eq( { 1, 1, 2, 0, 2 }, - exec_lua [[ - vim.diagnostic.enable(false, { bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }) - return { - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN), - count_extmarks(diagnostic_bufnr, diagnostic_ns), - count_extmarks(diagnostic_bufnr, other_ns), - } - ]] + exec_lua(function() + vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }) + return { + _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN), + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns), + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns), + } + end) ) -- Show diagnostics from namespace 1 again eq( all_highlights, - exec_lua([[ - vim.diagnostic.enable(true, { bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }) - return { - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN), - count_extmarks(diagnostic_bufnr, diagnostic_ns), - count_extmarks(diagnostic_bufnr, other_ns), - } - ]]) + exec_lua(function() + vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }) + return { + _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN), + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns), + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns), + } + end) ) end) it('does not display diagnostics when disabled', function() eq( { 0, 2 }, - exec_lua [[ - local ns_1_diags = { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 3), - } - local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 3), - } + exec_lua(function() + local ns_1_diags = { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 2, 1, 2, 3), + } + local ns_2_diags = { + _G.make_warning('Warning 1', 2, 1, 2, 3), + } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags) - vim.diagnostic.enable(false, { bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }) + vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }) - return { - count_extmarks(diagnostic_bufnr, diagnostic_ns), - count_extmarks(diagnostic_bufnr, other_ns), - } - ]] + return { + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns), + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns), + } + end) ) eq( { 4, 0 }, - exec_lua [[ - vim.diagnostic.enable(true, { bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }) - vim.diagnostic.enable(false, { bufnr = diagnostic_bufnr, ns_id = other_ns }) + exec_lua(function() + vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }) + vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.other_ns }) - return { - count_extmarks(diagnostic_bufnr, diagnostic_ns), - count_extmarks(diagnostic_bufnr, other_ns), - } - ]] + return { + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns), + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns), + } + end) ) end) describe('show() and hide()', function() it('works', function() - local result = exec_lua [[ + local result = exec_lua(function() local other_bufnr = vim.api.nvim_create_buf(true, false) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) local result = {} vim.diagnostic.config({ underline = false, virtual_text = true }) local ns_1_diags = { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 5), + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 2, 1, 2, 5), } local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 5), + _G.make_warning('Warning 1', 2, 1, 2, 5), } local other_buffer_diags = { - make_info("This is interesting", 0, 0, 0, 0) + _G.make_info('This is interesting', 0, 0, 0, 0), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) - vim.diagnostic.set(diagnostic_ns, other_bufnr, other_buffer_diags) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags) + vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags) -- All buffers and namespaces - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) -- Hide one namespace - vim.diagnostic.hide(diagnostic_ns) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + vim.diagnostic.hide(_G.diagnostic_ns) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) -- Show one namespace - vim.diagnostic.show(diagnostic_ns) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + vim.diagnostic.show(_G.diagnostic_ns) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) -- Hide one buffer vim.diagnostic.hide(nil, other_bufnr) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) -- Hide everything vim.diagnostic.hide() - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) -- Show one buffer - vim.diagnostic.show(nil, diagnostic_bufnr) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + vim.diagnostic.show(nil, _G.diagnostic_bufnr) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) return result - ]] + end) eq(4, result[1]) eq(1, result[2]) @@ -468,13 +558,17 @@ describe('vim.diagnostic', function() end) it("doesn't error after bwipeout on buffer", function() - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {{ lnum = 0, end_lnum = 0, col = 0, end_col = 0 }}) - vim.cmd("bwipeout! " .. diagnostic_bufnr) + exec_lua(function() + vim.diagnostic.set( + _G.diagnostic_ns, + _G.diagnostic_bufnr, + { { lnum = 0, end_lnum = 0, col = 0, end_col = 0 } } + ) + vim.cmd('bwipeout! ' .. _G.diagnostic_bufnr) - vim.diagnostic.show(diagnostic_ns) - vim.diagnostic.hide(diagnostic_ns) - ]] + vim.diagnostic.show(_G.diagnostic_ns) + vim.diagnostic.hide(_G.diagnostic_ns) + end) end) end) @@ -505,52 +599,64 @@ describe('vim.diagnostic', function() end) it('without arguments', function() - local result = exec_lua [[ - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + local result = exec_lua(function() + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) local result = {} vim.diagnostic.config({ underline = false, virtual_text = true }) local ns_1_diags = { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 5), + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 2, 1, 2, 5), } local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 5), + _G.make_warning('Warning 1', 2, 1, 2, 5), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + ) vim.diagnostic.enable(false) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + ) -- Create a new buffer local other_bufnr = vim.api.nvim_create_buf(true, false) local other_buffer_diags = { - make_info("This is interesting", 0, 0, 0, 0) + _G.make_info('This is interesting', 0, 0, 0, 0), } - vim.diagnostic.set(diagnostic_ns, other_bufnr, other_buffer_diags) + vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) vim.diagnostic.enable() - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) return result - ]] + end) eq(3, result[1]) eq(0, result[2]) @@ -559,54 +665,66 @@ describe('vim.diagnostic', function() end) it('with buffer argument', function() - local result = exec_lua [[ + local result = exec_lua(function() local other_bufnr = vim.api.nvim_create_buf(true, false) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) local result = {} vim.diagnostic.config({ underline = false, virtual_text = true }) local ns_1_diags = { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 5), + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 2, 1, 2, 5), } local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 5), + _G.make_warning('Warning 1', 2, 1, 2, 5), } local other_buffer_diags = { - make_info("This is interesting", 0, 0, 0, 0) + _G.make_info('This is interesting', 0, 0, 0, 0), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) - vim.diagnostic.set(diagnostic_ns, other_bufnr, other_buffer_diags) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags) + vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) - vim.diagnostic.enable(false, { bufnr = diagnostic_bufnr }) + vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr }) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) - vim.diagnostic.enable(true, { bufnr = diagnostic_bufnr }) + vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr }) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) vim.diagnostic.enable(false, { bufnr = other_bufnr }) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) return result - ]] + end) eq(4, result[1]) eq(1, result[2]) @@ -615,44 +733,56 @@ describe('vim.diagnostic', function() end) it('with a namespace argument', function() - local result = exec_lua [[ - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + local result = exec_lua(function() + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) local result = {} vim.diagnostic.config({ underline = false, virtual_text = true }) local ns_1_diags = { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 5), + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 2, 1, 2, 5), } local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 5), + _G.make_warning('Warning 1', 2, 1, 2, 5), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + ) - vim.diagnostic.enable(false, { ns_id = diagnostic_ns }) + vim.diagnostic.enable(false, { ns_id = _G.diagnostic_ns }) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + ) - vim.diagnostic.enable(true, { ns_id = diagnostic_ns }) + vim.diagnostic.enable(true, { ns_id = _G.diagnostic_ns }) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + ) - vim.diagnostic.enable(false, { ns_id = other_ns }) + vim.diagnostic.enable(false, { ns_id = _G.other_ns }) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + ) return result - ]] + end) eq(3, result[1]) eq(1, result[2]) @@ -662,82 +792,93 @@ describe('vim.diagnostic', function() --- @return table local function test_enable(legacy) - local result = exec_lua( - [[ - local legacy = ... + local result = exec_lua(function(legacy0) local other_bufnr = vim.api.nvim_create_buf(true, false) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) local result = {} vim.diagnostic.config({ underline = false, virtual_text = true }) local ns_1_diags = { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 5), + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 2, 1, 2, 5), } local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 5), + _G.make_warning('Warning 1', 2, 1, 2, 5), } local other_buffer_diags = { - make_info("This is interesting", 0, 0, 0, 0) + _G.make_info('This is interesting', 0, 0, 0, 0), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) - vim.diagnostic.set(diagnostic_ns, other_bufnr, other_buffer_diags) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags) + vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags) - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) - if legacy then - vim.diagnostic.disable(diagnostic_bufnr, diagnostic_ns) + if legacy0 then + vim.diagnostic.disable(_G.diagnostic_bufnr, _G.diagnostic_ns) else - vim.diagnostic.enable(false, { bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }) + vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }) end - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) - if legacy then - vim.diagnostic.disable(diagnostic_bufnr, other_ns) + if legacy0 then + vim.diagnostic.disable(_G.diagnostic_bufnr, _G.other_ns) else - vim.diagnostic.enable(false, { bufnr = diagnostic_bufnr, ns_id = other_ns }) + vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.other_ns }) end - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) - if legacy then - vim.diagnostic.enable(diagnostic_bufnr, diagnostic_ns) + if legacy0 then + vim.diagnostic.enable(_G.diagnostic_bufnr, _G.diagnostic_ns) else - vim.diagnostic.enable(true, { bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }) + vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }) end - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) - if legacy then + if legacy0 then -- Should have no effect - vim.diagnostic.disable(other_bufnr, other_ns) + vim.diagnostic.disable(other_bufnr, _G.other_ns) else -- Should have no effect - vim.diagnostic.enable(false, { bufnr = other_bufnr, ns_id = other_ns }) + vim.diagnostic.enable(false, { bufnr = other_bufnr, ns_id = _G.other_ns }) end - table.insert(result, count_extmarks(diagnostic_bufnr, diagnostic_ns) + - count_extmarks(diagnostic_bufnr, other_ns) + - count_extmarks(other_bufnr, diagnostic_ns)) + table.insert( + result, + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns) + + _G.count_extmarks(other_bufnr, _G.diagnostic_ns) + ) return result - ]], - legacy - ) + end, legacy) return result end @@ -772,74 +913,94 @@ describe('vim.diagnostic', function() local all_highlights = { 1, 1, 2, 4, 2 } eq( all_highlights, - exec_lua [[ - local ns_1_diags = { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 3), - } - local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 3), - } + exec_lua(function() + local ns_1_diags = { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 2, 1, 2, 3), + } + local ns_2_diags = { + _G.make_warning('Warning 1', 2, 1, 2, 3), + } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diags) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags) - return { - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN), - count_extmarks(diagnostic_bufnr, diagnostic_ns), - count_extmarks(diagnostic_bufnr, other_ns), - } - ]] + return { + _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN), + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns), + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns), + } + end) ) -- Reset diagnostics from namespace 1 - exec_lua([[ vim.diagnostic.reset(diagnostic_ns) ]]) + exec_lua([[ vim.diagnostic.reset( _G.diagnostic_ns) ]]) -- Make sure we have the right diagnostic count eq( { 0, 1, 1, 0, 2 }, - exec_lua [[ - local diagnostic_count = {} - vim.wait(100, function () diagnostic_count = { - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN), - count_extmarks(diagnostic_bufnr, diagnostic_ns), - count_extmarks(diagnostic_bufnr, other_ns), - } end ) - return diagnostic_count - ]] + exec_lua(function() + local diagnostic_count = {} + vim.wait(100, function() + diagnostic_count = { + _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN), + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns), + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns), + } + end) + return diagnostic_count + end) ) -- Reset diagnostics from namespace 2 - exec_lua([[ vim.diagnostic.reset(other_ns) ]]) + exec_lua([[ vim.diagnostic.reset(_G.other_ns) ]]) -- Make sure we have the right diagnostic count eq( { 0, 0, 0, 0, 0 }, - exec_lua [[ - local diagnostic_count = {} - vim.wait(100, function () diagnostic_count = { - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN, other_ns), - count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.WARN), - count_extmarks(diagnostic_bufnr, diagnostic_ns), - count_extmarks(diagnostic_bufnr, other_ns), - } end ) - return diagnostic_count - ]] + exec_lua(function() + local diagnostic_count = {} + vim.wait(100, function() + diagnostic_count = { + _G.count_diagnostics( + _G.diagnostic_bufnr, + vim.diagnostic.severity.ERROR, + _G.diagnostic_ns + ), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns), + _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN), + _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns), + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns), + } + end) + return diagnostic_count + end) ) end) it("doesn't error after bwipeout called on buffer", function() - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {{ lnum = 0, end_lnum = 0, col = 0, end_col = 0 }}) - vim.cmd("bwipeout! " .. diagnostic_bufnr) + exec_lua(function() + vim.diagnostic.set( + _G.diagnostic_ns, + _G.diagnostic_bufnr, + { { lnum = 0, end_lnum = 0, col = 0, end_col = 0 } } + ) + vim.cmd('bwipeout! ' .. _G.diagnostic_bufnr) - vim.diagnostic.reset(diagnostic_ns) - ]] + vim.diagnostic.reset(_G.diagnostic_ns) + end) end) end) @@ -847,206 +1008,206 @@ describe('vim.diagnostic', function() it('can find the next pos with only one namespace', function() eq( { 1, 1 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) local next = vim.diagnostic.get_next() return { next.lnum, next.col } - ]] + end) ) end) it('can find next pos with two errors', function() eq( { 4, 4 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - make_error('Diagnostic #2', 4, 4, 4, 4), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + _G.make_error('Diagnostic #2', 4, 4, 4, 4), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {3, 1}) - local next = vim.diagnostic.get_next({ namespace = diagnostic_ns }) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 3, 1 }) + local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns }) return { next.lnum, next.col } - ]] + end) ) end) it('can cycle when position is past error', function() eq( { 1, 1 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {3, 1}) - local next = vim.diagnostic.get_next({ namespace = diagnostic_ns }) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 3, 1 }) + local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns }) return { next.lnum, next.col } - ]] + end) ) end) it('will not cycle when wrap is off', function() eq( vim.NIL, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {3, 1}) - local next = vim.diagnostic.get_next({ namespace = diagnostic_ns, wrap = false }) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 3, 1 }) + local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns, wrap = false }) return next - ]] + end) ) end) it('can cycle even from the last line', function() eq( { 4, 4 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #2', 4, 4, 4, 4), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #2', 4, 4, 4, 4), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {vim.api.nvim_buf_line_count(0), 1}) - local prev = vim.diagnostic.get_prev({ namespace = diagnostic_ns }) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(0), 1 }) + local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns }) return { prev.lnum, prev.col } - ]] + end) ) end) it('works with diagnostics past the end of the line #16349', function() eq( { 4, 0 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 3, 9001, 3, 9001), - make_error('Diagnostic #2', 4, 0, 4, 0), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 3, 9001, 3, 9001), + _G.make_error('Diagnostic #2', 4, 0, 4, 0), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {1, 1}) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) vim.diagnostic.jump({ count = 1, float = false }) - local next = vim.diagnostic.get_next({ namespace = diagnostic_ns }) + local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns }) return { next.lnum, next.col } - ]] + end) ) end) it('works with diagnostics before the start of the line', function() eq( { 4, 0 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 3, 9001, 3, 9001), - make_error('Diagnostic #2', 4, -1, 4, -1), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 3, 9001, 3, 9001), + _G.make_error('Diagnostic #2', 4, -1, 4, -1), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {1, 1}) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) vim.diagnostic.jump({ count = 1, float = false }) - local next = vim.diagnostic.get_next({ namespace = diagnostic_ns }) + local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns }) return { next.lnum, next.col } - ]] + end) ) end) it('jumps to diagnostic with highest severity', function() - exec_lua([[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_info('Info', 1, 0, 1, 1), - make_error('Error', 2, 0, 2, 1), - make_warning('Warning', 3, 0, 3, 1), - make_error('Error', 4, 0, 4, 1), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_info('Info', 1, 0, 1, 1), + _G.make_error('Error', 2, 0, 2, 1), + _G.make_warning('Warning', 3, 0, 3, 1), + _G.make_error('Error', 4, 0, 4, 1), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {1, 0}) - ]]) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + end) eq( { 3, 0 }, - exec_lua([[ - vim.diagnostic.jump({ count = 1, _highest = true }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.diagnostic.jump({ count = 1, _highest = true }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 5, 0 }, - exec_lua([[ - vim.diagnostic.jump({ count = 1, _highest = true }) - return vim.api.nvim_win_get_cursor(0) - ]]) - ) - - exec_lua([[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_info('Info', 1, 0, 1, 1), - make_hint('Hint', 2, 0, 2, 1), - make_warning('Warning', 3, 0, 3, 1), - make_hint('Hint', 4, 0, 4, 1), - make_warning('Warning', 5, 0, 5, 1), + exec_lua(function() + vim.diagnostic.jump({ count = 1, _highest = true }) + return vim.api.nvim_win_get_cursor(0) + end) + ) + + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_info('Info', 1, 0, 1, 1), + _G.make_hint('Hint', 2, 0, 2, 1), + _G.make_warning('Warning', 3, 0, 3, 1), + _G.make_hint('Hint', 4, 0, 4, 1), + _G.make_warning('Warning', 5, 0, 5, 1), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {1, 0}) - ]]) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + end) eq( { 4, 0 }, - exec_lua([[ - vim.diagnostic.jump({ count = 1, _highest = true }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.diagnostic.jump({ count = 1, _highest = true }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 6, 0 }, - exec_lua([[ - vim.diagnostic.jump({ count = 1, _highest = true }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.diagnostic.jump({ count = 1, _highest = true }) + return vim.api.nvim_win_get_cursor(0) + end) ) end) it('jumps to next diagnostic if severity is non-nil', function() - exec_lua([[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_info('Info', 1, 0, 1, 1), - make_error('Error', 2, 0, 2, 1), - make_warning('Warning', 3, 0, 3, 1), - make_error('Error', 4, 0, 4, 1), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_info('Info', 1, 0, 1, 1), + _G.make_error('Error', 2, 0, 2, 1), + _G.make_warning('Warning', 3, 0, 3, 1), + _G.make_error('Error', 4, 0, 4, 1), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {1, 0}) - ]]) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + end) eq( { 2, 0 }, - exec_lua([[ - vim.diagnostic.jump({ count = 1 }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.diagnostic.jump({ count = 1 }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 3, 0 }, - exec_lua([[ - vim.diagnostic.jump({ count = 1 }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.diagnostic.jump({ count = 1 }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 4, 0 }, - exec_lua([[ - vim.diagnostic.jump({ count = 1 }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.diagnostic.jump({ count = 1 }) + return vim.api.nvim_win_get_cursor(0) + end) ) end) end) @@ -1055,282 +1216,297 @@ describe('vim.diagnostic', function() it('can find the previous diagnostic with only one namespace', function() eq( { 1, 1 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {3, 1}) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 3, 1 }) local prev = vim.diagnostic.get_prev() return { prev.lnum, prev.col } - ]] + end) ) end) it('can find the previous diagnostic with two errors', function() eq( { 1, 1 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - make_error('Diagnostic #2', 4, 4, 4, 4), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + _G.make_error('Diagnostic #2', 4, 4, 4, 4), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {3, 1}) - local prev = vim.diagnostic.get_prev({ namespace = diagnostic_ns }) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 3, 1 }) + local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns }) return { prev.lnum, prev.col } - ]] + end) ) end) it('can cycle when position is past error', function() eq( { 4, 4 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #2', 4, 4, 4, 4), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #2', 4, 4, 4, 4), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {3, 1}) - local prev = vim.diagnostic.get_prev({ namespace = diagnostic_ns }) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 3, 1 }) + local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns }) return { prev.lnum, prev.col } - ]] + end) ) end) it('respects wrap parameter', function() eq( vim.NIL, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #2', 4, 4, 4, 4), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #2', 4, 4, 4, 4), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.api.nvim_win_set_cursor(0, {3, 1}) - local prev = vim.diagnostic.get_prev({ namespace = diagnostic_ns, wrap = false }) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.api.nvim_win_set_cursor(0, { 3, 1 }) + local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns, wrap = false }) return prev - ]] + end) ) end) it('works on blank line #28397', function() eq( { 0, 2 }, - exec_lua [[ - local test_bufnr = vim.api.nvim_create_buf(true, false) - vim.api.nvim_buf_set_lines(test_bufnr, 0, -1, false, { - 'first line', - '', - '', - 'end line', - }) - vim.diagnostic.set(diagnostic_ns, test_bufnr, { - make_info('Diagnostic #1', 0, 2, 0, 2), - make_info('Diagnostic #2', 2, 0, 2, 0), - make_info('Diagnostic #3', 2, 0, 2, 0), - }) - vim.api.nvim_win_set_buf(0, test_bufnr) - vim.api.nvim_win_set_cursor(0, {3, 0}) - return vim.diagnostic.get_prev_pos { namespace = diagnostic_ns} - ]] + exec_lua(function() + local test_bufnr = vim.api.nvim_create_buf(true, false) + vim.api.nvim_buf_set_lines(test_bufnr, 0, -1, false, { + 'first line', + '', + '', + 'end line', + }) + vim.diagnostic.set(_G.diagnostic_ns, test_bufnr, { + _G.make_info('Diagnostic #1', 0, 2, 0, 2), + _G.make_info('Diagnostic #2', 2, 0, 2, 0), + _G.make_info('Diagnostic #3', 2, 0, 2, 0), + }) + vim.api.nvim_win_set_buf(0, test_bufnr) + vim.api.nvim_win_set_cursor(0, { 3, 0 }) + return vim.diagnostic.get_prev_pos { namespace = _G.diagnostic_ns } + end) ) end) end) describe('jump()', function() before_each(function() - exec_lua([[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 0, 0, 0, 2), - make_error('Diagnostic #2', 1, 1, 1, 4), - make_warning('Diagnostic #3', 2, -1, 2, -1), - make_info('Diagnostic #4', 3, 0, 3, 3), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 0, 0, 0, 2), + _G.make_error('Diagnostic #2', 1, 1, 1, 4), + _G.make_warning('Diagnostic #3', 2, -1, 2, -1), + _G.make_info('Diagnostic #4', 3, 0, 3, 3), }) - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - ]]) + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + end) end) it('can move forward', function() eq( { 2, 1 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 1, 0 }) - vim.diagnostic.jump({ count = 1 }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + vim.diagnostic.jump({ count = 1 }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 4, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 1, 0 }) - vim.diagnostic.jump({ count = 3 }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + vim.diagnostic.jump({ count = 3 }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 4, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 1, 0 }) - vim.diagnostic.jump({ count = math.huge, wrap = false }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + vim.diagnostic.jump({ count = math.huge, wrap = false }) + return vim.api.nvim_win_get_cursor(0) + end) ) end) it('can move backward', function() eq( { 3, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 4, 0 }) - vim.diagnostic.jump({ count = -1 }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 4, 0 }) + vim.diagnostic.jump({ count = -1 }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 1, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 4, 0 }) - vim.diagnostic.jump({ count = -3 }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 4, 0 }) + vim.diagnostic.jump({ count = -3 }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 1, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 4, 0 }) - vim.diagnostic.jump({ count = -math.huge, wrap = false }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 4, 0 }) + vim.diagnostic.jump({ count = -math.huge, wrap = false }) + return vim.api.nvim_win_get_cursor(0) + end) ) end) it('can filter by severity', function() eq( { 3, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 1, 0 }) - vim.diagnostic.jump({ count = 1, severity = vim.diagnostic.severity.WARN }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + vim.diagnostic.jump({ count = 1, severity = vim.diagnostic.severity.WARN }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 3, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 1, 0 }) - vim.diagnostic.jump({ count = 9999, severity = vim.diagnostic.severity.WARN }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + vim.diagnostic.jump({ count = 9999, severity = vim.diagnostic.severity.WARN }) + return vim.api.nvim_win_get_cursor(0) + end) ) end) it('can wrap', function() eq( { 1, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 4, 0 }) - vim.diagnostic.jump({ count = 1, wrap = true }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 4, 0 }) + vim.diagnostic.jump({ count = 1, wrap = true }) + return vim.api.nvim_win_get_cursor(0) + end) ) eq( { 4, 0 }, - exec_lua([[ - vim.api.nvim_win_set_cursor(0, { 1, 0 }) - vim.diagnostic.jump({ count = -1, wrap = true }) - return vim.api.nvim_win_get_cursor(0) - ]]) + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + vim.diagnostic.jump({ count = -1, wrap = true }) + return vim.api.nvim_win_get_cursor(0) + end) ) end) end) describe('get()', function() it('returns an empty table when no diagnostics are present', function() - eq({}, exec_lua [[return vim.diagnostic.get(diagnostic_bufnr, {namespace=diagnostic_ns})]]) + eq( + {}, + exec_lua [[return vim.diagnostic.get( _G.diagnostic_bufnr, {namespace=diagnostic_ns})]] + ) end) it('returns all diagnostics when no severity is supplied', function() eq( 2, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 3), - }) + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 1, 1, 2, 3), + }) - return #vim.diagnostic.get(diagnostic_bufnr) - ]] + return #vim.diagnostic.get(_G.diagnostic_bufnr) + end) ) end) it('returns only requested diagnostics when severity range is supplied', function() eq( { 2, 3, 2 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 3), - make_info("Ignored information", 1, 1, 2, 3), - make_hint("Here's a hint", 1, 1, 2, 3), - }) + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 1, 1, 2, 3), + _G.make_info('Ignored information', 1, 1, 2, 3), + _G.make_hint("Here's a hint", 1, 1, 2, 3), + }) - return { - #vim.diagnostic.get(diagnostic_bufnr, { severity = {min=vim.diagnostic.severity.WARN} }), - #vim.diagnostic.get(diagnostic_bufnr, { severity = {max=vim.diagnostic.severity.WARN} }), - #vim.diagnostic.get(diagnostic_bufnr, { - severity = { - min=vim.diagnostic.severity.INFO, - max=vim.diagnostic.severity.WARN, - } - }), - } - ]] + return { + #vim.diagnostic.get( + _G.diagnostic_bufnr, + { severity = { min = vim.diagnostic.severity.WARN } } + ), + #vim.diagnostic.get( + _G.diagnostic_bufnr, + { severity = { max = vim.diagnostic.severity.WARN } } + ), + #vim.diagnostic.get(_G.diagnostic_bufnr, { + severity = { + min = vim.diagnostic.severity.INFO, + max = vim.diagnostic.severity.WARN, + }, + }), + } + end) ) end) it('returns only requested diagnostics when severities are supplied', function() eq( { 1, 1, 2 }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 3), - make_info("Ignored information", 1, 1, 2, 3), - make_hint("Here's a hint", 1, 1, 2, 3), - }) + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 1, 1, 2, 3), + _G.make_info('Ignored information', 1, 1, 2, 3), + _G.make_hint("Here's a hint", 1, 1, 2, 3), + }) - return { - #vim.diagnostic.get(diagnostic_bufnr, { severity = {vim.diagnostic.severity.WARN} }), - #vim.diagnostic.get(diagnostic_bufnr, { severity = {vim.diagnostic.severity.ERROR} }), - #vim.diagnostic.get(diagnostic_bufnr, { - severity = { - vim.diagnostic.severity.INFO, - vim.diagnostic.severity.WARN, - } - }), - } - ]] + return { + #vim.diagnostic.get( + _G.diagnostic_bufnr, + { severity = { vim.diagnostic.severity.WARN } } + ), + #vim.diagnostic.get( + _G.diagnostic_bufnr, + { severity = { vim.diagnostic.severity.ERROR } } + ), + #vim.diagnostic.get(_G.diagnostic_bufnr, { + severity = { + vim.diagnostic.severity.INFO, + vim.diagnostic.severity.WARN, + }, + }), + } + end) ) end) it('allows filtering by line', function() eq( 2, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 3), - make_info("Ignored information", 1, 1, 2, 3), - make_error("Error On Other Line", 3, 1, 3, 5), - }) + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 1, 1, 2, 3), + _G.make_info('Ignored information', 1, 1, 2, 3), + _G.make_error('Error On Other Line', 3, 1, 3, 5), + }) - return #vim.diagnostic.get(diagnostic_bufnr, {lnum = 2}) - ]] + return #vim.diagnostic.get(_G.diagnostic_bufnr, { lnum = 2 }) + end) ) end) end) @@ -1344,35 +1520,35 @@ describe('vim.diagnostic', function() [vim.diagnostic.severity.INFO] = 2, [vim.diagnostic.severity.HINT] = 1, }]], - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 2), - make_error("Error 2", 1, 3, 1, 4), - make_error("Error 3", 1, 5, 1, 6), - make_error("Error 4", 1, 7, 1, 8), - make_warning("Warning 1", 2, 1, 2, 2), - make_warning("Warning 2", 2, 3, 2, 4), - make_warning("Warning 3", 2, 5, 2, 6), - make_info("Info 1", 3, 1, 3, 2), - make_info("Info 2", 3, 3, 3, 4), - make_hint("Hint 1", 4, 1, 4, 2), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 2), + _G.make_error('Error 2', 1, 3, 1, 4), + _G.make_error('Error 3', 1, 5, 1, 6), + _G.make_error('Error 4', 1, 7, 1, 8), + _G.make_warning('Warning 1', 2, 1, 2, 2), + _G.make_warning('Warning 2', 2, 3, 2, 4), + _G.make_warning('Warning 3', 2, 5, 2, 6), + _G.make_info('Info 1', 3, 1, 3, 2), + _G.make_info('Info 2', 3, 3, 3, 4), + _G.make_hint('Hint 1', 4, 1, 4, 2), }) - return vim.diagnostic.count(diagnostic_bufnr) - ]] + return vim.diagnostic.count(_G.diagnostic_bufnr) + end) ) eq( exec_lua [[return { [vim.diagnostic.severity.ERROR] = 2, [vim.diagnostic.severity.INFO] = 1, }]], - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 2), - make_error("Error 2", 1, 3, 1, 4), - make_info("Info 1", 3, 1, 3, 2), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 2), + _G.make_error('Error 2', 1, 3, 1, 4), + _G.make_info('Info 1', 3, 1, 3, 2), }) - return vim.diagnostic.count(diagnostic_bufnr) - ]] + return vim.diagnostic.count(_G.diagnostic_bufnr) + end) ) end) @@ -1383,25 +1559,31 @@ describe('vim.diagnostic', function() { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1, [vim.diagnostic.severity.HINT] = 1 }, { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1 }, }]], - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 3), - make_info("Ignored information", 1, 1, 2, 3), - make_hint("Here's a hint", 1, 1, 2, 3), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 1, 1, 2, 3), + _G.make_info('Ignored information', 1, 1, 2, 3), + _G.make_hint("Here's a hint", 1, 1, 2, 3), }) return { - vim.diagnostic.count(diagnostic_bufnr, { severity = {min=vim.diagnostic.severity.WARN} }), - vim.diagnostic.count(diagnostic_bufnr, { severity = {max=vim.diagnostic.severity.WARN} }), - vim.diagnostic.count(diagnostic_bufnr, { + vim.diagnostic.count( + _G.diagnostic_bufnr, + { severity = { min = vim.diagnostic.severity.WARN } } + ), + vim.diagnostic.count( + _G.diagnostic_bufnr, + { severity = { max = vim.diagnostic.severity.WARN } } + ), + vim.diagnostic.count(_G.diagnostic_bufnr, { severity = { - min=vim.diagnostic.severity.INFO, - max=vim.diagnostic.severity.WARN, - } + min = vim.diagnostic.severity.INFO, + max = vim.diagnostic.severity.WARN, + }, }), } - ]] + end) ) end) @@ -1412,25 +1594,31 @@ describe('vim.diagnostic', function() { [vim.diagnostic.severity.ERROR] = 1 }, { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1 }, }]], - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 3), - make_info("Ignored information", 1, 1, 2, 3), - make_hint("Here's a hint", 1, 1, 2, 3), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 1, 1, 2, 3), + _G.make_info('Ignored information', 1, 1, 2, 3), + _G.make_hint("Here's a hint", 1, 1, 2, 3), }) return { - vim.diagnostic.count(diagnostic_bufnr, { severity = {vim.diagnostic.severity.WARN} }), - vim.diagnostic.count(diagnostic_bufnr, { severity = {vim.diagnostic.severity.ERROR} }), - vim.diagnostic.count(diagnostic_bufnr, { + vim.diagnostic.count( + _G.diagnostic_bufnr, + { severity = { vim.diagnostic.severity.WARN } } + ), + vim.diagnostic.count( + _G.diagnostic_bufnr, + { severity = { vim.diagnostic.severity.ERROR } } + ), + vim.diagnostic.count(_G.diagnostic_bufnr, { severity = { vim.diagnostic.severity.INFO, vim.diagnostic.severity.WARN, - } + }, }), } - ]] + end) ) end) @@ -1440,16 +1628,16 @@ describe('vim.diagnostic', function() [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1, }]], - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 3), - make_info("Ignored information", 1, 1, 2, 3), - make_error("Error On Other Line", 3, 1, 3, 5), + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 1, 1, 1, 5), + _G.make_warning('Warning on Server 1', 1, 1, 2, 3), + _G.make_info('Ignored information', 1, 1, 2, 3), + _G.make_error('Error On Other Line', 3, 1, 3, 5), }) - return vim.diagnostic.count(diagnostic_bufnr, {lnum = 2}) - ]] + return vim.diagnostic.count(_G.diagnostic_bufnr, { lnum = 2 }) + end) ) end) end) @@ -1458,137 +1646,138 @@ describe('vim.diagnostic', function() it('works with global, namespace, and ephemeral options', function() eq( 1, - exec_lua [[ - vim.diagnostic.config({ - virtual_text = false, - }) + exec_lua(function() + vim.diagnostic.config({ + virtual_text = false, + }) - vim.diagnostic.config({ - virtual_text = true, - underline = false, - }, diagnostic_ns) + vim.diagnostic.config({ + virtual_text = true, + underline = false, + }, _G.diagnostic_ns) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Some Error', 4, 4, 4, 4), - }) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Some Error', 4, 4, 4, 4), + }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]] + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end) ) eq( 1, - exec_lua [[ - vim.diagnostic.config({ - virtual_text = false, - }) + exec_lua(function() + vim.diagnostic.config({ + virtual_text = false, + }) - vim.diagnostic.config({ - virtual_text = false, - underline = false, - }, diagnostic_ns) + vim.diagnostic.config({ + virtual_text = false, + underline = false, + }, _G.diagnostic_ns) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Some Error', 4, 4, 4, 4), - }, {virtual_text = true}) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Some Error', 4, 4, 4, 4), + }, { virtual_text = true }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]] + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end) ) eq( 0, - exec_lua [[ - vim.diagnostic.config({ - virtual_text = false, - }) + exec_lua(function() + vim.diagnostic.config({ + virtual_text = false, + }) - vim.diagnostic.config({ - virtual_text = {severity=vim.diagnostic.severity.ERROR}, - underline = false, - }, diagnostic_ns) + vim.diagnostic.config({ + virtual_text = { severity = vim.diagnostic.severity.ERROR }, + underline = false, + }, _G.diagnostic_ns) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning('Some Warning', 4, 4, 4, 4), - }, {virtual_text = true}) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Some Warning', 4, 4, 4, 4), + }, { virtual_text = true }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]] + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end) ) eq( 1, - exec_lua [[ - vim.diagnostic.config({ - virtual_text = false, - }) + exec_lua(function() + vim.diagnostic.config({ + virtual_text = false, + }) - vim.diagnostic.config({ - virtual_text = {severity=vim.diagnostic.severity.ERROR}, - underline = false, - }, diagnostic_ns) + vim.diagnostic.config({ + virtual_text = { severity = vim.diagnostic.severity.ERROR }, + underline = false, + }, _G.diagnostic_ns) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning('Some Warning', 4, 4, 4, 4), - }, { - virtual_text = {} -- An empty table uses default values - }) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Some Warning', 4, 4, 4, 4), + }, { + virtual_text = {}, -- An empty table uses default values + }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]] + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end) ) end) it('can use functions for config values', function() - exec_lua [[ + exec_lua(function() vim.diagnostic.config({ - virtual_text = function() return true end, - }, diagnostic_ns) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Delayed Diagnostic', 4, 4, 4, 4), + virtual_text = function() + return true + end, + }, _G.diagnostic_ns) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Delayed Diagnostic', 4, 4, 4, 4), }) - ]] + end) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(2, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) + eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) -- Now, don't enable virtual text. -- We should have one less extmark displayed. - exec_lua [[ + exec_lua(function() vim.diagnostic.config({ - virtual_text = function() return false end, - }, diagnostic_ns) - ]] + virtual_text = function() + return false + end, + }, _G.diagnostic_ns) + end) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(1, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) + eq(1, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) end) it('allows filtering by severity', function() local get_extmark_count_with_severity = function(min_severity) - return exec_lua( - [[ + return exec_lua(function(min_severity0) vim.diagnostic.config({ underline = false, virtual_text = { - severity = {min=...}, + severity = { min = min_severity0 }, }, }) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning('Delayed Diagnostic', 4, 4, 4, 4), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Delayed Diagnostic', 4, 4, 4, 4), }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]], - min_severity - ) + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end, min_severity) end -- No messages with Error or higher @@ -1600,152 +1789,158 @@ describe('vim.diagnostic', function() end) it('allows sorting by severity', function() - exec_lua [[ + exec_lua(function() vim.diagnostic.config({ underline = false, signs = true, virtual_text = true, }) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning('Warning', 4, 4, 4, 4), - make_error('Error', 4, 4, 4, 4), - make_info('Info', 4, 4, 4, 4), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Warning', 4, 4, 4, 4), + _G.make_error('Error', 4, 4, 4, 4), + _G.make_info('Info', 4, 4, 4, 4), }) - function get_virt_text_and_signs(severity_sort) + function _G.get_virt_text_and_signs(severity_sort) vim.diagnostic.config({ severity_sort = severity_sort, }) - local virt_text = get_virt_text_extmarks(diagnostic_ns)[1][4].virt_text + local virt_text = _G.get_virt_text_extmarks(_G.diagnostic_ns)[1][4].virt_text local virt_texts = {} for i = 2, #virt_text - 1 do - table.insert(virt_texts, (string.gsub(virt_text[i][2], "DiagnosticVirtualText", ""))) + table.insert(virt_texts, (string.gsub(virt_text[i][2], 'DiagnosticVirtualText', ''))) end - local ns = vim.diagnostic.get_namespace(diagnostic_ns) + local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns) local sign_ns = ns.user_data.sign_ns local signs = {} - local all_signs = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, sign_ns, 0, -1, {type = 'sign', details = true}) + local all_signs = vim.api.nvim_buf_get_extmarks( + _G.diagnostic_bufnr, + sign_ns, + 0, + -1, + { type = 'sign', details = true } + ) table.sort(all_signs, function(a, b) return a[1] > b[1] end) for _, v in ipairs(all_signs) do - local s = v[4].sign_hl_group:gsub('DiagnosticSign', "") + local s = v[4].sign_hl_group:gsub('DiagnosticSign', '') if not vim.tbl_contains(signs, s) then signs[#signs + 1] = s end end - return {virt_texts, signs} + return { virt_texts, signs } end - ]] + end) - local result = exec_lua [[return get_virt_text_and_signs(false)]] + local result = exec_lua [[return _G.get_virt_text_and_signs(false)]] -- Virt texts are defined lowest priority to highest, signs from -- highest to lowest eq({ 'Warn', 'Error', 'Info' }, result[1]) eq({ 'Info', 'Error', 'Warn' }, result[2]) - result = exec_lua [[return get_virt_text_and_signs(true)]] + result = exec_lua [[return _G.get_virt_text_and_signs(true)]] eq({ 'Info', 'Warn', 'Error' }, result[1]) eq({ 'Error', 'Warn', 'Info' }, result[2]) - result = exec_lua [[return get_virt_text_and_signs({ reverse = true })]] + result = exec_lua [[return _G.get_virt_text_and_signs({ reverse = true })]] eq({ 'Error', 'Warn', 'Info' }, result[1]) eq({ 'Info', 'Warn', 'Error' }, result[2]) end) it('can show diagnostic sources in virtual text', function() - local result = exec_lua [[ + local result = exec_lua(function() local diagnostics = { - make_error('Some error', 0, 0, 0, 0, 'source x'), + _G.make_error('Some error', 0, 0, 0, 0, 'source x'), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, { underline = false, virtual_text = { prefix = '', source = 'always', - } + }, }) - local extmarks = get_virt_text_extmarks(diagnostic_ns) + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) local virt_text = extmarks[1][4].virt_text[3][1] return virt_text - ]] + end) eq(' source x: Some error', result) - result = exec_lua [[ + result = exec_lua(function() vim.diagnostic.config({ underline = false, virtual_text = { prefix = '', source = 'if_many', - } - }, diagnostic_ns) + }, + }, _G.diagnostic_ns) - local extmarks = get_virt_text_extmarks(diagnostic_ns) + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) local virt_text = extmarks[1][4].virt_text[3][1] return virt_text - ]] + end) eq(' Some error', result) - result = exec_lua [[ + result = exec_lua(function() local diagnostics = { - make_error('Some error', 0, 0, 0, 0, 'source x'), - make_error('Another error', 1, 1, 1, 1, 'source y'), + _G.make_error('Some error', 0, 0, 0, 0, 'source x'), + _G.make_error('Another error', 1, 1, 1, 1, 'source y'), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, { underline = false, virtual_text = { prefix = '', source = 'if_many', - } + }, }) - local extmarks = get_virt_text_extmarks(diagnostic_ns) - local virt_text = {extmarks[1][4].virt_text[3][1], extmarks[2][4].virt_text[3][1]} + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) + local virt_text = { extmarks[1][4].virt_text[3][1], extmarks[2][4].virt_text[3][1] } return virt_text - ]] + end) eq(' source x: Some error', result[1]) eq(' source y: Another error', result[2]) end) it('supports a format function for diagnostic messages', function() - local result = exec_lua [[ + local result = exec_lua(function() vim.diagnostic.config({ underline = false, virtual_text = { prefix = '', format = function(diagnostic) if diagnostic.severity == vim.diagnostic.severity.ERROR then - return string.format("🔥 %s", diagnostic.message) + return string.format('🔥 %s', diagnostic.message) end - return string.format("👀 %s", diagnostic.message) + return string.format('👀 %s', diagnostic.message) end, - } + }, }) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning('Warning', 0, 0, 0, 0), - make_error('Error', 1, 0, 1, 0), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Warning', 0, 0, 0, 0), + _G.make_error('Error', 1, 0, 1, 0), }) - local extmarks = get_virt_text_extmarks(diagnostic_ns) - return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} - ]] + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) + return { extmarks[1][4].virt_text, extmarks[2][4].virt_text } + end) eq(' 👀 Warning', result[1][3][1]) eq(' 🔥 Error', result[2][3][1]) end) it('includes source for formatted diagnostics', function() - local result = exec_lua [[ + local result = exec_lua(function() vim.diagnostic.config({ underline = false, virtual_text = { @@ -1753,21 +1948,21 @@ describe('vim.diagnostic', function() source = 'always', format = function(diagnostic) if diagnostic.severity == vim.diagnostic.severity.ERROR then - return string.format("🔥 %s", diagnostic.message) + return string.format('🔥 %s', diagnostic.message) end - return string.format("👀 %s", diagnostic.message) + return string.format('👀 %s', diagnostic.message) end, - } + }, }) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning('Warning', 0, 0, 0, 0, 'some_linter'), - make_error('Error', 1, 0, 1, 0, 'another_linter'), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Warning', 0, 0, 0, 0, 'some_linter'), + _G.make_error('Error', 1, 0, 1, 0, 'another_linter'), }) - local extmarks = get_virt_text_extmarks(diagnostic_ns) - return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} - ]] + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) + return { extmarks[1][4].virt_text, extmarks[2][4].virt_text } + end) eq(' some_linter: 👀 Warning', result[1][3][1]) eq(' another_linter: 🔥 Error', result[2][3][1]) end) @@ -1775,90 +1970,94 @@ describe('vim.diagnostic', function() it('can add a prefix to virtual text', function() eq( 'E Some error', - exec_lua [[ - local diagnostics = { - make_error('Some error', 0, 0, 0, 0), - } - - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { - underline = false, - virtual_text = { - prefix = 'E', - suffix = '', + exec_lua(function() + local diagnostics = { + _G.make_error('Some error', 0, 0, 0, 0), } - }) - local extmarks = get_virt_text_extmarks(diagnostic_ns) - local prefix = extmarks[1][4].virt_text[2][1] - local message = extmarks[1][4].virt_text[3][1] - return prefix .. message - ]] + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, { + underline = false, + virtual_text = { + prefix = 'E', + suffix = '', + }, + }) + + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) + local prefix = extmarks[1][4].virt_text[2][1] + local message = extmarks[1][4].virt_text[3][1] + return prefix .. message + end) ) eq( '[(1/1) err-code] Some error', - exec_lua [[ - local diagnostics = { - make_error('Some error', 0, 0, 0, 0, nil, 'err-code'), - } - - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { - underline = false, - virtual_text = { - prefix = function(diag, i, total) return string.format('[(%d/%d) %s]', i, total, diag.code) end, - suffix = '', + exec_lua(function() + local diagnostics = { + _G.make_error('Some error', 0, 0, 0, 0, nil, 'err-code'), } - }) - local extmarks = get_virt_text_extmarks(diagnostic_ns) - local prefix = extmarks[1][4].virt_text[2][1] - local message = extmarks[1][4].virt_text[3][1] - return prefix .. message - ]] + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, { + underline = false, + virtual_text = { + prefix = function(diag, i, total) + return string.format('[(%d/%d) %s]', i, total, diag.code) + end, + suffix = '', + }, + }) + + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) + local prefix = extmarks[1][4].virt_text[2][1] + local message = extmarks[1][4].virt_text[3][1] + return prefix .. message + end) ) end) it('can add a suffix to virtual text', function() eq( ' Some error ✘', - exec_lua [[ - local diagnostics = { - make_error('Some error', 0, 0, 0, 0), - } - - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { - underline = false, - virtual_text = { - prefix = '', - suffix = ' ✘', + exec_lua(function() + local diagnostics = { + _G.make_error('Some error', 0, 0, 0, 0), } - }) - local extmarks = get_virt_text_extmarks(diagnostic_ns) - local virt_text = extmarks[1][4].virt_text[3][1] - return virt_text - ]] + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, { + underline = false, + virtual_text = { + prefix = '', + suffix = ' ✘', + }, + }) + + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) + local virt_text = extmarks[1][4].virt_text[3][1] + return virt_text + end) ) eq( ' Some error [err-code]', - exec_lua [[ - local diagnostics = { - make_error('Some error', 0, 0, 0, 0, nil, 'err-code'), - } - - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { - underline = false, - virtual_text = { - prefix = '', - suffix = function(diag) return string.format(' [%s]', diag.code) end, + exec_lua(function() + local diagnostics = { + _G.make_error('Some error', 0, 0, 0, 0, nil, 'err-code'), } - }) - local extmarks = get_virt_text_extmarks(diagnostic_ns) - local virt_text = extmarks[1][4].virt_text[3][1] - return virt_text - ]] + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, { + underline = false, + virtual_text = { + prefix = '', + suffix = function(diag) + return string.format(' [%s]', diag.code) + end, + }, + }) + + local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns) + local virt_text = extmarks[1][4].virt_text[3][1] + return virt_text + end) ) end) end) @@ -1872,80 +2071,80 @@ describe('vim.diagnostic', function() end) it('can perform updates after insert_leave', function() - exec_lua [[vim.api.nvim_set_current_buf(diagnostic_bufnr)]] + exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]] api.nvim_input('o') eq({ mode = 'i', blocking = false }, api.nvim_get_mode()) -- Save the diagnostics - exec_lua [[ + exec_lua(function() vim.diagnostic.config({ update_in_insert = false, }) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Delayed Diagnostic', 4, 4, 4, 4), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Delayed Diagnostic', 4, 4, 4, 4), }) - ]] + end) -- No diagnostics displayed yet. eq({ mode = 'i', blocking = false }, api.nvim_get_mode()) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(0, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) + eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) api.nvim_input('<esc>') eq({ mode = 'n', blocking = false }, api.nvim_get_mode()) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(2, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) + eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) end) it('does not perform updates when not needed', function() - exec_lua [[vim.api.nvim_set_current_buf(diagnostic_bufnr)]] + exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]] api.nvim_input('o') eq({ mode = 'i', blocking = false }, api.nvim_get_mode()) -- Save the diagnostics - exec_lua [[ + exec_lua(function() vim.diagnostic.config({ update_in_insert = false, virtual_text = true, }) - DisplayCount = 0 + _G.DisplayCount = 0 local set_virtual_text = vim.diagnostic.handlers.virtual_text.show vim.diagnostic.handlers.virtual_text.show = function(...) - DisplayCount = DisplayCount + 1 + _G.DisplayCount = _G.DisplayCount + 1 return set_virtual_text(...) end - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Delayed Diagnostic', 4, 4, 4, 4), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Delayed Diagnostic', 4, 4, 4, 4), }) - ]] + end) -- No diagnostics displayed yet. eq({ mode = 'i', blocking = false }, api.nvim_get_mode()) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(0, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) - eq(0, exec_lua [[return DisplayCount]]) + eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) + eq(0, exec_lua [[return _G.DisplayCount]]) api.nvim_input('<esc>') eq({ mode = 'n', blocking = false }, api.nvim_get_mode()) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(2, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) - eq(1, exec_lua [[return DisplayCount]]) + eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) + eq(1, exec_lua [[return _G.DisplayCount]]) -- Go in and out of insert mode one more time. api.nvim_input('o') @@ -1955,52 +2154,51 @@ describe('vim.diagnostic', function() eq({ mode = 'n', blocking = false }, api.nvim_get_mode()) -- Should not have set the virtual text again. - eq(1, exec_lua [[return DisplayCount]]) + eq(1, exec_lua [[return _G.DisplayCount]]) end) it('never sets virtual text, in combination with insert leave', function() - exec_lua [[vim.api.nvim_set_current_buf(diagnostic_bufnr)]] + exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]] api.nvim_input('o') eq({ mode = 'i', blocking = false }, api.nvim_get_mode()) -- Save the diagnostics - exec_lua [[ + exec_lua(function() vim.diagnostic.config({ update_in_insert = false, virtual_text = false, }) - - DisplayCount = 0 + _G.DisplayCount = 0 local set_virtual_text = vim.diagnostic.handlers.virtual_text.show vim.diagnostic.handlers.virtual_text.show = function(...) - DisplayCount = DisplayCount + 1 + _G.DisplayCount = _G.DisplayCount + 1 return set_virtual_text(...) end - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Delayed Diagnostic', 4, 4, 4, 4), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Delayed Diagnostic', 4, 4, 4, 4), }) - ]] + end) -- No diagnostics displayed yet. eq({ mode = 'i', blocking = false }, api.nvim_get_mode()) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(0, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) - eq(0, exec_lua [[return DisplayCount]]) + eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) + eq(0, exec_lua [[return _G.DisplayCount]]) api.nvim_input('<esc>') eq({ mode = 'n', blocking = false }, api.nvim_get_mode()) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(1, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) - eq(0, exec_lua [[return DisplayCount]]) + eq(1, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) + eq(0, exec_lua [[return _G.DisplayCount]]) -- Go in and out of insert mode one more time. api.nvim_input('o') @@ -2010,124 +2208,136 @@ describe('vim.diagnostic', function() eq({ mode = 'n', blocking = false }, api.nvim_get_mode()) -- Should not have set the virtual text still. - eq(0, exec_lua [[return DisplayCount]]) + eq(0, exec_lua [[return _G.DisplayCount]]) end) it('can perform updates while in insert mode, if desired', function() - exec_lua [[vim.api.nvim_set_current_buf(diagnostic_bufnr)]] + exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]] api.nvim_input('o') eq({ mode = 'i', blocking = false }, api.nvim_get_mode()) -- Save the diagnostics - exec_lua [[ + exec_lua(function() vim.diagnostic.config({ update_in_insert = true, }) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Delayed Diagnostic', 4, 4, 4, 4), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Delayed Diagnostic', 4, 4, 4, 4), }) - ]] + end) -- Diagnostics are displayed, because the user wanted them that way! eq({ mode = 'i', blocking = false }, api.nvim_get_mode()) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(2, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) + eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) api.nvim_input('<esc>') eq({ mode = 'n', blocking = false }, api.nvim_get_mode()) eq( 1, - exec_lua [[return count_diagnostics(diagnostic_bufnr, vim.diagnostic.severity.ERROR, diagnostic_ns)]] + exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] ) - eq(2, exec_lua [[return count_extmarks(diagnostic_bufnr, diagnostic_ns)]]) + eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]]) end) it('can set diagnostics without displaying them', function() eq( 0, - exec_lua [[ - vim.diagnostic.enable(false, { bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), - }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]] + exec_lua(function() + vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), + }) + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end) ) eq( 2, - exec_lua [[ - vim.diagnostic.enable(true, { bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]] + exec_lua(function() + vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }) + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end) ) end) it('can set display options', function() eq( 0, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), - }, { virtual_text = false, underline = false }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]] + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), + }, { virtual_text = false, underline = false }) + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end) ) eq( 1, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), - }, { virtual_text = true, underline = false }) - return count_extmarks(diagnostic_bufnr, diagnostic_ns) - ]] + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1), + }, { virtual_text = true, underline = false }) + return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns) + end) ) end) it('sets and clears signs #26193 #26555', function() do - local result = exec_lua [[ + local result = exec_lua(function() vim.diagnostic.config({ signs = true, }) local diagnostics = { - make_error('Error', 1, 1, 1, 2), - make_warning('Warning', 3, 3, 3, 3), + _G.make_error('Error', 1, 1, 1, 2), + _G.make_warning('Warning', 3, 3, 3, 3), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) - local ns = vim.diagnostic.get_namespace(diagnostic_ns) + local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns) local sign_ns = ns.user_data.sign_ns - local signs = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, sign_ns, 0, -1, {type ='sign', details = true}) + local signs = vim.api.nvim_buf_get_extmarks( + _G.diagnostic_bufnr, + sign_ns, + 0, + -1, + { type = 'sign', details = true } + ) local result = {} for _, s in ipairs(signs) do result[#result + 1] = { lnum = s[2] + 1, name = s[4].sign_hl_group } end return result - ]] + end) eq({ 2, 'DiagnosticSignError' }, { result[1].lnum, result[1].name }) eq({ 4, 'DiagnosticSignWarn' }, { result[2].lnum, result[2].name }) end do - local result = exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {}) + local result = exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {}) - local ns = vim.diagnostic.get_namespace(diagnostic_ns) + local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns) local sign_ns = ns.user_data.sign_ns - return vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, sign_ns, 0, -1, {type ='sign', details = true}) - ]] + return vim.api.nvim_buf_get_extmarks( + _G.diagnostic_bufnr, + sign_ns, + 0, + -1, + { type = 'sign', details = true } + ) + end) eq({}, result) end @@ -2142,22 +2352,28 @@ describe('vim.diagnostic', function() n.command('sign define DiagnosticSignInfo text= texthl= linehl=Underlined numhl=Underlined') n.command('sign define DiagnosticSignHint text= texthl= linehl=Underlined numhl=Underlined') - local result = exec_lua [[ + local result = exec_lua(function() vim.diagnostic.config({ signs = true, }) local diagnostics = { - make_error('Error', 1, 1, 1, 2), - make_warning('Warning', 3, 3, 3, 3), + _G.make_error('Error', 1, 1, 1, 2), + _G.make_warning('Warning', 3, 3, 3, 3), } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) - local ns = vim.diagnostic.get_namespace(diagnostic_ns) + local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns) local sign_ns = ns.user_data.sign_ns - local signs = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, sign_ns, 0, -1, {type ='sign', details = true}) + local signs = vim.api.nvim_buf_get_extmarks( + _G.diagnostic_bufnr, + sign_ns, + 0, + -1, + { type = 'sign', details = true } + ) local result = {} for _, s in ipairs(signs) do result[#result + 1] = { @@ -2169,7 +2385,7 @@ describe('vim.diagnostic', function() } end return result - ]] + end) eq({ lnum = 2, @@ -2193,65 +2409,67 @@ describe('vim.diagnostic', function() it('can display a header', function() eq( { 'Diagnostics:', '1. Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float() - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float() + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { "We're no strangers to love...", '1. Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({header = "We're no strangers to love..."}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = + vim.diagnostic.open_float({ header = "We're no strangers to love..." }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { 'You know the rules', '1. Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({header = {'You know the rules', 'Search'}}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = + vim.diagnostic.open_float({ header = { 'You know the rules', 'Search' } }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) end) it('can show diagnostics from the whole buffer', function() eq( { '1. Syntax error', '2. Some warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - make_warning("Some warning", 1, 1, 1, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({header = false, scope="buffer"}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + _G.make_warning('Some warning', 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) end) @@ -2259,69 +2477,70 @@ describe('vim.diagnostic', function() -- Using cursor position eq( { '1. Some warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - make_warning("Some warning", 1, 1, 1, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {2, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float({header=false}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + _G.make_warning('Some warning', 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 2, 1 }) + local float_bufnr, winnr = vim.diagnostic.open_float({ header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- With specified position eq( { '1. Some warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - make_warning("Some warning", 1, 1, 1, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {1, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float({header=false, pos=1}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + _G.make_warning('Some warning', 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, pos = 1 }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- End position is exclusive eq( vim.NIL, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 1, 1, 2, 0), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {1, 1}) - local _, winnr = vim.diagnostic.open_float(0, {header=false, pos={2,0}}) - return winnr - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 1, 1, 2, 0), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + local _, winnr = vim.diagnostic.open_float(0, { header = false, pos = { 2, 0 } }) + return winnr + end) ) -- Works when width == 0 eq( { '1. Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 2, 0, 2, 0), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {1, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, pos={2,1}}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 2, 0, 2, 0), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + local float_bufnr, winnr = + vim.diagnostic.open_float(0, { header = false, pos = { 2, 1 } }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) end) @@ -2329,87 +2548,94 @@ describe('vim.diagnostic', function() -- Using cursor position eq( { 'Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 1, 1, 1, 3), - make_warning("Some warning", 1, 3, 1, 4), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {2, 2}) - local float_bufnr, winnr = vim.diagnostic.open_float({header=false, scope="cursor"}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 1, 1, 1, 3), + _G.make_warning('Some warning', 1, 3, 1, 4), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 2, 2 }) + local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'cursor' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- With specified position eq( { 'Some warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 1, 1, 1, 3), - make_warning("Some warning", 1, 3, 1, 4), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {1, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float({header=false, scope="cursor", pos={1,3}}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 1, 1, 1, 3), + _G.make_warning('Some warning', 1, 3, 1, 4), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + local float_bufnr, winnr = + vim.diagnostic.open_float({ header = false, scope = 'cursor', pos = { 1, 3 } }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- With column position past the end of the line. #16062 eq( { 'Syntax error' }, - exec_lua [[ - local first_line_len = #vim.api.nvim_buf_get_lines(diagnostic_bufnr, 0, 1, true)[1] - local diagnostics = { - make_error("Syntax error", 0, first_line_len + 1, 1, 0), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {1, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float({header=false, scope="cursor", pos={0,first_line_len}}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local first_line_len = #vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, 1, true)[1] + local diagnostics = { + _G.make_error('Syntax error', 0, first_line_len + 1, 1, 0), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + local float_bufnr, winnr = vim.diagnostic.open_float({ + header = false, + scope = 'cursor', + pos = { 0, first_line_len }, + }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- End position is exclusive eq( vim.NIL, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 1, 1, 1, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {1, 1}) - local _, winnr = vim.diagnostic.open_float(0, {header=false, scope="cursor", pos={1,3}}) - return winnr - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + local _, winnr = + vim.diagnostic.open_float(0, { header = false, scope = 'cursor', pos = { 1, 3 } }) + return winnr + end) ) -- Works when width == 0 eq( { 'Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 2, 0, 2, 0), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - vim.api.nvim_win_set_cursor(0, {1, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float({header=false, scope="cursor", pos={2,1}}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 2, 0, 2, 0), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + local float_bufnr, winnr = + vim.diagnostic.open_float({ header = false, scope = 'cursor', pos = { 2, 1 } }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) end) @@ -2421,17 +2647,17 @@ describe('vim.diagnostic', function() -- 1. <msg> eq( 2, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return #lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines + end) ) end ) @@ -2439,43 +2665,44 @@ describe('vim.diagnostic', function() it('only reports diagnostics from the current buffer when bufnr is omitted #15710', function() eq( 2, - exec_lua [[ - local other_bufnr = vim.api.nvim_create_buf(true, false) - local buf_1_diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - local buf_2_diagnostics = { - make_warning("Some warning", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, buf_1_diagnostics) - vim.diagnostic.set(other_ns, other_bufnr, buf_2_diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float() - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return #lines - ]] + exec_lua(function() + local other_bufnr = vim.api.nvim_create_buf(true, false) + local buf_1_diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + local buf_2_diagnostics = { + _G.make_warning('Some warning', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, buf_1_diagnostics) + vim.diagnostic.set(_G.other_ns, other_bufnr, buf_2_diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float() + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines + end) ) end) it('allows filtering by namespace', function() eq( 2, - exec_lua [[ - local ns_1_diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - local ns_2_diagnostics = { - make_warning("Some warning", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diagnostics) - vim.diagnostic.set(other_ns, diagnostic_bufnr, ns_2_diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {namespace = diagnostic_ns}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return #lines - ]] + exec_lua(function() + local ns_1_diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + local ns_2_diagnostics = { + _G.make_warning('Some warning', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diagnostics) + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diagnostics) + local float_bufnr, winnr = + vim.diagnostic.open_float(_G.diagnostic_bufnr, { namespace = _G.diagnostic_ns }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines + end) ) end) @@ -2486,17 +2713,18 @@ describe('vim.diagnostic', function() -- 1. <msg> eq( 1, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {header = false}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return #lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = + vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines + end) ) end ) @@ -2504,138 +2732,141 @@ describe('vim.diagnostic', function() it('clamps diagnostic line numbers within the valid range', function() eq( 1, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 6, 0, 6, 0), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {header = false, pos = 5}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return #lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 6, 0, 6, 0), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = + vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false, pos = 5 }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return #lines + end) ) end) it('can show diagnostic source', function() - exec_lua [[vim.api.nvim_win_set_buf(0, diagnostic_bufnr)]] + exec_lua [[vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)]] eq( { '1. Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3, "source x"), - } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { - header = false, - source = "if_many", - }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3, 'source x'), + } + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, { + header = false, + source = 'if_many', + }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { '1. source x: Syntax error' }, - exec_lua [[ - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { - header = false, - source = "always", - }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, { + header = false, + source = 'always', + }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { '1. source x: Syntax error', '2. source y: Another error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3, "source x"), - make_error("Another error", 0, 1, 0, 3, "source y"), - } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { - header = false, - source = "if_many", - }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3, 'source x'), + _G.make_error('Another error', 0, 1, 0, 3, 'source y'), + } + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, { + header = false, + source = 'if_many', + }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) end) it('respects severity_sort', function() - exec_lua [[vim.api.nvim_win_set_buf(0, diagnostic_bufnr)]] + exec_lua [[vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)]] eq( { '1. Syntax error', '2. Info', '3. Error', '4. Warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - make_info('Info', 0, 3, 0, 4), - make_error('Error', 0, 2, 0, 2), - make_warning('Warning', 0, 0, 0, 1), - } + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + _G.make_info('Info', 0, 3, 0, 4), + _G.make_error('Error', 0, 2, 0, 2), + _G.make_warning('Warning', 0, 0, 0, 1), + } - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) - vim.diagnostic.config({severity_sort = false}) + vim.diagnostic.config({ severity_sort = false }) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + local float_bufnr, winnr = + vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { '1. Syntax error', '2. Error', '3. Warning', '4. Info' }, - exec_lua [[ - vim.diagnostic.config({severity_sort = true}) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + vim.diagnostic.config({ severity_sort = true }) + local float_bufnr, winnr = + vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { '1. Info', '2. Warning', '3. Error', '4. Syntax error' }, - exec_lua [[ - vim.diagnostic.config({severity_sort = { reverse = true } }) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + vim.diagnostic.config({ severity_sort = { reverse = true } }) + local float_bufnr, winnr = + vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) end) it('can filter by severity', function() local count_diagnostics_with_severity = function(min_severity, max_severity) - return exec_lua( - [[ - local min_severity, max_severity = ... + return exec_lua(function(min_severity0, max_severity0) vim.diagnostic.config({ float = { - severity = {min=min_severity, max=max_severity}, + severity = { min = min_severity0, max = max_severity0 }, }, }) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error("Syntax error", 0, 1, 0, 3), - make_info('Info', 0, 3, 0, 4), - make_error('Error', 0, 2, 0, 2), - make_warning('Warning', 0, 0, 0, 1), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Syntax error', 0, 1, 0, 3), + _G.make_info('Info', 0, 3, 0, 4), + _G.make_error('Error', 0, 2, 0, 2), + _G.make_warning('Warning', 0, 0, 0, 1), }) - local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false }) + local float_bufnr, winnr = + vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false }) if not float_bufnr then return 0 end @@ -2643,10 +2874,7 @@ describe('vim.diagnostic', function() local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) vim.api.nvim_win_close(winnr, true) return #lines - ]], - min_severity, - max_severity - ) + end, min_severity, max_severity) end eq(2, count_diagnostics_with_severity('ERROR')) @@ -2660,83 +2888,84 @@ describe('vim.diagnostic', function() -- Default is to add a number eq( { '1. Syntax error', '2. Some warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - make_warning("Some warning", 1, 1, 1, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({header = false, scope = "buffer"}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + _G.make_warning('Some warning', 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { 'Syntax error', 'Some warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - make_warning("Some warning", 1, 1, 1, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({header = false, scope = "buffer", prefix = ""}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + _G.make_warning('Some warning', 1, 1, 1, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = + vim.diagnostic.open_float({ header = false, scope = 'buffer', prefix = '' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { '1. Syntax error', '2. Some warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - make_warning("Some warning", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({ - header = false, - prefix = function(_, i, total) - -- Only show a number if there is more than one diagnostic - if total > 1 then - return string.format("%d. ", i) - end - return "" - end, - }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + _G.make_warning('Some warning', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float({ + header = false, + prefix = function(_, i, total) + -- Only show a number if there is more than one diagnostic + if total > 1 then + return string.format('%d. ', i) + end + return '' + end, + }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { 'Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({ - header = false, - prefix = function(_, i, total) - -- Only show a number if there is more than one diagnostic - if total > 1 then - return string.format("%d. ", i) - end - return "" - end, - }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float({ + header = false, + prefix = function(_, i, total) + -- Only show a number if there is more than one diagnostic + if total > 1 then + return string.format('%d. ', i) + end + return '' + end, + }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( @@ -2749,50 +2978,51 @@ describe('vim.diagnostic', function() -- Default is to render the diagnostic error code eq( { '1. Syntax error [code-x]', '2. Some warning [code-y]' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3, nil, "code-x"), - make_warning("Some warning", 1, 1, 1, 3, nil, "code-y"), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({header = false, scope = "buffer"}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3, nil, 'code-x'), + _G.make_warning('Some warning', 1, 1, 1, 3, nil, 'code-y'), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( { '1. Syntax error', '2. Some warning' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3, nil, "code-x"), - make_warning("Some warning", 1, 1, 1, 3, nil, "code-y"), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({header = false, scope = "buffer", suffix = ""}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3, nil, 'code-x'), + _G.make_warning('Some warning', 1, 1, 1, 3, nil, 'code-y'), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = + vim.diagnostic.open_float({ header = false, scope = 'buffer', suffix = '' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- Suffix is rendered on the last line of a multiline diagnostic eq( { '1. Syntax error', ' More context [code-x]' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error\nMore context", 0, 1, 0, 3, nil, "code-x"), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float({header = false, scope = "buffer"}) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error\nMore context', 0, 1, 0, 3, nil, 'code-x'), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) eq( @@ -2804,132 +3034,134 @@ describe('vim.diagnostic', function() it('works with the old signature', function() eq( { '1. Syntax error' }, - exec_lua [[ - local diagnostics = { - make_error("Syntax error", 0, 1, 0, 3), - } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + local diagnostics = { + _G.make_error('Syntax error', 0, 1, 0, 3), + } + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) end) it('works for multi-line diagnostics #21949', function() -- create diagnostic - exec_lua [[ + exec_lua(function() local diagnostics = { - make_error("Error in two lines lnum is 1 and end_lnum is 2", 1, 1, 2, 3), + _G.make_error('Error in two lines lnum is 1 and end_lnum is 2', 1, 1, 2, 3), } - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics) - ]] + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics) + end) -- open float failed non diagnostic lnum eq( vim.NIL, - exec_lua [[ - vim.api.nvim_win_set_cursor(0, {1, 0}) - local _, winnr = vim.diagnostic.open_float(0, { header = false }) - return winnr - ]] + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + local _, winnr = vim.diagnostic.open_float(0, { header = false }) + return winnr + end) ) eq( vim.NIL, - exec_lua [[ - vim.api.nvim_win_set_cursor(0, {1, 0}) - local _, winnr = vim.diagnostic.open_float(0, { header = false, scope = "cursor" }) - return winnr - ]] + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + local _, winnr = vim.diagnostic.open_float(0, { header = false, scope = 'cursor' }) + return winnr + end) ) -- can open a float window on lnum 1 eq( { '1. Error in two lines lnum is 1 and end_lnum is 2' }, - exec_lua [[ - vim.api.nvim_win_set_cursor(0, {2, 0}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- can open a cursor-scoped float window on lnum 1 eq( { 'Error in two lines lnum is 1 and end_lnum is 2' }, - exec_lua [[ - vim.api.nvim_win_set_cursor(0, {2, 1}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false, scope = "cursor" }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 2, 1 }) + local float_bufnr, winnr = + vim.diagnostic.open_float(0, { header = false, scope = 'cursor' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- can open a float window on end_lnum 2 eq( { '1. Error in two lines lnum is 1 and end_lnum is 2' }, - exec_lua [[ - vim.api.nvim_win_set_cursor(0, {3, 0}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 3, 0 }) + local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) -- can open a cursor-scoped float window on end_lnum 2 eq( { 'Error in two lines lnum is 1 and end_lnum is 2' }, - exec_lua [[ - vim.api.nvim_win_set_cursor(0, {3, 2}) - local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false, scope = "cursor" }) - local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) - vim.api.nvim_win_close(winnr, true) - return lines - ]] + exec_lua(function() + vim.api.nvim_win_set_cursor(0, { 3, 2 }) + local float_bufnr, winnr = + vim.diagnostic.open_float(0, { header = false, scope = 'cursor' }) + local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false) + vim.api.nvim_win_close(winnr, true) + return lines + end) ) end) end) describe('setloclist()', function() it('sets diagnostics in lnum order', function() - local loc_list = exec_lua [[ - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + local loc_list = exec_lua(function() + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Farther Diagnostic', 4, 4, 4, 4), - make_error('Lower Diagnostic', 1, 1, 1, 1), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Farther Diagnostic', 4, 4, 4, 4), + _G.make_error('Lower Diagnostic', 1, 1, 1, 1), }) vim.diagnostic.setloclist() return vim.fn.getloclist(0) - ]] + end) assert(loc_list[1].lnum < loc_list[2].lnum) end) it('sets diagnostics in lnum order, regardless of namespace', function() - local loc_list = exec_lua [[ - vim.api.nvim_win_set_buf(0, diagnostic_bufnr) + local loc_list = exec_lua(function() + vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Lower Diagnostic', 1, 1, 1, 1), + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Lower Diagnostic', 1, 1, 1, 1), }) - vim.diagnostic.set(other_ns, diagnostic_bufnr, { - make_warning('Farther Diagnostic', 4, 4, 4, 4), + vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, { + _G.make_warning('Farther Diagnostic', 4, 4, 4, 4), }) vim.diagnostic.setloclist() return vim.fn.getloclist(0) - ]] + end) assert(loc_list[1].lnum < loc_list[2].lnum) end) @@ -2948,22 +3180,23 @@ describe('vim.diagnostic', function() } eq( diagnostic, - exec_lua( - [[ - return vim.diagnostic.match(..., "^(%w+): [^:]+:(%d+):(%d+):(.+)$", {"severity", "lnum", "col", "message"}) - ]], - msg - ) + exec_lua(function(msg0) + return vim.diagnostic.match( + msg0, + '^(%w+): [^:]+:(%d+):(%d+):(.+)$', + { 'severity', 'lnum', 'col', 'message' } + ) + end, msg) ) end) it('returns nil if the pattern fails to match', function() eq( NIL, - exec_lua [[ - local msg = "The answer to life, the universe, and everything is" - return vim.diagnostic.match(msg, "This definitely will not match", {}) - ]] + exec_lua(function() + local msg = 'The answer to life, the universe, and everything is' + return vim.diagnostic.match(msg, 'This definitely will not match', {}) + end) ) end) @@ -2979,12 +3212,15 @@ describe('vim.diagnostic', function() } eq( diagnostic, - exec_lua( - [[ - return vim.diagnostic.match(..., "^[^:]+:(%d+):(.+)$", {"lnum", "message"}, nil, {severity = vim.diagnostic.severity.INFO}) - ]], - msg - ) + exec_lua(function(msg0) + return vim.diagnostic.match( + msg0, + '^[^:]+:(%d+):(.+)$', + { 'lnum', 'message' }, + nil, + { severity = vim.diagnostic.severity.INFO } + ) + end, msg) ) end) @@ -3000,38 +3236,40 @@ describe('vim.diagnostic', function() } eq( diagnostic, - exec_lua( - [[ - return vim.diagnostic.match(..., "^(%d+):(%w+):(.+)$", {"lnum", "severity", "message"}, {FATAL = vim.diagnostic.severity.ERROR}) - ]], - msg - ) + exec_lua(function(msg0) + return vim.diagnostic.match( + msg0, + '^(%d+):(%w+):(.+)$', + { 'lnum', 'severity', 'message' }, + { FATAL = vim.diagnostic.severity.ERROR } + ) + end, msg) ) end) end) describe('toqflist() and fromqflist()', function() it('works', function() - local result = exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Error 1', 0, 1, 0, 1), - make_error('Error 2', 1, 1, 1, 1), - make_warning('Warning', 2, 2, 2, 2), - }) + local result = exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error 1', 0, 1, 0, 1), + _G.make_error('Error 2', 1, 1, 1, 1), + _G.make_warning('Warning', 2, 2, 2, 2), + }) - local diagnostics = vim.diagnostic.get(diagnostic_bufnr) - vim.fn.setqflist(vim.diagnostic.toqflist(diagnostics)) - local list = vim.fn.getqflist() - local new_diagnostics = vim.diagnostic.fromqflist(list) + local diagnostics = vim.diagnostic.get(_G.diagnostic_bufnr) + vim.fn.setqflist(vim.diagnostic.toqflist(diagnostics)) + local list = vim.fn.getqflist() + local new_diagnostics = vim.diagnostic.fromqflist(list) - -- Remove namespace since it isn't present in the return value of - -- fromlist() - for _, v in ipairs(diagnostics) do - v.namespace = nil - end + -- Remove namespace since it isn't present in the return value of + -- fromlist() + for _, v in ipairs(diagnostics) do + v.namespace = nil + end - return {diagnostics, new_diagnostics} - ]] + return { diagnostics, new_diagnostics } + end) eq(result[1], result[2]) end) end) @@ -3051,179 +3289,181 @@ describe('vim.diagnostic', function() it('can add new handlers', function() eq( true, - exec_lua [[ - local handler_called = false - vim.diagnostic.handlers.test = { - show = function(namespace, bufnr, diagnostics, opts) - assert(namespace == diagnostic_ns) - assert(bufnr == diagnostic_bufnr) - assert(#diagnostics == 1) - assert(opts.test.some_opt == 42) - handler_called = true - end, - } + exec_lua(function() + local handler_called = false + vim.diagnostic.handlers.test = { + show = function(namespace, bufnr, diagnostics, opts) + assert(namespace == _G.diagnostic_ns) + assert(bufnr == _G.diagnostic_bufnr) + assert(#diagnostics == 1) + assert(opts.test.some_opt == 42) + handler_called = true + end, + } - vim.diagnostic.config({test = {some_opt = 42}}) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning("Warning", 0, 0, 0, 0), - }) - return handler_called - ]] + vim.diagnostic.config({ test = { some_opt = 42 } }) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Warning', 0, 0, 0, 0), + }) + return handler_called + end) ) end) it('can disable handlers by setting the corresponding option to false', function() eq( false, - exec_lua [[ - local handler_called = false - vim.diagnostic.handlers.test = { - show = function(namespace, bufnr, diagnostics, opts) - handler_called = true - end, - } + exec_lua(function() + local handler_called = false + vim.diagnostic.handlers.test = { + show = function(_, _, _, _) + handler_called = true + end, + } - vim.diagnostic.config({test = false}) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning("Warning", 0, 0, 0, 0), - }) - return handler_called - ]] + vim.diagnostic.config({ test = false }) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Warning', 0, 0, 0, 0), + }) + return handler_called + end) ) end) it("always calls a handler's hide function if defined", function() eq( { false, true }, - exec_lua [[ - local hide_called = false - local show_called = false - vim.diagnostic.handlers.test = { - show = function(namespace, bufnr, diagnostics, opts) - show_called = true - end, - hide = function(namespace, bufnr) - assert(namespace == diagnostic_ns) - assert(bufnr == diagnostic_bufnr) - hide_called = true - end, - } + exec_lua(function() + local hide_called = false + local show_called = false + vim.diagnostic.handlers.test = { + show = function(_, _, _, _) + show_called = true + end, + hide = function(namespace, bufnr) + assert(namespace == _G.diagnostic_ns) + assert(bufnr == _G.diagnostic_bufnr) + hide_called = true + end, + } - vim.diagnostic.config({test = false}) - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_warning("Warning", 0, 0, 0, 0), - }) - vim.diagnostic.hide(diagnostic_ns, diagnostic_bufnr) - return {show_called, hide_called} - ]] + vim.diagnostic.config({ test = false }) + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_warning('Warning', 0, 0, 0, 0), + }) + vim.diagnostic.hide(_G.diagnostic_ns, _G.diagnostic_bufnr) + return { show_called, hide_called } + end) ) end) it('triggers the autocommand when diagnostics are set', function() eq( { true, true }, - exec_lua [[ - -- Set a different buffer as current to test that <abuf> is being set properly in - -- DiagnosticChanged callbacks - local tmp = vim.api.nvim_create_buf(false, true) - vim.api.nvim_set_current_buf(tmp) - - local triggered = {} - vim.api.nvim_create_autocmd('DiagnosticChanged', { - callback = function(args) - triggered = {args.buf, #args.data.diagnostics} - end, - }) - vim.api.nvim_buf_set_name(diagnostic_bufnr, "test | test") - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic', 0, 0, 0, 0) - }) - return { - triggered[1] == diagnostic_bufnr, - triggered[2] == 1, - } - ]] + exec_lua(function() + -- Set a different buffer as current to test that <abuf> is being set properly in + -- DiagnosticChanged callbacks + local tmp = vim.api.nvim_create_buf(false, true) + vim.api.nvim_set_current_buf(tmp) + + local triggered = {} + vim.api.nvim_create_autocmd('DiagnosticChanged', { + callback = function(args) + triggered = { args.buf, #args.data.diagnostics } + end, + }) + vim.api.nvim_buf_set_name(_G.diagnostic_bufnr, 'test | test') + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic', 0, 0, 0, 0), + }) + return { + triggered[1] == _G.diagnostic_bufnr, + triggered[2] == 1, + } + end) ) end) it('triggers the autocommand when diagnostics are cleared', function() eq( true, - exec_lua [[ - local tmp = vim.api.nvim_create_buf(false, true) - vim.api.nvim_set_current_buf(tmp) - vim.g.diagnostic_autocmd_triggered = 0 - vim.cmd('autocmd DiagnosticChanged * let g:diagnostic_autocmd_triggered = +expand("<abuf>")') - vim.api.nvim_buf_set_name(diagnostic_bufnr, "test | test") - vim.diagnostic.reset(diagnostic_ns, diagnostic_bufnr) - return vim.g.diagnostic_autocmd_triggered == diagnostic_bufnr - ]] + exec_lua(function() + local tmp = vim.api.nvim_create_buf(false, true) + vim.api.nvim_set_current_buf(tmp) + vim.g.diagnostic_autocmd_triggered = 0 + vim.cmd( + 'autocmd DiagnosticChanged * let g:diagnostic_autocmd_triggered = +expand("<abuf>")' + ) + vim.api.nvim_buf_set_name(_G.diagnostic_bufnr, 'test | test') + vim.diagnostic.reset(_G.diagnostic_ns, _G.diagnostic_bufnr) + return vim.g.diagnostic_autocmd_triggered == _G.diagnostic_bufnr + end) ) end) it('is_enabled', function() eq( { false, false, false, false, false }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - }) - vim.api.nvim_set_current_buf(diagnostic_bufnr) - vim.diagnostic.enable(false) - return { - vim.diagnostic.is_enabled(), - vim.diagnostic.is_enabled{ bufnr = 0 }, - vim.diagnostic.is_enabled{ bufnr = diagnostic_bufnr }, - vim.diagnostic.is_enabled{ bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }, - vim.diagnostic.is_enabled{ bufnr = 0, ns_id = diagnostic_ns }, - } - ]] + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + }) + vim.api.nvim_set_current_buf(_G.diagnostic_bufnr) + vim.diagnostic.enable(false) + return { + vim.diagnostic.is_enabled(), + vim.diagnostic.is_enabled { bufnr = 0 }, + vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr }, + vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }, + vim.diagnostic.is_enabled { bufnr = 0, ns_id = _G.diagnostic_ns }, + } + end) ) eq( { true, true, true, true, true }, - exec_lua [[ - vim.diagnostic.enable() - return { - vim.diagnostic.is_enabled(), - vim.diagnostic.is_enabled{ bufnr = 0 }, - vim.diagnostic.is_enabled{ bufnr = diagnostic_bufnr }, - vim.diagnostic.is_enabled{ bufnr = diagnostic_bufnr, ns_id = diagnostic_ns }, - vim.diagnostic.is_enabled{ bufnr = 0, ns_id = diagnostic_ns }, - } - ]] + exec_lua(function() + vim.diagnostic.enable() + return { + vim.diagnostic.is_enabled(), + vim.diagnostic.is_enabled { bufnr = 0 }, + vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr }, + vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns }, + vim.diagnostic.is_enabled { bufnr = 0, ns_id = _G.diagnostic_ns }, + } + end) ) end) it('is_disabled (deprecated)', function() eq( { true, true, true, true }, - exec_lua [[ - vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { - make_error('Diagnostic #1', 1, 1, 1, 1), - }) - vim.api.nvim_set_current_buf(diagnostic_bufnr) - vim.diagnostic.disable() - return { - vim.diagnostic.is_disabled(), - vim.diagnostic.is_disabled(diagnostic_bufnr), - vim.diagnostic.is_disabled(diagnostic_bufnr, diagnostic_ns), - vim.diagnostic.is_disabled(_, diagnostic_ns), - } - ]] + exec_lua(function() + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Diagnostic #1', 1, 1, 1, 1), + }) + vim.api.nvim_set_current_buf(_G.diagnostic_bufnr) + vim.diagnostic.disable() + return { + vim.diagnostic.is_disabled(), + vim.diagnostic.is_disabled(_G.diagnostic_bufnr), + vim.diagnostic.is_disabled(_G.diagnostic_bufnr, _G.diagnostic_ns), + vim.diagnostic.is_disabled(0, _G.diagnostic_ns), + } + end) ) eq( { false, false, false, false }, - exec_lua [[ - vim.diagnostic.enable() - return { - vim.diagnostic.is_disabled(), - vim.diagnostic.is_disabled(diagnostic_bufnr), - vim.diagnostic.is_disabled(diagnostic_bufnr, diagnostic_ns), - vim.diagnostic.is_disabled(_, diagnostic_ns), - } - ]] + exec_lua(function() + vim.diagnostic.enable() + return { + vim.diagnostic.is_disabled(), + vim.diagnostic.is_disabled(_G.diagnostic_bufnr), + vim.diagnostic.is_disabled(_G.diagnostic_bufnr, _G.diagnostic_ns), + vim.diagnostic.is_disabled(0, _G.diagnostic_ns), + } + end) ) end) end) diff --git a/test/functional/lua/filetype_spec.lua b/test/functional/lua/filetype_spec.lua index 7db04e6f6b..b5eb9fab23 100644 --- a/test/functional/lua/filetype_spec.lua +++ b/test/functional/lua/filetype_spec.lua @@ -18,90 +18,82 @@ describe('vim.filetype', function() before_each(function() clear() - exec_lua [[ + exec_lua(function() local bufnr = vim.api.nvim_create_buf(true, false) vim.api.nvim_set_current_buf(bufnr) - ]] + end) end) it('works with extensions', function() eq( 'radicalscript', - exec_lua [[ - vim.filetype.add({ - extension = { - rs = 'radicalscript', - }, - }) - return vim.filetype.match({ filename = 'main.rs' }) - ]] + exec_lua(function() + vim.filetype.add({ + extension = { + rs = 'radicalscript', + }, + }) + return vim.filetype.match({ filename = 'main.rs' }) + end) ) end) it('prioritizes filenames over extensions', function() eq( 'somethingelse', - exec_lua [[ - vim.filetype.add({ - extension = { - rs = 'radicalscript', - }, - filename = { - ['main.rs'] = 'somethingelse', - }, - }) - return vim.filetype.match({ filename = 'main.rs' }) - ]] + exec_lua(function() + vim.filetype.add({ + extension = { + rs = 'radicalscript', + }, + filename = { + ['main.rs'] = 'somethingelse', + }, + }) + return vim.filetype.match({ filename = 'main.rs' }) + end) ) end) it('works with filenames', function() eq( 'nim', - exec_lua [[ - vim.filetype.add({ - filename = { - ['s_O_m_e_F_i_l_e'] = 'nim', - }, - }) - return vim.filetype.match({ filename = 's_O_m_e_F_i_l_e' }) - ]] + exec_lua(function() + vim.filetype.add({ + filename = { + ['s_O_m_e_F_i_l_e'] = 'nim', + }, + }) + return vim.filetype.match({ filename = 's_O_m_e_F_i_l_e' }) + end) ) eq( 'dosini', - exec_lua( - [[ - local root = ... - vim.filetype.add({ - filename = { - ['config'] = 'toml', - [root .. '/.config/fun/config'] = 'dosini', - }, - }) - return vim.filetype.match({ filename = root .. '/.config/fun/config' }) - ]], - root - ) + exec_lua(function(root0) + vim.filetype.add({ + filename = { + ['config'] = 'toml', + [root0 .. '/.config/fun/config'] = 'dosini', + }, + }) + return vim.filetype.match({ filename = root0 .. '/.config/fun/config' }) + end, root) ) end) it('works with patterns', function() eq( 'markdown', - exec_lua( - [[ - local root = ... - vim.env.HOME = '/a-funky+home%dir' - vim.filetype.add({ - pattern = { - ['~/blog/.*%.txt'] = 'markdown', - } - }) - return vim.filetype.match({ filename = '~/blog/why_neovim_is_awesome.txt' }) - ]], - root - ) + exec_lua(function() + vim.env.HOME = '/a-funky+home%dir' + vim.filetype.add({ + pattern = { + ['~/blog/.*%.txt'] = 'markdown', + }, + }) + return vim.filetype.match({ filename = '~/blog/why_neovim_is_awesome.txt' }) + end) ) end) @@ -110,43 +102,43 @@ describe('vim.filetype', function() command('file relevant_to_me') eq( 'foss', - exec_lua [[ - vim.filetype.add({ - pattern = { - ["relevant_to_(%a+)"] = function(path, bufnr, capture) - if capture == "me" then - return "foss" - end - end, - } - }) - return vim.filetype.match({ buf = 0 }) - ]] + exec_lua(function() + vim.filetype.add({ + pattern = { + ['relevant_to_(%a+)'] = function(_, _, capture) + if capture == 'me' then + return 'foss' + end + end, + }, + }) + return vim.filetype.match({ buf = 0 }) + end) ) end) it('works with contents #22180', function() eq( 'sh', - exec_lua [[ - -- Needs to be set so detect#sh doesn't fail - vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$' - return vim.filetype.match({ contents = { '#!/usr/bin/env bash' } }) - ]] + exec_lua(function() + -- Needs to be set so detect#sh doesn't fail + vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$' + return vim.filetype.match({ contents = { '#!/usr/bin/env bash' } }) + end) ) end) it('considers extension mappings when matching from hashbang', function() eq( 'fooscript', - exec_lua [[ - vim.filetype.add({ - extension = { - foo = 'fooscript', - } - }) - return vim.filetype.match({ contents = { '#!/usr/bin/env foo' } }) - ]] + exec_lua(function() + vim.filetype.add({ + extension = { + foo = 'fooscript', + }, + }) + return vim.filetype.match({ contents = { '#!/usr/bin/env foo' } }) + end) ) end) diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index aba02ab01b..4848787ed2 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -141,19 +141,14 @@ describe('vim.fs', function() it('works', function() eq( true, - exec_lua( - [[ - local dir, nvim = ... - for name, type in vim.fs.dir(dir) do - if name == nvim and type == 'file' then - return true + exec_lua(function(dir, nvim) + for name, type in vim.fs.dir(dir) do + if name == nvim and type == 'file' then + return true + end end - end - return false - ]], - nvim_dir, - nvim_prog_basename - ) + return false + end, nvim_dir, nvim_prog_basename) ) end) @@ -172,27 +167,21 @@ describe('vim.fs', function() io.open('testd/a/b/c/c4', 'w'):close() local function run(dir, depth, skip) - local r = exec_lua( - [[ - local dir, depth, skip = ... + local r = exec_lua(function(dir0, depth0, skip0) local r = {} local skip_f - if skip then - skip_f = function(n) - if vim.tbl_contains(skip or {}, n) then + if skip0 then + skip_f = function(n0) + if vim.tbl_contains(skip0 or {}, n0) then return false end end end - for name, type_ in vim.fs.dir(dir, { depth = depth, skip = skip_f }) do + for name, type_ in vim.fs.dir(dir0, { depth = depth0, skip = skip_f }) do r[name] = type_ end return r - ]], - dir, - depth, - skip - ) + end, dir, depth, skip) return r end @@ -263,13 +252,9 @@ describe('vim.fs', function() opts = { path = test_source_path .. '/contrib', limit = math.huge } eq( - exec_lua( - [[ - local dir = ... - return vim.tbl_map(vim.fs.basename, vim.fn.glob(dir..'/contrib/*', false, true)) - ]], - test_source_path - ), + exec_lua(function(dir) + return vim.tbl_map(vim.fs.basename, vim.fn.glob(dir .. '/contrib/*', false, true)) + end, test_source_path), vim.tbl_map( vim.fs.basename, vim.fs.find(function(_, d) @@ -299,11 +284,11 @@ describe('vim.fs', function() it('works with a function', function() ---@type string - local result = exec_lua([[ - return vim.fs.root(0, function(name, path) + local result = exec_lua(function() + return vim.fs.root(0, function(name, _) return name:match('%.txt$') end) - ]]) + end) eq(vim.fs.joinpath(test_source_path, 'test/functional/fixtures'), result) end) @@ -352,13 +337,10 @@ describe('vim.fs', function() local xdg_config_home = test_build_dir .. '/.config' eq( xdg_config_home .. '/nvim', - exec_lua( - [[ - vim.env.XDG_CONFIG_HOME = ... - return vim.fs.normalize('$XDG_CONFIG_HOME/nvim') - ]], - xdg_config_home - ) + exec_lua(function(...) + vim.env.XDG_CONFIG_HOME = ... + return vim.fs.normalize('$XDG_CONFIG_HOME/nvim') + end, xdg_config_home) ) end) diff --git a/test/functional/lua/glob_spec.lua b/test/functional/lua/glob_spec.lua index b95d874bb5..6f1e5be501 100644 --- a/test/functional/lua/glob_spec.lua +++ b/test/functional/lua/glob_spec.lua @@ -9,14 +9,9 @@ describe('glob', function() after_each(n.clear) local match = function(...) - return exec_lua( - [[ - local pattern = select(1, ...) - local str = select(2, ...) - return require("vim.glob").to_lpeg(pattern):match(str) ~= nil - ]], - ... - ) + return exec_lua(function(pattern, str) + return require('vim.glob').to_lpeg(pattern):match(str) ~= nil + end, ...) end describe('glob matching', function() diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index a7ba851215..83db4f303c 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -57,31 +57,30 @@ describe('LSP', function() -- Run an instance of nvim on the file which contains our "scripts". -- Pass TEST_NAME to pick the script. local test_name = 'basic_init' - exec_lua( - [=[ - lsp = require('vim.lsp') - local test_name, fake_lsp_code, fake_lsp_logfile = ... - function test__start_client() - return lsp.start_client { + exec_lua(function(test_name0, fake_lsp_code0, fake_lsp_logfile0) + _G.lsp = require('vim.lsp') + function _G.test__start_client() + return vim.lsp.start_client { cmd_env = { - NVIM_LOG_FILE = fake_lsp_logfile; - NVIM_APPNAME = "nvim_lsp_test"; - }; + NVIM_LOG_FILE = fake_lsp_logfile0, + NVIM_APPNAME = 'nvim_lsp_test', + }, cmd = { - vim.v.progpath, '-l', fake_lsp_code, test_name; - }; - workspace_folders = {{ + vim.v.progpath, + '-l', + fake_lsp_code0, + test_name0, + }, + workspace_folders = { + { uri = 'file://' .. vim.uv.cwd(), name = 'test_folder', - }}; + }, + }, } end - TEST_CLIENT1 = test__start_client() - ]=], - test_name, - fake_lsp_code, - fake_lsp_logfile - ) + _G.TEST_CLIENT1 = _G.test__start_client() + end, test_name, fake_lsp_code, fake_lsp_logfile) end) after_each(function() @@ -96,17 +95,17 @@ describe('LSP', function() end) eq( 2, - exec_lua([[ - TEST_CLIENT2 = test__start_client() - return TEST_CLIENT2 - ]]) + exec_lua(function() + _G.TEST_CLIENT2 = _G.test__start_client() + return _G.TEST_CLIENT2 + end) ) eq( 3, - exec_lua([[ - TEST_CLIENT3 = test__start_client() - return TEST_CLIENT3 - ]]) + exec_lua(function() + _G.TEST_CLIENT3 = _G.test__start_client() + return _G.TEST_CLIENT3 + end) ) retry(nil, 4000, function() eq(3, exec_lua('return #lsp.get_clients()')) @@ -127,10 +126,10 @@ describe('LSP', function() end) it('stop_client() also works on client objects', function() - exec_lua([[ - TEST_CLIENT2 = test__start_client() - TEST_CLIENT3 = test__start_client() - ]]) + exec_lua(function() + _G.TEST_CLIENT2 = _G.test__start_client() + _G.TEST_CLIENT3 = _G.test__start_client() + end) retry(nil, 4000, function() eq(3, exec_lua('return #lsp.get_clients()')) end) @@ -221,28 +220,28 @@ describe('LSP', function() function() clear() exec_lua(create_server_definition) - local result = exec_lua([[ - local server = _create_server({ - capabilities = { - positionEncoding = "utf-8" - }, - }) + local result = exec_lua(function() + local server = _G._create_server({ + capabilities = { + positionEncoding = 'utf-8', + }, + }) - local client_id = vim.lsp.start({ - name = 'dummy', - cmd = server.cmd, - }) + local client_id = vim.lsp.start({ + name = 'dummy', + cmd = server.cmd, + }) - if not client_id then - return 'vim.lsp.start did not return client_id' - end + if not client_id then + return 'vim.lsp.start did not return client_id' + end - local client = vim.lsp.get_client_by_id(client_id) - if not client then - return 'No client found with id ' .. client_id - end - return client.offset_encoding - ]]) + local client = vim.lsp.get_client_by_id(client_id) + if not client then + return 'No client found with id ' .. client_id + end + return client.offset_encoding + end) eq('utf-8', result) end ) @@ -285,14 +284,14 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_finish', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - ]] - eq(true, exec_lua('return lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)')) - eq(true, exec_lua('return lsp.buf_is_attached(BUFFER, TEST_RPC_CLIENT_ID)')) - exec_lua [[ - vim.api.nvim_command(BUFFER.."bwipeout") - ]] + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + end) + eq(true, exec_lua('return lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)')) + eq(true, exec_lua('return lsp.buf_is_attached(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)')) + exec_lua(function() + vim.api.nvim_command(_G.BUFFER .. 'bwipeout') + end) end, on_init = function(_client) client = _client @@ -305,8 +304,8 @@ describe('LSP', function() on_handler = function(err, result, ctx) eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') if ctx.method == 'finish' then - exec_lua('return lsp.buf_detach_client(BUFFER, TEST_RPC_CLIENT_ID)') - eq(false, exec_lua('return lsp.buf_is_attached(BUFFER, TEST_RPC_CLIENT_ID)')) + exec_lua('return vim.lsp.buf_detach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)') + eq(false, exec_lua('return vim.lsp.buf_is_attached(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)')) client.stop() end end, @@ -318,31 +317,31 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_init', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) + exec_lua(function() + _G.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 + local client0 = vim.lsp.get_client_by_id(args.data.client_id) + vim.g.lsp_attached = client0.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 + local client0 = vim.lsp.get_client_by_id(args.data.client_id) + vim.g.lsp_detached = client0.name end, }) - ]] + end) end, on_init = function(_client) client = _client - eq(true, exec_lua('return lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)')) + eq(true, exec_lua('return vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)')) client.notify('finish') end, on_handler = function(_, _, ctx) if ctx.method == 'finish' then eq('basic_init', api.nvim_get_var('lsp_attached')) - exec_lua('return lsp.buf_detach_client(BUFFER, TEST_RPC_CLIENT_ID)') + exec_lua('return vim.lsp.buf_detach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)') eq('basic_init', api.nvim_get_var('lsp_detached')) client.stop() end @@ -356,10 +355,10 @@ describe('LSP', function() test_name = 'set_defaults_all_capabilities', on_init = function(_client) client = _client - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID) - ]] + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) + end) end, on_handler = function(_, _, ctx) if ctx.method == 'test' then @@ -369,13 +368,13 @@ describe('LSP', function() eq('', get_buf_option('keywordprg')) eq( true, - exec_lua [[ - local keymap - vim._with({buf = BUFFER}, function() - keymap = vim.fn.maparg("K", "n", false, true) + exec_lua(function() + local keymap + vim._with({ buf = _G.BUFFER }, function() + keymap = vim.fn.maparg('K', 'n', false, true) + end) + return keymap.callback == vim.lsp.buf.hover end) - return keymap.callback == vim.lsp.buf.hover - ]] ) client.stop() end @@ -386,13 +385,13 @@ describe('LSP', function() eq('', get_buf_option('formatexpr')) eq( '', - exec_lua [[ - local keymap - vim._with({buf = BUFFER}, function() - keymap = vim.fn.maparg("K", "n", false, false) + exec_lua(function() + local keymap + vim._with({ buf = _G.BUFFER }, function() + keymap = vim.fn.maparg('K', 'n', false, false) + end) + return keymap end) - return keymap - ]] ) end, } @@ -404,36 +403,36 @@ describe('LSP', function() test_name = 'set_defaults_all_capabilities', on_init = function(_client) client = _client - exec_lua [[ + exec_lua(function() vim.api.nvim_command('filetype plugin on') - BUFFER_1 = vim.api.nvim_create_buf(false, true) - BUFFER_2 = vim.api.nvim_create_buf(false, true) - vim.api.nvim_set_option_value('filetype', 'man', { buf = BUFFER_1 }) - vim.api.nvim_set_option_value('filetype', 'xml', { buf = BUFFER_2 }) - ]] + _G.BUFFER_1 = vim.api.nvim_create_buf(false, true) + _G.BUFFER_2 = vim.api.nvim_create_buf(false, true) + vim.api.nvim_set_option_value('filetype', 'man', { buf = _G.BUFFER_1 }) + vim.api.nvim_set_option_value('filetype', 'xml', { buf = _G.BUFFER_2 }) + end) -- Sanity check to ensure that some values are set after setting filetype. - eq("v:lua.require'man'.goto_tag", get_buf_option('tagfunc', 'BUFFER_1')) - eq('xmlcomplete#CompleteTags', get_buf_option('omnifunc', 'BUFFER_2')) - eq('xmlformat#Format()', get_buf_option('formatexpr', 'BUFFER_2')) + eq("v:lua.require'man'.goto_tag", get_buf_option('tagfunc', '_G.BUFFER_1')) + eq('xmlcomplete#CompleteTags', get_buf_option('omnifunc', '_G.BUFFER_2')) + eq('xmlformat#Format()', get_buf_option('formatexpr', '_G.BUFFER_2')) - exec_lua [[ - lsp.buf_attach_client(BUFFER_1, TEST_RPC_CLIENT_ID) - lsp.buf_attach_client(BUFFER_2, TEST_RPC_CLIENT_ID) - ]] + exec_lua(function() + vim.lsp.buf_attach_client(_G.BUFFER_1, _G.TEST_RPC_CLIENT_ID) + vim.lsp.buf_attach_client(_G.BUFFER_2, _G.TEST_RPC_CLIENT_ID) + end) end, on_handler = function(_, _, ctx) if ctx.method == 'test' then - eq('v:lua.vim.lsp.tagfunc', get_buf_option('tagfunc', 'BUFFER_1')) - eq('v:lua.vim.lsp.omnifunc', get_buf_option('omnifunc', 'BUFFER_2')) - eq('v:lua.vim.lsp.formatexpr()', get_buf_option('formatexpr', 'BUFFER_2')) + eq('v:lua.vim.lsp.tagfunc', get_buf_option('tagfunc', '_G.BUFFER_1')) + eq('v:lua.vim.lsp.omnifunc', get_buf_option('omnifunc', '_G.BUFFER_2')) + eq('v:lua.vim.lsp.formatexpr()', get_buf_option('formatexpr', '_G.BUFFER_2')) client.stop() end end, on_exit = function(_, _) - eq('', get_buf_option('tagfunc', 'BUFFER_1')) - eq('', get_buf_option('omnifunc', 'BUFFER_2')) - eq('', get_buf_option('formatexpr', 'BUFFER_2')) + eq('', get_buf_option('tagfunc', '_G.BUFFER_1')) + eq('', get_buf_option('omnifunc', '_G.BUFFER_2')) + eq('', get_buf_option('formatexpr', '_G.BUFFER_2')) end, } end) @@ -444,13 +443,13 @@ describe('LSP', function() test_name = 'set_defaults_all_capabilities', on_init = function(_client) client = _client - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_set_option_value('tagfunc', 'tfu', { buf = BUFFER }) - vim.api.nvim_set_option_value('omnifunc', 'ofu', { buf = BUFFER }) - vim.api.nvim_set_option_value('formatexpr', 'fex', { buf = BUFFER }) - lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID) - ]] + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_set_option_value('tagfunc', 'tfu', { buf = _G.BUFFER }) + vim.api.nvim_set_option_value('omnifunc', 'ofu', { buf = _G.BUFFER }) + vim.api.nvim_set_option_value('formatexpr', 'fex', { buf = _G.BUFFER }) + vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) + end) end, on_handler = function(_, _, ctx) if ctx.method == 'test' then @@ -471,18 +470,18 @@ describe('LSP', function() it('should detach buffer on bufwipe', function() clear() exec_lua(create_server_definition) - local result = exec_lua([[ - local server = _create_server() + local result = exec_lua(function() + local server = _G._create_server() local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_set_current_buf(bufnr) local detach_called = false - vim.api.nvim_create_autocmd("LspDetach", { + vim.api.nvim_create_autocmd('LspDetach', { callback = function() detach_called = true - end + end, }) local client_id = vim.lsp.start({ name = 'detach-dummy', cmd = server.cmd }) - assert(client_id, "lsp.start must return client_id") + assert(client_id, 'lsp.start must return client_id') local client = vim.lsp.get_client_by_id(client_id) local num_attached_before = vim.tbl_count(client.attached_buffers) vim.api.nvim_buf_delete(bufnr, { force = true }) @@ -494,7 +493,7 @@ describe('LSP', function() num_attached_after = num_attached_after, detach_called = detach_called, } - ]]) + end) eq(true, result ~= nil, 'exec_lua must return result') eq(1, result.num_attached_before) eq(0, result.num_attached_after) @@ -504,15 +503,15 @@ describe('LSP', function() it('should not re-attach buffer if it was deleted in on_init #28575', function() clear() exec_lua(create_server_definition) - exec_lua([[ - local server = _create_server({ + exec_lua(function() + local server = _G._create_server({ handlers = { - initialize = function(method, params, callback) + initialize = function(_, _, callback) vim.schedule(function() callback(nil, { capabilities = {} }) end) - end - } + end, + }, }) local bufnr = vim.api.nvim_create_buf(false, true) local on_init_called = false @@ -522,40 +521,44 @@ describe('LSP', function() on_init = function() vim.api.nvim_buf_delete(bufnr, {}) on_init_called = true - end + end, }) vim.lsp.buf_attach_client(bufnr, client_id) - local ok = vim.wait(1000, function() return on_init_called end) - assert(ok, "on_init was not called") - ]]) + local ok = vim.wait(1000, function() + return on_init_called + end) + assert(ok, 'on_init was not called') + end) end) it('should allow on_lines + nvim_buf_delete during LSP initialization #28575', function() clear() exec_lua(create_server_definition) - exec_lua([[ + exec_lua(function() local initialized = false - local server = _create_server({ + local server = _G._create_server({ handlers = { - initialize = function(method, params, callback) + initialize = function(_, _, callback) vim.schedule(function() callback(nil, { capabilities = {} }) initialized = true end) - end - } + end, + }, }) local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_set_current_buf(bufnr) - local client_id = vim.lsp.start({ + vim.lsp.start({ name = 'detach-dummy', cmd = server.cmd, }) - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {"hello"}) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'hello' }) vim.api.nvim_buf_delete(bufnr, {}) - local ok = vim.wait(1000, function() return initialized end) - assert(ok, "lsp did not initialize") - ]]) + local ok = vim.wait(1000, function() + return initialized + end) + assert(ok, 'lsp did not initialize') + end) end) it('client should return settings via workspace/configuration handler', function() @@ -588,22 +591,23 @@ describe('LSP', function() on_handler = function(err, result, ctx) eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') if ctx.method == 'start' then - exec_lua([=[ - local client = vim.lsp.get_client_by_id(TEST_RPC_CLIENT_ID) - client.settings = { - testSetting1 = true; - testSetting2 = false; - test = {Setting3 = 'nested' }; - }]=]) + exec_lua(function() + local client0 = vim.lsp.get_client_by_id(_G.TEST_RPC_CLIENT_ID) + client0.settings = { + testSetting1 = true, + testSetting2 = false, + test = { Setting3 = 'nested' }, + } + end) end if ctx.method == 'workspace/configuration' then - local server_result = exec_lua( - [=[ - local method, params = ... - return require'vim.lsp.handlers'['workspace/configuration'](err, params, {method=method, client_id=TEST_RPC_CLIENT_ID})]=], - ctx.method, - result - ) + local server_result = exec_lua(function(method, params) + return require 'vim.lsp.handlers'['workspace/configuration']( + err, + params, + { method = method, client_id = _G.TEST_RPC_CLIENT_ID } + ) + end, ctx.method, result) client.notify('workspace/configuration', server_result) end if ctx.method == 'shutdown' then @@ -622,15 +626,19 @@ describe('LSP', function() c.stop() end, on_setup = function() - result = exec_lua [[ - local result = { - items = { - {section = 'foo'}, - {section = 'bar'}, + result = exec_lua(function() + local result0 = { + items = { + { section = 'foo' }, + { section = 'bar' }, + }, } - } - return vim.lsp.handlers['workspace/configuration'](nil, result, {client_id=TEST_RPC_CLIENT_ID}) - ]] + return vim.lsp.handlers['workspace/configuration']( + nil, + result0, + { client_id = _G.TEST_RPC_CLIENT_ID } + ) + end) end, } eq({ NIL, NIL }, result) @@ -678,11 +686,11 @@ describe('LSP', function() 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) - vim.api.nvim_exec_autocmds('BufWritePost', { buffer = BUFFER, modeline = false }) - ]=]) + exec_lua(function() + _G.BUFFER = vim.api.nvim_get_current_buf() + vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) + vim.api.nvim_exec_autocmds('BufWritePost', { buffer = _G.BUFFER, modeline = false }) + end) else client.stop() end @@ -693,13 +701,13 @@ describe('LSP', function() it('BufWritePre does not send notifications if server lacks willSave capabilities', function() clear() exec_lua(create_server_definition) - local messages = exec_lua([[ - local server = _create_server({ + local messages = exec_lua(function() + local server = _G._create_server({ capabilities = { textDocumentSync = { willSave = false, willSaveWaitUntil = false, - } + }, }, }) local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) @@ -707,7 +715,7 @@ describe('LSP', function() vim.api.nvim_exec_autocmds('BufWritePre', { buffer = buf, modeline = false }) vim.lsp.stop_client(client_id) return server.messages - ]]) + end) eq(4, #messages) eq('initialize', messages[1].method) eq('initialized', messages[2].method) @@ -718,13 +726,13 @@ describe('LSP', function() it('BufWritePre sends willSave / willSaveWaitUntil, applies textEdits', function() clear() exec_lua(create_server_definition) - local result = exec_lua([[ - local server = _create_server({ + local result = exec_lua(function() + local server = _G._create_server({ capabilities = { textDocumentSync = { willSave = true, willSaveWaitUntil = true, - } + }, }, handlers = { ['textDocument/willSaveWaitUntil'] = function(_, _, callback) @@ -733,10 +741,10 @@ describe('LSP', function() start = { line = 0, character = 0 }, ['end'] = { line = 0, character = 0 }, }, - newText = 'Hello' + newText = 'Hello', } - callback(nil, { text_edit, }) - end + callback(nil, { text_edit }) + end, }, }) local buf = vim.api.nvim_get_current_buf() @@ -745,9 +753,9 @@ describe('LSP', function() vim.lsp.stop_client(client_id) return { messages = server.messages, - lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) + lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true), } - ]]) + end) local messages = result.messages eq('textDocument/willSave', messages[3].method) eq('textDocument/willSaveWaitUntil', messages[4].method) @@ -775,18 +783,15 @@ describe('LSP', function() local tmpfile_old = tmpname() local tmpfile_new = 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._with({buf = BUFFER}, function() vim.cmd('saveas ' .. newname) end) - ]=], - tmpfile_old, - tmpfile_new - ) + exec_lua(function(oldname, newname) + _G.BUFFER = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_name(_G.BUFFER, oldname) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, true, { 'help me' }) + vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) + vim._with({ buf = _G.BUFFER }, function() + vim.cmd('saveas ' .. newname) + end) + end, tmpfile_old, tmpfile_new) else client.stop() end @@ -812,12 +817,12 @@ describe('LSP', function() 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) - vim.api.nvim_exec_autocmds('BufWritePost', { buffer = BUFFER, modeline = false }) - ]=]) + exec_lua(function() + _G.BUFFER = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, true, { 'help me' }) + vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) + vim.api.nvim_exec_autocmds('BufWritePost', { buffer = _G.BUFFER, modeline = false }) + end) else client.stop() end @@ -871,12 +876,12 @@ describe('LSP', function() test_rpc_server { test_name = 'capabilities_for_client_supports_method', on_setup = function() - exec_lua([=[ - BUFFER = vim.api.nvim_get_current_buf() - lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID) - vim.lsp.handlers['textDocument/typeDefinition'] = function() end - vim.cmd(BUFFER.."bwipeout") - ]=]) + exec_lua(function() + _G.BUFFER = vim.api.nvim_get_current_buf() + vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) + vim.lsp.handlers['textDocument/typeDefinition'] = function() end + vim.cmd(_G.BUFFER .. 'bwipeout') + end) end, on_init = function(client) client.stop() @@ -901,9 +906,9 @@ describe('LSP', function() test_rpc_server { test_name = 'capabilities_for_client_supports_method', on_setup = function() - exec_lua([=[ + exec_lua(function() vim.lsp.handlers['textDocument/typeDefinition'] = function() end - ]=]) + end) end, on_init = function(client) client.stop() @@ -990,7 +995,9 @@ describe('LSP', function() on_init = function(_client) client = _client client.request('slow_request') - local request = exec_lua([=[ return TEST_RPC_CLIENT.requests[2] ]=]) + local request = exec_lua(function() + return _G.TEST_RPC_CLIENT.requests[2] + end) eq('slow_request', request.method) eq('pending', request.type) client.notify('release') @@ -1003,7 +1010,9 @@ describe('LSP', function() on_handler = function(err, _, ctx) eq(table.remove(expected_handlers), { err, {}, ctx }, 'expected handler') if ctx.method == 'slow_request' then - local request = exec_lua([=[ return TEST_RPC_CLIENT.requests[2] ]=]) + local request = exec_lua(function() + return _G.TEST_RPC_CLIENT.requests[2] + end) eq(NIL, request) client.notify('finish') end @@ -1025,7 +1034,9 @@ describe('LSP', function() client = _client client.request('slow_request') client.cancel_request(2) - local request = exec_lua([=[ return TEST_RPC_CLIENT.requests[2] ]=]) + local request = exec_lua(function() + return _G.TEST_RPC_CLIENT.requests[2] + end) eq('slow_request', request.method) eq('cancel', request.type) client.notify('release') @@ -1037,7 +1048,9 @@ describe('LSP', function() end, on_handler = function(err, _, ctx) eq(table.remove(expected_handlers), { err, {}, ctx }, 'expected handler') - local request = exec_lua([=[ return TEST_RPC_CLIENT.requests[2] ]=]) + local request = exec_lua(function() + return _G.TEST_RPC_CLIENT.requests[2] + end) eq(NIL, request) if ctx.method == 'finish' then client.stop() @@ -1057,11 +1070,15 @@ describe('LSP', function() on_init = function(_client) client = _client client.request('slow_request') - local request = exec_lua([=[ return TEST_RPC_CLIENT.requests[2] ]=]) + local request = exec_lua(function() + return _G.TEST_RPC_CLIENT.requests[2] + end) eq('slow_request', request.method) eq('pending', request.type) client.cancel_request(2) - request = exec_lua([=[ return TEST_RPC_CLIENT.requests[2] ]=]) + request = exec_lua(function() + return _G.TEST_RPC_CLIENT.requests[2] + end) eq('slow_request', request.method) eq('cancel', request.type) client.notify('release') @@ -1074,7 +1091,9 @@ describe('LSP', function() on_handler = function(err, _, ctx) eq(table.remove(expected_handlers), { err, {}, ctx }, 'expected handler') if ctx.method == 'slow_request' then - local request = exec_lua([=[ return TEST_RPC_CLIENT.requests[2] ]=]) + local request = exec_lua(function() + return _G.TEST_RPC_CLIENT.requests[2] + end) eq(NIL, request) client.notify('finish') end @@ -1130,17 +1149,17 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_finish', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - assert(TEST_RPC_CLIENT_ID == 1) - assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) - assert(lsp.buf_is_attached(BUFFER, TEST_RPC_CLIENT_ID)) - vim.cmd(BUFFER.."bwipeout") - ]] + assert(_G.TEST_RPC_CLIENT_ID == 1) + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + assert(vim.lsp.buf_is_attached(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + vim.cmd(_G.BUFFER .. 'bwipeout') + end) end, on_init = function(_client) client = _client @@ -1172,25 +1191,28 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_check_buffer_open', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] - exec_lua [[ - assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) - ]] + end) + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") 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), "Already attached, returns true") - ]] + exec_lua(function() + assert( + vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID), + 'Already attached, returns true' + ) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1218,22 +1240,22 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_check_buffer_open', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] + end) end, on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") 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)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1261,22 +1283,22 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_check_buffer_open_and_change', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] + end) end, on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") 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)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1284,11 +1306,11 @@ describe('LSP', function() end, on_handler = function(err, result, ctx) if ctx.method == 'start' then - exec_lua [[ - vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { - "boop"; + exec_lua(function() + vim.api.nvim_buf_set_lines(_G.BUFFER, 1, 2, false, { + 'boop', }) - ]] + end) client.notify('finish') end eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') @@ -1309,23 +1331,23 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_check_buffer_open_and_change_noeol', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - vim.bo[BUFFER].eol = false - ]] + vim.bo[_G.BUFFER].eol = false + end) end, on_init = function(_client) client = _client local full_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") 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)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1333,11 +1355,11 @@ describe('LSP', function() end, on_handler = function(err, result, ctx) if ctx.method == 'start' then - exec_lua [[ - vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { - "boop"; + exec_lua(function() + vim.api.nvim_buf_set_lines(_G.BUFFER, 1, 2, false, { + 'boop', }) - ]] + end) client.notify('finish') end eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') @@ -1377,21 +1399,21 @@ describe('LSP', function() test_rpc_server { test_name = 'inlay_hint', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - vim.bo[BUFFER].eol = false - ]] + vim.bo[_G.BUFFER].eol = false + end) end, on_init = function(_client) client = _client eq(true, client.supports_method('textDocument/inlayHint')) - exec_lua [[ - assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1399,9 +1421,9 @@ describe('LSP', function() end, on_handler = function(err, result, ctx) if ctx.method == 'start' then - exec_lua [[ - vim.lsp.inlay_hint.enable(true, { bufnr = BUFFER }) - ]] + exec_lua(function() + vim.lsp.inlay_hint.enable(true, { bufnr = _G.BUFFER }) + end) end if ctx.method == 'textDocument/inlayHint' then client.notify('finish') @@ -1427,13 +1449,13 @@ describe('LSP', function() allow_incremental_sync = true, }, on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] + end) end, on_init = function(_client) client = _client @@ -1441,9 +1463,9 @@ describe('LSP', function() exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Incremental") 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)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1451,11 +1473,11 @@ describe('LSP', function() end, on_handler = function(err, result, ctx) if ctx.method == 'start' then - exec_lua [[ - vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { - "123boop"; + exec_lua(function() + vim.api.nvim_buf_set_lines(_G.BUFFER, 1, 2, false, { + '123boop', }) - ]] + end) client.notify('finish') end eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') @@ -1479,13 +1501,13 @@ describe('LSP', function() debounce_text_changes = 5, }, on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] + end) end, on_init = function(_client) client = _client @@ -1493,9 +1515,9 @@ describe('LSP', function() exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Incremental") 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)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1503,11 +1525,11 @@ describe('LSP', function() end, on_handler = function(err, result, ctx) if ctx.method == 'start' then - exec_lua [[ - vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { - "123boop"; + exec_lua(function() + vim.api.nvim_buf_set_lines(_G.BUFFER, 1, 2, false, { + '123boop', }) - ]] + end) client.notify('finish') end eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') @@ -1529,13 +1551,13 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_check_buffer_open_and_change_incremental_editing', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] + end) end, on_init = function(_client) client = _client @@ -1543,9 +1565,9 @@ describe('LSP', function() exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Incremental") 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)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1574,22 +1596,22 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_check_buffer_open_and_change_multi', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] + end) end, on_init = function(_client) client = _client local sync_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") 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)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1597,14 +1619,14 @@ describe('LSP', function() end, on_handler = function(err, result, ctx) if ctx.method == 'start' then - exec_lua [[ - vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { - "321"; + exec_lua(function() + vim.api.nvim_buf_set_lines(_G.BUFFER, 1, 2, false, { + '321', }) - vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { - "boop"; + vim.api.nvim_buf_set_lines(_G.BUFFER, 1, 2, false, { + 'boop', }) - ]] + end) client.notify('finish') end eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') @@ -1625,22 +1647,22 @@ describe('LSP', function() test_rpc_server { test_name = 'basic_check_buffer_open_and_change_multi_and_close', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] + end) end, on_init = function(_client) client = _client local sync_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Full") 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)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1648,15 +1670,15 @@ describe('LSP', function() end, on_handler = function(err, result, ctx) if ctx.method == 'start' then - exec_lua [[ - vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { - "321"; + exec_lua(function() + vim.api.nvim_buf_set_lines(_G.BUFFER, 1, 2, false, { + '321', }) - vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { - "boop"; + vim.api.nvim_buf_set_lines(_G.BUFFER, 1, 2, false, { + 'boop', }) - vim.api.nvim_command(BUFFER.."bwipeout") - ]] + vim.api.nvim_command(_G.BUFFER .. 'bwipeout') + end) client.notify('finish') end eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') @@ -1712,19 +1734,19 @@ describe('LSP', function() test_rpc_server { test_name = 'decode_nil', on_setup = function() - exec_lua [[ - BUFFER = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { - "testing"; - "123"; + exec_lua(function() + _G.BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(_G.BUFFER, 0, -1, false, { + 'testing', + '123', }) - ]] + end) end, on_init = function(_client) client = _client - exec_lua [[ - assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) - ]] + exec_lua(function() + assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -1794,9 +1816,9 @@ describe('LSP', function() eq({ '012WorldHelloNo8901234567890123456789' }, buf_lines(1)) end) it('multiline', function() - exec_lua([[ - vim.api.nvim_buf_set_lines(1, 0, 0, true, {' {', ' "foo": "bar"', ' }'}) - ]]) + exec_lua(function() + vim.api.nvim_buf_set_lines(1, 0, 0, true, { ' {', ' "foo": "bar"', ' }' }) + end) eq({ ' {', ' "foo": "bar"', ' }', '' }, buf_lines(1)) local edits = { make_edit(0, 0, 3, 0, { '' }), @@ -2154,12 +2176,12 @@ describe('LSP', function() } end before_each(function() - target_bufnr = exec_lua [[ - local bufnr = vim.uri_to_bufnr("file:///fake/uri") - local lines = {"1st line of text", "2nd line of 语text"} + target_bufnr = exec_lua(function() + local bufnr = vim.uri_to_bufnr('file:///fake/uri') + local lines = { '1st line of text', '2nd line of 语text' } vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines) return bufnr - ]] + end) end) it('correctly goes ahead with the edit if all is normal', function() exec_lua("vim.lsp.util.apply_text_document_edit(..., nil, 'utf-16')", text_document_edit(5)) @@ -2169,17 +2191,10 @@ describe('LSP', function() }, buf_lines(target_bufnr)) end) it('always accepts edit with version = 0', function() - exec_lua( - [[ - local args = {...} - local bufnr = select(1, ...) - local text_edit = select(2, ...) + exec_lua(function(bufnr, text_edit) vim.lsp.util.buf_versions[bufnr] = 10 vim.lsp.util.apply_text_document_edit(text_edit, nil, 'utf-16') - ]], - target_bufnr, - text_document_edit(0) - ) + end, target_bufnr, text_document_edit(0)) eq({ 'First ↥ 🤦 🦄 line of text', '2nd line of 语text', @@ -2187,16 +2202,10 @@ describe('LSP', function() end) it('skips the edit if the version of the edit is behind the local buffer ', function() local apply_edit_mocking_current_version = function(edit, versionedBuf) - exec_lua( - [[ - local args = {...} - local versionedBuf = args[2] - vim.lsp.util.buf_versions[versionedBuf.bufnr] = versionedBuf.currentVersion - vim.lsp.util.apply_text_document_edit(args[1], nil, 'utf-16') - ]], - edit, - versionedBuf - ) + exec_lua(function(edit0, versionedBuf0) + vim.lsp.util.buf_versions[versionedBuf0.bufnr] = versionedBuf0.currentVersion + vim.lsp.util.apply_text_document_edit(edit0, nil, 'utf-16') + end, edit, versionedBuf) end local baseText = { @@ -2249,13 +2258,17 @@ describe('LSP', function() } 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}) - ]] + exec_lua(function() + local apply_edit = { + label = nil, + edit = {}, + } + return vim.lsp.handlers['workspace/applyEdit']( + nil, + apply_edit, + { client_id = _G.TEST_RPC_CLIENT_ID } + ) + end) ) eq(table.remove(expected_handlers), { ... }) end, @@ -2288,11 +2301,11 @@ describe('LSP', function() local target_bufnr, changedtick = nil, nil before_each(function() - local ret = exec_lua [[ - local bufnr = vim.uri_to_bufnr("file:///fake/uri") + local ret = exec_lua(function() + local bufnr = vim.uri_to_bufnr('file:///fake/uri') local lines = { - "Original Line #1", - "Original Line #2" + 'Original Line #1', + 'Original Line #2', } vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) @@ -2305,11 +2318,11 @@ describe('LSP', function() vim.api.nvim_buf_attach(bufnr, false, { on_changedtick = function() update_changed_tick() - end + end, }) - return {bufnr, vim.api.nvim_buf_get_var(bufnr, 'changedtick')} - ]] + return { bufnr, vim.api.nvim_buf_get_var(bufnr, 'changedtick') } + end) target_bufnr = ret[1] changedtick = ret[2] @@ -2330,19 +2343,11 @@ describe('LSP', function() 'First Line', 'Original Line #2', }, - exec_lua( - [[ - local args = {...} - local workspace_edits = args[1] - local target_bufnr = args[2] - - vim.lsp.util.apply_workspace_edit(workspace_edits, 'utf-16') + exec_lua(function(workspace_edits, target_bufnr0) + 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 - ) + return vim.api.nvim_buf_get_lines(target_bufnr0, 0, -1, false) + end, make_workspace_edit(edits), target_bufnr) ) end) @@ -2359,19 +2364,11 @@ describe('LSP', function() eq( new_lines, - exec_lua( - [[ - local args = {...} - local workspace_edits = args[1] - local target_bufnr = args[2] + exec_lua(function(workspace_edits, target_bufnr0) + vim.lsp.util.apply_workspace_edit(workspace_edits, 'utf-16') - 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 - ) + return vim.api.nvim_buf_get_lines(target_bufnr0, 0, -1, false) + end, make_workspace_edit(edits), target_bufnr) ) end) it('Supports file creation with CreateFile payload', function() @@ -2450,15 +2447,11 @@ describe('LSP', function() it('DeleteFile delete file and buffer', function() local tmpfile = tmpname() write_file(tmpfile, 'Be gone') - local uri = exec_lua( - [[ - local fname = select(1, ...) + local uri = exec_lua(function(fname) local bufnr = vim.fn.bufadd(fname) vim.fn.bufload(bufnr) return vim.uri_from_fname(fname) - ]], - tmpfile - ) + end, tmpfile) local edit = { documentChanges = { { @@ -2499,21 +2492,15 @@ describe('LSP', function() write_file(old, 'Test content') local new = tmpname() os.remove(new) -- only reserve the name, file must not exist for the test scenario - local lines = exec_lua( - [[ - local old = select(1, ...) - local new = select(2, ...) - local old_bufnr = vim.fn.bufadd(old) + local lines = exec_lua(function(old0, new0) + local old_bufnr = vim.fn.bufadd(old0) vim.fn.bufload(old_bufnr) - vim.lsp.util.rename(old, new) + vim.lsp.util.rename(old0, new0) -- the existing buffer is renamed in-place and its contents is kept - local new_bufnr = vim.fn.bufadd(new) + local new_bufnr = vim.fn.bufadd(new0) vim.fn.bufload(new_bufnr) return (old_bufnr == new_bufnr) and vim.api.nvim_buf_get_lines(new_bufnr, 0, -1, true) - ]], - old, - new - ) + end, old, new) eq({ 'Test content' }, lines) local exists = vim.uv.fs_stat(old) ~= nil eq(false, exists) @@ -2533,25 +2520,15 @@ describe('LSP', function() local file = 'file.txt' write_file(old_dir .. pathsep .. file, 'Test content') - local lines = exec_lua( - [[ - local old_dir = select(1, ...) - local new_dir = select(2, ...) - local pathsep = select(3, ...) - local file = select(4, ...) - local old_bufnr = vim.fn.bufadd(old_dir .. pathsep .. file) + local lines = exec_lua(function(old_dir0, new_dir0, pathsep0, file0) + local old_bufnr = vim.fn.bufadd(old_dir0 .. pathsep0 .. file0) vim.fn.bufload(old_bufnr) - vim.lsp.util.rename(old_dir, new_dir) + vim.lsp.util.rename(old_dir0, new_dir0) -- the existing buffer is renamed in-place and its contents is kept - local new_bufnr = vim.fn.bufadd(new_dir .. pathsep .. file) + local new_bufnr = vim.fn.bufadd(new_dir0 .. pathsep0 .. file0) vim.fn.bufload(new_bufnr) return (old_bufnr == new_bufnr) and vim.api.nvim_buf_get_lines(new_bufnr, 0, -1, true) - ]], - old_dir, - new_dir, - pathsep, - file - ) + end, old_dir, new_dir, pathsep, file) eq({ 'Test content' }, lines) eq(false, vim.uv.fs_stat(old_dir) ~= nil) eq(true, vim.uv.fs_stat(new_dir) ~= nil) @@ -2566,36 +2543,28 @@ describe('LSP', function() os.remove(new) n.mkdir_p(old) - local result = exec_lua( - [[ - local old = select(1, ...) - local new = select(2, ...) - - local old_prefixed = 'explorer://' .. old - local old_suffixed = old .. '.bak' - local new_prefixed = 'explorer://' .. new - local new_suffixed = new .. '.bak' + local result = exec_lua(function(old0, new0) + local old_prefixed = 'explorer://' .. old0 + local old_suffixed = old0 .. '.bak' + local new_prefixed = 'explorer://' .. new0 + local new_suffixed = new0 .. '.bak' local old_prefixed_buf = vim.fn.bufadd(old_prefixed) local old_suffixed_buf = vim.fn.bufadd(old_suffixed) local new_prefixed_buf = vim.fn.bufadd(new_prefixed) local new_suffixed_buf = vim.fn.bufadd(new_suffixed) - vim.lsp.util.rename(old, new) - - return - vim.api.nvim_buf_is_valid(old_prefixed_buf) and - vim.api.nvim_buf_is_valid(old_suffixed_buf) and - vim.api.nvim_buf_is_valid(new_prefixed_buf) and - vim.api.nvim_buf_is_valid(new_suffixed_buf) and - vim.api.nvim_buf_get_name(old_prefixed_buf) == old_prefixed and - vim.api.nvim_buf_get_name(old_suffixed_buf) == old_suffixed and - vim.api.nvim_buf_get_name(new_prefixed_buf) == new_prefixed and - vim.api.nvim_buf_get_name(new_suffixed_buf) == new_suffixed - ]], - old, - new - ) + vim.lsp.util.rename(old0, new0) + + return vim.api.nvim_buf_is_valid(old_prefixed_buf) + and vim.api.nvim_buf_is_valid(old_suffixed_buf) + and vim.api.nvim_buf_is_valid(new_prefixed_buf) + and vim.api.nvim_buf_is_valid(new_suffixed_buf) + and vim.api.nvim_buf_get_name(old_prefixed_buf) == old_prefixed + and vim.api.nvim_buf_get_name(old_suffixed_buf) == old_suffixed + and vim.api.nvim_buf_get_name(new_prefixed_buf) == new_prefixed + and vim.api.nvim_buf_get_name(new_suffixed_buf) == new_suffixed + end, old, new) eq(true, result) os.remove(new) @@ -2608,30 +2577,16 @@ describe('LSP', function() local new = tmpname() write_file(new, 'New file') - exec_lua( - [[ - local old = select(1, ...) - local new = select(2, ...) - - vim.lsp.util.rename(old, new, { ignoreIfExists = true }) - ]], - old, - new - ) + exec_lua(function(old0, new0) + vim.lsp.util.rename(old0, new0, { ignoreIfExists = true }) + end, old, new) eq(true, vim.uv.fs_stat(old) ~= nil) eq('New file', read_file(new)) - exec_lua( - [[ - local old = select(1, ...) - local new = select(2, ...) - - vim.lsp.util.rename(old, new, { overwrite = false }) - ]], - old, - new - ) + exec_lua(function(old0, new0) + vim.lsp.util.rename(old0, new0, { overwrite = false }) + end, old, new) eq(true, vim.uv.fs_stat(old) ~= nil) eq('New file', read_file(new)) @@ -2643,26 +2598,20 @@ describe('LSP', function() local new = tmpname() os.remove(new) - local undo_kept = exec_lua( - [[ - local old = select(1, ...) - local new = select(2, ...) + local undo_kept = exec_lua(function(old0, new0) vim.opt.undofile = true - vim.cmd.edit(old) + vim.cmd.edit(old0) vim.cmd.normal('dd') vim.cmd.write() local undotree = vim.fn.undotree() - vim.lsp.util.rename(old, new) + vim.lsp.util.rename(old0, new0) -- Renaming uses :saveas, which updates the "last write" information. -- Other than that, the undotree should remain the same. undotree.save_cur = undotree.save_cur + 1 undotree.save_last = undotree.save_last + 1 undotree.entries[1].save = undotree.entries[1].save + 1 return vim.deep_equal(undotree, vim.fn.undotree()) - ]], - old, - new - ) + end, old, new) eq(false, vim.uv.fs_stat(old) ~= nil) eq(true, vim.uv.fs_stat(new) ~= nil) eq(true, undo_kept) @@ -2673,23 +2622,17 @@ describe('LSP', function() local new = tmpname() os.remove(new) - local undo_kept = exec_lua( - [[ - local old = select(1, ...) - local new = select(2, ...) + local undo_kept = exec_lua(function(old0, new0) vim.opt.undofile = true - vim.cmd.split(old) + vim.cmd.split(old0) vim.cmd.normal('dd') vim.cmd.write() local undotree = vim.fn.undotree() vim.cmd.bdelete() - vim.lsp.util.rename(old, new) - vim.cmd.edit(new) + vim.lsp.util.rename(old0, new0) + vim.cmd.edit(new0) return vim.deep_equal(undotree, vim.fn.undotree()) - ]], - old, - new - ) + end, old, new) eq(false, vim.uv.fs_stat(old) ~= nil) eq(true, vim.uv.fs_stat(new) ~= nil) eq(true, undo_kept) @@ -2700,22 +2643,16 @@ describe('LSP', function() local new = tmpname() os.remove(new) - local lines = exec_lua( - [[ - local old = select(1, ...) - local new = select(2, ...) - local old_buf = vim.fn.bufadd(old) - vim.fn.bufload(old_buf) - local conflict_buf = vim.api.nvim_create_buf(true, false) - vim.api.nvim_buf_set_name(conflict_buf, new) - vim.api.nvim_buf_set_lines(conflict_buf, 0, -1, true, {'conflict'}) - vim.api.nvim_win_set_buf(0, conflict_buf) - vim.lsp.util.rename(old, new) - return vim.api.nvim_buf_get_lines(conflict_buf, 0, -1, true) - ]], - old, - new - ) + local lines = exec_lua(function(old0, new0) + local old_buf = vim.fn.bufadd(old0) + vim.fn.bufload(old_buf) + local conflict_buf = vim.api.nvim_create_buf(true, false) + vim.api.nvim_buf_set_name(conflict_buf, new0) + vim.api.nvim_buf_set_lines(conflict_buf, 0, -1, true, { 'conflict' }) + vim.api.nvim_win_set_buf(0, conflict_buf) + vim.lsp.util.rename(old0, new0) + return vim.api.nvim_buf_get_lines(conflict_buf, 0, -1, true) + end, old, new) eq({ 'conflict' }, lines) eq('Old File', read_file(old)) end) @@ -2724,16 +2661,9 @@ describe('LSP', function() write_file(old, 'Old file') local new = tmpname() write_file(new, 'New file') - exec_lua( - [[ - local old = select(1, ...) - local new = select(2, ...) - - vim.lsp.util.rename(old, new, { overwrite = true }) - ]], - old, - new - ) + exec_lua(function(old0, new0) + vim.lsp.util.rename(old0, new0, { overwrite = true }) + end, old, new) eq(false, vim.uv.fs_stat(old) ~= nil) eq(true, vim.uv.fs_stat(new) ~= nil) @@ -2760,9 +2690,9 @@ describe('LSP', function() }, }, } - local actual = exec_lua [[ - local bufnr = vim.uri_to_bufnr("file:///fake/uri") - local lines = {"testing", "123"} + local actual = exec_lua(function() + local bufnr = vim.uri_to_bufnr('file:///fake/uri') + local lines = { 'testing', '123' } vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines) local locations = { { @@ -2770,11 +2700,11 @@ describe('LSP', function() range = { start = { line = 0, character = 2 }, ['end'] = { line = 1, character = 3 }, - } + }, }, } return vim.lsp.util.locations_to_items(locations, 'utf-16') - ]] + end) eq(expected, actual) end) it('Convert LocationLink[] to items', function() @@ -2799,9 +2729,9 @@ describe('LSP', function() }, }, } - local actual = exec_lua [[ - local bufnr = vim.uri_to_bufnr("file:///fake/uri") - local lines = {"testing", "123"} + local actual = exec_lua(function() + local bufnr = vim.uri_to_bufnr('file:///fake/uri') + local lines = { 'testing', '123' } vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines) local locations = { { @@ -2813,11 +2743,11 @@ describe('LSP', function() targetSelectionRange = { start = { line = 0, character = 2 }, ['end'] = { line = 0, character = 3 }, - } + }, }, } return vim.lsp.util.locations_to_items(locations, 'utf-16') - ]] + end) eq(expected, actual) end) end) @@ -2850,92 +2780,92 @@ describe('LSP', function() } eq( expected, - exec_lua [[ - local doc_syms = { - { - deprecated = false, - detail = "A", - kind = 1, - name = "TestA", - range = { - start = { - character = 0, - line = 1 + exec_lua(function() + local doc_syms = { + { + deprecated = false, + detail = 'A', + kind = 1, + name = 'TestA', + range = { + start = { + character = 0, + line = 1, + }, + ['end'] = { + character = 0, + line = 2, + }, }, - ["end"] = { - character = 0, - line = 2 - } - }, - selectionRange = { - start = { - character = 0, - line = 1 + selectionRange = { + start = { + character = 0, + line = 1, + }, + ['end'] = { + character = 4, + line = 1, + }, }, - ["end"] = { - character = 4, - line = 1 - } - }, - children = { - { - children = {}, - deprecated = false, - detail = "B", - kind = 2, - name = "TestB", - range = { - start = { - character = 0, - line = 3 + children = { + { + children = {}, + deprecated = false, + detail = 'B', + kind = 2, + name = 'TestB', + range = { + start = { + character = 0, + line = 3, + }, + ['end'] = { + character = 0, + line = 4, + }, }, - ["end"] = { - character = 0, - line = 4 - } - }, - selectionRange = { - start = { - character = 0, - line = 3 + selectionRange = { + start = { + character = 0, + line = 3, + }, + ['end'] = { + character = 4, + line = 3, + }, }, - ["end"] = { - character = 4, - line = 3 - } - } - } - } - }, - { - deprecated = false, - detail = "C", - kind = 3, - name = "TestC", - range = { - start = { - character = 0, - line = 5 + }, }, - ["end"] = { - character = 0, - line = 6 - } }, - selectionRange = { - start = { - character = 0, - line = 5 + { + deprecated = false, + detail = 'C', + kind = 3, + name = 'TestC', + range = { + start = { + character = 0, + line = 5, + }, + ['end'] = { + character = 0, + line = 6, + }, }, - ["end"] = { - character = 4, - line = 5 - } - } + selectionRange = { + start = { + character = 0, + line = 5, + }, + ['end'] = { + character = 4, + line = 5, + }, + }, + }, } - } - return vim.lsp.util.symbols_to_items(doc_syms, nil) - ]] + return vim.lsp.util.symbols_to_items(doc_syms, nil) + end) ) end) it('DocumentSymbol has no children', function() @@ -2957,63 +2887,63 @@ describe('LSP', function() } eq( expected, - exec_lua [[ - local doc_syms = { - { - deprecated = false, - detail = "A", - kind = 1, - name = "TestA", - range = { - start = { - character = 0, - line = 1 + exec_lua(function() + local doc_syms = { + { + deprecated = false, + detail = 'A', + kind = 1, + name = 'TestA', + range = { + start = { + character = 0, + line = 1, + }, + ['end'] = { + character = 0, + line = 2, + }, }, - ["end"] = { - character = 0, - line = 2 - } - }, - selectionRange = { - start = { - character = 0, - line = 1 + selectionRange = { + start = { + character = 0, + line = 1, + }, + ['end'] = { + character = 4, + line = 1, + }, }, - ["end"] = { - character = 4, - line = 1 - } }, - }, - { - deprecated = false, - detail = "C", - kind = 3, - name = "TestC", - range = { - start = { - character = 0, - line = 5 + { + deprecated = false, + detail = 'C', + kind = 3, + name = 'TestC', + range = { + start = { + character = 0, + line = 5, + }, + ['end'] = { + character = 0, + line = 6, + }, }, - ["end"] = { - character = 0, - line = 6 - } - }, - selectionRange = { - start = { - character = 0, - line = 5 + selectionRange = { + start = { + character = 0, + line = 5, + }, + ['end'] = { + character = 4, + line = 5, + }, }, - ["end"] = { - character = 4, - line = 5 - } - } + }, } - } - return vim.lsp.util.symbols_to_items(doc_syms, nil) - ]] + return vim.lsp.util.symbols_to_items(doc_syms, nil) + end) ) end) end) @@ -3036,49 +2966,49 @@ describe('LSP', function() } eq( expected, - exec_lua [[ + exec_lua(function() local sym_info = { { deprecated = false, kind = 1, - name = "TestA", + name = 'TestA', location = { range = { start = { character = 0, - line = 1 + line = 1, }, - ["end"] = { + ['end'] = { character = 0, - line = 2 - } + line = 2, + }, }, - uri = "file:///test_a" + uri = 'file:///test_a', }, - containerName = "TestAContainer" + containerName = 'TestAContainer', }, { deprecated = false, kind = 2, - name = "TestB", + name = 'TestB', location = { range = { start = { character = 0, - line = 3 + line = 3, }, - ["end"] = { + ['end'] = { character = 0, - line = 4 - } + line = 4, + }, }, - uri = "file:///test_b" + uri = 'file:///test_b', }, - containerName = "TestBContainer" - } + containerName = 'TestBContainer', + }, } return vim.lsp.util.symbols_to_items(sym_info, nil) - ]] + end) ) end) end) @@ -3099,12 +3029,12 @@ describe('LSP', function() local target_bufnr --- @type integer before_each(function() - target_bufnr = exec_lua [[ - local bufnr = vim.uri_to_bufnr("file:///fake/uri") - local lines = {"1st line of text", "å å ɧ 汉语 ↥ 🤦 🦄"} + target_bufnr = exec_lua(function() + local bufnr = vim.uri_to_bufnr('file:///fake/uri') + local lines = { '1st line of text', 'å å ɧ 汉语 ↥ 🤦 🦄' } vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines) return bufnr - ]] + end) end) local location = function(start_line, start_char, end_line, end_char) @@ -3173,19 +3103,19 @@ describe('LSP', function() local target_bufnr2 --- @type integer before_each(function() - target_bufnr = exec_lua([[ - local bufnr = vim.uri_to_bufnr("file:///fake/uri") - local lines = {"1st line of text", "å å ɧ 汉语 ↥ 🤦 🦄"} + target_bufnr = exec_lua(function() + local bufnr = vim.uri_to_bufnr('file:///fake/uri') + local lines = { '1st line of text', 'å å ɧ 汉语 ↥ 🤦 🦄' } vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines) return bufnr - ]]) + end) - target_bufnr2 = exec_lua([[ - local bufnr = vim.uri_to_bufnr("file:///fake/uri2") - local lines = {"1st line of text", "å å ɧ 汉语 ↥ 🤦 🦄"} + target_bufnr2 = exec_lua(function() + local bufnr = vim.uri_to_bufnr('file:///fake/uri2') + local lines = { '1st line of text', 'å å ɧ 汉语 ↥ 🤦 🦄' } vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines) return bufnr - ]]) + end) end) local location = function(start_line, start_char, end_line, end_char, second_uri) @@ -3225,14 +3155,14 @@ describe('LSP', function() it('jumps to a Location if focus is true via handler', function() exec_lua(create_server_definition) - local result = exec_lua([[ - local server = _create_server() + local result = exec_lua(function() + local server = _G._create_server() local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) local result = { uri = 'file:///fake/uri', selection = { start = { line = 0, character = 9 }, - ['end'] = { line = 0, character = 9 } + ['end'] = { line = 0, character = 9 }, }, takeFocus = true, } @@ -3243,9 +3173,9 @@ describe('LSP', function() vim.lsp.handlers['window/showDocument'](nil, result, ctx) vim.lsp.stop_client(client_id) return { - cursor = vim.api.nvim_win_get_cursor(0) + cursor = vim.api.nvim_win_get_cursor(0), } - ]]) + end) eq(1, result.cursor[1]) eq(9, result.cursor[2]) end) @@ -3320,7 +3250,9 @@ describe('LSP', function() api.nvim_win_set_buf(0, target_bufnr) api.nvim_win_set_cursor(0, { 2, 3 }) - exec_lua([[vim.cmd.new()]]) + exec_lua(function() + vim.cmd.new() + end) api.nvim_win_set_buf(0, target_bufnr2) api.nvim_win_set_cursor(0, { 2, 3 }) @@ -3336,7 +3268,9 @@ describe('LSP', function() api.nvim_win_set_buf(0, target_bufnr) local win = api.nvim_get_current_win() - exec_lua([[vim.cmd.new()]]) + exec_lua(function() + vim.cmd.new() + end) api.nvim_win_set_buf(0, target_bufnr2) api.nvim_win_set_cursor(0, { 2, 3 }) local split = api.nvim_get_current_win() @@ -3357,34 +3291,53 @@ describe('LSP', function() describe('lsp.util._make_floating_popup_size', function() before_each(function() - exec_lua [[ contents = - {"text tαxt txtα tex", - "text tααt tααt text", - "text tαxt tαxt"} - ]] + exec_lua(function() + _G.contents = { 'text tαxt txtα tex', 'text tααt tααt text', 'text tαxt tαxt' } + end) end) it('calculates size correctly', function() - eq({ 19, 3 }, exec_lua [[ return {vim.lsp.util._make_floating_popup_size(contents)} ]]) + eq( + { 19, 3 }, + exec_lua(function() + return { vim.lsp.util._make_floating_popup_size(_G.contents) } + end) + ) end) it('calculates size correctly with wrapping', function() eq( { 15, 5 }, - exec_lua [[ return {vim.lsp.util._make_floating_popup_size(contents,{width = 15, wrap_at = 14})} ]] + exec_lua(function() + return { + vim.lsp.util._make_floating_popup_size(_G.contents, { width = 15, wrap_at = 14 }), + } + end) ) end) it('handles NUL bytes in text', function() - exec_lua([[ contents = { - '\000\001\002\003\004\005\006\007\008\009', - '\010\011\012\013\014\015\016\017\018\019', - '\020\021\022\023\024\025\026\027\028\029', - } ]]) + exec_lua(function() + _G.contents = { + '\000\001\002\003\004\005\006\007\008\009', + '\010\011\012\013\014\015\016\017\018\019', + '\020\021\022\023\024\025\026\027\028\029', + } + end) command('set list listchars=') - eq({ 20, 3 }, exec_lua [[ return {vim.lsp.util._make_floating_popup_size(contents)} ]]) + eq( + { 20, 3 }, + exec_lua(function() + return { vim.lsp.util._make_floating_popup_size(_G.contents) } + end) + ) command('set display+=uhex') - eq({ 40, 3 }, exec_lua [[ return {vim.lsp.util._make_floating_popup_size(contents)} ]]) + eq( + { 40, 3 }, + exec_lua(function() + return { vim.lsp.util._make_floating_popup_size(_G.contents) } + end) + ) end) end) @@ -3392,27 +3345,29 @@ describe('LSP', function() it('properly trims empty lines', function() eq( { { 'foo', 'bar' } }, - exec_lua [[ return vim.lsp.util.trim_empty_lines({{ "foo", "bar" }, nil}) ]] + exec_lua(function() + return vim.lsp.util.trim_empty_lines({ { 'foo', 'bar' }, nil }) + end) ) end) end) describe('lsp.util.convert_signature_help_to_markdown_lines', function() it('can handle negative activeSignature', function() - local result = exec_lua [[ + local result = exec_lua(function() local signature_help = { activeParameter = 0, activeSignature = -1, signatures = { { - documentation = "some doc", - label = "TestEntity.TestEntity()", - parameters = {} + documentation = 'some doc', + label = 'TestEntity.TestEntity()', + parameters = {}, }, - } + }, } - return vim.lsp.util.convert_signature_help_to_markdown_lines(signature_help, 'cs', {','}) - ]] + return vim.lsp.util.convert_signature_help_to_markdown_lines(signature_help, 'cs', { ',' }) + end) local expected = { '```cs', 'TestEntity.TestEntity()', '```', 'some doc' } eq(expected, result) end) @@ -3440,57 +3395,61 @@ describe('LSP', function() describe('vim.lsp.buf.outgoing_calls', function() it('does nothing for an empty response', function() - local qflist_count = exec_lua([=[ - require'vim.lsp.handlers'['callHierarchy/outgoingCalls'](nil, nil, {}, nil) + local qflist_count = exec_lua(function() + require 'vim.lsp.handlers'['callHierarchy/outgoingCalls'](nil, nil, {}, nil) return #vim.fn.getqflist() - ]=]) + end) eq(0, qflist_count) end) it('opens the quickfix list with the right caller', function() - local qflist = exec_lua([=[ - local rust_analyzer_response = { { - fromRanges = { { - ['end'] = { - character = 7, - line = 3 - }, - start = { - character = 4, - line = 3 - } - } }, - to = { - detail = "fn foo()", - kind = 12, - name = "foo", - range = { - ['end'] = { - character = 11, - line = 0 + local qflist = exec_lua(function() + local rust_analyzer_response = { + { + fromRanges = { + { + ['end'] = { + character = 7, + line = 3, + }, + start = { + character = 4, + line = 3, + }, }, - start = { - character = 0, - line = 0 - } }, - selectionRange = { - ['end'] = { - character = 6, - line = 0 + to = { + detail = 'fn foo()', + kind = 12, + name = 'foo', + range = { + ['end'] = { + character = 11, + line = 0, + }, + start = { + character = 0, + line = 0, + }, }, - start = { - character = 3, - line = 0 - } + selectionRange = { + ['end'] = { + character = 6, + line = 0, + }, + start = { + character = 3, + line = 0, + }, + }, + uri = 'file:///src/main.rs', }, - uri = "file:///src/main.rs" - } - } } - local handler = require'vim.lsp.handlers'['callHierarchy/outgoingCalls'] + }, + } + local handler = require 'vim.lsp.handlers'['callHierarchy/outgoingCalls'] handler(nil, rust_analyzer_response, {}) return vim.fn.getqflist() - ]=]) + end) local expected = { { @@ -3515,58 +3474,62 @@ describe('LSP', function() describe('vim.lsp.buf.incoming_calls', function() it('does nothing for an empty response', function() - local qflist_count = exec_lua([=[ - require'vim.lsp.handlers'['callHierarchy/incomingCalls'](nil, nil, {}) + local qflist_count = exec_lua(function() + require 'vim.lsp.handlers'['callHierarchy/incomingCalls'](nil, nil, {}) return #vim.fn.getqflist() - ]=]) + end) eq(0, qflist_count) end) it('opens the quickfix list with the right callee', function() - local qflist = exec_lua([=[ - local rust_analyzer_response = { { - from = { - detail = "fn main()", - kind = 12, - name = "main", - range = { - ['end'] = { - character = 1, - line = 4 + local qflist = exec_lua(function() + local rust_analyzer_response = { + { + from = { + detail = 'fn main()', + kind = 12, + name = 'main', + range = { + ['end'] = { + character = 1, + line = 4, + }, + start = { + character = 0, + line = 2, + }, }, - start = { - character = 0, - line = 2 - } + selectionRange = { + ['end'] = { + character = 7, + line = 2, + }, + start = { + character = 3, + line = 2, + }, + }, + uri = 'file:///src/main.rs', }, - selectionRange = { - ['end'] = { - character = 7, - line = 2 + fromRanges = { + { + ['end'] = { + character = 7, + line = 3, + }, + start = { + character = 4, + line = 3, + }, }, - start = { - character = 3, - line = 2 - } }, - uri = "file:///src/main.rs" }, - fromRanges = { { - ['end'] = { - character = 7, - line = 3 - }, - start = { - character = 4, - line = 3 - } - } } - } } + } - local handler = require'vim.lsp.handlers'['callHierarchy/incomingCalls'] + local handler = require 'vim.lsp.handlers'['callHierarchy/incomingCalls'] handler(nil, rust_analyzer_response, {}) return vim.fn.getqflist() - ]=]) + end) local expected = { { @@ -3591,103 +3554,118 @@ describe('LSP', function() describe('vim.lsp.buf.typehierarchy subtypes', function() it('does nothing for an empty response', function() - local qflist_count = exec_lua([=[ - require'vim.lsp.handlers'['typeHierarchy/subtypes'](nil, nil, {}) + local qflist_count = exec_lua(function() + require 'vim.lsp.handlers'['typeHierarchy/subtypes'](nil, nil, {}) return #vim.fn.getqflist() - ]=]) + end) eq(0, qflist_count) end) it('opens the quickfix list with the right subtypes', function() clear() exec_lua(create_server_definition) - local qflist = exec_lua([=[ - local clangd_response = { { - data = { - parents = { { - parents = { { - parents = { { - parents = {}, - symbolID = "62B3D268A01B9978" - } }, - symbolID = "DC9B0AD433B43BEC" - } }, - symbolID = "06B5F6A19BA9F6A8" - } }, - symbolID = "EDC336589C09ABB2" - }, - kind = 5, - name = "D2", - range = { - ["end"] = { - character = 8, - line = 9 + local qflist = exec_lua(function() + local clangd_response = { + { + data = { + parents = { + { + parents = { + { + parents = { + { + parents = {}, + symbolID = '62B3D268A01B9978', + }, + }, + symbolID = 'DC9B0AD433B43BEC', + }, + }, + symbolID = '06B5F6A19BA9F6A8', + }, + }, + symbolID = 'EDC336589C09ABB2', }, - start = { - character = 6, - line = 9 - } - }, - selectionRange = { - ["end"] = { - character = 8, - line = 9 + kind = 5, + name = 'D2', + range = { + ['end'] = { + character = 8, + line = 9, + }, + start = { + character = 6, + line = 9, + }, }, - start = { - character = 6, - line = 9 - } - }, - uri = "file:///home/jiangyinzuo/hello.cpp" - }, { - data = { - parents = { { - parents = { { - parents = { { - parents = {}, - symbolID = "62B3D268A01B9978" - } }, - symbolID = "DC9B0AD433B43BEC" - } }, - symbolID = "06B5F6A19BA9F6A8" - } }, - symbolID = "AFFCAED15557EF08" - }, - kind = 5, - name = "D1", - range = { - ["end"] = { - character = 8, - line = 8 + selectionRange = { + ['end'] = { + character = 8, + line = 9, + }, + start = { + character = 6, + line = 9, + }, }, - start = { - character = 6, - line = 8 - } + uri = 'file:///home/jiangyinzuo/hello.cpp', }, - selectionRange = { - ["end"] = { - character = 8, - line = 8 + { + data = { + parents = { + { + parents = { + { + parents = { + { + parents = {}, + symbolID = '62B3D268A01B9978', + }, + }, + symbolID = 'DC9B0AD433B43BEC', + }, + }, + symbolID = '06B5F6A19BA9F6A8', + }, + }, + symbolID = 'AFFCAED15557EF08', }, - start = { - character = 6, - line = 8 - } + kind = 5, + name = 'D1', + range = { + ['end'] = { + character = 8, + line = 8, + }, + start = { + character = 6, + line = 8, + }, + }, + selectionRange = { + ['end'] = { + character = 8, + line = 8, + }, + start = { + character = 6, + line = 8, + }, + }, + uri = 'file:///home/jiangyinzuo/hello.cpp', }, - uri = "file:///home/jiangyinzuo/hello.cpp" - } } + } - local server = _create_server({ + local server = _G._create_server({ capabilities = { - positionEncoding = "utf-8" + positionEncoding = 'utf-8', }, }) local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) - local handler = require'vim.lsp.handlers'['typeHierarchy/subtypes'] + local handler = require 'vim.lsp.handlers'['typeHierarchy/subtypes'] handler(nil, clangd_response, { client_id = client_id, bufnr = 1 }) return vim.fn.getqflist() - ]=]) + end) local expected = { { @@ -3726,7 +3704,7 @@ describe('LSP', function() it('opens the quickfix list with the right subtypes and details', function() clear() exec_lua(create_server_definition) - local qflist = exec_lua([=[ + local qflist = exec_lua(function() local jdtls_response = { { data = { element = '=hello-java_ed323c3c/_<{Main.java[Main[A' }, @@ -3762,16 +3740,16 @@ describe('LSP', function() }, } - local server = _create_server({ + local server = _G._create_server({ capabilities = { - positionEncoding = "utf-8" + positionEncoding = 'utf-8', }, }) local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) - local handler = require'vim.lsp.handlers'['typeHierarchy/subtypes'] + local handler = require 'vim.lsp.handlers'['typeHierarchy/subtypes'] handler(nil, jdtls_response, { client_id = client_id, bufnr = 1 }) return vim.fn.getqflist() - ]=]) + end) local expected = { { @@ -3809,103 +3787,118 @@ describe('LSP', function() describe('vim.lsp.buf.typehierarchy supertypes', function() it('does nothing for an empty response', function() - local qflist_count = exec_lua([=[ - require'vim.lsp.handlers'['typeHierarchy/supertypes'](nil, nil, {}) + local qflist_count = exec_lua(function() + require 'vim.lsp.handlers'['typeHierarchy/supertypes'](nil, nil, {}) return #vim.fn.getqflist() - ]=]) + end) eq(0, qflist_count) end) it('opens the quickfix list with the right supertypes', function() clear() exec_lua(create_server_definition) - local qflist = exec_lua([=[ - local clangd_response = { { - data = { - parents = { { - parents = { { - parents = { { - parents = {}, - symbolID = "62B3D268A01B9978" - } }, - symbolID = "DC9B0AD433B43BEC" - } }, - symbolID = "06B5F6A19BA9F6A8" - } }, - symbolID = "EDC336589C09ABB2" - }, - kind = 5, - name = "D2", - range = { - ["end"] = { - character = 8, - line = 9 + local qflist = exec_lua(function() + local clangd_response = { + { + data = { + parents = { + { + parents = { + { + parents = { + { + parents = {}, + symbolID = '62B3D268A01B9978', + }, + }, + symbolID = 'DC9B0AD433B43BEC', + }, + }, + symbolID = '06B5F6A19BA9F6A8', + }, + }, + symbolID = 'EDC336589C09ABB2', }, - start = { - character = 6, - line = 9 - } - }, - selectionRange = { - ["end"] = { - character = 8, - line = 9 + kind = 5, + name = 'D2', + range = { + ['end'] = { + character = 8, + line = 9, + }, + start = { + character = 6, + line = 9, + }, }, - start = { - character = 6, - line = 9 - } - }, - uri = "file:///home/jiangyinzuo/hello.cpp" - }, { - data = { - parents = { { - parents = { { - parents = { { - parents = {}, - symbolID = "62B3D268A01B9978" - } }, - symbolID = "DC9B0AD433B43BEC" - } }, - symbolID = "06B5F6A19BA9F6A8" - } }, - symbolID = "AFFCAED15557EF08" - }, - kind = 5, - name = "D1", - range = { - ["end"] = { - character = 8, - line = 8 + selectionRange = { + ['end'] = { + character = 8, + line = 9, + }, + start = { + character = 6, + line = 9, + }, }, - start = { - character = 6, - line = 8 - } + uri = 'file:///home/jiangyinzuo/hello.cpp', }, - selectionRange = { - ["end"] = { - character = 8, - line = 8 + { + data = { + parents = { + { + parents = { + { + parents = { + { + parents = {}, + symbolID = '62B3D268A01B9978', + }, + }, + symbolID = 'DC9B0AD433B43BEC', + }, + }, + symbolID = '06B5F6A19BA9F6A8', + }, + }, + symbolID = 'AFFCAED15557EF08', }, - start = { - character = 6, - line = 8 - } + kind = 5, + name = 'D1', + range = { + ['end'] = { + character = 8, + line = 8, + }, + start = { + character = 6, + line = 8, + }, + }, + selectionRange = { + ['end'] = { + character = 8, + line = 8, + }, + start = { + character = 6, + line = 8, + }, + }, + uri = 'file:///home/jiangyinzuo/hello.cpp', }, - uri = "file:///home/jiangyinzuo/hello.cpp" - } } + } - local server = _create_server({ + local server = _G._create_server({ capabilities = { - positionEncoding = "utf-8" + positionEncoding = 'utf-8', }, }) local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) - local handler = require'vim.lsp.handlers'['typeHierarchy/supertypes'] + local handler = require 'vim.lsp.handlers'['typeHierarchy/supertypes'] handler(nil, clangd_response, { client_id = client_id, bufnr = 1 }) return vim.fn.getqflist() - ]=]) + end) local expected = { { @@ -3944,7 +3937,7 @@ describe('LSP', function() it('opens the quickfix list with the right supertypes and details', function() clear() exec_lua(create_server_definition) - local qflist = exec_lua([=[ + local qflist = exec_lua(function() local jdtls_response = { { data = { element = '=hello-java_ed323c3c/_<{Main.java[Main[A' }, @@ -3980,16 +3973,16 @@ describe('LSP', function() }, } - local server = _create_server({ + local server = _G._create_server({ capabilities = { - positionEncoding = "utf-8" + positionEncoding = 'utf-8', }, }) local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) - local handler = require'vim.lsp.handlers'['typeHierarchy/supertypes'] + local handler = require 'vim.lsp.handlers'['typeHierarchy/supertypes'] handler(nil, jdtls_response, { client_id = client_id, bufnr = 1 }) return vim.fn.getqflist() - ]=]) + end) local expected = { { @@ -4073,18 +4066,18 @@ describe('LSP', function() eq(true, client.server_capabilities().renameProvider.prepareProvider) end, on_setup = function() - exec_lua([=[ - local bufnr = vim.api.nvim_get_current_buf() - lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) - vim.lsp._stubs = {} - vim.fn.input = function(opts, on_confirm) - vim.lsp._stubs.input_prompt = opts.prompt - vim.lsp._stubs.input_text = opts.default - return 'renameto' -- expect this value in fake lsp - end - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {'', 'this is line two'}) - vim.fn.cursor(2, 13) -- the space between "line" and "two" - ]=]) + exec_lua(function() + local bufnr = vim.api.nvim_get_current_buf() + vim.lsp.buf_attach_client(bufnr, _G.TEST_RPC_CLIENT_ID) + vim.lsp._stubs = {} + vim.fn.input = function(opts, _) + vim.lsp._stubs.input_prompt = opts.prompt + vim.lsp._stubs.input_text = opts.default + return 'renameto' -- expect this value in fake lsp + end + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { '', 'this is line two' }) + vim.fn.cursor(2, 13) -- the space between "line" and "two" + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -4133,20 +4126,24 @@ describe('LSP', function() on_handler = function(err, result, ctx) eq(table.remove(expected_handlers), { err, result, ctx }) if ctx.method == 'start' then - exec_lua([[ - vim.lsp.commands['dummy1'] = function(cmd) - vim.lsp.commands['dummy2'] = function() - end + exec_lua(function() + vim.lsp.commands['dummy1'] = function(_) + vim.lsp.commands['dummy2'] = function() end end local bufnr = vim.api.nvim_get_current_buf() - vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.lsp.buf_attach_client(bufnr, _G.TEST_RPC_CLIENT_ID) vim.fn.inputlist = function() return 1 end vim.lsp.buf.code_action() - ]]) + end) elseif ctx.method == 'shutdown' then - eq('function', exec_lua [[return type(vim.lsp.commands['dummy2'])]]) + eq( + 'function', + exec_lua(function() + return type(vim.lsp.commands['dummy2']) + end) + ) client.stop() end end, @@ -4178,14 +4175,14 @@ describe('LSP', function() ctx.version = nil eq(table.remove(expected_handlers), { err, result, ctx }) if ctx.method == 'start' then - exec_lua([[ + exec_lua(function() local bufnr = vim.api.nvim_get_current_buf() - vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.lsp.buf_attach_client(bufnr, _G.TEST_RPC_CLIENT_ID) vim.fn.inputlist = function() return 1 end vim.lsp.buf.code_action() - ]]) + end) elseif ctx.method == 'shutdown' then client.stop() end @@ -4211,38 +4208,56 @@ describe('LSP', function() 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 + exec_lua(function() + vim.lsp.commands['preferred_command'] = function(_) + vim.lsp.commands['executed_preferred'] = function() end end - vim.lsp.commands['type_annotate_command'] = function(cmd) - vim.lsp.commands['executed_type_annotate'] = function() - end + vim.lsp.commands['type_annotate_command'] = function(_) + vim.lsp.commands['executed_type_annotate'] = 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_attach_client(bufnr, _G.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 'type-annotate' and 'type-annotate.foo' - context = { only = { 'type-annotate' }, }, - apply = true, - filter = function(a) - if a.kind == 'type-annotate.foo' then - vim.lsp.commands['filtered_type_annotate_foo'] = function() end - return false - elseif a.kind == 'type-annotate' then - return true - else - assert(nil, 'unreachable') - end - end, + -- expect to be returned actions 'type-annotate' and 'type-annotate.foo' + context = { only = { 'type-annotate' } }, + apply = true, + filter = function(a) + if a.kind == 'type-annotate.foo' then + vim.lsp.commands['filtered_type_annotate_foo'] = function() end + return false + elseif a.kind == 'type-annotate' then + return true + else + assert(nil, 'unreachable') + end + 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_type_annotate_foo'])]]) - eq('function', exec_lua [[return type(vim.lsp.commands['executed_type_annotate'])]]) + eq( + 'function', + exec_lua(function() + return type(vim.lsp.commands['executed_preferred']) + end) + ) + eq( + 'function', + exec_lua(function() + return type(vim.lsp.commands['filtered_type_annotate_foo']) + end) + ) + eq( + 'function', + exec_lua(function() + return type(vim.lsp.commands['executed_type_annotate']) + end) + ) client.stop() end end, @@ -4251,43 +4266,43 @@ describe('LSP', function() it('Fallback to command execution on resolve error', function() clear() exec_lua(create_server_definition) - local result = exec_lua([[ - local server = _create_server({ + local result = exec_lua(function() + local server = _G._create_server({ capabilities = { executeCommandProvider = { - commands = {"command:1"}, + commands = { 'command:1' }, }, codeActionProvider = { - resolveProvider = true - } + resolveProvider = true, + }, }, handlers = { - ["textDocument/codeAction"] = function(_, _, callback) + ['textDocument/codeAction'] = function(_, _, callback) callback(nil, { { - title = "Code Action 1", + title = 'Code Action 1', command = { - title = "Command 1", - command = "command:1", - } - } + title = 'Command 1', + command = 'command:1', + }, + }, }) end, - ["codeAction/resolve"] = function(_, _, callback) - callback("resolve failed", nil) + ['codeAction/resolve'] = function(_, _, callback) + callback('resolve failed', nil) end, - } + }, }) local client_id = vim.lsp.start({ - name = "dummy", + name = 'dummy', cmd = server.cmd, }) vim.lsp.buf.code_action({ apply = true }) vim.lsp.stop_client(client_id) return server.messages - ]]) + end) eq('codeAction/resolve', result[4].method) eq('workspace/executeCommand', result[5].method) eq('command:1', result[5].params.command) @@ -4330,32 +4345,32 @@ describe('LSP', function() eq(table.remove(expected_handlers), { err, result, ctx }) if ctx.method == 'start' then local fake_uri = 'file:///fake/uri' - local cmd = exec_lua( - [[ - fake_uri = ... - local bufnr = vim.uri_to_bufnr(fake_uri) + local cmd = exec_lua(function(fake_uri0) + local bufnr = vim.uri_to_bufnr(fake_uri0) vim.fn.bufload(bufnr) - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {'One line'}) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'One line' }) local lenses = { { range = { - start = { line = 0, character = 0, }, - ['end'] = { line = 0, character = 8 } + start = { line = 0, character = 0 }, + ['end'] = { line = 0, character = 8 }, }, - command = { title = 'Lens1', command = 'Dummy' } + command = { title = 'Lens1', command = 'Dummy' }, }, } - vim.lsp.codelens.on_codelens(nil, lenses, {method='textDocument/codeLens', client_id=1, bufnr=bufnr}) + vim.lsp.codelens.on_codelens( + nil, + lenses, + { method = 'textDocument/codeLens', client_id = 1, bufnr = bufnr } + ) local cmd_called = nil - vim.lsp.commands['Dummy'] = function(command) - cmd_called = command + vim.lsp.commands['Dummy'] = function(command0) + cmd_called = command0 end vim.api.nvim_set_current_buf(bufnr) vim.lsp.codelens.run() return cmd_called - ]], - fake_uri - ) + end, fake_uri) eq({ command = 'Dummy', title = 'Lens1' }, cmd) elseif ctx.method == 'shutdown' then client.stop() @@ -4376,20 +4391,20 @@ describe('LSP', function() 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 - ]=]) + exec_lua(function() + 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, _G.TEST_RPC_CLIENT_ID) + + _G.CALLED = false + _G.RESPONSE = nil + local on_codelens = vim.lsp.codelens.on_codelens + vim.lsp.codelens.on_codelens = function(err, result, ...) + _G.CALLED = true + _G.RESPONSE = { err = err, result = result } + return on_codelens(err, result, ...) + end + end) end, on_exit = function(code, signal) eq(0, code, 'exit code') @@ -4399,42 +4414,52 @@ describe('LSP', function() eq(table.remove(expected_handlers), { err, result, ctx }) if ctx.method == 'start' then -- 1. first codelens request errors - local response = exec_lua([=[ - CALLED = false + local response = exec_lua(function() + _G.CALLED = false vim.lsp.codelens.refresh() - vim.wait(100, function () return CALLED end) - return RESPONSE - ]=]) + vim.wait(100, function() + return _G.CALLED + end) + return _G.RESPONSE + end) eq({ err = { code = -32002, message = 'ServerNotInitialized' } }, response) -- 2. second codelens request runs - response = exec_lua([=[ - CALLED = false + response = exec_lua(function() + _G.CALLED = false local cmd_called = nil - vim.lsp.commands["Dummy"] = function (command) - cmd_called = command + vim.lsp.commands['Dummy'] = function(command0) + cmd_called = command0 end vim.lsp.codelens.refresh() - vim.wait(100, function () return CALLED end) + vim.wait(100, function() + return _G.CALLED + end) vim.lsp.codelens.run() - vim.wait(100, function () return cmd_called end) + vim.wait(100, function() + return cmd_called + end) return cmd_called - ]=]) + end) eq({ command = 'Dummy', title = 'Lens1' }, response) -- 3. third codelens request runs - response = exec_lua([=[ - CALLED = false + response = exec_lua(function() + _G.CALLED = false local cmd_called = nil - vim.lsp.commands["Dummy"] = function (command) - cmd_called = command + vim.lsp.commands['Dummy'] = function(command0) + cmd_called = command0 end vim.lsp.codelens.refresh() - vim.wait(100, function () return CALLED end) + vim.wait(100, function() + return _G.CALLED + end) vim.lsp.codelens.run() - vim.wait(100, function () return cmd_called end) + vim.wait(100, function() + return cmd_called + end) return cmd_called - ]=]) + end) eq({ command = 'Dummy', title = 'Lens2' }, response) elseif ctx.method == 'shutdown' then client.stop() @@ -4452,79 +4477,73 @@ describe('LSP', function() exec_lua(create_server_definition) -- setup lsp - exec_lua( - [[ - local lens_title_per_fake_uri = ... - local server = _create_server({ - capabilities = { - codeLensProvider = { - resolveProvider = true - }, + exec_lua(function(lens_title_per_fake_uri0) + local server = _G._create_server({ + capabilities = { + codeLensProvider = { + resolveProvider = true, }, - handlers = { - ["textDocument/codeLens"] = function(method, params, callback) - local lenses = { - { - range = { - start = { line = 0, character = 0 }, - ['end'] = { line = 0, character = 0 }, - }, - command = { - title = lens_title_per_fake_uri[params.textDocument.uri], - command = 'Dummy', - }, + }, + handlers = { + ['textDocument/codeLens'] = function(_, params, callback) + local lenses = { + { + range = { + start = { line = 0, character = 0 }, + ['end'] = { line = 0, character = 0 }, }, - } - callback(nil, lenses) - end, - } - }) + command = { + title = lens_title_per_fake_uri0[params.textDocument.uri], + command = 'Dummy', + }, + }, + } + callback(nil, lenses) + end, + }, + }) - CLIENT_ID = vim.lsp.start({ - name = "dummy", - cmd = server.cmd, - }) - ]], - lens_title_per_fake_uri - ) + _G.CLIENT_ID = vim.lsp.start({ + name = 'dummy', + cmd = server.cmd, + }) + end, lens_title_per_fake_uri) -- create buffers and setup handler - exec_lua( - [[ - local lens_title_per_fake_uri = ... - local default_buf = vim.api.nvim_get_current_buf() - for fake_uri, _ in pairs(lens_title_per_fake_uri) do - local bufnr = vim.uri_to_bufnr(fake_uri) - vim.api.nvim_set_current_buf(bufnr) - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {'Some contents'}) - vim.lsp.buf_attach_client(bufnr, CLIENT_ID) - end - vim.api.nvim_buf_delete(default_buf, {force = true}) - - REQUEST_COUNT = vim.tbl_count(lens_title_per_fake_uri) - RESPONSES = {} - local on_codelens = vim.lsp.codelens.on_codelens - vim.lsp.codelens.on_codelens = function (err, result, ctx, ...) - table.insert(RESPONSES, { err = err, result = result, ctx = ctx }) - return on_codelens(err, result, ctx, ...) - end - ]], - lens_title_per_fake_uri - ) + exec_lua(function(lens_title_per_fake_uri0) + local default_buf = vim.api.nvim_get_current_buf() + for fake_uri, _ in pairs(lens_title_per_fake_uri0) do + local bufnr = vim.uri_to_bufnr(fake_uri) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'Some contents' }) + vim.lsp.buf_attach_client(bufnr, _G.CLIENT_ID) + end + vim.api.nvim_buf_delete(default_buf, { force = true }) + + _G.REQUEST_COUNT = vim.tbl_count(lens_title_per_fake_uri0) + _G.RESPONSES = {} + local on_codelens = vim.lsp.codelens.on_codelens + vim.lsp.codelens.on_codelens = function(err, result, ctx, ...) + table.insert(_G.RESPONSES, { err = err, result = result, ctx = ctx }) + return on_codelens(err, result, ctx, ...) + end + end, lens_title_per_fake_uri) -- call codelens refresh - local cmds = exec_lua([[ - RESPONSES = {} + local cmds = exec_lua(function() + _G.RESPONSES = {} vim.lsp.codelens.refresh() - vim.wait(100, function () return #RESPONSES >= REQUEST_COUNT end) + vim.wait(100, function() + return #_G.RESPONSES >= _G.REQUEST_COUNT + end) local cmds = {} - for _, resp in ipairs(RESPONSES) do + for _, resp in ipairs(_G.RESPONSES) do local uri = resp.ctx.params.textDocument.uri cmds[uri] = resp.result[1].command end return cmds - ]]) + end) eq({ command = 'Dummy', title = 'Lens1' }, cmds['file:///fake/uri1']) eq({ command = 'Dummy', title = 'Lens2' }, cmds['file:///fake/uri2']) end) @@ -4539,18 +4558,18 @@ describe('LSP', function() client = c end, on_handler = function() - local notify_msg = exec_lua([[ + local notify_msg = exec_lua(function() local bufnr = vim.api.nvim_get_current_buf() - vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.lsp.buf_attach_client(bufnr, _G.TEST_RPC_CLIENT_ID) local notify_msg local notify = vim.notify - vim.notify = function(msg, log_level) + vim.notify = function(msg, _) notify_msg = msg end vim.lsp.buf.format({ name = 'does-not-exist' }) vim.notify = notify return notify_msg - ]]) + end) eq('[LSP] Format request failed, no matching language servers.', notify_msg) client.stop() end, @@ -4570,18 +4589,18 @@ describe('LSP', function() on_handler = function(_, _, ctx) table.remove(expected_handlers) if ctx.method == 'start' then - local notify_msg = exec_lua([[ + local notify_msg = exec_lua(function() local bufnr = vim.api.nvim_get_current_buf() - vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.lsp.buf_attach_client(bufnr, _G.TEST_RPC_CLIENT_ID) local notify_msg local notify = vim.notify - vim.notify = function(msg, log_level) + vim.notify = function(msg, _) notify_msg = msg end vim.lsp.buf.format({ bufnr = bufnr }) vim.notify = notify return notify_msg - ]]) + end) eq(NIL, notify_msg) elseif ctx.method == 'shutdown' then client.stop() @@ -4603,22 +4622,25 @@ describe('LSP', function() on_handler = function(_, _, ctx) table.remove(expected_handlers) if ctx.method == 'start' then - local notify_msg = exec_lua([[ + local notify_msg = exec_lua(function() local bufnr = vim.api.nvim_get_current_buf() - vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, {'foo', 'bar'}) - vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { 'foo', 'bar' }) + vim.lsp.buf_attach_client(bufnr, _G.TEST_RPC_CLIENT_ID) local notify_msg local notify = vim.notify - vim.notify = function(msg, log_level) + vim.notify = function(msg, _) notify_msg = msg end - vim.lsp.buf.format({ bufnr = bufnr, range = { - start = {1, 1}, - ['end'] = {1, 1}, - }}) + vim.lsp.buf.format({ + bufnr = bufnr, + range = { + start = { 1, 1 }, + ['end'] = { 1, 1 }, + }, + }) vim.notify = notify return notify_msg - ]]) + end) eq(NIL, notify_msg) elseif ctx.method == 'shutdown' then client.stop() @@ -4640,28 +4662,31 @@ describe('LSP', function() on_handler = function(_, _, ctx) table.remove(expected_handlers) if ctx.method == 'start' then - local notify_msg = exec_lua([[ + local notify_msg = exec_lua(function() local bufnr = vim.api.nvim_get_current_buf() - vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, {'foo', 'bar', 'baz'}) - vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { 'foo', 'bar', 'baz' }) + vim.lsp.buf_attach_client(bufnr, _G.TEST_RPC_CLIENT_ID) local notify_msg local notify = vim.notify - vim.notify = function(msg, log_level) + vim.notify = function(msg, _) notify_msg = msg end - vim.lsp.buf.format({ bufnr = bufnr, range = { - { - start = {1, 1}, - ['end'] = {1, 1}, + vim.lsp.buf.format({ + bufnr = bufnr, + range = { + { + start = { 1, 1 }, + ['end'] = { 1, 1 }, + }, + { + start = { 2, 2 }, + ['end'] = { 2, 2 }, + }, }, - { - start = {2, 2}, - ['end'] = {2, 2}, - } - }}) + }) vim.notify = notify return notify_msg - ]]) + end) eq(NIL, notify_msg) elseif ctx.method == 'shutdown' then client.stop() @@ -4683,29 +4708,31 @@ describe('LSP', function() on_handler = function(_, _, ctx) table.remove(expected_handlers) if ctx.method == 'start' then - local result = exec_lua([[ + local result = exec_lua(function() local bufnr = vim.api.nvim_get_current_buf() - vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID) + vim.lsp.buf_attach_client(bufnr, _G.TEST_RPC_CLIENT_ID) local notify_msg local notify = vim.notify - vim.notify = function(msg, log_level) + vim.notify = function(msg, _) notify_msg = msg end local handler = vim.lsp.handlers['textDocument/formatting'] local handler_called = false - vim.lsp.handlers['textDocument/formatting'] = function(...) + 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.wait(1000, function() + return handler_called + end) vim.notify = notify vim.lsp.handlers['textDocument/formatting'] = handler - return {notify = notify_msg, handler_called = handler_called} - ]]) + return { notify = notify_msg, handler_called = handler_called } + end) eq({ handler_called = true }, result) elseif ctx.method == 'shutdown' then client.stop() @@ -4715,22 +4742,24 @@ describe('LSP', function() end) it('format formats range in visual mode', function() exec_lua(create_server_definition) - local result = exec_lua([[ - local server = _create_server({ capabilities = { - documentFormattingProvider = true, - documentRangeFormattingProvider = true, - }}) + local result = exec_lua(function() + local server = _G._create_server({ + capabilities = { + documentFormattingProvider = true, + documentRangeFormattingProvider = true, + }, + }) local bufnr = vim.api.nvim_get_current_buf() local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) vim.api.nvim_win_set_buf(0, bufnr) - vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, {'foo', 'bar'}) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { 'foo', 'bar' }) vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.cmd.normal('v') vim.api.nvim_win_set_cursor(0, { 2, 3 }) vim.lsp.buf.format({ bufnr = bufnr, false }) vim.lsp.stop_client(client_id) return server.messages - ]]) + end) eq('textDocument/rangeFormatting', result[3].method) local expected_range = { start = { line = 0, character = 0 }, @@ -4740,15 +4769,17 @@ describe('LSP', function() end) it('format formats range in visual line mode', function() exec_lua(create_server_definition) - local result = exec_lua([[ - local server = _create_server({ capabilities = { - documentFormattingProvider = true, - documentRangeFormattingProvider = true, - }}) + local result = exec_lua(function() + local server = _G._create_server({ + capabilities = { + documentFormattingProvider = true, + documentRangeFormattingProvider = true, + }, + }) local bufnr = vim.api.nvim_get_current_buf() local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) vim.api.nvim_win_set_buf(0, bufnr) - vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, {'foo', 'bar baz'}) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { 'foo', 'bar baz' }) vim.api.nvim_win_set_cursor(0, { 1, 2 }) vim.cmd.normal('V') vim.api.nvim_win_set_cursor(0, { 2, 1 }) @@ -4756,7 +4787,7 @@ describe('LSP', function() -- Format again with visual lines going from bottom to top -- Must result in same formatting - vim.cmd.normal("<ESC>") + vim.cmd.normal('<ESC>') vim.api.nvim_win_set_cursor(0, { 2, 1 }) vim.cmd.normal('V') vim.api.nvim_win_set_cursor(0, { 1, 2 }) @@ -4764,7 +4795,7 @@ describe('LSP', function() vim.lsp.stop_client(client_id) return server.messages - ]]) + end) local expected_methods = { 'initialize', 'initialized', @@ -4791,37 +4822,43 @@ describe('LSP', function() end) it('Aborts with notify if no clients support requested method', function() exec_lua(create_server_definition) - exec_lua([[ + exec_lua(function() vim.notify = function(msg, _) - notify_msg = msg + _G.notify_msg = msg end - ]]) + end) local fail_msg = '[LSP] Format request failed, no matching language servers.' --- @param name string --- @param formatting boolean --- @param range_formatting boolean local function check_notify(name, formatting, range_formatting) local timeout_msg = '[LSP][' .. name .. '] timeout' - exec_lua( - [[ - local formatting, range_formatting, name = ... - local server = _create_server({ capabilities = { - documentFormattingProvider = formatting, - documentRangeFormattingProvider = range_formatting, - }}) - vim.lsp.start({ name = name, cmd = server.cmd }) - notify_msg = nil - vim.lsp.buf.format({ name = name, timeout_ms = 1 }) - ]], - formatting, - range_formatting, - name - ) + exec_lua(function(formatting0, range_formatting0, name0) + local server = _G._create_server({ + capabilities = { + documentFormattingProvider = formatting0, + documentRangeFormattingProvider = range_formatting0, + }, + }) + vim.lsp.start({ name = name0, cmd = server.cmd }) + _G.notify_msg = nil + vim.lsp.buf.format({ name = name0, timeout_ms = 1 }) + end, formatting, range_formatting, name) eq(formatting and timeout_msg or fail_msg, exec_lua('return notify_msg')) - exec_lua([[ - notify_msg = nil - vim.lsp.buf.format({ name = name, timeout_ms = 1, range = {start={1, 0}, ['end']={1, 0}}}) - ]]) + exec_lua(function() + _G.notify_msg = nil + vim.lsp.buf.format({ + name = name, + timeout_ms = 1, + range = { + start = { 1, 0 }, + ['end'] = { + 1, + 0, + }, + }, + }) + end) eq(range_formatting and timeout_msg or fail_msg, exec_lua('return notify_msg')) end check_notify('none', false, false) @@ -4852,10 +4889,9 @@ describe('LSP', function() }, } exec_lua(create_server_definition) - exec_lua( - [[ - _G.mock_locations = ... - _G.server = _create_server({ + exec_lua(function(mock_locations0) + _G.mock_locations = mock_locations0 + _G.server = _G._create_server({ ---@type lsp.ServerCapabilities capabilities = { definitionProvider = true, @@ -4879,26 +4915,24 @@ describe('LSP', function() name = 'vim.foobar', kind = 12, ---@type lsp.SymbolKind location = _G.mock_locations[2], - } + }, }) end, }, }) - _G.client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd }) - ]], - mock_locations - ) + _G.client_id = vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd }) + end, mock_locations) end) after_each(function() - exec_lua [[ + exec_lua(function() vim.lsp.stop_client(_G.client_id) - ]] + end) end) it('with flags=c, returns matching tags using textDocument/definition', function() - local result = exec_lua [[ + local result = exec_lua(function() return vim.lsp.tagfunc('foobar', 'c') - ]] + end) eq({ { cmd = '/\\%6l\\%1c/', -- for location (5, 23) @@ -4909,9 +4943,9 @@ describe('LSP', function() end) it('without flags=c, returns all matching tags using workspace/symbol', function() - local result = exec_lua [[ + local result = exec_lua(function() return vim.lsp.tagfunc('foobar', '') - ]] + end) eq({ { cmd = '/\\%6l\\%1c/', -- for location (5, 23) @@ -4931,7 +4965,7 @@ describe('LSP', function() describe('cmd', function() it('can connect to lsp server via rpc.connect', function() - local result = exec_lua [[ + local result = exec_lua(function() local uv = vim.uv local server = uv.new_tcp() local init = nil @@ -4947,12 +4981,14 @@ describe('LSP', function() end) local port = server:getsockname().port vim.lsp.start({ name = 'dummy', cmd = vim.lsp.rpc.connect('127.0.0.1', port) }) - vim.wait(1000, function() return init ~= nil end) - assert(init, "server must receive `initialize` request") + vim.wait(1000, function() + return init ~= nil + end) + assert(init, 'server must receive `initialize` request') server:close() server:shutdown() return vim.json.decode(init) - ]] + end) eq('initialize', result.method) end) it('can connect to lsp server via pipe or domain_socket', function() @@ -4963,39 +4999,37 @@ describe('LSP', function() tmpfile = tmpname() os.remove(tmpfile) end - local result = exec_lua( - [[ - local SOCK = ... + local result = exec_lua(function(SOCK) local uv = vim.uv local server = uv.new_pipe(false) server:bind(SOCK) local init = nil server:listen(127, function(err) - assert(not err, err) - local client = uv.new_pipe() - server:accept(client) - client:read_start(require("vim.lsp.rpc").create_read_loop(function(body) - init = body - client:close() - end)) + assert(not err, err) + local client = uv.new_pipe() + server:accept(client) + client:read_start(require('vim.lsp.rpc').create_read_loop(function(body) + init = body + client:close() + end)) + end) + vim.lsp.start({ name = 'dummy', cmd = vim.lsp.rpc.connect(SOCK) }) + vim.wait(1000, function() + return init ~= nil end) - vim.lsp.start({ name = "dummy", cmd = vim.lsp.rpc.connect(SOCK) }) - vim.wait(1000, function() return init ~= nil end) - assert(init, "server must receive `initialize` request") + assert(init, 'server must receive `initialize` request') server:close() server:shutdown() return vim.json.decode(init) - ]], - tmpfile - ) + end, tmpfile) eq('initialize', result.method) end) end) describe('handlers', function() it('handler can return false as response', function() - local result = exec_lua [[ + local result = exec_lua(function() local uv = vim.uv local server = uv.new_tcp() local messages = {} @@ -5014,10 +5048,10 @@ describe('LSP', function() id = payload.id, jsonrpc = '2.0', result = { - capabilities = {} + capabilities = {}, }, }) - socket:write(table.concat({'Content-Length: ', tostring(#msg), '\r\n\r\n', msg})) + socket:write(table.concat({ 'Content-Length: ', tostring(#msg), '\r\n\r\n', msg })) elseif payload.method == 'initialized' then local msg = vim.json.encode({ id = 10, @@ -5025,7 +5059,7 @@ describe('LSP', function() method = 'dummy', params = {}, }) - socket:write(table.concat({'Content-Length: ', tostring(#msg), '\r\n\r\n', msg})) + socket:write(table.concat({ 'Content-Length: ', tostring(#msg), '\r\n\r\n', msg })) end else table.insert(responses, payload) @@ -5035,20 +5069,24 @@ describe('LSP', function() end) local port = server:getsockname().port local handler_called = false - vim.lsp.handlers['dummy'] = function(err, result) + vim.lsp.handlers['dummy'] = function(_, _) handler_called = true return false end - local client_id = vim.lsp.start({ name = 'dummy', cmd = vim.lsp.rpc.connect('127.0.0.1', port) }) - local client = vim.lsp.get_client_by_id(client_id) - vim.wait(1000, function() return #messages == 2 and handler_called and #responses == 1 end) + local client_id = + vim.lsp.start({ name = 'dummy', cmd = vim.lsp.rpc.connect('127.0.0.1', port) }) + vim.lsp.get_client_by_id(client_id) + vim.wait(1000, function() + return #messages == 2 and handler_called and #responses == 1 + end) server:close() server:shutdown() return { messages = messages, handler_called = handler_called, - responses = responses } - ]] + responses = responses, + } + end) local expected = { messages = { 'initialize', 'initialized' }, handler_called = true, @@ -5077,17 +5115,14 @@ describe('LSP', function() end exec_lua(create_server_definition) - local result = exec_lua( - [[ - local root_dir, tmpfile = ... - - local server = _create_server() + local result = exec_lua(function(root_dir0, tmpfile0) + local server = _G._create_server() local client_id = vim.lsp.start({ name = 'dynamic-test', cmd = server.cmd, - root_dir = root_dir, + root_dir = root_dir0, get_language_id = function() - return "dummy-lang" + return 'dummy-lang' end, capabilities = { textDocument = { @@ -5101,17 +5136,17 @@ describe('LSP', function() }, }) - local expected_messages = 2 -- initialize, initialized - vim.lsp.handlers['client/registerCapability'](nil, { registrations = { { id = 'formatting', method = 'textDocument/formatting', registerOptions = { - documentSelector = {{ - pattern = root_dir .. '/*.foo', - }}, + documentSelector = { + { + pattern = root_dir0 .. '/*.foo', + }, + }, }, }, }, @@ -5123,12 +5158,12 @@ describe('LSP', function() id = 'range-formatting', method = 'textDocument/rangeFormatting', registerOptions = { - documentSelector = { + documentSelector = { { - language = "dummy-lang" + language = 'dummy-lang', }, - } - } + }, + }, }, }, }, { client_id = client_id }) @@ -5149,22 +5184,18 @@ describe('LSP', function() result[#result + 1] = { method = method, fname = fname, - supported = client.supports_method(method, {bufnr = bufnr}) + supported = client.supports_method(method, { bufnr = bufnr }), } end - - check("textDocument/formatting") - check("textDocument/formatting", tmpfile) - check("textDocument/rangeFormatting") - check("textDocument/rangeFormatting", tmpfile) - check("textDocument/completion") + check('textDocument/formatting') + check('textDocument/formatting', tmpfile0) + check('textDocument/rangeFormatting') + check('textDocument/rangeFormatting', tmpfile0) + check('textDocument/completion') return result - ]], - root_dir, - tmpfile - ) + end, root_dir, tmpfile) eq(5, #result) eq({ method = 'textDocument/formatting', supported = false }, result[1]) @@ -5207,82 +5238,76 @@ describe('LSP', function() mkdir(root_dir) exec_lua(create_server_definition) - local result = exec_lua( - [[ - local root_dir, watchfunc = ... - - local server = _create_server() - local client_id = vim.lsp.start({ - name = 'watchfiles-test', - cmd = server.cmd, - root_dir = root_dir, - capabilities = { - workspace = { - didChangeWatchedFiles = { - dynamicRegistration = true, + local result = exec_lua(function(root_dir0, watchfunc0) + local server = _G._create_server() + local client_id = vim.lsp.start({ + name = 'watchfiles-test', + cmd = server.cmd, + root_dir = root_dir0, + capabilities = { + workspace = { + didChangeWatchedFiles = { + dynamicRegistration = true, + }, }, }, - }, - }) + }) - require('vim.lsp._watchfiles')._watchfunc = require('vim._watch')[watchfunc] + require('vim.lsp._watchfiles')._watchfunc = require('vim._watch')[watchfunc0] - local expected_messages = 0 + local expected_messages = 0 - local msg_wait_timeout = watchfunc == 'watch' and 200 or 2500 + local msg_wait_timeout = watchfunc0 == 'watch' and 200 or 2500 - local function wait_for_message(incr) - expected_messages = expected_messages + (incr or 1) - assert( - vim.wait(msg_wait_timeout, function() - return #server.messages == expected_messages - end), - 'Timed out waiting for expected number of messages. Current messages seen so far: ' - .. vim.inspect(server.messages) - ) - end + local function wait_for_message(incr) + expected_messages = expected_messages + (incr or 1) + assert( + vim.wait(msg_wait_timeout, function() + return #server.messages == expected_messages + end), + 'Timed out waiting for expected number of messages. Current messages seen so far: ' + .. vim.inspect(server.messages) + ) + end - wait_for_message(2) -- initialize, initialized + wait_for_message(2) -- initialize, initialized - vim.lsp.handlers['client/registerCapability'](nil, { - registrations = { - { - id = 'watchfiles-test-0', - method = 'workspace/didChangeWatchedFiles', - registerOptions = { - watchers = { - { - globPattern = '**/watch', - kind = 7, + vim.lsp.handlers['client/registerCapability'](nil, { + registrations = { + { + id = 'watchfiles-test-0', + method = 'workspace/didChangeWatchedFiles', + registerOptions = { + watchers = { + { + globPattern = '**/watch', + kind = 7, + }, }, }, }, }, - }, - }, { client_id = client_id }) + }, { client_id = client_id }) - if watchfunc ~= 'watch' then - vim.wait(100) - end + if watchfunc0 ~= 'watch' then + vim.wait(100) + end - local path = root_dir .. '/watch' - local tmp = vim.fn.tempname() - io.open(tmp, 'w'):close() - vim.uv.fs_rename(tmp, path) + local path = root_dir0 .. '/watch' + local tmp = vim.fn.tempname() + io.open(tmp, 'w'):close() + vim.uv.fs_rename(tmp, path) - wait_for_message() + wait_for_message() - os.remove(path) + os.remove(path) - wait_for_message() + wait_for_message() - vim.lsp.stop_client(client_id) + vim.lsp.stop_client(client_id) - return server.messages - ]], - root_dir, - watchfunc - ) + return server.messages + end, root_dir, watchfunc) local uri = vim.uri_from_fname(root_dir .. '/watch') @@ -5322,15 +5347,12 @@ describe('LSP', function() it('correctly registers and unregisters', function() local root_dir = '/some_dir' exec_lua(create_server_definition) - local result = exec_lua( - [[ - local root_dir = ... - - local server = _create_server() + local result = exec_lua(function(root_dir0) + local server = _G._create_server() local client_id = vim.lsp.start({ name = 'watchfiles-test', cmd = server.cmd, - root_dir = root_dir, + root_dir = root_dir0, capabilities = { workspace = { didChangeWatchedFiles = { @@ -5342,7 +5364,13 @@ describe('LSP', function() local expected_messages = 2 -- initialize, initialized local function wait_for_messages() - assert(vim.wait(200, function() return #server.messages == expected_messages end), 'Timed out waiting for expected number of messages. Current messages seen so far: ' .. vim.inspect(server.messages)) + assert( + vim.wait(200, function() + return #server.messages == expected_messages + end), + 'Timed out waiting for expected number of messages. Current messages seen so far: ' + .. vim.inspect(server.messages) + ) end wait_for_messages() @@ -5376,8 +5404,8 @@ describe('LSP', function() }, }, { client_id = client_id }) - send_event(root_dir .. '/file.watch0', vim._watch.FileChangeType.Created) - send_event(root_dir .. '/file.watch1', vim._watch.FileChangeType.Created) + send_event(root_dir0 .. '/file.watch0', vim._watch.FileChangeType.Created) + send_event(root_dir0 .. '/file.watch1', vim._watch.FileChangeType.Created) expected_messages = expected_messages + 1 wait_for_messages() @@ -5407,16 +5435,14 @@ describe('LSP', function() }, }, { client_id = client_id }) - send_event(root_dir .. '/file.watch0', vim._watch.FileChangeType.Created) - send_event(root_dir .. '/file.watch1', vim._watch.FileChangeType.Created) + send_event(root_dir0 .. '/file.watch0', vim._watch.FileChangeType.Created) + send_event(root_dir0 .. '/file.watch1', vim._watch.FileChangeType.Created) expected_messages = expected_messages + 1 wait_for_messages() return server.messages - ]], - root_dir - ) + end, root_dir) local function watched_uri(fname) return vim.uri_from_fname(root_dir .. '/' .. fname) @@ -5446,15 +5472,12 @@ describe('LSP', function() it('correctly handles the registered watch kind', function() local root_dir = 'some_dir' exec_lua(create_server_definition) - local result = exec_lua( - [[ - local root_dir = ... - - local server = _create_server() + local result = exec_lua(function(root_dir0) + local server = _G._create_server() local client_id = vim.lsp.start({ name = 'watchfiles-test', cmd = server.cmd, - root_dir = root_dir, + root_dir = root_dir0, capabilities = { workspace = { didChangeWatchedFiles = { @@ -5466,7 +5489,13 @@ describe('LSP', function() local expected_messages = 2 -- initialize, initialized local function wait_for_messages() - assert(vim.wait(200, function() return #server.messages == expected_messages end), 'Timed out waiting for expected number of messages. Current messages seen so far: ' .. vim.inspect(server.messages)) + assert( + vim.wait(200, function() + return #server.messages == expected_messages + end), + 'Timed out waiting for expected number of messages. Current messages seen so far: ' + .. vim.inspect(server.messages) + ) end wait_for_messages() @@ -5487,12 +5516,14 @@ describe('LSP', function() local protocol = require('vim.lsp.protocol') local watchers = {} - local max_kind = protocol.WatchKind.Create + protocol.WatchKind.Change + protocol.WatchKind.Delete + local max_kind = protocol.WatchKind.Create + + protocol.WatchKind.Change + + protocol.WatchKind.Delete for i = 0, max_kind do table.insert(watchers, { globPattern = { baseUri = vim.uri_from_fname('/dir'), - pattern = 'watch'..tostring(i), + pattern = 'watch' .. tostring(i), }, kind = i, }) @@ -5520,9 +5551,7 @@ describe('LSP', function() wait_for_messages() return server.messages - ]], - root_dir - ) + end, root_dir) local function watched_uri(fname) return vim.uri_from_fname('/dir/' .. fname) @@ -5587,15 +5616,12 @@ describe('LSP', function() it('prunes duplicate events', function() local root_dir = 'some_dir' exec_lua(create_server_definition) - local result = exec_lua( - [[ - local root_dir = ... - - local server = _create_server() + local result = exec_lua(function(root_dir0) + local server = _G._create_server() local client_id = vim.lsp.start({ name = 'watchfiles-test', cmd = server.cmd, - root_dir = root_dir, + root_dir = root_dir0, capabilities = { workspace = { didChangeWatchedFiles = { @@ -5607,7 +5633,13 @@ describe('LSP', function() local expected_messages = 2 -- initialize, initialized local function wait_for_messages() - assert(vim.wait(200, function() return #server.messages == expected_messages end), 'Timed out waiting for expected number of messages. Current messages seen so far: ' .. vim.inspect(server.messages)) + assert( + vim.wait(200, function() + return #server.messages == expected_messages + end), + 'Timed out waiting for expected number of messages. Current messages seen so far: ' + .. vim.inspect(server.messages) + ) end wait_for_messages() @@ -5646,9 +5678,7 @@ describe('LSP', function() wait_for_messages() return server.messages - ]], - root_dir - ) + end, root_dir) eq(3, #result) eq('workspace/didChangeWatchedFiles', result[3].method) @@ -5672,26 +5702,27 @@ describe('LSP', function() it("ignores registrations by servers when the client doesn't advertise support", function() exec_lua(create_server_definition) - exec_lua([[ - server = _create_server() - require('vim.lsp._watchfiles')._watchfunc = function(_, _, callback) + exec_lua(function() + _G.server = _G._create_server() + require('vim.lsp._watchfiles')._watchfunc = function(_, _, _) -- Since the registration is ignored, this should not execute and `watching` should stay false - watching = true + _G.watching = true return function() end end - ]]) + end) local function check_registered(capabilities) - return exec_lua( - [[ - watching = false + return exec_lua(function(capabilities0) + _G.watching = false local client_id = vim.lsp.start({ name = 'watchfiles-test', - cmd = server.cmd, + cmd = _G.server.cmd, root_dir = 'some_dir', - capabilities = ..., + capabilities = capabilities0, }, { - reuse_client = function() return false end, + reuse_client = function() + return false + end, }) vim.lsp.handlers['client/registerCapability'](nil, { @@ -5721,10 +5752,8 @@ describe('LSP', function() }, { client_id = client_id }) vim.lsp.stop_client(client_id, true) - return watching - ]], - capabilities - ) + return _G.watching + end, capabilities) end eq(is_os('mac') or is_os('win'), check_registered(nil)) -- start{_client}() defaults to make_client_capabilities(). diff --git a/test/functional/treesitter/fold_spec.lua b/test/functional/treesitter/fold_spec.lua index 3e81cebe71..24b085920c 100644 --- a/test/functional/treesitter/fold_spec.lua +++ b/test/functional/treesitter/fold_spec.lua @@ -48,13 +48,13 @@ void ui_refresh(void) end local function get_fold_levels() - return exec_lua([[ - local res = {} - for i = 1, vim.api.nvim_buf_line_count(0) do - res[i] = vim.treesitter.foldexpr(i) - end - return res - ]]) + return exec_lua(function() + local res = {} + for i = 1, vim.api.nvim_buf_line_count(0) do + res[i] = vim.treesitter.foldexpr(i) + end + return res + end) end it('can compute fold levels', function() @@ -246,9 +246,13 @@ function f() end -- comment]]) - exec_lua( - [[vim.treesitter.query.set('lua', 'folds', '[(function_declaration) (parameters) (arguments)] @fold')]] - ) + exec_lua(function() + vim.treesitter.query.set( + 'lua', + 'folds', + '[(function_declaration) (parameters) (arguments)] @fold' + ) + end) parse('lua') eq({ @@ -290,9 +294,13 @@ function f() ) end]]) - exec_lua( - [[vim.treesitter.query.set('lua', 'folds', '[(function_declaration) (function_definition) (parameters) (arguments)] @fold')]] - ) + exec_lua(function() + vim.treesitter.query.set( + 'lua', + 'folds', + '[(function_declaration) (function_definition) (parameters) (arguments)] @fold' + ) + end) parse('lua') -- If fold1.stop = fold2.start, then move fold1's stop up so that fold2.start gets proper level. @@ -333,9 +341,13 @@ function f(a) end end]]) - exec_lua( - [[vim.treesitter.query.set('lua', 'folds', '[(if_statement) (function_declaration) (parameters) (arguments) (table_constructor)] @fold')]] - ) + exec_lua(function() + vim.treesitter.query.set( + 'lua', + 'folds', + '[(if_statement) (function_declaration) (parameters) (arguments) (table_constructor)] @fold' + ) + end) parse('lua') eq({ diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index b0ac180738..da001e2ab1 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -156,7 +156,7 @@ local injection_grid_expected_c = [[ ]] describe('treesitter highlighting (C)', function() - local screen + local screen --- @type test.functional.ui.screen before_each(function() clear() @@ -176,7 +176,6 @@ describe('treesitter highlighting (C)', function() [11] = { foreground = Screen.colors.Cyan4 }, } - exec_lua([[ hl_query = ... ]], hl_query_c) command [[ hi link @error ErrorMsg ]] command [[ hi link @warning WarningMsg ]] end) @@ -188,22 +187,28 @@ describe('treesitter highlighting (C)', function() -- legacy syntax highlighting is used by default screen:expect(hl_grid_legacy_c) - exec_lua([[ + exec_lua(function(hl_query) vim.treesitter.query.set('c', 'highlights', hl_query) vim.treesitter.start() - ]]) + end, hl_query_c) -- treesitter highlighting is used screen:expect(hl_grid_ts_c) - exec_lua('vim.treesitter.stop()') + exec_lua(function() + vim.treesitter.stop() + end) -- legacy syntax highlighting is used screen:expect(hl_grid_legacy_c) - exec_lua('vim.treesitter.start()') + exec_lua(function() + vim.treesitter.start() + end) -- treesitter highlighting is used screen:expect(hl_grid_ts_c) - exec_lua('vim.treesitter.stop()') + exec_lua(function() + vim.treesitter.stop() + end) -- legacy syntax highlighting is used screen:expect(hl_grid_legacy_c) end) @@ -233,11 +238,11 @@ describe('treesitter highlighting (C)', function() ]], } - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c") + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c') local highlighter = vim.treesitter.highlighter - test_hl = highlighter.new(parser, {queries = {c = hl_query}}) - ]] + highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect(hl_grid_ts_c) feed('5Goc<esc>dd') @@ -364,10 +369,10 @@ describe('treesitter highlighting (C)', function() it('is updated with :sort', function() insert(test_text_c) - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c") - test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query}}) - ]] + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c') + vim.treesitter.highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect { grid = [[ {3:int} width = {5:INT_MAX}, height = {5:INT_MAX}; | @@ -470,19 +475,19 @@ describe('treesitter highlighting (C)', function() ]], } - exec_lua [[ - parser = vim.treesitter.get_parser(0, "c") - query = vim.treesitter.query.parse("c", "(declaration) @decl") + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') + local query = vim.treesitter.query.parse('c', '(declaration) @decl') local nodes = {} for _, node in query:iter_captures(parser:parse()[1]:root(), 0, 0, 19) do table.insert(nodes, node) end - parser:set_included_regions({nodes}) + parser:set_included_regions({ nodes }) - local hl = vim.treesitter.highlighter.new(parser, {queries = {c = "(identifier) @type"}}) - ]] + vim.treesitter.highlighter.new(parser, { queries = { c = '(identifier) @type' } }) + end) screen:expect { grid = [[ @@ -513,13 +518,15 @@ describe('treesitter highlighting (C)', function() screen:expect { grid = injection_grid_c } - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c", { - injections = {c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'} + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c', { + injections = { + c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))', + }, }) local highlighter = vim.treesitter.highlighter - test_hl = highlighter.new(parser, {queries = {c = hl_query}}) - ]] + highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect { grid = injection_grid_expected_c } end) @@ -529,14 +536,16 @@ describe('treesitter highlighting (C)', function() screen:expect { grid = injection_grid_c } - exec_lua [[ - vim.treesitter.language.register("c", "foo") - local parser = vim.treesitter.get_parser(0, "c", { - injections = {c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "foo")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "foo"))'} + exec_lua(function(hl_query) + vim.treesitter.language.register('c', 'foo') + local parser = vim.treesitter.get_parser(0, 'c', { + injections = { + c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "foo")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "foo"))', + }, }) local highlighter = vim.treesitter.highlighter - test_hl = highlighter.new(parser, {queries = {c = hl_query}}) - ]] + highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect { grid = injection_grid_expected_c } end) @@ -550,13 +559,14 @@ describe('treesitter highlighting (C)', function() } ]]) - exec_lua [[ - local injection_query = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' - vim.treesitter.query.set("c", "highlights", hl_query) - vim.treesitter.query.set("c", "injections", injection_query) + exec_lua(function(hl_query) + local injection_query = + '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' + vim.treesitter.query.set('c', 'highlights', hl_query) + vim.treesitter.query.set('c', 'injections', injection_query) - vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, "c")) - ]] + vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c')) + end, hl_query_c) screen:expect { grid = [[ @@ -576,10 +586,10 @@ describe('treesitter highlighting (C)', function() insert(hl_text_c) feed('gg') - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c") - test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query}}) - ]] + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c') + vim.treesitter.highlighter.new(parser, { queries = { c = hl_query } }) + end, hl_query_c) screen:expect(hl_grid_ts_c) @@ -619,10 +629,14 @@ describe('treesitter highlighting (C)', function() } ]]) - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c") - test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query..'\n((translation_unit) @constant (#set! "priority" 101))\n'}}) - ]] + exec_lua(function(hl_query) + local parser = vim.treesitter.get_parser(0, 'c') + vim.treesitter.highlighter.new(parser, { + queries = { + c = hl_query .. '\n((translation_unit) @constant (#set! "priority" 101))\n', + }, + }) + end, hl_query_c) -- expect everything to have Constant highlight screen:expect { grid = [[ @@ -669,11 +683,14 @@ describe('treesitter highlighting (C)', function() hi link @foo.bar Type hi link @foo String ]] - exec_lua [[ - local parser = vim.treesitter.get_parser(0, "c", {}) - local highlighter = vim.treesitter.highlighter - test_hl = highlighter.new(parser, {queries = {c = "(primitive_type) @foo.bar (string_literal) @foo"}}) - ]] + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c', {}) + local highlighter = vim.treesitter.highlighter + highlighter.new( + parser, + { queries = { c = '(primitive_type) @foo.bar (string_literal) @foo' } } + ) + end) screen:expect { grid = [[ @@ -701,10 +718,12 @@ describe('treesitter highlighting (C)', function() insert(hl_text_c) -- conceal can be empty or a single cchar. - exec_lua [=[ + exec_lua(function() vim.opt.cole = 2 - local parser = vim.treesitter.get_parser(0, "c") - test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = [[ + local parser = vim.treesitter.get_parser(0, 'c') + vim.treesitter.highlighter.new(parser, { + queries = { + c = [[ ("static" @keyword (#set! conceal "R")) @@ -717,8 +736,10 @@ describe('treesitter highlighting (C)', function() arguments: (argument_list) @arguments) (#eq? @function "multiqueue_put") (#set! @function conceal "V")) - ]]}}) - ]=] + ]], + }, + }) + end) screen:expect { grid = [[ @@ -775,11 +796,11 @@ describe('treesitter highlighting (C)', function() int z = 6; ]]) - exec_lua([[ + exec_lua(function() local query = '((declaration)+ @string)' vim.treesitter.query.set('c', 'highlights', query) vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c')) - ]]) + end) screen:expect { grid = [[ @@ -805,14 +826,10 @@ describe('treesitter highlighting (C)', function() declarator: (pointer_declarator) @variable.parameter) ]] - exec_lua( - [[ - local query = ... - vim.treesitter.query.set('c', 'highlights', query) + exec_lua(function(query_str) + vim.treesitter.query.set('c', 'highlights', query_str) vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c')) - ]], - query - ) + end, query) screen:expect { grid = [[ @@ -847,10 +864,10 @@ describe('treesitter highlighting (lua)', function() ffi.cdef("int (*fun)(int, char *);") ]] - exec_lua [[ + exec_lua(function() vim.bo.filetype = 'lua' vim.treesitter.start() - ]] + end) screen:expect { grid = [[ @@ -888,10 +905,10 @@ describe('treesitter highlighting (help)', function() < ]] - exec_lua [[ + exec_lua(function() vim.bo.filetype = 'help' vim.treesitter.start() - ]] + end) screen:expect { grid = [[ @@ -943,15 +960,15 @@ describe('treesitter highlighting (help)', function() ]] ]=]) - exec_lua [[ - parser = vim.treesitter.get_parser(0, "lua", { + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'lua', { injections = { - lua = '(string content: (_) @injection.content (#set! injection.language lua))' - } + lua = '(string content: (_) @injection.content (#set! injection.language lua))', + }, }) vim.treesitter.highlighter.new(parser) - ]] + end) screen:expect { grid = [=[ @@ -967,7 +984,7 @@ describe('treesitter highlighting (help)', function() end) describe('treesitter highlighting (nested injections)', function() - local screen + local screen --- @type test.functional.ui.screen before_each(function() clear() @@ -996,11 +1013,11 @@ vim.cmd([[ ]]) ]=] - exec_lua [[ + exec_lua(function() vim.opt.scrolloff = 0 vim.bo.filetype = 'lua' vim.treesitter.start() - ]] + end) -- invalidate the language tree feed('ggi--[[<ESC>04x') @@ -1041,10 +1058,10 @@ describe('treesitter highlighting (markdown)', function() clear() screen = Screen.new(40, 6) screen:attach() - exec_lua([[ + exec_lua(function() vim.bo.filetype = 'markdown' vim.treesitter.start() - ]]) + end) end) it('supports hyperlinks', function() diff --git a/test/functional/treesitter/inspect_tree_spec.lua b/test/functional/treesitter/inspect_tree_spec.lua index ef2ed8d970..6629751152 100644 --- a/test/functional/treesitter/inspect_tree_spec.lua +++ b/test/functional/treesitter/inspect_tree_spec.lua @@ -22,10 +22,10 @@ describe('vim.treesitter.inspect_tree', function() print() ]]) - exec_lua([[ + exec_lua(function() vim.treesitter.start(0, 'lua') vim.treesitter.inspect_tree() - ]]) + end) expect_tree [[ (chunk ; [0, 0] - [2, 0] @@ -40,10 +40,10 @@ describe('vim.treesitter.inspect_tree', function() print('hello') ]]) - exec_lua([[ + exec_lua(function() vim.treesitter.start(0, 'lua') vim.treesitter.inspect_tree() - ]]) + end) feed('a') expect_tree [[ @@ -67,11 +67,11 @@ describe('vim.treesitter.inspect_tree', function() ``` ]]) - exec_lua([[ + exec_lua(function() vim.treesitter.start(0, 'markdown') vim.treesitter.get_parser():parse() vim.treesitter.inspect_tree() - ]]) + end) expect_tree [[ (document ; [0, 0] - [4, 0] @@ -96,11 +96,11 @@ describe('vim.treesitter.inspect_tree', function() ``` ]]) - exec_lua([[ + exec_lua(function() vim.treesitter.start(0, 'markdown') vim.treesitter.get_parser():parse() vim.treesitter.inspect_tree() - ]]) + end) feed('I') expect_tree [[ @@ -125,28 +125,28 @@ describe('vim.treesitter.inspect_tree', function() ]]) -- setup two windows for the source buffer - exec_lua([[ - source_win = vim.api.nvim_get_current_win() + exec_lua(function() + _G.source_win = vim.api.nvim_get_current_win() vim.api.nvim_open_win(0, false, { win = 0, - split = 'left' + split = 'left', }) - ]]) + end) -- setup three windows for the tree buffer - exec_lua([[ + exec_lua(function() vim.treesitter.start(0, 'lua') vim.treesitter.inspect_tree() - tree_win = vim.api.nvim_get_current_win() - tree_win_copy_1 = vim.api.nvim_open_win(0, false, { + _G.tree_win = vim.api.nvim_get_current_win() + _G.tree_win_copy_1 = vim.api.nvim_open_win(0, false, { win = 0, - split = 'left' + split = 'left', }) - tree_win_copy_2 = vim.api.nvim_open_win(0, false, { + _G.tree_win_copy_2 = vim.api.nvim_open_win(0, false, { win = 0, - split = 'left' + split = 'left', }) - ]]) + end) -- close original source window exec_lua('vim.api.nvim_win_close(source_win, false)') diff --git a/test/functional/treesitter/language_spec.lua b/test/functional/treesitter/language_spec.lua index e71c39244f..ba2d2218e3 100644 --- a/test/functional/treesitter/language_spec.lua +++ b/test/functional/treesitter/language_spec.lua @@ -54,10 +54,10 @@ describe('treesitter language API', function() end) it('inspects language', function() - local keys, fields, symbols = unpack(exec_lua([[ + local keys, fields, symbols = unpack(exec_lua(function() local lang = vim.treesitter.language.inspect('c') local keys, symbols = {}, {} - for k,_ in pairs(lang) do + for k, _ in pairs(lang) do keys[k] = true end @@ -66,8 +66,8 @@ describe('treesitter language API', function() for _, v in pairs(lang.symbols) do table.insert(symbols, v) end - return {keys, lang.fields, symbols} - ]])) + return { keys, lang.fields, symbols } + end)) eq({ fields = true, symbols = true, _abi_version = true }, keys) @@ -113,12 +113,14 @@ describe('treesitter language API', function() int x = 3; }]]) - exec_lua([[ - langtree = vim.treesitter.get_parser(0, "c") - tree = langtree:tree_for_range({1, 3, 1, 3}) - ]]) - - eq('<node translation_unit>', exec_lua('return tostring(tree:root())')) + eq( + '<node translation_unit>', + exec_lua(function() + local langtree = vim.treesitter.get_parser(0, 'c') + local tree = langtree:tree_for_range({ 1, 3, 1, 3 }) + return tostring(tree:root()) + end) + ) end) it('retrieve the tree given a range when range is out of bounds relative to buffer', function() @@ -127,12 +129,14 @@ describe('treesitter language API', function() int x = 3; }]]) - exec_lua([[ - langtree = vim.treesitter.get_parser(0, "c") - tree = langtree:tree_for_range({10, 10, 10, 10}) - ]]) - - eq('<node translation_unit>', exec_lua('return tostring(tree:root())')) + eq( + '<node translation_unit>', + exec_lua(function() + local langtree = vim.treesitter.get_parser(0, 'c') + local tree = langtree:tree_for_range({ 10, 10, 10, 10 }) + return tostring(tree:root()) + end) + ) end) it('retrieve the node given a range', function() @@ -141,12 +145,14 @@ describe('treesitter language API', function() int x = 3; }]]) - exec_lua([[ - langtree = vim.treesitter.get_parser(0, "c") - node = langtree:named_node_for_range({1, 3, 1, 3}) - ]]) - - eq('<node primitive_type>', exec_lua('return tostring(node)')) + eq( + '<node primitive_type>', + exec_lua(function() + local langtree = vim.treesitter.get_parser(0, 'c') + local node = langtree:named_node_for_range({ 1, 3, 1, 3 }) + return tostring(node) + end) + ) end) it('retrieve an anonymous node given a range', function() diff --git a/test/functional/treesitter/node_spec.lua b/test/functional/treesitter/node_spec.lua index 6270ea3aa1..0e77c10e16 100644 --- a/test/functional/treesitter/node_spec.lua +++ b/test/functional/treesitter/node_spec.lua @@ -18,39 +18,39 @@ describe('treesitter node API', function() it('double free tree', function() insert('F') - exec_lua([[ + exec_lua(function() vim.treesitter.start(0, 'lua') vim.treesitter.get_node():tree() vim.treesitter.get_node():tree() collectgarbage() - ]]) + end) assert_alive() end) it('double free tree 2', function() - exec_lua([[ - parser = vim.treesitter.get_parser(0, "c") + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') local x = parser:parse()[1]:root():tree() - vim.api.nvim_buf_set_text(0, 0,0, 0,0, {'y'}) + vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { 'y' }) parser:parse() - vim.api.nvim_buf_set_text(0, 0,0, 0,1, {'z'}) + vim.api.nvim_buf_set_text(0, 0, 0, 0, 1, { 'z' }) parser:parse() collectgarbage() x:root() - ]]) + end) assert_alive() end) it('get_node() with lang given', function() -- this buffer doesn't have filetype set! insert('local foo = function() end') - exec_lua([[ - node = vim.treesitter.get_node({ + exec_lua(function() + _G.node = vim.treesitter.get_node({ bufnr = 0, - pos = { 0, 6 }, -- on "foo" + pos = { 0, 6 }, -- on "foo" lang = 'lua', }) - ]]) + end) eq('foo', lua_eval('vim.treesitter.get_node_text(node, 0)')) eq('identifier', lua_eval('node:type()')) end) @@ -79,16 +79,16 @@ describe('treesitter node API', function() } ]]) - exec_lua([[ - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - root = tree:root() - lang = vim.treesitter.language.inspect('c') + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + _G.root = tree:root() + vim.treesitter.language.inspect('c') - function node_text(node) + function _G.node_text(node) return vim.treesitter.get_node_text(node, 0) end - ]]) + end) exec_lua 'node = root:descendant_for_range(0, 11, 0, 16)' eq('int x', lua_eval('node_text(node)')) @@ -118,13 +118,13 @@ describe('treesitter node API', function() int x = 3; }]]) - local len = exec_lua([[ - tree = vim.treesitter.get_parser(0, "c"):parse()[1] - node = tree:root():child(0) - children = node:named_children() + local len = exec_lua(function() + local tree = vim.treesitter.get_parser(0, 'c'):parse()[1] + local node = tree:root():child(0) + _G.children = node:named_children() - return #children - ]]) + return #_G.children + end) eq(3, len) eq('<node compound_statement>', lua_eval('tostring(children[3])')) @@ -136,11 +136,11 @@ describe('treesitter node API', function() int x = 3; }]]) - exec_lua([[ - tree = vim.treesitter.get_parser(0, "c"):parse()[1] - root = tree:root() - node = root:child(0):child(2) - ]]) + exec_lua(function() + local tree = vim.treesitter.get_parser(0, 'c'):parse()[1] + _G.root = tree:root() + _G.node = _G.root:child(0):child(2) + end) eq(lua_eval('tostring(root)'), lua_eval('tostring(node:root())')) end) @@ -151,11 +151,11 @@ describe('treesitter node API', function() int x = 3; }]]) - exec_lua([[ - tree = vim.treesitter.get_parser(0, "c"):parse()[1] - root = tree:root() - child = root:child(0):child(0) - ]]) + exec_lua(function() + local tree = vim.treesitter.get_parser(0, 'c'):parse()[1] + _G.root = tree:root() + _G.child = _G.root:child(0):child(0) + end) eq(28, lua_eval('root:byte_length()')) eq(3, lua_eval('child:byte_length()')) @@ -167,15 +167,15 @@ describe('treesitter node API', function() int x = 3; }]]) - exec_lua([[ - tree = vim.treesitter.get_parser(0, "c"):parse()[1] - root = tree:root() - main = root:child(0) - body = main:child(2) - statement = body:child(1) - declarator = statement:child(1) - value = declarator:child(1) - ]]) + exec_lua(function() + local tree = vim.treesitter.get_parser(0, 'c'):parse()[1] + _G.root = tree:root() + _G.main = _G.root:child(0) + _G.body = _G.main:child(2) + _G.statement = _G.body:child(1) + _G.declarator = _G.statement:child(1) + _G.value = _G.declarator:child(1) + end) eq(lua_eval('main:type()'), lua_eval('root:child_containing_descendant(value):type()')) eq(lua_eval('body:type()'), lua_eval('main:child_containing_descendant(value):type()')) diff --git a/test/functional/treesitter/parser_spec.lua b/test/functional/treesitter/parser_spec.lua index dbd6bb3c23..46e6a6002a 100644 --- a/test/functional/treesitter/parser_spec.lua +++ b/test/functional/treesitter/parser_spec.lua @@ -12,9 +12,9 @@ local feed = n.feed describe('treesitter parser API', function() before_each(function() clear() - exec_lua [[ + exec_lua(function() vim.g.__ts_debug = 1 - ]] + end) end) it('parses buffer', function() @@ -23,12 +23,12 @@ describe('treesitter parser API', function() int x = 3; }]]) - exec_lua([[ - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - root = tree:root() - lang = vim.treesitter.language.inspect('c') - ]]) + exec_lua(function() + _G.parser = vim.treesitter.get_parser(0, 'c') + _G.tree = _G.parser:parse()[1] + _G.root = _G.tree:root() + _G.lang = vim.treesitter.language.inspect('c') + end) eq('<tree>', exec_lua('return tostring(tree)')) eq('<node translation_unit>', exec_lua('return tostring(root)')) @@ -59,11 +59,11 @@ describe('treesitter parser API', function() ) feed('2G7|ay') - exec_lua([[ - tree2 = parser:parse()[1] - root2 = tree2:root() - descendant2 = root2:descendant_for_range(1,2,1,13) - ]]) + exec_lua(function() + _G.tree2 = _G.parser:parse()[1] + _G.root2 = _G.tree2:root() + _G.descendant2 = _G.root2:descendant_for_range(1, 2, 1, 13) + end) eq(false, exec_lua('return tree2 == tree1')) eq(false, exec_lua('return root2 == root')) eq('<node declaration>', exec_lua('return tostring(descendant2)')) @@ -112,17 +112,17 @@ void ui_refresh(void) it('allows to iterate over nodes children', function() insert(test_text) - local res = exec_lua([[ - parser = vim.treesitter.get_parser(0, "c") + local res = exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') - func_node = parser:parse()[1]:root():child(0) + local func_node = parser:parse()[1]:root():child(0) - res = {} + local res = {} for node, field in func_node:iter_children() do table.insert(res, { node:type(), field }) end return res - ]]) + end) eq({ { 'primitive_type', 'type' }, @@ -148,43 +148,43 @@ void ui_refresh(void) it('allows to get a child by field', function() insert(test_text) - local res = exec_lua([[ - parser = vim.treesitter.get_parser(0, "c") + local res = exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') - func_node = parser:parse()[1]:root():child(0) + _G.func_node = parser:parse()[1]:root():child(0) local res = {} - for _, node in ipairs(func_node:field("type")) do + for _, node in ipairs(_G.func_node:field('type')) do table.insert(res, { node:type(), node:range() }) end return res - ]]) + end) eq({ { 'primitive_type', 0, 0, 0, 4 } }, res) - local res_fail = exec_lua([[ - parser = vim.treesitter.get_parser(0, "c") + local res_fail = exec_lua(function() + vim.treesitter.get_parser(0, 'c') - return #func_node:field("foo") == 0 - ]]) + return #_G.func_node:field('foo') == 0 + end) assert(res_fail) end) it('supports getting text of multiline node', function() insert(test_text) - local res = exec_lua([[ - local parser = vim.treesitter.get_parser(0, "c") + local res = exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') local tree = parser:parse()[1] return vim.treesitter.get_node_text(tree:root(), 0) - ]]) + end) eq(test_text, res) - local res2 = exec_lua([[ - local parser = vim.treesitter.get_parser(0, "c") + local res2 = exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') local root = parser:parse()[1]:root() return vim.treesitter.get_node_text(root:child(0):child(0), 0) - ]]) + end) eq('void', res2) end) @@ -196,7 +196,7 @@ end]] insert(text) eq( '', - exec_lua [[ + exec_lua(function() local fake_node = {} function fake_node:start() return 3, 0, 23 @@ -211,7 +211,7 @@ end]] return 3, 0, 3, 0 end return vim.treesitter.get_node_text(fake_node, 0) - ]] + end) ) end) @@ -221,7 +221,7 @@ end]] {} ```]] insert(text) - local result = exec_lua([[ + local result = exec_lua(function() local fake_node = {} function fake_node:start() return 1, 0, 7 @@ -233,38 +233,38 @@ end]] return 1, 0, 1, 0 end return vim.treesitter.get_node_text(fake_node, 0) == '' - ]]) + end) eq(true, result) end) it('allows to set simple ranges', function() insert(test_text) - local res = exec_lua [[ - parser = vim.treesitter.get_parser(0, "c") - return { parser:parse()[1]:root():range() } - ]] + local res = exec_lua(function() + _G.parser = vim.treesitter.get_parser(0, 'c') + return { _G.parser:parse()[1]:root():range() } + end) eq({ 0, 0, 19, 0 }, res) -- The following sets the included ranges for the current parser -- As stated here, this only includes the function (thus the whole buffer, without the last line) - local res2 = exec_lua [[ - local root = parser:parse()[1]:root() - parser:set_included_regions({{root:child(0)}}) - parser:invalidate() - return { parser:parse(true)[1]:root():range() } - ]] + local res2 = exec_lua(function() + local root = _G.parser:parse()[1]:root() + _G.parser:set_included_regions({ { root:child(0) } }) + _G.parser:invalidate() + return { _G.parser:parse(true)[1]:root():range() } + end) eq({ 0, 0, 18, 1 }, res2) eq({ { { 0, 0, 0, 18, 1, 512 } } }, exec_lua [[ return parser:included_regions() ]]) - local range_tbl = exec_lua [[ - parser:set_included_regions { { { 0, 0, 17, 1 } } } - parser:parse() - return parser:included_regions() - ]] + local range_tbl = exec_lua(function() + _G.parser:set_included_regions { { { 0, 0, 17, 1 } } } + _G.parser:parse() + return _G.parser:included_regions() + end) eq({ { { 0, 0, 0, 17, 1, 508 } } }, range_tbl) end) @@ -272,25 +272,25 @@ end]] it('allows to set complex ranges', function() insert(test_text) - local res = exec_lua [[ - parser = vim.treesitter.get_parser(0, "c") - query = vim.treesitter.query.parse("c", "(declaration) @decl") + local res = exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') + local query = vim.treesitter.query.parse('c', '(declaration) @decl') - local nodes = {} - for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do - table.insert(nodes, node) - end + local nodes = {} + for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do + table.insert(nodes, node) + end - parser:set_included_regions({nodes}) + parser:set_included_regions({ nodes }) - local root = parser:parse(true)[1]:root() + local root = parser:parse(true)[1]:root() - local res = {} - for i=0,(root:named_child_count() - 1) do - table.insert(res, { root:named_child(i):range() }) - end - return res - ]] + local res = {} + for i = 0, (root:named_child_count() - 1) do + table.insert(res, { root:named_child(i):range() }) + end + return res + end) eq({ { 2, 2, 2, 40 }, @@ -304,10 +304,10 @@ end]] end) it('allows to create string parsers', function() - local ret = exec_lua [[ - local parser = vim.treesitter.get_string_parser("int foo = 42;", "c") + local ret = exec_lua(function() + local parser = vim.treesitter.get_string_parser('int foo = 42;', 'c') return { parser:parse()[1]:root():range() } - ]] + end) eq({ 0, 0, 0, 13 }, ret) end) @@ -318,33 +318,31 @@ end]] int bar = 13; ]] - local ret = exec_lua( - [[ - local str = ... - local parser = vim.treesitter.get_string_parser(str, "c") + local ret = exec_lua(function(str) + local parser = vim.treesitter.get_string_parser(str, 'c') - local nodes = {} - local query = vim.treesitter.query.parse("c", '((identifier) @id (#eq? @id "foo"))') + local nodes = {} + local query = vim.treesitter.query.parse('c', '((identifier) @id (#eq? @id "foo"))') - for _, node in query:iter_captures(parser:parse()[1]:root(), str) do - table.insert(nodes, { node:range() }) - end + for _, node in query:iter_captures(parser:parse()[1]:root(), str) do + table.insert(nodes, { node:range() }) + end - return nodes - ]], - txt - ) + return nodes + end, txt) eq({ { 0, 10, 0, 13 } }, ret) end) describe('when creating a language tree', function() local function get_ranges() - return exec_lua [[ + return exec_lua(function() local result = {} - parser:for_each_tree(function(tree) table.insert(result, {tree:root():range()}) end) + _G.parser:for_each_tree(function(tree) + table.insert(result, { tree:root():range() }) + end) return result - ]] + end) end before_each(function() @@ -360,16 +358,17 @@ int x = INT_MAX; describe('when parsing regions independently', function() it('should inject a language', function() - exec_lua([[ - parser = vim.treesitter.get_parser(0, "c", { + exec_lua(function() + _G.parser = vim.treesitter.get_parser(0, 'c', { injections = { c = ( - '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) ' .. - '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' - ) - }}) - parser:parse(true) - ]]) + '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) ' + .. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' + ), + }, + }) + _G.parser:parse(true) + end) eq('table', exec_lua('return type(parser:children().c)')) eq(5, exec_lua('return #parser:children().c:trees()')) @@ -397,16 +396,17 @@ int x = INT_MAX; describe('when parsing regions combined', function() it('should inject a language', function() - exec_lua([[ - parser = vim.treesitter.get_parser(0, "c", { + exec_lua(function() + _G.parser = vim.treesitter.get_parser(0, 'c', { injections = { c = ( - '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined)) ' .. - '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined))' - ) - }}) - parser:parse(true) - ]]) + '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined)) ' + .. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined))' + ), + }, + }) + _G.parser:parse(true) + end) eq('table', exec_lua('return type(parser:children().c)')) eq(2, exec_lua('return #parser:children().c:trees()')) @@ -447,16 +447,17 @@ int x = INT_MAX; describe('when using injection.self', function() it('should inject the source language', function() - exec_lua([[ - parser = vim.treesitter.get_parser(0, "c", { + exec_lua(function() + _G.parser = vim.treesitter.get_parser(0, 'c', { injections = { c = ( - '(preproc_def (preproc_arg) @injection.content (#set! injection.self)) ' .. - '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.self))' - ) - }}) - parser:parse(true) - ]]) + '(preproc_def (preproc_arg) @injection.content (#set! injection.self)) ' + .. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.self))' + ), + }, + }) + _G.parser:parse(true) + end) eq('table', exec_lua('return type(parser:children().c)')) eq(5, exec_lua('return #parser:children().c:trees()')) @@ -484,16 +485,17 @@ int x = INT_MAX; describe('when using the offset directive', function() it('should shift the range by the directive amount', function() - exec_lua([[ - parser = vim.treesitter.get_parser(0, "c", { + exec_lua(function() + _G.parser = vim.treesitter.get_parser(0, 'c', { injections = { c = ( - '(preproc_def ((preproc_arg) @injection.content (#set! injection.language "c") (#offset! @injection.content 0 2 0 -1))) ' .. - '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' - ) - }}) - parser:parse(true) - ]]) + '(preproc_def ((preproc_arg) @injection.content (#set! injection.language "c") (#offset! @injection.content 0 2 0 -1))) ' + .. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' + ), + }, + }) + _G.parser:parse(true) + end) eq('table', exec_lua('return type(parser:children().c)')) eq({ @@ -506,7 +508,7 @@ int x = INT_MAX; }, get_ranges()) end) it('should list all directives', function() - local res_list = exec_lua [[ + local res_list = exec_lua(function() local query = vim.treesitter.query local list = query.list_directives() @@ -514,7 +516,7 @@ int x = INT_MAX; table.sort(list) return list - ]] + end) eq({ 'gsub!', 'offset!', 'set!', 'trim!' }, res_list) end) @@ -530,18 +532,18 @@ int x = INT_MAX; end) it('should return the correct language tree', function() - local result = exec_lua([[ - parser = vim.treesitter.get_parser(0, "c", { + local result = exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c', { injections = { - c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))' - } + c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))', + }, }) parser:parse(true) - local sub_tree = parser:language_for_range({1, 18, 1, 19}) + local sub_tree = parser:language_for_range({ 1, 18, 1, 19 }) return sub_tree == parser:children().c - ]]) + end) eq(true, result) end) @@ -555,23 +557,23 @@ print() end) it('ignores optional captures #23100', function() - local result = exec_lua([[ - parser = vim.treesitter.get_parser(0, "lua", { + local result = exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'lua', { injections = { lua = ( - '(function_call ' .. - '(arguments ' .. - '(string)? @injection.content ' .. - '(number)? @injection.content ' .. - '(#offset! @injection.content 0 1 0 -1) ' .. - '(#set! injection.language "c")))' - ) - } + '(function_call ' + .. '(arguments ' + .. '(string)? @injection.content ' + .. '(number)? @injection.content ' + .. '(#offset! @injection.content 0 1 0 -1) ' + .. '(#set! injection.language "c")))' + ), + }, }) parser:parse(true) return parser:is_valid() - ]]) + end) eq(true, result) end) @@ -584,18 +586,15 @@ print() int x = 3; ]]) - local result = exec_lua([[ - local result - - query = vim.treesitter.query.parse("c", '((number_literal) @number (#set! "key" "value"))') - parser = vim.treesitter.get_parser(0, "c") + local result = exec_lua(function() + local query = + vim.treesitter.query.parse('c', '((number_literal) @number (#set! "key" "value"))') + local parser = vim.treesitter.get_parser(0, 'c') - for pattern, match, metadata in query:iter_matches(parser:parse()[1]:root(), 0, 0, -1, { all = true }) do - result = metadata.key - end - - return result - ]]) + local _, _, metadata = + query:iter_matches(parser:parse()[1]:root(), 0, 0, -1, { all = true })() + return metadata.key + end) eq('value', result) end) @@ -606,19 +605,18 @@ print() int x = 3; ]]) - local result = exec_lua([[ - local query = vim.treesitter.query - local value - - query = vim.treesitter.query.parse("c", '((number_literal) @number (#set! @number "key" "value"))') - parser = vim.treesitter.get_parser(0, "c") + local result = exec_lua(function() + local query = vim.treesitter.query.parse( + 'c', + '((number_literal) @number (#set! @number "key" "value"))' + ) + local parser = vim.treesitter.get_parser(0, 'c') - for pattern, match, metadata in query:iter_matches(parser:parse()[1]:root(), 0, 0, -1, { all = true }) do - for _, nested_tbl in pairs(metadata) do - return nested_tbl.key - end - end - ]]) + local _, _, metadata = + query:iter_matches(parser:parse()[1]:root(), 0, 0, -1, { all = true })() + local _, nested_tbl = next(metadata) + return nested_tbl.key + end) eq('value', result) end) @@ -628,19 +626,18 @@ print() int x = 3; ]]) - local result = exec_lua([[ - local query = vim.treesitter.query - local result - - query = vim.treesitter.query.parse("c", '((number_literal) @number (#set! @number "key" "value") (#set! @number "key2" "value2"))') - parser = vim.treesitter.get_parser(0, "c") + local result = exec_lua(function() + local query = vim.treesitter.query.parse( + 'c', + '((number_literal) @number (#set! @number "key" "value") (#set! @number "key2" "value2"))' + ) + local parser = vim.treesitter.get_parser(0, 'c') - for pattern, match, metadata in query:iter_matches(parser:parse()[1]:root(), 0, 0, -1, { all = true }) do - for _, nested_tbl in pairs(metadata) do - return nested_tbl - end - end - ]]) + local _, _, metadata = + query:iter_matches(parser:parse()[1]:root(), 0, 0, -1, { all = true })() + local _, nested_tbl = next(metadata) + return nested_tbl + end) local expected = { ['key'] = 'value', ['key2'] = 'value2', @@ -663,24 +660,21 @@ print() (function_definition) @function ]] - exec_lua([[ + exec_lua(function() vim.treesitter.start(0, 'c') - ]]) + end) local function run_query() - return exec_lua( - [[ - local query = vim.treesitter.query.parse("c", ...) - parser = vim.treesitter.get_parser() - tree = parser:parse()[1] - res = {} - for id, node in query:iter_captures(tree:root()) do - table.insert(res, {query.captures[id], node:range()}) - end - return res - ]], - query0 - ) + return exec_lua(function(query_str) + local query = vim.treesitter.query.parse('c', query_str) + local parser = vim.treesitter.get_parser() + local tree = parser:parse()[1] + local res = {} + for id, node in query:iter_captures(tree:root()) do + table.insert(res, { query.captures[id], node:range() }) + end + return res + end, query0) end eq({ @@ -718,18 +712,15 @@ print() ]] ]==] - local r = exec_lua( - [[ - local parser = vim.treesitter.get_string_parser(..., 'lua') - parser:parse(true) - local ranges = {} - parser:for_each_tree(function(tstree, tree) - ranges[tree:lang()] = { tstree:root():range(true) } - end) - return ranges - ]], - source - ) + local r = exec_lua(function(src) + local parser = vim.treesitter.get_string_parser(src, 'lua') + parser:parse(true) + local ranges = {} + parser:for_each_tree(function(tstree, tree) + ranges[tree:lang()] = { tstree:root():range(true) } + end) + return ranges + end, source) eq({ lua = { 0, 6, 6, 16, 4, 438 }, @@ -741,19 +732,14 @@ print() -- the ranges but only provide a Range4. Strip the byte entries from the ranges and make sure -- add_bytes() produces the same result. - local rb = exec_lua( - [[ - local r, source = ... - local add_bytes = require('vim.treesitter._range').add_bytes - for lang, range in pairs(r) do - r[lang] = {range[1], range[2], range[4], range[5]} - r[lang] = add_bytes(source, r[lang]) - end - return r - ]], - r, - source - ) + local rb = exec_lua(function(r0, source0) + local add_bytes = require('vim.treesitter._range').add_bytes + for lang, range in pairs(r0) do + r0[lang] = { range[1], range[2], range[4], range[5] } + r0[lang] = add_bytes(source0, r0[lang]) + end + return r0 + end, r, source) eq(rb, r) end) @@ -766,25 +752,25 @@ print() ]] -- This is not a valid injection since (code) has children and include-children is not set - exec_lua [[ - parser1 = require('vim.treesitter.languagetree').new(0, "vimdoc", { + exec_lua(function() + _G.parser1 = require('vim.treesitter.languagetree').new(0, 'vimdoc', { injections = { - vimdoc = "((codeblock (language) @injection.language (code) @injection.content))" - } + vimdoc = '((codeblock (language) @injection.language (code) @injection.content))', + }, }) - parser1:parse(true) - ]] + _G.parser1:parse(true) + end) eq(0, exec_lua('return #vim.tbl_keys(parser1:children())')) - exec_lua [[ - parser2 = require('vim.treesitter.languagetree').new(0, "vimdoc", { + exec_lua(function() + _G.parser2 = require('vim.treesitter.languagetree').new(0, 'vimdoc', { injections = { - vimdoc = "((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))" - } + vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))', + }, }) - parser2:parse(true) - ]] + _G.parser2:parse(true) + end) eq(1, exec_lua('return #vim.tbl_keys(parser2:children())')) eq({ { { 1, 0, 21, 2, 0, 42 } } }, exec_lua('return parser2:children().lua:included_regions()')) @@ -821,46 +807,46 @@ print() < ]]) - exec_lua [[ - parser = require('vim.treesitter.languagetree').new(0, "vimdoc", { + exec_lua(function() + _G.parser = require('vim.treesitter.languagetree').new(0, 'vimdoc', { injections = { - vimdoc = "((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))" - } + vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))', + }, }) - ]] + end) --- Do not parse injections by default eq( 0, - exec_lua [[ - parser:parse() - return #vim.tbl_keys(parser:children()) - ]] + exec_lua(function() + _G.parser:parse() + return #vim.tbl_keys(_G.parser:children()) + end) ) --- Only parse injections between lines 0, 2 eq( 1, - exec_lua [[ - parser:parse({0, 2}) - return #parser:children().lua:trees() - ]] + exec_lua(function() + _G.parser:parse({ 0, 2 }) + return #_G.parser:children().lua:trees() + end) ) eq( 2, - exec_lua [[ - parser:parse({2, 6}) - return #parser:children().lua:trees() - ]] + exec_lua(function() + _G.parser:parse({ 2, 6 }) + return #_G.parser:children().lua:trees() + end) ) eq( 7, - exec_lua [[ - parser:parse(true) - return #parser:children().lua:trees() - ]] + exec_lua(function() + _G.parser:parse(true) + return #_G.parser:children().lua:trees() + end) ) end) @@ -876,13 +862,13 @@ print() feed(':set ft=help<cr>') - exec_lua [[ - vim.treesitter.get_parser(0, "vimdoc", { + exec_lua(function() + vim.treesitter.get_parser(0, 'vimdoc', { injections = { - vimdoc = "((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))" - } + vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))', + }, }) - ]] + end) end) it('is valid excluding, invalid including children initially', function() diff --git a/test/functional/treesitter/query_spec.lua b/test/functional/treesitter/query_spec.lua index 2212f787af..00e8071738 100644 --- a/test/functional/treesitter/query_spec.lua +++ b/test/functional/treesitter/query_spec.lua @@ -10,28 +10,26 @@ local pcall_err = t.pcall_err local api = n.api local fn = n.fn -local get_query_result_code = [[ - function get_query_result(query_text) - cquery = vim.treesitter.query.parse("c", query_text) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} - for cid, node in cquery:iter_captures(tree:root(), 0) do - -- can't transmit node over RPC. just check the name, range, and text - local text = vim.treesitter.get_node_text(node, 0) - local range = {node:range()} - table.insert(res, { cquery.captures[cid], node:type(), range, text }) - end - return res +local function get_query_result(query_text) + local cquery = vim.treesitter.query.parse('c', query_text) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} + for cid, node in cquery:iter_captures(tree:root(), 0) do + -- can't transmit node over RPC. just check the name, range, and text + local text = vim.treesitter.get_node_text(node, 0) + local range = { node:range() } + table.insert(res, { cquery.captures[cid], node:type(), range, text }) end -]] + return res +end describe('treesitter query API', function() before_each(function() clear() - exec_lua [[ + exec_lua(function() vim.g.__ts_debug = 1 - ]] + end) end) local test_text = [[ @@ -71,9 +69,9 @@ void ui_refresh(void) it('supports runtime queries', function() ---@type string[] - local ret = exec_lua [[ - return vim.treesitter.query.get("c", "highlights").captures - ]] + local ret = exec_lua(function() + return vim.treesitter.query.get('c', 'highlights').captures + end) -- see $VIMRUNTIME/queries/c/highlights.scm eq('variable', ret[1]) @@ -84,22 +82,17 @@ void ui_refresh(void) local long_query = test_query:rep(100) ---@return number local function q(_n) - return exec_lua( - [[ - local query, n = ... - local before = vim.api.nvim__stats().ts_query_parse_count - collectgarbage("stop") - for i=1, n, 1 do - cquery = vim.treesitter.query.parse("c", ...) - end - collectgarbage("restart") - collectgarbage("collect") - local after = vim.api.nvim__stats().ts_query_parse_count - return after - before - ]], - long_query, - _n - ) + return exec_lua(function(query, n0) + local before = vim.api.nvim__stats().ts_query_parse_count + collectgarbage('stop') + for _ = 1, n0, 1 do + vim.treesitter.query.parse('c', query, n0) + end + collectgarbage('restart') + collectgarbage('collect') + local after = vim.api.nvim__stats().ts_query_parse_count + return after - before + end, long_query, _n) end eq(1, q(1)) @@ -110,20 +103,17 @@ void ui_refresh(void) it('supports query and iter by capture (iter_captures)', function() insert(test_text) - local res = exec_lua( - [[ - cquery = vim.treesitter.query.parse("c", ...) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} - for cid, node in cquery:iter_captures(tree:root(), 0, 7, 14) do - -- can't transmit node over RPC. just check the name and range - table.insert(res, { '@' .. cquery.captures[cid], node:type(), node:range() }) - end - return res - ]], - test_query - ) + local res = exec_lua(function(test_query0) + local cquery = vim.treesitter.query.parse('c', test_query0) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} + for cid, node in cquery:iter_captures(tree:root(), 0, 7, 14) do + -- can't transmit node over RPC. just check the name and range + table.insert(res, { '@' .. cquery.captures[cid], node:type(), node:range() }) + end + return res + end, test_query) eq({ { '@type', 'primitive_type', 8, 2, 8, 6 }, -- bool @@ -143,26 +133,23 @@ void ui_refresh(void) insert(test_text) ---@type table - local res = exec_lua( - [[ - cquery = vim.treesitter.query.parse("c", ...) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} - for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = true }) do - -- can't transmit node over RPC. just check the name and range - local mrepr = {} - for cid, nodes in pairs(match) do - for _, node in ipairs(nodes) do - table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() }) - end + local res = exec_lua(function(test_query0) + local cquery = vim.treesitter.query.parse('c', test_query0) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} + for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = true }) do + -- can't transmit node over RPC. just check the name and range + local mrepr = {} + for cid, nodes in pairs(match) do + for _, node in ipairs(nodes) do + table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() }) end - table.insert(res, { pattern, mrepr }) end - return res - ]], - test_query - ) + table.insert(res, { pattern, mrepr }) + end + return res + end, test_query) eq({ { 3, { { '@type', 'primitive_type', 8, 2, 8, 6 } } }, @@ -191,20 +178,20 @@ void ui_refresh(void) it('supports query and iter by capture for quantifiers', function() insert(test_text) - local res = exec_lua( - [[ - cquery = vim.treesitter.query.parse("c", ...) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} - for cid, node in cquery:iter_captures(tree:root(), 0, 7, 14) do - -- can't transmit node over RPC. just check the name and range - table.insert(res, { '@' .. cquery.captures[cid], node:type(), node:range() }) - end - return res - ]], - '(expression_statement (assignment_expression (call_expression)))+ @funccall' - ) + local res = exec_lua(function() + local cquery = vim.treesitter.query.parse( + 'c', + '(expression_statement (assignment_expression (call_expression)))+ @funccall' + ) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} + for cid, node in cquery:iter_captures(tree:root(), 0, 7, 14) do + -- can't transmit node over RPC. just check the name and range + table.insert(res, { '@' .. cquery.captures[cid], node:type(), node:range() }) + end + return res + end) eq({ { '@funccall', 'expression_statement', 11, 4, 11, 34 }, @@ -216,26 +203,26 @@ void ui_refresh(void) it('supports query and iter by match for quantifiers', function() insert(test_text) - local res = exec_lua( - [[ - cquery = vim.treesitter.query.parse("c", ...) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} - for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = true }) do - -- can't transmit node over RPC. just check the name and range - local mrepr = {} - for cid, nodes in pairs(match) do - for _, node in ipairs(nodes) do - table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() }) - end + local res = exec_lua(function() + local cquery = vim.treesitter.query.parse( + 'c', + '(expression_statement (assignment_expression (call_expression)))+ @funccall' + ) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} + for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = true }) do + -- can't transmit node over RPC. just check the name and range + local mrepr = {} + for cid, nodes in pairs(match) do + for _, node in ipairs(nodes) do + table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() }) end - table.insert(res, {pattern, mrepr}) end - return res - ]], - '(expression_statement (assignment_expression (call_expression)))+ @funccall' - ) + table.insert(res, { pattern, mrepr }) + end + return res + end, '(expression_statement (assignment_expression (call_expression)))+ @funccall') eq({ { @@ -265,26 +252,26 @@ void ui_refresh(void) } ]]) - local res = exec_lua( - [[ - cquery = vim.treesitter.query.parse("c", ...) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} - for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = true }) do - -- can't transmit node over RPC. just check the name and range - local mrepr = {} - for cid, nodes in pairs(match) do - for _, node in ipairs(nodes) do - table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() }) - end + local res = exec_lua(function() + local cquery = vim.treesitter.query.parse( + 'c', + '(expression_statement (assignment_expression (call_expression)))+ @funccall' + ) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} + for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = true }) do + -- can't transmit node over RPC. just check the name and range + local mrepr = {} + for cid, nodes in pairs(match) do + for _, node in ipairs(nodes) do + table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() }) end - table.insert(res, {pattern, mrepr}) end - return res - ]], - '(expression_statement (assignment_expression (call_expression)))+ @funccall' - ) + table.insert(res, { pattern, mrepr }) + end + return res + end) eq({ { @@ -308,18 +295,18 @@ void ui_refresh(void) insert('char* astring = "\\n"; (1 + 1) * 2 != 2;') ---@type table - local res = exec_lua([[ - query = ( - '([_] @plus (#vim-match? @plus "^\\\\+$"))' .. - '([_] @times (#vim-match? @times "^\\\\*$"))' .. - '([_] @paren (#vim-match? @paren "^\\\\($"))' .. - '([_] @escape (#vim-match? @escape "^\\\\\\\\n$"))' .. - '([_] @string (#vim-match? @string "^\\"\\\\\\\\n\\"$"))' + local res = exec_lua(function() + local query = ( + '([_] @plus (#vim-match? @plus "^\\\\+$"))' + .. '([_] @times (#vim-match? @times "^\\\\*$"))' + .. '([_] @paren (#vim-match? @paren "^\\\\($"))' + .. '([_] @escape (#vim-match? @escape "^\\\\\\\\n$"))' + .. '([_] @string (#vim-match? @string "^\\"\\\\\\\\n\\"$"))' ) - cquery = vim.treesitter.query.parse("c", query) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} + local cquery = vim.treesitter.query.parse('c', query) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} for pattern, match in cquery:iter_matches(tree:root(), 0, 0, -1, { all = true }) do -- can't transmit node over RPC. just check the name and range local mrepr = {} @@ -331,7 +318,7 @@ void ui_refresh(void) table.insert(res, { pattern, mrepr }) end return res - ]]) + end) eq({ { 2, { { '@times', '*', 0, 4, 0, 5 } } }, @@ -362,10 +349,9 @@ void ui_refresh(void) return 0; } ]]) - exec_lua(get_query_result_code) local res0 = exec_lua( - [[return get_query_result(...)]], + get_query_result, [[((primitive_type) @c-keyword (#any-of? @c-keyword "int" "float"))]] ) eq({ @@ -374,7 +360,7 @@ void ui_refresh(void) }, res0) local res1 = exec_lua( - [[return get_query_result(...)]], + get_query_result, [[ ((string_literal) @fizzbuzz-strings (#any-of? @fizzbuzz-strings "\"number= %d FizzBuzz\\n\"" @@ -395,16 +381,15 @@ void ui_refresh(void) int x = 123; enum C { y = 124 }; int main() { int z = 125; }]]) - exec_lua(get_query_result_code) local result = exec_lua( - [[return get_query_result(...)]], + get_query_result, [[((number_literal) @literal (#has-ancestor? @literal "function_definition"))]] ) eq({ { 'literal', 'number_literal', { 2, 21, 2, 24 }, '125' } }, result) result = exec_lua( - [[return get_query_result(...)]], + get_query_result, [[((number_literal) @literal (#has-ancestor? @literal "function_definition" "enum_specifier"))]] ) eq({ @@ -413,7 +398,7 @@ void ui_refresh(void) }, result) result = exec_lua( - [[return get_query_result(...)]], + get_query_result, [[((number_literal) @literal (#not-has-ancestor? @literal "enum_specifier"))]] ) eq({ @@ -425,11 +410,14 @@ void ui_refresh(void) it('allows loading query with escaped quotes and capture them `#{lua,vim}-match`?', function() insert('char* astring = "Hello World!";') - local res = exec_lua([[ - cquery = vim.treesitter.query.parse("c", '([_] @quote (#vim-match? @quote "^\\"$")) ([_] @quote (#lua-match? @quote "^\\"$"))') - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} + local res = exec_lua(function() + local cquery = vim.treesitter.query.parse( + 'c', + '([_] @quote (#vim-match? @quote "^\\"$")) ([_] @quote (#lua-match? @quote "^\\"$"))' + ) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} for pattern, match in cquery:iter_matches(tree:root(), 0, 0, -1, { all = true }) do -- can't transmit node over RPC. just check the name and range local mrepr = {} @@ -441,7 +429,7 @@ void ui_refresh(void) table.insert(res, { pattern, mrepr }) end return res - ]]) + end) eq({ { 1, { { '@quote', '"', 0, 16, 0, 17 } } }, @@ -461,70 +449,64 @@ void ui_refresh(void) local custom_query = '((identifier) @main (#is-main? @main))' do - local res = exec_lua( - [[ - local query = vim.treesitter.query - - local function is_main(match, pattern, bufnr, predicate) - local nodes = match[ predicate[2] ] - for _, node in ipairs(nodes) do - if vim.treesitter.get_node_text(node, bufnr) == 'main' then - return true - end + local res = exec_lua(function(custom_query0) + local query = vim.treesitter.query + + local function is_main(match, _pattern, bufnr, predicate) + local nodes = match[predicate[2]] + for _, node in ipairs(nodes) do + if vim.treesitter.get_node_text(node, bufnr) == 'main' then + return true end - return false end + return false + end - local parser = vim.treesitter.get_parser(0, "c") + local parser = vim.treesitter.get_parser(0, 'c') - -- Time bomb: update this in 0.12 - if vim.fn.has('nvim-0.12') == 1 then - return 'Update this test to remove this message and { all = true } from add_predicate' - end - query.add_predicate("is-main?", is_main, { all = true }) + -- Time bomb: update this in 0.12 + if vim.fn.has('nvim-0.12') == 1 then + return 'Update this test to remove this message and { all = true } from add_predicate' + end + query.add_predicate('is-main?', is_main, { all = true }) - local query = query.parse("c", ...) + local query0 = query.parse('c', custom_query0) - local nodes = {} - for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do - table.insert(nodes, {node:range()}) - end + local nodes = {} + for _, node in query0:iter_captures(parser:parse()[1]:root(), 0) do + table.insert(nodes, { node:range() }) + end - return nodes - ]], - custom_query - ) + return nodes + end, custom_query) eq({ { 0, 4, 0, 8 } }, res) end -- Once with the old API. Remove this whole 'do' block in 0.12 do - local res = exec_lua( - [[ - local query = vim.treesitter.query + local res = exec_lua(function(custom_query0) + local query = vim.treesitter.query - local function is_main(match, pattern, bufnr, predicate) - local node = match[ predicate[2] ] + local function is_main(match, _pattern, bufnr, predicate) + local node = match[predicate[2]] - return vim.treesitter.get_node_text(node, bufnr) == 'main' - end + return vim.treesitter.get_node_text(node, bufnr) == 'main' + end - local parser = vim.treesitter.get_parser(0, "c") + local parser = vim.treesitter.get_parser(0, 'c') - query.add_predicate("is-main?", is_main, true) + query.add_predicate('is-main?', is_main, true) - local query = query.parse("c", ...) + local query0 = query.parse('c', custom_query0) - local nodes = {} - for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do - table.insert(nodes, {node:range()}) - end + local nodes = {} + for _, node in query0:iter_captures(parser:parse()[1]:root(), 0) do + table.insert(nodes, { node:range() }) + end - return nodes - ]], - custom_query - ) + return nodes + end, custom_query) -- Remove this 'do' block in 0.12 eq(0, fn.has('nvim-0.12')) @@ -532,16 +514,16 @@ void ui_refresh(void) end do - local res = exec_lua [[ + local res = exec_lua(function() local query = vim.treesitter.query - local t = {} + local r = {} for _, v in ipairs(query.list_predicates()) do - t[v] = true + r[v] = true end - return t - ]] + return r + end) eq(true, res['is-main?']) end @@ -560,18 +542,15 @@ void ui_refresh(void) local function test(input, query) api.nvim_buf_set_lines(0, 0, -1, true, vim.split(dedent(input), '\n')) - return exec_lua( - [[ - local parser = vim.treesitter.get_parser(0, "lua") - local query = vim.treesitter.query.parse("lua", ...) - local nodes = {} - for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do - nodes[#nodes+1] = { node:range() } - end - return nodes - ]], - query - ) + return exec_lua(function(query_str) + local parser = vim.treesitter.get_parser(0, 'lua') + local query0 = vim.treesitter.query.parse('lua', query_str) + local nodes = {} + for _, node in query0:iter_captures(parser:parse()[1]:root(), 0) do + nodes[#nodes + 1] = { node:range() } + end + return nodes + end, query) end eq( @@ -638,23 +617,21 @@ void ui_refresh(void) -- Comment ]]) - local query = [[ + local result = exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'lua') + local query = vim.treesitter.query.parse( + 'lua', + [[ (((comment (comment_content))+) @bar (#lua-match? @bar "Comment")) ]] - - local result = exec_lua( - [[ - local parser = vim.treesitter.get_parser(0, "lua") - local query = vim.treesitter.query.parse("lua", ...) - local nodes = {} - for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do - nodes[#nodes+1] = { node:range() } - end - return nodes - ]], - query - ) + ) + local nodes = {} + for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do + nodes[#nodes + 1] = { node:range() } + end + return nodes + end) eq({ { 0, 2, 0, 12 }, @@ -668,23 +645,20 @@ void ui_refresh(void) eq(0, fn.has('nvim-0.12')) insert(test_text) - local res = exec_lua( - [[ - cquery = vim.treesitter.query.parse("c", ...) - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - res = {} - for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14) do - local mrepr = {} - for cid, node in pairs(match) do - table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() }) - end - table.insert(res, { pattern, mrepr }) + local res = exec_lua(function(test_query0) + local cquery = vim.treesitter.query.parse('c', test_query0) + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local res = {} + for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14) do + local mrepr = {} + for cid, node in pairs(match) do + table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() }) end - return res - ]], - test_query - ) + table.insert(res, { pattern, mrepr }) + end + return res + end, test_query) eq({ { 3, { { '@type', 'primitive_type', 8, 2, 8, 6 } } }, @@ -716,23 +690,19 @@ void ui_refresh(void) int bar = 13; ]] - local ret = exec_lua( - [[ - local str = ... - local parser = vim.treesitter.get_string_parser(str, "c") + local ret = exec_lua(function(str) + local parser = vim.treesitter.get_string_parser(str, 'c') - local nodes = {} - local query = vim.treesitter.query.parse("c", '((identifier) @foo)') - local first_child = parser:parse()[1]:root():child(1) + local nodes = {} + local query = vim.treesitter.query.parse('c', '((identifier) @foo)') + local first_child = parser:parse()[1]:root():child(1) - for _, node in query:iter_captures(first_child, str) do - table.insert(nodes, { node:range() }) - end + for _, node in query:iter_captures(first_child, str) do + table.insert(nodes, { node:range() }) + end - return nodes - ]], - txt - ) + return nodes + end, txt) eq({ { 1, 10, 1, 13 } }, ret) end) @@ -789,7 +759,10 @@ void ui_refresh(void) const char *sql = "SELECT * FROM Students WHERE name = 'Robert'); DROP TABLE Students;--"; ]]) - local query = [[ + local result = exec_lua(function() + local query = vim.treesitter.query.parse( + 'c', + [[ (declaration type: (_) declarator: (init_declarator @@ -800,20 +773,15 @@ void ui_refresh(void) (#set! injection.language "sql") (#contains? @_id "sql")) ]] - - local result = exec_lua( - [=[ - local query = vim.treesitter.query.parse("c", ...) - local parser = vim.treesitter.get_parser(0, "c") + ) + local parser = vim.treesitter.get_parser(0, 'c') local root = parser:parse()[1]:root() - local t = {} - for id, node, metadata in query:iter_captures(root, 0) do - t[query.captures[id]] = metadata + local res = {} + for id, _, metadata in query:iter_captures(root, 0) do + res[query.captures[id]] = metadata end - return t - ]=], - query - ) + return res + end) eq({ ['_id'] = { ['injection.language'] = 'sql' }, @@ -837,25 +805,22 @@ void ui_refresh(void) (#eq? @function.name "foo")) ]] - local result = exec_lua( - [[ - local query = vim.treesitter.query.parse("c", ...) - local match_preds = query.match_preds + local result = exec_lua(function(query_str) + local query0 = vim.treesitter.query.parse('c', query_str) + local match_preds = query0.match_preds local called = 0 - function query:match_preds(...) + function query0:match_preds(...) called = called + 1 return match_preds(self, ...) end - local parser = vim.treesitter.get_parser(0, "c") + local parser = vim.treesitter.get_parser(0, 'c') local root = parser:parse()[1]:root() local captures = {} - for id, node in query:iter_captures(root, 0) do + for id in query0:iter_captures(root, 0) do captures[#captures + 1] = id end return { called, captures } - ]], - query - ) + end, query) eq({ 2, { 1, 1, 2, 2 } }, result) end) diff --git a/test/functional/treesitter/utils_spec.lua b/test/functional/treesitter/utils_spec.lua index bca0aca0cb..e079a7c8e7 100644 --- a/test/functional/treesitter/utils_spec.lua +++ b/test/functional/treesitter/utils_spec.lua @@ -17,26 +17,26 @@ describe('treesitter utils', function() int x = 3; }]]) - exec_lua([[ - parser = vim.treesitter.get_parser(0, "c") - tree = parser:parse()[1] - root = tree:root() - ancestor = root:child(0) - child = ancestor:child(0) - ]]) - - eq(true, exec_lua('return vim.treesitter.is_ancestor(ancestor, child)')) - eq(false, exec_lua('return vim.treesitter.is_ancestor(child, ancestor)')) + exec_lua(function() + local parser = vim.treesitter.get_parser(0, 'c') + local tree = parser:parse()[1] + local root = tree:root() + _G.ancestor = root:child(0) + _G.child = _G.ancestor:child(0) + end) + + eq(true, exec_lua('return vim.treesitter.is_ancestor(_G.ancestor, _G.child)')) + eq(false, exec_lua('return vim.treesitter.is_ancestor(_G.child, _G.ancestor)')) end) it('can detect if a position is contained in a node', function() - exec_lua([[ - node = { + exec_lua(function() + _G.node = { range = function() return 0, 4, 0, 8 end, } - ]]) + end) eq(false, exec_lua('return vim.treesitter.is_in_node_range(node, 0, 3)')) for i = 4, 7 do |