info.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. print("Add highlight for word: " .. v)
  119. vim.cmd('let m=matchadd("' .. highlight_group .. '", "' .. v .. '")')
  120. end
  121. end
  122. function M.toggle_popup(ft)
  123. local client = u.get_active_client_by_ft(ft)
  124. local is_client_active = not client.is_stopped()
  125. local client_enabled_caps = require("lsp").get_ls_capabilities(client.id)
  126. local num_caps = vim.tbl_count(client_enabled_caps)
  127. local null_ls_providers = null_ls_handler.get_registered_providers_by_filetype(ft)
  128. local missing_linters = lvim.lang[ft].linters._failed_requests or {}
  129. local missing_formatters = lvim.lang[ft].formatters._failed_requests or {}
  130. local buf_lines = {}
  131. vim.list_extend(buf_lines, M.banner)
  132. local header = {
  133. indent .. "Detected filetype: " .. tostring(ft),
  134. indent .. "Treesitter active: " .. tostring(next(vim.treesitter.highlighter.active) ~= nil),
  135. "",
  136. }
  137. vim.list_extend(buf_lines, header)
  138. local lsp_info = {
  139. indent .. "Language Server Protocol (LSP) info",
  140. indent .. "* Associated server: " .. client.name,
  141. indent .. "* Active: " .. tostring(is_client_active) .. " (id: " .. tostring(client.id) .. ")",
  142. indent .. "* Supports formatting: " .. tostring(client.resolved_capabilities.document_formatting),
  143. indent .. "* Capabilities list: " .. table.concat(vim.list_slice(client_enabled_caps, 1, num_caps / 2), ", "),
  144. indent .. indent .. indent .. table.concat(vim.list_slice(client_enabled_caps, ((num_caps / 2) + 1)), ", "),
  145. "",
  146. }
  147. vim.list_extend(buf_lines, lsp_info)
  148. local null_ls_info = {
  149. indent .. "Formatters and linters",
  150. indent .. "* Configured providers: " .. table.concat(null_ls_providers, "  , ") .. "  ",
  151. }
  152. vim.list_extend(buf_lines, null_ls_info)
  153. local missing_formatters_status
  154. if vim.tbl_count(missing_formatters) > 0 then
  155. missing_formatters_status = {
  156. indent .. "* Missing formatters: " .. table.concat(missing_formatters, "  , ") .. "  ",
  157. }
  158. vim.list_extend(buf_lines, missing_formatters_status)
  159. end
  160. local missing_linters_status
  161. if vim.tbl_count(missing_linters) > 0 then
  162. missing_linters_status = {
  163. indent .. "* Missing linters: " .. table.concat(missing_linters, "  , ") .. "  ",
  164. }
  165. vim.list_extend(buf_lines, missing_linters_status)
  166. end
  167. vim.list_extend(buf_lines, { "" })
  168. vim.list_extend(buf_lines, get_formatter_suggestion_msg(ft))
  169. vim.list_extend(buf_lines, get_linter_suggestion_msg(ft))
  170. local function set_syntax_hl()
  171. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  172. vim.cmd [[highlight link LvimInfoHeader Type]]
  173. vim.cmd [[let m=matchadd("DashboardHeader", "Language Server Protocol (LSP) info")]]
  174. vim.cmd [[let m=matchadd("DashboardHeader", "Formatters and linters")]]
  175. vim.cmd('let m=matchadd("LvimInfoIdentifier", " ' .. ft .. '$")')
  176. vim.cmd 'let m=matchadd("luaString", "true")'
  177. vim.cmd 'let m=matchadd("luaError", "false")'
  178. tbl_set_highlight(null_ls_providers, "LvimInfoIdentifier")
  179. tbl_set_highlight(missing_formatters, "LvimInfoIdentifier")
  180. tbl_set_highlight(missing_linters, "LvimInfoIdentifier")
  181. -- tbl_set_highlight(u.get_supported_formatters_by_filetype(ft), "LvimInfoIdentifier")
  182. -- tbl_set_highlight(u.get_supported_linters_by_filetype(ft), "LvimInfoIdentifier")
  183. vim.cmd('let m=matchadd("LvimInfoIdentifier", "' .. client.name .. '")')
  184. end
  185. return M.create_simple_popup(buf_lines, set_syntax_hl)
  186. end
  187. return M