info.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_override_info(ft)
  92. local available = lsp_utils.get_supported_servers_per_filetype(ft)
  93. local overridden = vim.tbl_filter(function(name)
  94. return vim.tbl_contains(available, name)
  95. end, lvim.lsp.override)
  96. local info_lines = { "" }
  97. if #overridden == 0 then
  98. return info_lines
  99. end
  100. info_lines = {
  101. fmt("Overridden %s server(s)", ft),
  102. fmt("* list: %s", str_list(overridden)),
  103. }
  104. return info_lines
  105. end
  106. function M.toggle_popup(ft)
  107. local clients = vim.lsp.get_active_clients()
  108. local client_names = {}
  109. local bufnr = vim.api.nvim_get_current_buf()
  110. local ts_active_buffers = vim.tbl_keys(vim.treesitter.highlighter.active)
  111. local is_treesitter_active = function()
  112. local status = "inactive"
  113. if vim.tbl_contains(ts_active_buffers, bufnr) then
  114. status = "active"
  115. end
  116. return status
  117. end
  118. local header = {
  119. "Buffer info",
  120. fmt("* filetype: %s", ft),
  121. fmt("* bufnr: %s", bufnr),
  122. fmt("* treesitter status: %s", is_treesitter_active()),
  123. }
  124. local lsp_info = {
  125. "Active client(s)",
  126. }
  127. for _, client in pairs(clients) do
  128. local client_info = make_client_info(client)
  129. if client_info then
  130. vim.list_extend(lsp_info, client_info)
  131. end
  132. table.insert(client_names, client.name)
  133. end
  134. local override_info = make_override_info(ft)
  135. local formatters_info = make_formatters_info(ft)
  136. local linters_info = make_linters_info(ft)
  137. local code_actions_info = make_code_actions_info(ft)
  138. local content_provider = function(popup)
  139. local content = {}
  140. for _, section in ipairs {
  141. M.banner,
  142. { "" },
  143. { "" },
  144. header,
  145. { "" },
  146. lsp_info,
  147. { "" },
  148. override_info,
  149. { "" },
  150. formatters_info,
  151. { "" },
  152. linters_info,
  153. { "" },
  154. code_actions_info,
  155. } do
  156. vim.list_extend(content, section)
  157. end
  158. return text.align_left(popup, content, 0.5)
  159. end
  160. local function set_syntax_hl()
  161. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  162. vim.cmd [[highlight link LvimInfoHeader Type]]
  163. vim.fn.matchadd("LvimInfoHeader", "Buffer info")
  164. vim.fn.matchadd("LvimInfoHeader", "Active client(s)")
  165. vim.fn.matchadd("LvimInfoHeader", fmt("Overridden %s server(s)", ft))
  166. vim.fn.matchadd("LvimInfoHeader", "Formatters info")
  167. vim.fn.matchadd("LvimInfoHeader", "Linters info")
  168. vim.fn.matchadd("LvimInfoHeader", "Code actions info")
  169. vim.fn.matchadd("LvimInfoIdentifier", " " .. ft .. "$")
  170. vim.fn.matchadd("string", "true")
  171. vim.fn.matchadd("string", "active")
  172. vim.fn.matchadd("string", "")
  173. vim.fn.matchadd("boolean", "inactive")
  174. vim.fn.matchadd("error", "false")
  175. tbl_set_highlight(require("lvim.lsp.null-ls.formatters").list_registered(ft), "LvimInfoIdentifier")
  176. tbl_set_highlight(require("lvim.lsp.null-ls.linters").list_registered(ft), "LvimInfoIdentifier")
  177. tbl_set_highlight(require("lvim.lsp.null-ls.code_actions").list_registered(ft), "LvimInfoIdentifier")
  178. end
  179. local Popup = require("lvim.interface.popup"):new {
  180. win_opts = { number = false },
  181. buf_opts = { modifiable = false, filetype = "lspinfo" },
  182. }
  183. Popup:display(content_provider)
  184. set_syntax_hl()
  185. return Popup
  186. end
  187. return M