info.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 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_providers(ft)
  20. -- print("reg", vim.inspect(registered_formatters))
  21. local supported_formatters = null_formatters.list_available(ft)
  22. local section = {
  23. "Formatters info",
  24. fmt(
  25. "* Active: %s%s",
  26. table.concat(registered_formatters, "  , "),
  27. vim.tbl_count(registered_formatters) > 0 and "  " or ""
  28. ),
  29. fmt("* Supported: %s", str_list(supported_formatters)),
  30. }
  31. return section
  32. end
  33. local function make_code_actions_info(ft)
  34. local null_actions = require "lvim.lsp.null-ls.code_actions"
  35. local registered_actions = null_actions.list_registered_providers(ft)
  36. local supported_actions = null_actions.list_available(ft)
  37. local section = {
  38. "Code actions info",
  39. fmt(
  40. "* Active: %s%s",
  41. table.concat(registered_actions, "  , "),
  42. vim.tbl_count(registered_actions) > 0 and "  " or ""
  43. ),
  44. fmt("* Supported: %s", str_list(supported_actions)),
  45. }
  46. return section
  47. end
  48. local function make_linters_info(ft)
  49. local null_linters = require "lvim.lsp.null-ls.linters"
  50. local supported_linters = null_linters.list_available(ft)
  51. local registered_linters = null_linters.list_registered_providers(ft)
  52. local section = {
  53. "Linters info",
  54. fmt(
  55. "* Active: %s%s",
  56. table.concat(registered_linters, "  , "),
  57. vim.tbl_count(registered_linters) > 0 and "  " or ""
  58. ),
  59. fmt("* Supported: %s", str_list(supported_linters)),
  60. }
  61. return section
  62. end
  63. local function tbl_set_highlight(terms, highlight_group)
  64. for _, v in pairs(terms) do
  65. vim.cmd('let m=matchadd("' .. highlight_group .. '", "' .. v .. "[ ,│']\")")
  66. end
  67. end
  68. local function make_client_info(client)
  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 document_formatting = client.resolved_capabilities.document_formatting
  74. local attached_buffers_list = table.concat(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("* filetype(s): [%s]", table.concat(filetypes, ", ")),
  79. fmt("* Attached buffers: [%s]", tostring(attached_buffers_list)),
  80. fmt("* Supports formatting: %s", tostring(document_formatting)),
  81. }
  82. if not vim.tbl_isempty(client_enabled_caps) then
  83. local caps_text = "* Capabilities list: "
  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. function M.toggle_popup(ft)
  93. local clients = lsp_utils.get_active_clients_by_ft(ft)
  94. local client_names = {}
  95. local bufnr = vim.api.nvim_get_current_buf()
  96. local ts_active_buffers = vim.tbl_keys(vim.treesitter.highlighter.active)
  97. local is_treesitter_active = function()
  98. local status = "inactive"
  99. if vim.tbl_contains(ts_active_buffers, bufnr) then
  100. status = "active"
  101. end
  102. return status
  103. end
  104. local header = {
  105. fmt("Detected filetype: %s", ft),
  106. fmt("Current buffer number: [%s]", bufnr),
  107. }
  108. local ts_info = {
  109. "Treesitter info",
  110. fmt("* current buffer: %s", is_treesitter_active()),
  111. fmt("* list: [%s]", table.concat(ts_active_buffers, ", ")),
  112. }
  113. local lsp_info = {
  114. "Language Server Protocol (LSP) info",
  115. fmt "* Active server(s):",
  116. }
  117. for _, client in pairs(clients) do
  118. vim.list_extend(lsp_info, make_client_info(client))
  119. table.insert(client_names, client.name)
  120. end
  121. local formatters_info = make_formatters_info(ft)
  122. local linters_info = make_linters_info(ft)
  123. local code_actions_info = make_code_actions_info(ft)
  124. local content_provider = function(popup)
  125. local content = {}
  126. for _, section in ipairs {
  127. M.banner,
  128. { "" },
  129. { "" },
  130. header,
  131. { "" },
  132. ts_info,
  133. { "" },
  134. lsp_info,
  135. { "" },
  136. formatters_info,
  137. { "" },
  138. linters_info,
  139. { "" },
  140. code_actions_info,
  141. } do
  142. vim.list_extend(content, section)
  143. end
  144. return text.align_left(popup, content, 0.5)
  145. end
  146. local function set_syntax_hl()
  147. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  148. vim.cmd [[highlight link LvimInfoHeader Type]]
  149. vim.cmd [[let m=matchadd("LvimInfoHeader", "Treesitter info")]]
  150. vim.cmd [[let m=matchadd("LvimInfoHeader", "Language Server Protocol (LSP) info")]]
  151. vim.cmd [[let m=matchadd("LvimInfoHeader", "Formatters info")]]
  152. vim.cmd [[let m=matchadd("LvimInfoHeader", "Linters info")]]
  153. vim.cmd [[let m=matchadd("LvimInfoHeader", "Code actions info")]]
  154. vim.cmd('let m=matchadd("LvimInfoIdentifier", " ' .. ft .. '$")')
  155. vim.cmd 'let m=matchadd("string", "true")'
  156. vim.cmd 'let m=matchadd("string", "active")'
  157. vim.cmd 'let m=matchadd("boolean", "inactive")'
  158. vim.cmd 'let m=matchadd("string", "")'
  159. vim.cmd 'let m=matchadd("error", "false")'
  160. -- tbl_set_highlight(registered_providers, "LvimInfoIdentifier")
  161. tbl_set_highlight(require("lvim.lsp.null-ls.formatters").list_available(ft), "LvimInfoIdentifier")
  162. tbl_set_highlight(require("lvim.lsp.null-ls.linters").list_available(ft), "LvimInfoIdentifier")
  163. tbl_set_highlight(require("lvim.lsp.null-ls.code_actions").list_available(ft), "LvimInfoIdentifier")
  164. end
  165. local Popup = require("lvim.interface.popup"):new {
  166. win_opts = { number = false },
  167. buf_opts = { modifiable = false, filetype = "lspinfo" },
  168. }
  169. Popup:display(content_provider)
  170. set_syntax_hl()
  171. return Popup
  172. end
  173. return M