info.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. local M = {
  2. banner = {
  3. "",
  4. [[ __ _ ___ ]],
  5. [[ / / __ ______ ____ _____| | / (_)___ ___ ]],
  6. [[ / / / / / / __ \/ __ `/ ___/ | / / / __ `__ \]],
  7. [[ / /___/ /_/ / / / / /_/ / / | |/ / / / / / / /]],
  8. [[/_____/\__,_/_/ /_/\__,_/_/ |___/_/_/ /_/ /_/ ]],
  9. },
  10. }
  11. local fmt = string.format
  12. local function str_list(list)
  13. return fmt("[ %s ]", table.concat(list, ", "))
  14. end
  15. local function get_formatter_suggestion_msg(ft)
  16. local config = require "config"
  17. local null_formatters = require "lsp.null-ls.formatters"
  18. local supported_formatters = null_formatters.list_available(ft)
  19. local section = {
  20. " HINT ",
  21. "",
  22. fmt("* List of supported formatters: %s", str_list(supported_formatters)),
  23. }
  24. if not vim.tbl_isempty(supported_formatters) then
  25. vim.list_extend(section, {
  26. "* Configured formatter needs to be installed and executable.",
  27. fmt("* Enable installed formatter(s) with following config in %s", config.path),
  28. "",
  29. fmt(" lvim.lang.%s.formatters = { { exe = '%s' } }", ft, table.concat(supported_formatters, "│")),
  30. })
  31. end
  32. return section
  33. end
  34. local function get_linter_suggestion_msg(ft)
  35. local config = require "config"
  36. local null_linters = require "lsp.null-ls.linters"
  37. local supported_linters = null_linters.list_available(ft)
  38. local section = {
  39. " HINT ",
  40. "",
  41. fmt("* List of supported linters: %s", str_list(supported_linters)),
  42. }
  43. if not vim.tbl_isempty(supported_linters) then
  44. vim.list_extend(section, {
  45. "* Configured linter needs to be installed and executable.",
  46. fmt("* Enable installed linter(s) with following config in %s", config.path),
  47. "",
  48. fmt(" lvim.lang.%s.linters = { { exe = '%s' } }", ft, table.concat(supported_linters, "│")),
  49. })
  50. end
  51. return section
  52. end
  53. local function tbl_set_highlight(terms, highlight_group)
  54. for _, v in pairs(terms) do
  55. vim.cmd('let m=matchadd("' .. highlight_group .. '", "' .. v .. "[ ,│']\")")
  56. end
  57. end
  58. function M.toggle_popup(ft)
  59. local lsp_utils = require "lsp.utils"
  60. local client = lsp_utils.get_active_client_by_ft(ft)
  61. local is_client_active = false
  62. local client_enabled_caps = {}
  63. local client_name = ""
  64. local client_id = 0
  65. local document_formatting = false
  66. if client ~= nil then
  67. is_client_active = not client.is_stopped()
  68. client_enabled_caps = require("lsp").get_ls_capabilities(client.id)
  69. client_name = client.name
  70. client_id = client.id
  71. document_formatting = client.resolved_capabilities.document_formatting
  72. end
  73. local header = {
  74. fmt("Detected filetype: %s", ft),
  75. fmt("Treesitter active: %s", tostring(next(vim.treesitter.highlighter.active) ~= nil)),
  76. }
  77. local text = require "interface.text"
  78. local lsp_info = {
  79. "Language Server Protocol (LSP) info",
  80. fmt("* Associated server: %s", client_name),
  81. fmt("* Active: %s (id: %d)", tostring(is_client_active), client_id),
  82. fmt("* Supports formatting: %s", tostring(document_formatting)),
  83. }
  84. if not vim.tbl_isempty(client_enabled_caps) then
  85. local caps_text = "* Capabilities list: "
  86. local caps_text_len = caps_text:len()
  87. local enabled_caps = text.format_table(client_enabled_caps, 3, " | ")
  88. enabled_caps = text.shift_right(enabled_caps, caps_text_len)
  89. enabled_caps[1] = fmt("%s%s", caps_text, enabled_caps[1]:sub(caps_text_len + 1))
  90. vim.list_extend(lsp_info, enabled_caps)
  91. end
  92. local null_ls = require "lsp.null-ls"
  93. local registered_providers = null_ls.list_supported_provider_names(ft)
  94. local registered_count = vim.tbl_count(registered_providers)
  95. local null_ls_info = {
  96. "Formatters and linters",
  97. fmt(
  98. "* Configured providers: %s%s",
  99. table.concat(registered_providers, "  , "),
  100. registered_count > 0 and "  " or ""
  101. ),
  102. }
  103. local null_formatters = require "lsp.null-ls.formatters"
  104. local missing_formatters = null_formatters.list_unsupported_names(ft)
  105. local missing_formatters_status = {}
  106. if not vim.tbl_isempty(missing_formatters) then
  107. missing_formatters_status = {
  108. fmt("* Missing formatters: %s", table.concat(missing_formatters, "  , ") .. "  "),
  109. }
  110. end
  111. local null_linters = require "lsp.null-ls.linters"
  112. local missing_linters = null_linters.list_unsupported_names(ft)
  113. local missing_linters_status = {}
  114. if not vim.tbl_isempty(missing_linters) then
  115. missing_linters_status = {
  116. fmt("* Missing linters: %s", table.concat(missing_linters, "  , ") .. "  "),
  117. }
  118. end
  119. local content_provider = function(popup)
  120. local content = {}
  121. for _, section in ipairs {
  122. M.banner,
  123. { "" },
  124. { "" },
  125. header,
  126. { "" },
  127. lsp_info,
  128. { "" },
  129. null_ls_info,
  130. missing_formatters_status,
  131. missing_linters_status,
  132. { "" },
  133. { "" },
  134. get_formatter_suggestion_msg(ft),
  135. { "" },
  136. { "" },
  137. get_linter_suggestion_msg(ft),
  138. } do
  139. vim.list_extend(content, section)
  140. end
  141. return text.align_left(popup, content, 0.5)
  142. end
  143. local function set_syntax_hl()
  144. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  145. vim.cmd [[highlight link LvimInfoHeader Type]]
  146. vim.cmd [[let m=matchadd("LvimInfoHeader", "Language Server Protocol (LSP) info")]]
  147. vim.cmd [[let m=matchadd("LvimInfoHeader", "Formatters and linters")]]
  148. vim.cmd('let m=matchadd("LvimInfoIdentifier", " ' .. ft .. '$")')
  149. vim.cmd 'let m=matchadd("string", "true")'
  150. vim.cmd 'let m=matchadd("error", "false")'
  151. tbl_set_highlight(registered_providers, "LvimInfoIdentifier")
  152. tbl_set_highlight(missing_formatters, "LvimInfoIdentifier")
  153. tbl_set_highlight(missing_linters, "LvimInfoIdentifier")
  154. -- tbl_set_highlight(require("lsp.null-ls.formatters").list_available(ft), "LvimInfoIdentifier")
  155. -- tbl_set_highlight(require("lsp.null-ls.linters").list_available(ft), "LvimInfoIdentifier")
  156. vim.cmd('let m=matchadd("LvimInfoIdentifier", "' .. client_name .. '")')
  157. end
  158. local Popup = require("interface.popup"):new {
  159. win_opts = { number = false },
  160. buf_opts = { modifiable = false, filetype = "lspinfo" },
  161. }
  162. Popup:display(content_provider)
  163. set_syntax_hl()
  164. return Popup
  165. end
  166. return M