Repository: neovim/neovim
File: test/functional/ex_cmds/lsp_spec.lua (Lua)

Code before:

  it('stop with arguments', function()
    local running_clients = exec_lua(function()
      vim.lsp.enable('dummy')
      vim.cmd('lsp stop dummy')
      vim.wait(1000, function()
        return #vim.lsp.get_clients() == 0
      end)
      return #vim.lsp.get_clients()
    end)
    eq(0, running_clients)
  end)

  it('stop without arguments', function()
    local running_clients = exec_lua(function()
      vim.lsp.enable('dummy')
      vim.cmd('lsp stop')
      vim.wait(1000, function()
        return #vim.lsp.get_clients() == 0
      end)
      return #vim.lsp.get_clients()
    end)
    eq(0, running_clients)
  end)

  it('subcommand completion', function()
    local completions = exec_lua(function()
      return vim.fn.getcompletion('lsp ', 'cmdline')
    end)
    eq({ 'disable', 'enable', 'restart', 'stop' }, completions)
  end)

  it('argument completion', function()
    local completions = exec_lua(function()
      return vim.fn.getcompletion('lsp enable ', 'cmdline')
    end)
    eq({ 'dummy' }, completions)
  end)

  it('argument completion with spaces', function()
    local cmd_length = exec_lua(function()
      local server = _G._create_server()
      vim.lsp.config('client name with space', {
        cmd = server.cmd,
      })
      local completion = vim.fn.getcompletion('lsp enable cl ', 'cmdline')[1]
      return #vim.api.nvim_parse_cmd('lsp enable ' .. completion, {}).args
    end)
    eq(2, cmd_length)
  end)


Review comment (suggestion):
In case of bang, checking number of args is useless, you need to check that `nextcmd` is empty instead. But I have never felt certain that bang is necessary, so you could remove support for bang (in both `src/nvim/ex_cmds.lua` and the completion function in `_core/ex_cmd.lua`)

Code after:
    local completions = exec_lua(function()
      return vim.fn.getcompletion('lsp enable ', 'cmdline')
    end)
    eq({ 'dummy' }, completions)
  end)

  it('argument completion with spaces', function()
    local cmd_length = exec_lua(function()
      local server = _G._create_server()
      vim.lsp.config('client name with space', {
        cmd = server.cmd,
      })
      local completion = vim.fn.getcompletion('lsp enable cl ', 'cmdline')[1]
      return #vim.api.nvim_parse_cmd('lsp enable ' .. completion, {}).args
    end)
    eq(2, cmd_length)
  end)

  it('argument completion with special characters', function()
    local cmd_length = exec_lua(function()
      local server = _G._create_server()
      vim.lsp.config('client"name|with\tsymbols', {
        cmd = server.cmd,
      })
      local completion = vim.fn.getcompletion('lsp enable cl ', 'cmdline')[1]
      return #vim.api.nvim_parse_cmd('lsp enable ' .. completion, {}).args
    end)
    eq(2, cmd_length)
  end)

  it('fail with no runtime without crashing', function()
    clear {
      args_rm = { '-u' },
      args = { '-u', 'NONE' },
      env = { VIMRUNTIME = 'non-existent' },
    }
    eq(
      [[Vim(lsp):Lua: [string "<nvim>"]:0: module 'vim._core.ex_cmd.lsp' not found:]],
      vim.split(t.pcall_err(n.command, 'lsp enable dummy'), '\n')[1]
    )
  end)
end)