info.lua 6.1 KB

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