info.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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(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. local client_enabled_caps = lsp_utils.get_client_capabilities(client.id)
  67. local name = client.name
  68. local id = client.id
  69. local filetypes = lsp_utils.get_supported_filetypes(name)
  70. local document_formatting = client.resolved_capabilities.document_formatting
  71. local attached_buffers_list = table.concat(vim.lsp.get_buffers_by_client_id(client.id), ", ")
  72. local client_info = {
  73. fmt("* Name: %s", name),
  74. fmt("* Id: [%s]", tostring(id)),
  75. fmt("* filetype(s): [%s]", table.concat(filetypes, ", ")),
  76. fmt("* Attached buffers: [%s]", tostring(attached_buffers_list)),
  77. fmt("* Supports formatting: %s", tostring(document_formatting)),
  78. }
  79. if not vim.tbl_isempty(client_enabled_caps) then
  80. local caps_text = "* Capabilities list: "
  81. local caps_text_len = caps_text:len()
  82. local enabled_caps = text.format_table(client_enabled_caps, 3, " | ")
  83. enabled_caps = text.shift_right(enabled_caps, caps_text_len)
  84. enabled_caps[1] = fmt("%s%s", caps_text, enabled_caps[1]:sub(caps_text_len + 1))
  85. vim.list_extend(client_info, enabled_caps)
  86. end
  87. return client_info
  88. end
  89. function M.toggle_popup(ft)
  90. local clients = lsp_utils.get_active_clients_by_ft(ft)
  91. local client_names = {}
  92. local bufnr = vim.api.nvim_get_current_buf()
  93. local ts_active_buffers = vim.tbl_keys(vim.treesitter.highlighter.active)
  94. local is_treesitter_active = function()
  95. local status = "inactive"
  96. if vim.tbl_contains(ts_active_buffers, bufnr) then
  97. status = "active"
  98. end
  99. return status
  100. end
  101. local header = {
  102. fmt("Detected filetype: %s", ft),
  103. fmt("Current buffer number: [%s]", bufnr),
  104. }
  105. local ts_info = {
  106. "Treesitter info",
  107. fmt("* current buffer: %s", is_treesitter_active()),
  108. fmt("* list: [%s]", table.concat(ts_active_buffers, ", ")),
  109. }
  110. local lsp_info = {
  111. "Language Server Protocol (LSP) info",
  112. fmt "* Active server(s):",
  113. }
  114. for _, client in pairs(clients) do
  115. vim.list_extend(lsp_info, make_client_info(client))
  116. table.insert(client_names, client.name)
  117. end
  118. local formatters_info = make_formatters_info(ft)
  119. local linters_info = make_linters_info(ft)
  120. local code_actions_info = make_code_actions_info(ft)
  121. local content_provider = function(popup)
  122. local content = {}
  123. for _, section in ipairs {
  124. M.banner,
  125. { "" },
  126. { "" },
  127. header,
  128. { "" },
  129. ts_info,
  130. { "" },
  131. lsp_info,
  132. { "" },
  133. formatters_info,
  134. { "" },
  135. linters_info,
  136. { "" },
  137. code_actions_info,
  138. } do
  139. vim.list_extend(content, section)
  140. end
  141. return text.align_left(popup, content, 0.5)
  142. end
  143. local function set_syntax_hl()
  144. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  145. vim.cmd [[highlight link LvimInfoHeader Type]]
  146. vim.fn.matchadd("LvimInfoHeader", "Treesitter info")
  147. vim.fn.matchadd("LvimInfoHeader", "Language Server Protocol (LSP) info")
  148. vim.fn.matchadd("LvimInfoHeader", "Formatters info")
  149. vim.fn.matchadd("LvimInfoHeader", "Linters info")
  150. vim.fn.matchadd("LvimInfoHeader", "Code actions info")
  151. vim.fn.matchadd("LvimInfoIdentifier", " " .. ft .. "$")
  152. vim.fn.matchadd("string", "true")
  153. vim.fn.matchadd("string", "active")
  154. vim.fn.matchadd("string", "")
  155. vim.fn.matchadd("boolean", "inactive")
  156. vim.fn.matchadd("error", "false")
  157. tbl_set_highlight(require("lvim.lsp.null-ls.formatters").list_registered(ft), "LvimInfoIdentifier")
  158. tbl_set_highlight(require("lvim.lsp.null-ls.linters").list_registered(ft), "LvimInfoIdentifier")
  159. tbl_set_highlight(require("lvim.lsp.null-ls.code_actions").list_registered(ft), "LvimInfoIdentifier")
  160. end
  161. local Popup = require("lvim.interface.popup"):new {
  162. win_opts = { number = false },
  163. buf_opts = { modifiable = false, filetype = "lspinfo" },
  164. }
  165. Popup:display(content_provider)
  166. set_syntax_hl()
  167. return Popup
  168. end
  169. return M