info.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. local M = {}
  2. local u = require "utils"
  3. local null_ls_handler = require "lsp.null-ls"
  4. local indent = " "
  5. M.banner = {
  6. " ",
  7. indent
  8. .. "⠀⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀ ⠀⠀⠀ ⠀⠀ ⣺⡿⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀",
  9. indent
  10. .. "⠀⣿⠇⠀⠀⠀⠀⠀⣤⡄⠀⠀⢠⣤⡄⠀.⣠⣤⣤⣤⡀⠀⠀⢀⣤⣤⣤⣤⡄⠀⠀⠀⣤⣄⣤⣤⣤⠀⠀ ⣿⣯ ⣿⡟⠀ ⣤⣤⠀⠀⠀⠀⣠⣤⣤⣤⣄⣤⣤",
  11. indent
  12. .. "⢠⣿⠀⠀⠀⠀⠀⠀⣿⠃⠀⠀⣸⣿⠁⠀⣿⣿⠉⠀⠈⣿⡇⠀⠀⠛⠋⠀⠀⢹⣿⠀⠀⠀⣿⠏⠀⠸⠿⠃⠀⣿⣿⠀⣰⡟⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⣿⡟⢸⣿⡇⢀⣿",
  13. indent
  14. .. "⣸⡇⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⣿⡟⠀⢠⣿⡇⠀⠀⢰⣿⡇⠀⣰⣾⠟⠛⠛⣻⡇⠀⠀⢸⡿⠀⠀⠀⠀⠀⠀⢻⣿⢰⣿⠀⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⢸⣿⠇⢸⣿⠀⢸⡏",
  15. indent
  16. .. "⣿⣧⣤⣤⣤⡄⠀⠘⣿⣤⣤⡤⣿⠇⠀⢸⣿⠁⠀⠀⣼⣿⠀⠀⢿⣿⣤⣤⠔⣿⠃⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⢸⣿⣿⠋⠀⠀⠀⢠⣤⣤⣿⣥⣤⡄⠀⣼⣿⠀⣸⡏⠀⣿⠃",
  17. indent
  18. .. "⠉⠉⠉⠉⠉⠁⠀⠀⠈⠉⠉⠀⠉⠀⠀⠈⠉⠀⠀⠀⠉⠉⠀⠀⠀⠉⠉⠁⠈⠉⠀⠀⠀⠉⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠀⠀⠀⠀⠈⠉⠉⠉⠉⠉⠁⠀⠉⠁⠀⠉⠁⠀⠉⠀",
  19. "",
  20. }
  21. local function str_list(list)
  22. return "[ " .. table.concat(list, ", ") .. " ]"
  23. end
  24. local function get_formatter_suggestion_msg(ft)
  25. local supported_formatters = u.get_supported_formatters_by_filetype(ft)
  26. return {
  27. indent
  28. .. "───────────────────────────────────────────────────────────────────",
  29. "",
  30. indent .. " HINT ",
  31. "",
  32. indent .. "* List of supported formatters: " .. str_list(supported_formatters),
  33. indent .. "* Configured formatter needs to be installed and executable.",
  34. indent .. "* Enable installed formatter(s) with following config in ~/.config/lvim/config.lua",
  35. "",
  36. indent .. " lvim.lang." .. tostring(ft) .. [[.formatting = { { exe = ']] .. table.concat(
  37. supported_formatters,
  38. "│"
  39. ) .. [[' } }]],
  40. "",
  41. }
  42. end
  43. local function get_linter_suggestion_msg(ft)
  44. local supported_linters = u.get_supported_linters_by_filetype(ft)
  45. return {
  46. indent
  47. .. "───────────────────────────────────────────────────────────────────",
  48. "",
  49. indent .. " HINT ",
  50. "",
  51. indent .. "* List of supported linters: " .. str_list(supported_linters),
  52. indent .. "* Configured linter needs to be installed and executable.",
  53. indent .. "* Enable installed linter(s) with following config in ~/.config/lvim/config.lua",
  54. "",
  55. indent
  56. .. " lvim.lang."
  57. .. tostring(ft)
  58. .. [[.linters = { { exe = ']]
  59. .. table.concat(supported_linters, "│")
  60. .. [[' } }]],
  61. "",
  62. }
  63. end
  64. ---creates an average size popup
  65. ---@param buf_lines a list of lines to print
  66. ---@param callback could be used to set syntax highlighting rules for example
  67. ---@return bufnr buffer number of the created buffer
  68. ---@return win_id window ID of the created popup
  69. function M.create_simple_popup(buf_lines, callback)
  70. -- runtime/lua/vim/lsp/util.lua
  71. local bufnr = vim.api.nvim_create_buf(false, true)
  72. local height_percentage = 0.9
  73. local width_percentage = 0.8
  74. local row_start_percentage = (1 - height_percentage) / 2
  75. local col_start_percentage = (1 - width_percentage) / 2
  76. local opts = {}
  77. opts.relative = "editor"
  78. opts.height = math.min(math.ceil(vim.o.lines * height_percentage), #buf_lines)
  79. opts.row = math.ceil(vim.o.lines * row_start_percentage)
  80. opts.col = math.floor(vim.o.columns * col_start_percentage)
  81. opts.width = math.floor(vim.o.columns * width_percentage)
  82. opts.style = "minimal"
  83. opts.border = "rounded"
  84. --[[
  85. opts.border = {
  86. lvim.builtin.telescope.defaults.borderchars[5], -- "┌",
  87. lvim.builtin.telescope.defaults.borderchars[3], -- "-",
  88. lvim.builtin.telescope.defaults.borderchars[6], -- "┐",
  89. lvim.builtin.telescope.defaults.borderchars[2], -- "|",
  90. lvim.builtin.telescope.defaults.borderchars[7], -- "┘",
  91. lvim.builtin.telescope.defaults.borderchars[3], -- "-",
  92. lvim.builtin.telescope.defaults.borderchars[8], -- "└",
  93. lvim.builtin.telescope.defaults.borderchars[4], -- "|",
  94. }
  95. --]]
  96. local win_id = vim.api.nvim_open_win(bufnr, true, opts)
  97. vim.api.nvim_win_set_buf(win_id, bufnr)
  98. -- this needs to be window option!
  99. vim.api.nvim_win_set_option(win_id, "number", false)
  100. vim.cmd "setlocal nocursorcolumn"
  101. vim.cmd "setlocal wrap"
  102. -- set buffer options
  103. vim.api.nvim_buf_set_option(bufnr, "filetype", "lspinfo")
  104. vim.lsp.util.close_preview_autocmd({ "BufHidden", "BufLeave" }, win_id)
  105. buf_lines = vim.lsp.util._trim(buf_lines, {})
  106. vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, buf_lines)
  107. vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
  108. if type(callback) == "function" then
  109. callback()
  110. end
  111. return bufnr, win_id
  112. end
  113. local function tbl_set_highlight(terms, highlight_group)
  114. if type(terms) ~= "table" then
  115. return
  116. end
  117. for _, v in pairs(terms) do
  118. vim.cmd('let m=matchadd("' .. highlight_group .. '", "' .. v .. '")')
  119. end
  120. end
  121. function M.toggle_popup(ft)
  122. local client = u.get_active_client_by_ft(ft)
  123. local is_client_active = false
  124. local client_enabled_caps = {}
  125. local client_name = ""
  126. local client_id = 0
  127. local document_formatting = false
  128. local missing_linters = {}
  129. local missing_formatters = {}
  130. local num_caps = 0
  131. local null_ls_providers = null_ls_handler.get_registered_providers_by_filetype(ft)
  132. if client ~= nil then
  133. is_client_active = not client.is_stopped()
  134. client_enabled_caps = require("lsp").get_ls_capabilities(client.id)
  135. num_caps = vim.tbl_count(client_enabled_caps)
  136. client_name = client.name
  137. client_id = client.id
  138. document_formatting = client.resolved_capabilities.document_formatting
  139. end
  140. if lvim.lang[ft] ~= nil then
  141. missing_linters = lvim.lang[ft].linters._failed_requests or {}
  142. missing_formatters = lvim.lang[ft].formatters._failed_requests or {}
  143. end
  144. local buf_lines = {}
  145. vim.list_extend(buf_lines, M.banner)
  146. local header = {
  147. indent .. "Detected filetype: " .. tostring(ft),
  148. indent .. "Treesitter active: " .. tostring(next(vim.treesitter.highlighter.active) ~= nil),
  149. "",
  150. }
  151. vim.list_extend(buf_lines, header)
  152. local lsp_info = {
  153. indent .. "Language Server Protocol (LSP) info",
  154. indent .. "* Associated server: " .. client_name,
  155. indent .. "* Active: " .. tostring(is_client_active) .. " (id: " .. tostring(client_id) .. ")",
  156. indent .. "* Supports formatting: " .. tostring(document_formatting),
  157. indent .. "* Capabilities list: " .. table.concat(vim.list_slice(client_enabled_caps, 1, num_caps / 2), ", "),
  158. indent .. indent .. indent .. table.concat(vim.list_slice(client_enabled_caps, ((num_caps / 2) + 1)), ", "),
  159. "",
  160. }
  161. vim.list_extend(buf_lines, lsp_info)
  162. local null_ls_info = {
  163. indent .. "Formatters and linters",
  164. indent .. "* Configured providers: " .. table.concat(null_ls_providers, "  , ") .. "  ",
  165. }
  166. vim.list_extend(buf_lines, null_ls_info)
  167. local missing_formatters_status
  168. if vim.tbl_count(missing_formatters) > 0 then
  169. missing_formatters_status = {
  170. indent .. "* Missing formatters: " .. table.concat(missing_formatters, "  , ") .. "  ",
  171. }
  172. vim.list_extend(buf_lines, missing_formatters_status)
  173. end
  174. local missing_linters_status
  175. if vim.tbl_count(missing_linters) > 0 then
  176. missing_linters_status = {
  177. indent .. "* Missing linters: " .. table.concat(missing_linters, "  , ") .. "  ",
  178. }
  179. vim.list_extend(buf_lines, missing_linters_status)
  180. end
  181. vim.list_extend(buf_lines, { "" })
  182. vim.list_extend(buf_lines, get_formatter_suggestion_msg(ft))
  183. vim.list_extend(buf_lines, get_linter_suggestion_msg(ft))
  184. local function set_syntax_hl()
  185. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  186. vim.cmd [[highlight link LvimInfoHeader Type]]
  187. vim.cmd [[let m=matchadd("DashboardHeader", "Language Server Protocol (LSP) info")]]
  188. vim.cmd [[let m=matchadd("DashboardHeader", "Formatters and linters")]]
  189. vim.cmd('let m=matchadd("LvimInfoIdentifier", " ' .. ft .. '$")')
  190. vim.cmd 'let m=matchadd("string", "true")'
  191. vim.cmd 'let m=matchadd("error", "false")'
  192. tbl_set_highlight(null_ls_providers, "LvimInfoIdentifier")
  193. tbl_set_highlight(missing_formatters, "LvimInfoIdentifier")
  194. tbl_set_highlight(missing_linters, "LvimInfoIdentifier")
  195. -- tbl_set_highlight(u.get_supported_formatters_by_filetype(ft), "LvimInfoIdentifier")
  196. -- tbl_set_highlight(u.get_supported_linters_by_filetype(ft), "LvimInfoIdentifier")
  197. vim.cmd('let m=matchadd("LvimInfoIdentifier", "' .. client_name .. '")')
  198. end
  199. return M.create_simple_popup(buf_lines, set_syntax_hl)
  200. end
  201. return M