info.lua 5.4 KB

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