info.lua 5.6 KB

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