LoginSignup
6
1

ステータスラインにアクティブな言語サーバーを表示する方法(Neovim)

Last updated at Posted at 2023-05-25

vim-jpで教えていただいた内容がとてもよかったので記事にしました。

はじめに

Neovimのステータスラインに、アクティブな言語サーバーを表示する方法を紹介します。

私は lualine を使っていますが、 Heirline などでも同じ方法で表示できます。

環境

  • OS:macOS Ventura 13.3.1 (a)
  • Neovim:v0.10.0-dev-416+g70da793c5
  • lualine:commit 05d78e9
  • null-ls:commit 4b055d8

設定ファイル

設定ファイルを紹介します。

アクティブな言語サーバーの一覧を取得し、サーバー名をカンマ区切りで文字列にし、 lsp_names という変数に格納しています。
null-lsの場合はソース(リンターやフォーマッター)の一覧を取得し、括弧内にカンマ区切りの文字列で格納しています。

あとは lsp_names をlualineの表示したい箇所に含めるだけです。

lualine.lua
+ local lsp_names = function()
+   local clients = {}
+   for _, client in ipairs(vim.lsp.get_active_clients { bufnr = 0 }) do
+     if client.name == 'null-ls' then
+       local sources = {}
+       for _, source in ipairs(require('null-ls.sources').get_available(vim.bo.filetype)) do
+         table.insert(sources, source.name)
+       end
+       table.insert(clients, 'null-ls(' .. table.concat(sources, ', ') .. ')')
+     else
+       table.insert(clients, client.name)
+     end
+   end
+   return ' ' .. table.concat(clients, ', ')
+ end
+ 
require('lualine').setup {
  -- ...
  sections = {
    lualine_a = { 'mode' },
    lualine_b = { 'branch', 'diff', 'diagnostics' },
    lualine_c = {},
-     lualine_x = {},
+     lualine_x = { lsp_names },
    lualine_y = { 'encoding', 'fileformat', 'filetype' },
    lualine_z = { 'progress', 'location' },
  },
  -- ...
}

これでステータスラインにアクティブな言語サーバーの一覧が表示されました。
スクリーンショット 2023-05-31 0.27.37.png

null-ls(stylua)lua_lscopilot がアクティブなことがわかります。

おまけ: vim.iter()を使う

まだリリースされていませんが、 vim.iter() を使うとスマートに書けます。

lualine.lua
+ local lsp_names = function()
+   local clients = vim
+     .iter(vim.lsp.get_active_clients { bufnr = 0 })
+     :map(function(client)
+       if client.name == 'null-ls' then
+         return ('null-ls(%s)'):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
+       end
+     end)
+     :totable()
+   return ' ' .. table.concat(clients, ', ')
+ end
+ 
require('lualine').setup {
  -- ...
}

おわりに

ステータスラインからアクティブな言語サーバーがわかるようになり、 :LspInfo を実行しなくても確認できるようになりました。

参考リンク

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1