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 function str_list(list)
  14. return fmt("[ %s ]", table.concat(list, ", "))
  15. end
  16. local function get_formatter_suggestion_msg(ft)
  17. local config = require "config"
  18. local null_formatters = require "lsp.null-ls.formatters"
  19. local supported_formatters = null_formatters.list_available(ft)
  20. local section = {
  21. " HINT ",
  22. "",
  23. fmt("* List of supported formatters: %s", str_list(supported_formatters)),
  24. }
  25. if not vim.tbl_isempty(supported_formatters) then
  26. vim.list_extend(section, {
  27. "* Configured formatter needs to be installed and executable.",
  28. fmt("* Enable installed formatter(s) with following config in %s", config.path),
  29. "",
  30. fmt(" lvim.lang.%s.formatters = { { exe = '%s' } }", ft, table.concat(supported_formatters, "│")),
  31. })
  32. end
  33. return section
  34. end
  35. local function get_linter_suggestion_msg(ft)
  36. local config = require "config"
  37. local null_linters = require "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", config.path),
  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 = require("lsp.utils").get_ls_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 lsp_utils = require "lsp.utils"
  81. local clients = lsp_utils.get_active_client_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