Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for current buffer lsp clients

I am looking for add the current lsp client(s) in my lualine for Neovim. I thought I would use vim.lsp.get_active_clients() but I am having trouble getting the name from that data. It looks like the name should be in there...

I referenced these docs: https://neovim.io/doc/user/lsp.html#vim.lsp.get_active_clients() https://neovim.io/doc/user/lsp.html#lsp-client

I am trying this in my code:

local function lsp_client()
  local clients = vim.lsp.get_active_clients()
  if (clients ~= nil) then
    return dump(clients)
  else
    return "No Client Found"
  end
end

-- Table to String for printing
function dump(o)
    if type(o) == 'table' then
        local s = '{ '
        for k, v in pairs(o) do
            if type(k) ~= 'number' then k = '"' .. k .. '"' end
            s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
        end
        return s .. '} '
    else
        return tostring(o)
    end
end

The dump function is in my utilities.lua file. I can access that and it is giving me a string from the table but I just want the names of the lsp clients. What can I do differently?


1 Answers

I'm using the following configuration for lualine plugin to display the LSP client for the current buffer:

-- LSP clients attached to buffer
local clients_lsp = function ()
  local bufnr = vim.api.nvim_get_current_buf()

  local clients = vim.lsp.buf_get_clients(bufnr)
  if next(clients) == nil then
    return ''
  end

  local c = {}
  for _, client in pairs(clients) do
    table.insert(c, client.name)
  end
  return '\u{f085} ' .. table.concat(c, '|')
end

local config = {
  (...)
  sections = {
    lualine_a = { 'mode' },
    lualine_b = { branch, filename },
    lualine_c = { clients_lsp, diagnostics, },
    (...)
  },
}

lualine.setup(config)
like image 90
lcheylus Avatar answered Sep 02 '25 16:09

lcheylus