MunifTanjim / nougat.nvim

🍫 Hyperextensible Statusline / Tabline / Winbar for Neovim πŸš€
MIT License
192 stars 3 forks source link

Nut: LSP Server Names #49

Closed i3d closed 9 months ago

i3d commented 9 months ago

It would be also very nice if we could see LSP information on the statusline.

MunifTanjim commented 9 months ago

Can you give me a few examples? Which LSP information are you thinking about?

uhooi commented 9 months ago

This setting displays the name and ID of the language server. How's that?

nougat.nvim config https://github.com/uhooi/dotfiles/blob/12df54582f6a539fae0d30f6dcb88bf8c825a5fe/.config/nvim/lua/plugins/config/nouget.lua

local lsp_util = require('plugins.config.shared.lsp_util')

-- ...

-- lsp info {{{
local lsp_info = (function()
  local item = Item {
    sep_left = sep.left_half_circle_solid(true),
    content = {
      Item {
        hl = { bg = color.aqua, fg = color.bg },
        hidden = function(_, _)
          return lsp_util.names == ''
        end,
        content = lsp_util.names,
      },
    },
    sep_right = sep.right_half_circle_solid(true),
  }

  return item
end)()
-- }}}

-- tabline {{{
local tal = Bar('tabline')
tal:add_item(lsp_info)
bar_util.set_tabline(tal)
-- }}}

lsp_util.lua https://github.com/uhooi/dotfiles/blob/12df54582f6a539fae0d30f6dcb88bf8c825a5fe/.config/nvim/lua/plugins/config/shared/lsp_util.lua

local M = {}

function M:names()
  local clients = vim
    .iter(vim.lsp.get_clients { bufnr = 0 })
    :map(function(client)
      if client.name == 'null-ls' then
        return ('null-ls[%s](' .. client.id .. ')'):format(table.concat(
          vim
            .iter(require('null-ls.sources').get_available(vim.bo.filetype))
            :map(function(source)
              return source.name
            end)
            :totable(),
          ', '
        ))
      else
        return client.name .. '(' .. client.id .. ')'
      end
    end)
    :totable()
  return '' .. ' ' .. table.concat(clients, ', ')
end

return M

right-upper

γ‚Ήγ‚―γƒͺγƒΌγƒ³γ‚·γƒ§γƒƒγƒˆ 2023-11-21 11 52 08
MunifTanjim commented 9 months ago

Now you can do:

local lsp_servers = nut.lsp.servers.create({
  ctx = {
    content = {
      lua_ls = "LuaLS",
    },
    hl = {
      lua_ls = { fg = "cyan" },
    },
  },
  hl = { bg = "gray", fg = "white" },
  sep_left = sep.left_lower_triangle_solid(true),
  config = {
    content = function(client, item)
      return {
        content = item.ctx.content[client.name] or client.name,
        hl = item.ctx.hl[client.name],
      }
    end,
    sep = " ",
  },
  suffix = " ",
})

You can format/highlight the display value however you want in the config.content function. This is the type for it: https://github.com/MunifTanjim/nougat.nvim/blob/872166d155271fbcd8ba0a7e40f51c137729005f/lua/nougat/nut/lsp/servers.lua#L8

If you return nil that server will not be rendered.

@uhooi @i3d

i3d commented 8 months ago

This seems awesome. Do you have more thorough doc on how to integrate the above snippet into the configs (e.g. your example configs are thorough but I don't know how to integrate, sorry for the ignorance).

For now, I just copy&paste the snippet into one of the examples (slanty.lua) you have and I got this error

local lsp_servers = nut.lsp.servers.create({ <- attempt to index field 'lsp' (a nil value) 
MunifTanjim commented 8 months ago

You need to require it first. So it'll be like this:

local nut = {
  lsp = {
    servers = require("nougat.nut.lsp.servers"),
  }
}

local lsp_servers = nut.lsp.servers.create({

Or just:

local lsp_servers = require("nougat.nut.lsp.servers").create({
i3d commented 8 months ago

I see. How to config other lsps other than lua_ls? Looks like you had that for example, but I have a few lsps I'd like to add as well. Thanks so much!!

MunifTanjim commented 8 months ago
local lsp_servers = nut.lsp.servers.create({
  -- whatever you set here, will be available as `item.ctx`
  -- doc: https://github.com/MunifTanjim/nougat.nvim/tree/main/lua/nougat/item#ctx
  ctx = {
    content = {
      lua_ls = "LuaLS",
    },
    hl = {
      lua_ls = { fg = "cyan" },
    },
  },
  hl = { bg = "gray", fg = "white" },
  sep_left = sep.left_lower_triangle_solid(true),
  config = {
    content = function(client, item)
      if client.name == "copilot" then
        -- if you return nothing, that lsp server will be hidden.
        -- so 'copilot' lsp will be hidden
        return
      end

      return {
        -- `item.ctx.content` has value for `lua_ls`, so it'll be displayed as `LuaLS`.
        -- Other LSP servers will be displayed as default `client.name`
        content = item.ctx.content[client.name] or client.name,
        -- `item.ctx.hl` has value for `lua_ls`, so it'll be displayed with `cyan` color.
        -- Other LSP servers will have default/no colors.
        hl = item.ctx.hl[client.name],
      }
    end,
    sep = " ",
  },
  suffix = " ",
})

As you see, it's very flexible. You can do whatever you want.

If you want to give custom name (e.g. LuaLS) for other LSP servers, you can add them in ctx.content table. If you want to change highlight for them, set that in ctx.hl.

You can also use ctx for hiding lsp servers too, for example:

local lsp_servers = nut.lsp.servers.create({
  ctx = {
    hidden = {
      copilot = true,
    },
  },
  config = {
    content = function(client, item)
      if item.ctx.hidden[item.name] then
        return
      end
      -- do other stuffs
    end,
  },
})

Hope that helps.

uhooi commented 8 months ago

Nice! Thanks. https://github.com/uhooi/dotfiles/commit/53cc886547ad5965e5f3181add5076cea1c0945e

i3d commented 8 months ago

I also just wanted to say 'Huge Thanks' to MunifTanjim that this plugin is pretty much achieved all my wishes wanting for a flexible statusline (the LSP status also worked as well with the latest fix). I was using lualine and now I am fully switched. Thanks for the great work!