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

Code before:
        vim.lsp.enable('bar')
      end)

      eq(
        { 1, 'foo', 1, 'bar' },
        exec_lua(function()
          local foos = vim.lsp.get_clients({ bufnr = assert(_G.foo_buf) })
          local bars = vim.lsp.get_clients({ bufnr = assert(_G.bar_buf) })
          return { #foos, foos[1].name, #bars, bars[1].name }
        end)
      )

      -- Now disable the 'foo' lsp and confirm that it's detached from the buffer it was previous
      -- attached to.
      exec_lua([[vim.lsp.enable('foo', false)]])
      eq(
        { 0, 'foo', 1, 'bar' },
        exec_lua(function()
          local foos = vim.lsp.get_clients({ bufnr = assert(_G.foo_buf) })
          local bars = vim.lsp.get_clients({ bufnr = assert(_G.bar_buf) })
          return { #foos, 'foo', #bars, bars[1].name }
        end)
      )
    end)

    it('work in FileType event', function()
      local tmp = t.tmpname(true)
      local str = string.dump(create_server_definition)
      local encoded = exec_lua(function()
        return vim.base64.encode(str)
      end)
      n.clear({
        args = {
          '--cmd',
          string.format([[lua assert(loadstring(vim.base64.decode([==[%s]==])))()]], encoded),
          '--cmd',
          [[lua _G.server = _G._create_server({ handlers = {initialize = function(_, _, callback) callback(nil, {capabilities = {}}) end} })]],
          '--cmd',
          [[lua vim.lsp.config('foo', { cmd = _G.server.cmd, filetypes = { 'foo' }, root_markers = { '.foorc' } })]],
          '--cmd',
          [[au FileType * ++once lua vim.lsp.enable('foo')]],
          '-c',
          'set ft=foo',
          tmp,
        },
      })

      eq(
        { 1, 'foo' },
        exec_lua(function()
          local foos = vim.lsp.get_clients({ bufnr = 0 })

Review comment (suggestion):
based on your PR description (and test code), is this a more correct test name:

```suggestion
    it('in first FileType event (on startup)', function()
```

Code after:
        vim.lsp.enable('bar')
      end)

      eq(
        { 1, 'foo', 1, 'bar' },
        exec_lua(function()
          local foos = vim.lsp.get_clients({ bufnr = assert(_G.foo_buf) })
          local bars = vim.lsp.get_clients({ bufnr = assert(_G.bar_buf) })
          return { #foos, foos[1].name, #bars, bars[1].name }
        end)
      )

      -- Now disable the 'foo' lsp and confirm that it's detached from the buffer it was previous
      -- attached to.
      exec_lua([[vim.lsp.enable('foo', false)]])
      eq(
        { 0, 'foo', 1, 'bar' },
        exec_lua(function()
          local foos = vim.lsp.get_clients({ bufnr = assert(_G.foo_buf) })
          local bars = vim.lsp.get_clients({ bufnr = assert(_G.bar_buf) })
          return { #foos, 'foo', #bars, bars[1].name }
        end)
      )
    end)

    it('in first FileType event (on startup)', function()
      local tmp = tmpname()
      write_file(tmp, string.dump(create_server_definition))
      n.clear({
        args = {
          '--cmd',
          string.format([[lua assert(loadfile(%q))()]], tmp),
          '--cmd',
          [[lua _G.server = _G._create_server({ handlers = {initialize = function(_, _, callback) callback(nil, {capabilities = {}}) end} })]],
          '--cmd',
          [[lua vim.lsp.config('foo', { cmd = _G.server.cmd, filetypes = { 'foo' }, root_markers = { '.foorc' } })]],
          '--cmd',
          [[au FileType * ++once lua vim.lsp.enable('foo')]],
          '-c',
          'set ft=foo',
        },
      })

      eq(
        { 1, 'foo' },
        exec_lua(function()
          local foos = vim.lsp.get_clients({ bufnr = 0 })
          return { #foos, (foos[1] or {}).name }
        end)
      )
      exec_lua([[vim.lsp.enable('foo', false)]])