info.lua 5.5 KB

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