info.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_linters_info(ft)
  34. local null_linters = require "lvim.lsp.null-ls.linters"
  35. local supported_linters = null_linters.list_available(ft)
  36. local registered_linters = null_linters.list_registered_providers(ft)
  37. local section = {
  38. "Linters info",
  39. fmt(
  40. "* Active: %s%s",
  41. table.concat(registered_linters, "  , "),
  42. vim.tbl_count(registered_linters) > 0 and "  " or ""
  43. ),
  44. fmt("* Supported: %s", str_list(supported_linters)),
  45. }
  46. return section
  47. end
  48. local function tbl_set_highlight(terms, highlight_group)
  49. for _, v in pairs(terms) do
  50. vim.cmd('let m=matchadd("' .. highlight_group .. '", "' .. v .. "[ ,│']\")")
  51. end
  52. end
  53. local function make_client_info(client)
  54. local client_enabled_caps = lsp_utils.get_client_capabilities(client.id)
  55. local name = client.name
  56. local id = client.id
  57. local filetypes = lsp_utils.get_supported_filetypes(name)
  58. local document_formatting = client.resolved_capabilities.document_formatting
  59. local attached_buffers_list = table.concat(vim.lsp.get_buffers_by_client_id(client.id), ", ")
  60. local client_info = {
  61. fmt("* Name: %s", name),
  62. fmt("* Id: [%s]", tostring(id)),
  63. fmt("* filetype(s): [%s]", table.concat(filetypes, ", ")),
  64. fmt("* Attached buffers: [%s]", tostring(attached_buffers_list)),
  65. fmt("* Supports formatting: %s", tostring(document_formatting)),
  66. }
  67. if not vim.tbl_isempty(client_enabled_caps) then
  68. local caps_text = "* Capabilities list: "
  69. local caps_text_len = caps_text:len()
  70. local enabled_caps = text.format_table(client_enabled_caps, 3, " | ")
  71. enabled_caps = text.shift_right(enabled_caps, caps_text_len)
  72. enabled_caps[1] = fmt("%s%s", caps_text, enabled_caps[1]:sub(caps_text_len + 1))
  73. vim.list_extend(client_info, enabled_caps)
  74. end
  75. return client_info
  76. end
  77. function M.toggle_popup(ft)
  78. local clients = lsp_utils.get_active_clients_by_ft(ft)
  79. local client_names = {}
  80. local bufnr = vim.api.nvim_get_current_buf()
  81. local ts_active_buffers = vim.tbl_keys(vim.treesitter.highlighter.active)
  82. local is_treesitter_active = function()
  83. local status = "inactive"
  84. if vim.tbl_contains(ts_active_buffers, bufnr) then
  85. status = "active"
  86. end
  87. return status
  88. end
  89. local header = {
  90. fmt("Detected filetype: %s", ft),
  91. fmt("Current buffer number: [%s]", bufnr),
  92. }
  93. local ts_info = {
  94. "Treesitter info",
  95. fmt("* current buffer: %s", is_treesitter_active()),
  96. fmt("* list: [%s]", table.concat(ts_active_buffers, ", ")),
  97. }
  98. local lsp_info = {
  99. "Language Server Protocol (LSP) info",
  100. fmt "* Active server(s):",
  101. }
  102. for _, client in pairs(clients) do
  103. vim.list_extend(lsp_info, make_client_info(client))
  104. table.insert(client_names, client.name)
  105. end
  106. local formatters_info = make_formatters_info(ft)
  107. local linters_info = make_linters_info(ft)
  108. local content_provider = function(popup)
  109. local content = {}
  110. for _, section in ipairs {
  111. M.banner,
  112. { "" },
  113. { "" },
  114. header,
  115. { "" },
  116. ts_info,
  117. { "" },
  118. lsp_info,
  119. { "" },
  120. formatters_info,
  121. { "" },
  122. linters_info,
  123. } do
  124. vim.list_extend(content, section)
  125. end
  126. return text.align_left(popup, content, 0.5)
  127. end
  128. local function set_syntax_hl()
  129. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  130. vim.cmd [[highlight link LvimInfoHeader Type]]
  131. vim.cmd [[let m=matchadd("LvimInfoHeader", "Treesitter info")]]
  132. vim.cmd [[let m=matchadd("LvimInfoHeader", "Language Server Protocol (LSP) info")]]
  133. vim.cmd [[let m=matchadd("LvimInfoHeader", "Formatters info")]]
  134. vim.cmd [[let m=matchadd("LvimInfoHeader", "Linters info")]]
  135. vim.cmd('let m=matchadd("LvimInfoIdentifier", " ' .. ft .. '$")')
  136. vim.cmd 'let m=matchadd("string", "true")'
  137. vim.cmd 'let m=matchadd("string", "active")'
  138. vim.cmd 'let m=matchadd("boolean", "inactive")'
  139. vim.cmd 'let m=matchadd("string", "")'
  140. vim.cmd 'let m=matchadd("error", "false")'
  141. -- tbl_set_highlight(registered_providers, "LvimInfoIdentifier")
  142. tbl_set_highlight(require("lvim.lsp.null-ls.formatters").list_available(ft), "LvimInfoIdentifier")
  143. tbl_set_highlight(require("lvim.lsp.null-ls.linters").list_available(ft), "LvimInfoIdentifier")
  144. end
  145. local Popup = require("lvim.interface.popup"):new {
  146. win_opts = { number = false },
  147. buf_opts = { modifiable = false, filetype = "lspinfo" },
  148. }
  149. Popup:display(content_provider)
  150. set_syntax_hl()
  151. return Popup
  152. end
  153. return M