info.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. local M = {
  2. banner = {
  3. "",
  4. [[ __ _ ___ ]],
  5. [[ / / __ ______ ____ _____| | / (_)___ ___ ]],
  6. [[ / / / / / / __ \/ __ `/ ___/ | / / / __ `__ \]],
  7. [[ / /___/ /_/ / / / / /_/ / / | |/ / / / / / / /]],
  8. [[/_____/\__,_/_/ /_/\__,_/_/ |___/_/_/ /_/ /_/ ]],
  9. },
  10. }
  11. local fmt = string.format
  12. local text = require "lvim.interface.text"
  13. local lsp_utils = require "lvim.lsp.utils"
  14. local function str_list(list)
  15. return #list == 1 and list[1] or fmt("[%s]", table.concat(list, ", "))
  16. end
  17. local function make_formatters_info(ft)
  18. local null_formatters = require "lvim.lsp.null-ls.formatters"
  19. local registered_formatters = null_formatters.list_registered(ft)
  20. local supported_formatters = null_formatters.list_supported(ft)
  21. local section = {
  22. "Formatters info",
  23. fmt(
  24. "* Active: %s%s",
  25. table.concat(registered_formatters, " " .. lvim.icons.ui.BoxChecked .. " , "),
  26. vim.tbl_count(registered_formatters) > 0 and " " .. lvim.icons.ui.BoxChecked .. " " or ""
  27. ),
  28. fmt("* Supported: %s", str_list(supported_formatters)),
  29. }
  30. return section
  31. end
  32. local function make_code_actions_info(ft)
  33. local null_actions = require "lvim.lsp.null-ls.code_actions"
  34. local registered_actions = null_actions.list_registered(ft)
  35. local section = {
  36. "Code actions info",
  37. fmt(
  38. "* Active: %s%s",
  39. table.concat(registered_actions, " " .. lvim.icons.ui.BoxChecked .. " , "),
  40. vim.tbl_count(registered_actions) > 0 and " " .. lvim.icons.ui.BoxChecked .. " " or ""
  41. ),
  42. }
  43. return section
  44. end
  45. local function make_linters_info(ft)
  46. local null_linters = require "lvim.lsp.null-ls.linters"
  47. local supported_linters = null_linters.list_supported(ft)
  48. local registered_linters = null_linters.list_registered(ft)
  49. local section = {
  50. "Linters info",
  51. fmt(
  52. "* Active: %s%s",
  53. table.concat(registered_linters, " " .. lvim.icons.ui.BoxChecked .. " , "),
  54. vim.tbl_count(registered_linters) > 0 and " " .. lvim.icons.ui.BoxChecked .. " " or ""
  55. ),
  56. fmt("* Supported: %s", str_list(supported_linters)),
  57. }
  58. return section
  59. end
  60. local function tbl_set_highlight(terms, highlight_group)
  61. for _, v in pairs(terms) do
  62. vim.cmd('let m=matchadd("' .. highlight_group .. '", "' .. v .. "[ ,│']\")")
  63. vim.cmd('let m=matchadd("' .. highlight_group .. '", ", ' .. v .. '")')
  64. end
  65. end
  66. local function make_client_info(client)
  67. if client.name == "null-ls" then
  68. return
  69. end
  70. local client_enabled_caps = lsp_utils.get_client_capabilities(client.id)
  71. local name = client.name
  72. local id = client.id
  73. local filetypes = lsp_utils.get_supported_filetypes(name)
  74. local attached_buffers_list = str_list(vim.lsp.get_buffers_by_client_id(client.id))
  75. local client_info = {
  76. fmt("* name: %s", name),
  77. fmt("* id: %s", tostring(id)),
  78. fmt("* supported filetype(s): %s", str_list(filetypes)),
  79. fmt("* attached buffers: %s", tostring(attached_buffers_list)),
  80. fmt("* root_dir pattern: %s", tostring(attached_buffers_list)),
  81. }
  82. if not vim.tbl_isempty(client_enabled_caps) then
  83. local caps_text = "* capabilities: "
  84. local caps_text_len = caps_text:len()
  85. local enabled_caps = text.format_table(client_enabled_caps, 3, " | ")
  86. enabled_caps = text.shift_right(enabled_caps, caps_text_len)
  87. enabled_caps[1] = fmt("%s%s", caps_text, enabled_caps[1]:sub(caps_text_len + 1))
  88. vim.list_extend(client_info, enabled_caps)
  89. end
  90. return client_info
  91. end
  92. local function make_auto_lsp_info(ft)
  93. local skipped_filetypes = lvim.lsp.automatic_configuration.skipped_filetypes
  94. local skipped_servers = lvim.lsp.automatic_configuration.skipped_servers
  95. local info_lines = { "Automatic LSP info" }
  96. if vim.tbl_contains(skipped_filetypes, ft) then
  97. vim.list_extend(info_lines, { "* Status: disabled for " .. ft })
  98. return info_lines
  99. end
  100. local supported = lsp_utils.get_supported_servers(ft)
  101. local skipped = vim.tbl_filter(function(name)
  102. return vim.tbl_contains(supported, name)
  103. end, skipped_servers)
  104. if #skipped == 0 then
  105. return { "" }
  106. end
  107. vim.list_extend(info_lines, { fmt("* Skipped servers: %s", str_list(skipped)) })
  108. return info_lines
  109. end
  110. function M.toggle_popup(ft)
  111. local clients = vim.lsp.get_active_clients()
  112. local client_names = {}
  113. local bufnr = vim.api.nvim_get_current_buf()
  114. local ts_active_buffers = vim.tbl_keys(vim.treesitter.highlighter.active)
  115. local is_treesitter_active = function()
  116. local status = "inactive"
  117. if vim.tbl_contains(ts_active_buffers, bufnr) then
  118. status = "active"
  119. end
  120. return status
  121. end
  122. local header = {
  123. "Buffer info",
  124. fmt("* filetype: %s", ft),
  125. fmt("* bufnr: %s", bufnr),
  126. fmt("* treesitter status: %s", is_treesitter_active()),
  127. }
  128. local lsp_info = {
  129. "Active client(s)",
  130. }
  131. for _, client in pairs(clients) do
  132. local client_info = make_client_info(client)
  133. if client_info then
  134. vim.list_extend(lsp_info, client_info)
  135. end
  136. table.insert(client_names, client.name)
  137. end
  138. local auto_lsp_info = make_auto_lsp_info(ft)
  139. local formatters_info = make_formatters_info(ft)
  140. local linters_info = make_linters_info(ft)
  141. local code_actions_info = make_code_actions_info(ft)
  142. local content_provider = function(popup)
  143. local content = {}
  144. for _, section in ipairs {
  145. M.banner,
  146. { "" },
  147. { "" },
  148. header,
  149. { "" },
  150. lsp_info,
  151. { "" },
  152. auto_lsp_info,
  153. { "" },
  154. formatters_info,
  155. { "" },
  156. linters_info,
  157. { "" },
  158. code_actions_info,
  159. } do
  160. vim.list_extend(content, section)
  161. end
  162. return text.align_left(popup, content, 0.5)
  163. end
  164. local function set_syntax_hl()
  165. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  166. vim.cmd [[highlight link LvimInfoHeader Type]]
  167. vim.fn.matchadd("LvimInfoHeader", "Buffer info")
  168. vim.fn.matchadd("LvimInfoHeader", "Active client(s)")
  169. vim.fn.matchadd("LvimInfoHeader", fmt("Overridden %s server(s)", ft))
  170. vim.fn.matchadd("LvimInfoHeader", "Formatters info")
  171. vim.fn.matchadd("LvimInfoHeader", "Linters info")
  172. vim.fn.matchadd("LvimInfoHeader", "Code actions info")
  173. vim.fn.matchadd("LvimInfoHeader", "Automatic LSP info")
  174. vim.fn.matchadd("LvimInfoIdentifier", " " .. ft .. "$")
  175. vim.fn.matchadd("string", "true")
  176. vim.fn.matchadd("string", "active")
  177. vim.fn.matchadd("string", lvim.icons.ui.BoxChecked)
  178. vim.fn.matchadd("boolean", "inactive")
  179. vim.fn.matchadd("error", "false")
  180. tbl_set_highlight(require("lvim.lsp.null-ls.formatters").list_registered(ft), "LvimInfoIdentifier")
  181. tbl_set_highlight(require("lvim.lsp.null-ls.linters").list_registered(ft), "LvimInfoIdentifier")
  182. tbl_set_highlight(require("lvim.lsp.null-ls.code_actions").list_registered(ft), "LvimInfoIdentifier")
  183. end
  184. local Popup = require("lvim.interface.popup"):new {
  185. win_opts = { number = false },
  186. buf_opts = { modifiable = false, filetype = "lspinfo" },
  187. }
  188. Popup:display(content_provider)
  189. set_syntax_hl()
  190. return Popup
  191. end
  192. return M