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

Code before:
          filetypes = { 'foo' },
          root_markers = { '.foorc' },
        })

        vim.lsp.config('bar', {
          cmd = server.cmd,
          filetypes = { 'bar' },
          root_markers = { '.foorc' },
        })

        vim.lsp.enable('foo')
        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.
      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('does not attach to buffers more than once if no root_dir', function()
      exec_lua(create_server_definition)

      local tmp1 = t.tmpname(true)

      eq(
        1,
        exec_lua(function()
          local server = _G._create_server({
            handlers = {
              initialize = function(_, _, callback)
                callback(nil, { capabilities = {} })
              end,
            },
          })

Review comment (bug):
 ci failure, this needs to be done in `exec_lua()` so it runs in the test Nvim process, not the test runner.

```
test/functional/plugin/lsp_spec.lua:6426: attempt to index field 'lsp' (a nil value)

stack traceback:
	test/functional/plugin/lsp_spec.lua:6426: in function <test/functional/plugin/lsp_spec.lua:6376>
```



Code after:
          filetypes = { 'foo' },
          root_markers = { '.foorc' },
        })

        vim.lsp.config('bar', {
          cmd = server.cmd,
          filetypes = { 'bar' },
          root_markers = { '.foorc' },
        })

        vim.lsp.enable('foo')
        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('does not attach to buffers more than once if no root_dir', function()
      exec_lua(create_server_definition)

      local tmp1 = t.tmpname(true)

      eq(
        1,
        exec_lua(function()
          local server = _G._create_server({
            handlers = {
              initialize = function(_, _, callback)
                callback(nil, { capabilities = {} })
              end,
            },
          })