robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
4.0 KiB134 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local t_lsp = require('test.functional.plugin.lsp.testutil')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear = n.clear
7local eq = t.eq
8local exec_lua = n.exec_lua
9
10local create_server_definition = t_lsp.create_server_definition
11
12describe(':lsp', function()
13  before_each(function()
14    clear()
15    exec_lua(create_server_definition)
16    exec_lua(function()
17      local server = _G._create_server()
18      vim.lsp.config('dummy', {
19        filetypes = { 'lua' },
20        cmd = server.cmd,
21      })
22      vim.cmd('set ft=lua')
23    end)
24  end)
25
26  it('fails if runtime is missing/broken', function()
27    clear {
28      args_rm = { '-u' },
29      args = { '-u', 'NONE' },
30      env = { VIMRUNTIME = 'non-existent' },
31    }
32    t.matches(
33      [[.*module 'vim%.lsp' not found:]],
34      vim.split(t.pcall_err(n.command, 'lsp enable dummy'), '\n')[1]
35    )
36  end)
37
38  for _, test_with_arguments in ipairs({ true, false }) do
39    local test_message_suffix, lsp_command_suffix
40    if test_with_arguments then
41      test_message_suffix = ' with arguments'
42      lsp_command_suffix = ' dummy'
43    else
44      test_message_suffix = ' without arguments'
45      lsp_command_suffix = ''
46    end
47
48    it('enable' .. test_message_suffix, function()
49      local is_enabled = exec_lua(function()
50        vim.cmd('lsp enable' .. lsp_command_suffix)
51        return vim.lsp.is_enabled('dummy')
52      end)
53      eq(true, is_enabled)
54    end)
55
56    it('disable' .. test_message_suffix, function()
57      local is_enabled = exec_lua(function()
58        vim.lsp.enable('dummy')
59        vim.cmd('lsp disable' .. lsp_command_suffix)
60        return vim.lsp.is_enabled('dummy')
61      end)
62      eq(false, is_enabled)
63    end)
64
65    it('restart' .. test_message_suffix, function()
66      --- @type boolean, integer?
67      local ids_differ, attached_buffer_count = exec_lua(function()
68        vim.lsp.enable('dummy')
69        local old_id = vim.lsp.get_clients()[1].id
70
71        vim.cmd('lsp restart' .. lsp_command_suffix)
72        return vim.wait(1000, function()
73          local new_client = vim.lsp.get_clients()[1]
74          if new_client == nil then
75            return false
76          end
77          return old_id ~= new_client.id, #new_client.attached_buffers
78        end)
79      end)
80      eq(true, ids_differ)
81      eq(1, attached_buffer_count)
82    end)
83
84    it('stop' .. test_message_suffix, function()
85      local running_clients = exec_lua(function()
86        vim.lsp.enable('dummy')
87        vim.cmd('lsp stop' .. lsp_command_suffix)
88        vim.wait(1000, function()
89          return #vim.lsp.get_clients() == 0
90        end)
91        return #vim.lsp.get_clients()
92      end)
93      eq(0, running_clients)
94    end)
95  end
96
97  it('subcommand completion', function()
98    local completions = exec_lua(function()
99      return vim.fn.getcompletion('lsp ', 'cmdline')
100    end)
101    eq({ 'disable', 'enable', 'restart', 'stop' }, completions)
102  end)
103
104  it('argument completion', function()
105    local completions = exec_lua(function()
106      return vim.fn.getcompletion('lsp enable ', 'cmdline')
107    end)
108    eq({ 'dummy' }, completions)
109  end)
110
111  it('argument completion with spaces', function()
112    local cmd_length = exec_lua(function()
113      local server = _G._create_server()
114      vim.lsp.config('client name with space', {
115        cmd = server.cmd,
116      })
117      local completion = vim.fn.getcompletion('lsp enable cl ', 'cmdline')[1]
118      return #vim.api.nvim_parse_cmd('lsp enable ' .. completion, {}).args
119    end)
120    eq(2, cmd_length)
121  end)
122
123  it('argument completion with special characters', function()
124    local cmd_length = exec_lua(function()
125      local server = _G._create_server()
126      vim.lsp.config('client"name|with\tsymbols', {
127        cmd = server.cmd,
128      })
129      local completion = vim.fn.getcompletion('lsp enable cl ', 'cmdline')[1]
130      return #vim.api.nvim_parse_cmd('lsp enable ' .. completion, {}).args
131    end)
132    eq(2, cmd_length)
133  end)
134end)