info.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, "  , "),
  26. vim.tbl_count(registered_formatters) > 0 and "  " 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, "  , "),
  40. vim.tbl_count(registered_actions) > 0 and "  " 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, "  , "),
  54. vim.tbl_count(registered_linters) > 0 and "  " 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. end
  64. end
  65. local function make_client_info(client)
  66. if client.name == "null-ls" then
  67. return
  68. end
  69. local client_enabled_caps = lsp_utils.get_client_capabilities(client.id)
  70. local name = client.name
  71. local id = client.id
  72. local filetypes = lsp_utils.get_supported_filetypes(name)
  73. local attached_buffers_list = str_list(vim.lsp.get_buffers_by_client_id(client.id))
  74. local client_info = {
  75. fmt("* name: %s", name),
  76. fmt("* id: %s", tostring(id)),
  77. fmt("* supported filetype(s): %s", str_list(filetypes)),
  78. fmt("* attached buffers: %s", tostring(attached_buffers_list)),
  79. fmt("* root_dir pattern: %s", tostring(attached_buffers_list)),
  80. }
  81. if not vim.tbl_isempty(client_enabled_caps) then
  82. local caps_text = "* capabilities: "
  83. local caps_text_len = caps_text:len()
  84. local enabled_caps = text.format_table(client_enabled_caps, 3, " | ")
  85. enabled_caps = text.shift_right(enabled_caps, caps_text_len)
  86. enabled_caps[1] = fmt("%s%s", caps_text, enabled_caps[1]:sub(caps_text_len + 1))
  87. vim.list_extend(client_info, enabled_caps)
  88. end
  89. return client_info
  90. end
  91. local function make_auto_lsp_info(ft)
  92. local skipped_filetypes = lvim.lsp.automatic_configuration.skipped_filetypes
  93. local skipped_servers = lvim.lsp.automatic_configuration.skipped_servers
  94. local info_lines = { "Automatic LSP info" }
  95. if vim.tbl_contains(skipped_filetypes, ft) then
  96. vim.list_extend(info_lines, { "* Status: disabled for " .. ft })
  97. return info_lines
  98. end
  99. local available = lsp_utils.get_supported_servers_per_filetype(ft)
  100. local skipped = vim.tbl_filter(function(name)
  101. return vim.tbl_contains(available, name)
  102. end, skipped_servers)
  103. if #skipped == 0 then
  104. return { "" }
  105. end
  106. vim.list_extend(info_lines, { fmt("* Skipped servers: %s", str_list(skipped)) })
  107. return info_lines
  108. end
  109. function M.toggle_popup(ft)
  110. local clients = vim.lsp.get_active_clients()
  111. local client_names = {}
  112. local bufnr = vim.api.nvim_get_current_buf()
  113. local ts_active_buffers = vim.tbl_keys(vim.treesitter.highlighter.active)
  114. local is_treesitter_active = function()
  115. local status = "inactive"
  116. if vim.tbl_contains(ts_active_buffers, bufnr) then
  117. status = "active"
  118. end
  119. return status
  120. end
  121. local header = {
  122. "Buffer info",
  123. fmt("* filetype: %s", ft),
  124. fmt("* bufnr: %s", bufnr),
  125. fmt("* treesitter status: %s", is_treesitter_active()),
  126. }
  127. local lsp_info = {
  128. "Active client(s)",
  129. }
  130. for _, client in pairs(clients) do
  131. local client_info = make_client_info(client)
  132. if client_info then
  133. vim.list_extend(lsp_info, client_info)
  134. end
  135. table.insert(client_names, client.name)
  136. end
  137. local auto_lsp_info = make_auto_lsp_info(ft)
  138. local formatters_info = make_formatters_info(ft)
  139. local linters_info = make_linters_info(ft)
  140. local code_actions_info = make_code_actions_info(ft)
  141. local content_provider = function(popup)
  142. local content = {}
  143. for _, section in ipairs {
  144. M.banner,
  145. { "" },
  146. { "" },
  147. header,
  148. { "" },
  149. lsp_info,
  150. { "" },
  151. auto_lsp_info,
  152. { "" },
  153. formatters_info,
  154. { "" },
  155. linters_info,
  156. { "" },
  157. code_actions_info,
  158. } do
  159. vim.list_extend(content, section)
  160. end
  161. return text.align_left(popup, content, 0.5)
  162. end
  163. local function set_syntax_hl()
  164. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  165. vim.cmd [[highlight link LvimInfoHeader Type]]
  166. vim.fn.matchadd("LvimInfoHeader", "Buffer info")
  167. vim.fn.matchadd("LvimInfoHeader", "Active client(s)")
  168. vim.fn.matchadd("LvimInfoHeader", fmt("Overridden %s server(s)", ft))
  169. vim.fn.matchadd("LvimInfoHeader", "Formatters info")
  170. vim.fn.matchadd("LvimInfoHeader", "Linters info")
  171. vim.fn.matchadd("LvimInfoHeader", "Code actions info")
  172. vim.fn.matchadd("LvimInfoHeader", "Automatic LSP info")
  173. vim.fn.matchadd("LvimInfoIdentifier", " " .. ft .. "$")
  174. vim.fn.matchadd("string", "true")
  175. vim.fn.matchadd("string", "active")
  176. vim.fn.matchadd("string", "")
  177. vim.fn.matchadd("boolean", "inactive")
  178. vim.fn.matchadd("error", "false")
  179. tbl_set_highlight(require("lvim.lsp.null-ls.formatters").list_registered(ft), "LvimInfoIdentifier")
  180. tbl_set_highlight(require("lvim.lsp.null-ls.linters").list_registered(ft), "LvimInfoIdentifier")
  181. tbl_set_highlight(require("lvim.lsp.null-ls.code_actions").list_registered(ft), "LvimInfoIdentifier")
  182. end
  183. local Popup = require("lvim.interface.popup"):new {
  184. win_opts = { number = false },
  185. buf_opts = { modifiable = false, filetype = "lspinfo" },
  186. }
  187. Popup:display(content_provider)
  188. set_syntax_hl()
  189. return Popup
  190. end
  191. return M