info.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. local M = {}
  2. local indent = " "
  3. M.banner = {
  4. " ",
  5. indent
  6. .. "⠀⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀ ⠀⠀⠀ ⠀⠀ ⣺⡿⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀",
  7. indent
  8. .. "⠀⣿⠇⠀⠀⠀⠀⠀⣤⡄⠀⠀⢠⣤⡄⠀.⣠⣤⣤⣤⡀⠀⠀⢀⣤⣤⣤⣤⡄⠀⠀⠀⣤⣄⣤⣤⣤⠀⠀ ⣿⣯ ⣿⡟⠀ ⣤⣤⠀⠀⠀⠀⣠⣤⣤⣤⣄⣤⣤",
  9. indent
  10. .. "⢠⣿⠀⠀⠀⠀⠀⠀⣿⠃⠀⠀⣸⣿⠁⠀⣿⣿⠉⠀⠈⣿⡇⠀⠀⠛⠋⠀⠀⢹⣿⠀⠀⠀⣿⠏⠀⠸⠿⠃⠀⣿⣿⠀⣰⡟⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⣿⡟⢸⣿⡇⢀⣿",
  11. indent
  12. .. "⣸⡇⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⣿⡟⠀⢠⣿⡇⠀⠀⢰⣿⡇⠀⣰⣾⠟⠛⠛⣻⡇⠀⠀⢸⡿⠀⠀⠀⠀⠀⠀⢻⣿⢰⣿⠀⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⢸⣿⠇⢸⣿⠀⢸⡏",
  13. indent
  14. .. "⣿⣧⣤⣤⣤⡄⠀⠘⣿⣤⣤⡤⣿⠇⠀⢸⣿⠁⠀⠀⣼⣿⠀⠀⢿⣿⣤⣤⠔⣿⠃⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⢸⣿⣿⠋⠀⠀⠀⢠⣤⣤⣿⣥⣤⡄⠀⣼⣿⠀⣸⡏⠀⣿⠃",
  15. indent
  16. .. "⠉⠉⠉⠉⠉⠁⠀⠀⠈⠉⠉⠀⠉⠀⠀⠈⠉⠀⠀⠀⠉⠉⠀⠀⠀⠉⠉⠁⠈⠉⠀⠀⠀⠉⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠀⠀⠀⠀⠈⠉⠉⠉⠉⠉⠁⠀⠉⠁⠀⠉⠁⠀⠉⠀",
  17. "",
  18. }
  19. local function str_list(list)
  20. return "[ " .. table.concat(list, ", ") .. " ]"
  21. end
  22. local function get_formatter_suggestion_msg(ft)
  23. local null_formatters = require "lsp.null-ls.formatters"
  24. local supported_formatters = null_formatters.list_available(ft)
  25. return {
  26. indent
  27. .. "───────────────────────────────────────────────────────────────────",
  28. "",
  29. indent .. " HINT ",
  30. "",
  31. indent .. "* List of supported formatters: " .. str_list(supported_formatters),
  32. indent .. "* Configured formatter needs to be installed and executable.",
  33. indent .. "* Enable installed formatter(s) with following config in ~/.config/lvim/config.lua",
  34. "",
  35. indent .. " lvim.lang." .. tostring(ft) .. [[.formatters = { { exe = ']] .. table.concat(
  36. supported_formatters,
  37. "│"
  38. ) .. [[' } }]],
  39. "",
  40. }
  41. end
  42. local function get_linter_suggestion_msg(ft)
  43. local null_linters = require "lsp.null-ls.linters"
  44. local supported_linters = null_linters.list_available(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 lsp_utils = require "lsp.utils"
  123. local client = lsp_utils.get_active_client_by_ft(ft)
  124. local is_client_active = false
  125. local client_enabled_caps = {}
  126. local client_name = ""
  127. local client_id = 0
  128. local document_formatting = false
  129. local num_caps = 0
  130. if client ~= nil then
  131. is_client_active = not client.is_stopped()
  132. client_enabled_caps = require("lsp").get_ls_capabilities(client.id)
  133. num_caps = vim.tbl_count(client_enabled_caps)
  134. client_name = client.name
  135. client_id = client.id
  136. document_formatting = client.resolved_capabilities.document_formatting
  137. end
  138. local buf_lines = {}
  139. vim.list_extend(buf_lines, M.banner)
  140. local header = {
  141. indent .. "Detected filetype: " .. tostring(ft),
  142. indent .. "Treesitter active: " .. tostring(next(vim.treesitter.highlighter.active) ~= nil),
  143. "",
  144. }
  145. vim.list_extend(buf_lines, header)
  146. local lsp_info = {
  147. indent .. "Language Server Protocol (LSP) info",
  148. indent .. "* Associated server: " .. client_name,
  149. indent .. "* Active: " .. tostring(is_client_active) .. " (id: " .. tostring(client_id) .. ")",
  150. indent .. "* Supports formatting: " .. tostring(document_formatting),
  151. indent .. "* Capabilities list: " .. table.concat(vim.list_slice(client_enabled_caps, 1, num_caps / 2), ", "),
  152. indent .. indent .. indent .. table.concat(vim.list_slice(client_enabled_caps, ((num_caps / 2) + 1)), ", "),
  153. "",
  154. }
  155. vim.list_extend(buf_lines, lsp_info)
  156. local null_ls = require "lsp.null-ls"
  157. local registered_providers = null_ls.list_supported_provider_names(ft)
  158. local null_ls_info = {
  159. indent .. "Formatters and linters",
  160. indent .. "* Configured providers: " .. table.concat(registered_providers, "  , ") .. "  ",
  161. }
  162. vim.list_extend(buf_lines, null_ls_info)
  163. local null_formatters = require "lsp.null-ls.formatters"
  164. local missing_formatters = null_formatters.list_unsupported_names(ft)
  165. if vim.tbl_count(missing_formatters) > 0 then
  166. local missing_formatters_status = {
  167. indent .. "* Missing formatters: " .. table.concat(missing_formatters, "  , ") .. "  ",
  168. }
  169. vim.list_extend(buf_lines, missing_formatters_status)
  170. end
  171. local null_linters = require "lsp.null-ls.linters"
  172. local missing_linters = null_linters.list_unsupported_names(ft)
  173. if vim.tbl_count(missing_linters) > 0 then
  174. local missing_linters_status = {
  175. indent .. "* Missing linters: " .. table.concat(missing_linters, "  , ") .. "  ",
  176. }
  177. vim.list_extend(buf_lines, missing_linters_status)
  178. end
  179. vim.list_extend(buf_lines, { "" })
  180. vim.list_extend(buf_lines, get_formatter_suggestion_msg(ft))
  181. vim.list_extend(buf_lines, get_linter_suggestion_msg(ft))
  182. local function set_syntax_hl()
  183. vim.cmd [[highlight LvimInfoIdentifier gui=bold]]
  184. vim.cmd [[highlight link LvimInfoHeader Type]]
  185. vim.cmd [[let m=matchadd("LvimInfoHeader", "Language Server Protocol (LSP) info")]]
  186. vim.cmd [[let m=matchadd("LvimInfoHeader", "Formatters and linters")]]
  187. vim.cmd('let m=matchadd("LvimInfoIdentifier", " ' .. ft .. '$")')
  188. vim.cmd 'let m=matchadd("string", "true")'
  189. vim.cmd 'let m=matchadd("error", "false")'
  190. tbl_set_highlight(registered_providers, "LvimInfoIdentifier")
  191. tbl_set_highlight(missing_formatters, "LvimInfoIdentifier")
  192. tbl_set_highlight(missing_linters, "LvimInfoIdentifier")
  193. -- tbl_set_highlight(require("lsp.null-ls.formatters").list_available(ft), "LvimInfoIdentifier")
  194. -- tbl_set_highlight(require("lsp.null-ls.linters").list_available(ft), "LvimInfoIdentifier")
  195. vim.cmd('let m=matchadd("LvimInfoIdentifier", "' .. client_name .. '")')
  196. end
  197. return M.create_simple_popup(buf_lines, set_syntax_hl)
  198. end
  199. return M