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 user_config_file = require("lvim.config"):get_user_config_path()
  15. local function str_list(list)
  16. return fmt("[ %s ]", table.concat(list, ", "))
  17. end
  18. local function get_formatter_suggestion_msg(ft)
  19. local null_formatters = require "lvim.lsp.null-ls.formatters"
  20. local supported_formatters = null_formatters.list_available(ft)
  21. local section = {
  22. " HINT ",
  23. "",
  24. fmt("* List of supported formatters: %s", str_list(supported_formatters)),
  25. }
  26. if not vim.tbl_isempty(supported_formatters) then
  27. vim.list_extend(section, {
  28. "* Configured formatter needs to be installed and executable.",
  29. fmt("* Enable installed formatter(s) with following config in %s", user_config_file),
  30. "",
  31. fmt(" lvim.lang.%s.formatters = { { exe = '%s' } }", ft, table.concat(supported_formatters, "│")),
  32. })
  33. end
  34. return section
  35. end
  36. local function get_linter_suggestion_msg(ft)
  37. local null_linters = require "lvim.lsp.null-ls.linters"
  38. local supported_linters = null_linters.list_available(ft)
  39. local section = {
  40. " HINT ",
  41. "",
  42. fmt("* List of supported linters: %s", str_list(supported_linters)),
  43. }
  44. if not vim.tbl_isempty(supported_linters) then
  45. vim.list_extend(section, {
  46. "* Configured linter needs to be installed and executable.",
  47. fmt("* Enable installed linter(s) with following config in %s", user_config_file),
  48. "",
  49. fmt(" lvim.lang.%s.linters = { { exe = '%s' } }", ft, table.concat(supported_linters, "│")),
  50. })
  51. end
  52. return section
  53. end
  54. local function tbl_set_highlight(terms, highlight_group)
  55. for _, v in pairs(terms) do
  56. vim.cmd('let m=matchadd("' .. highlight_group .. '", "' .. v .. "[ ,│']\")")
  57. end
  58. end
  59. local function make_client_info(client)
  60. local client_enabled_caps = lsp_utils.get_client_capabilities(client.id)
  61. local name = client.name
  62. local id = client.id
  63. local document_formatting = client.resolved_capabilities.document_formatting
  64. local client_info = {
  65. fmt("* Name: %s", name),
  66. fmt("* Id: %s", tostring(id)),
  67. fmt("* Supports formatting: %s", tostring(document_formatting)),
  68. }
  69. if not vim.tbl_isempty(client_enabled_caps) then
  70. local caps_text = "* Capabilities list: "
  71. local caps_text_len = caps_text:len()
  72. local enabled_caps = text.format_table(client_enabled_caps, 3, " | ")
  73. enabled_caps = text.shift_right(enabled_caps, caps_text_len)
  74. enabled_caps[1] = fmt("%s%s", caps_text, enabled_caps[1]:sub(caps_text_len + 1))
  75. vim.list_extend(client_info, enabled_caps)
  76. end
  77. return client_info
  78. end
  79. function M.toggle_popup(ft)
  80. local clients = lsp_utils.get_active_clients_by_ft(ft)
  81. local client_names = {}
  82. local header = {
  83. fmt("Detected filetype: %s", ft),
  84. fmt("Treesitter active: %s", tostring(next(vim.treesitter.highlighter.active) ~= nil)),
  85. }
  86. local lsp_info = {
  87. "Language Server Protocol (LSP) info",
  88. fmt "* Associated server(s):",
  89. }
  90. for _, client in pairs(clients) do
  91. vim.list_extend(lsp_info, make_client_info(client))
  92. table.insert(client_names, client.name)
  93. end
  94. local null_formatters = require "lvim.lsp.null-ls.formatters"
  95. local null_linters = require "lvim.lsp.null-ls.linters"
  96. local registered_formatters = null_formatters.list_supported_names(ft)
  97. local registered_linters = null_linters.list_supported_names(ft)
  98. local registered_providers = {}
  99. vim.list_extend(registered_providers, registered_formatters)
  100. vim.list_extend(registered_providers, registered_linters)
  101. local registered_count = vim.tbl_count(registered_providers)
  102. local null_ls_info = {
  103. "Formatters and linters",
  104. fmt(
  105. "* Configured providers: %s%s",
  106. table.concat(registered_providers, "  , "),
  107. registered_count > 0 and "  " or ""
  108. ),
  109. }
  110. local content_provider = function(popup)
  111. local content = {}
  112. for _, section in ipairs {
  113. M.banner,
  114. { "" },
  115. { "" },
  116. header,
  117. { "" },
  118. lsp_info,
  119. { "" },
  120. null_ls_info,
  121. { "" },
  122. { "" },
  123. get_formatter_suggestion_msg(ft),
  124. { "" },
  125. { "" },
  126. get_linter_suggestion_msg(ft),
  127. } do
  128. vim.list_extend(content, section)
  129. end
  130. return text.align_left(popup, content, 0.5)
  131. end
  132. local function set_syntax_hl()
  133. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  134. vim.cmd [[highlight link LvimInfoHeader Type]]
  135. vim.cmd [[let m=matchadd("LvimInfoHeader", "Language Server Protocol (LSP) info")]]
  136. vim.cmd [[let m=matchadd("LvimInfoHeader", "Formatters and linters")]]
  137. vim.cmd('let m=matchadd("LvimInfoIdentifier", " ' .. ft .. '$")')
  138. vim.cmd 'let m=matchadd("string", "true")'
  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