/lua/telescope/health.lua

https://github.com/nvim-lua/telescope.nvim · Lua · 130 lines · 112 code · 14 blank · 4 comment · 19 complexity · 496901b28e3287802038e39c842d14dc MD5 · raw file

  1. local fn = vim.fn
  2. local extension_module = require "telescope._extensions"
  3. local extension_info = require("telescope").extensions
  4. local is_win = vim.api.nvim_call_function("has", { "win32" }) == 1
  5. local health_start = vim.fn["health#report_start"]
  6. local health_ok = vim.fn["health#report_ok"]
  7. local health_warn = vim.fn["health#report_warn"]
  8. local health_error = vim.fn["health#report_error"]
  9. local health_info = vim.fn["health#report_info"]
  10. local optional_dependencies = {
  11. {
  12. finder_name = "live-grep",
  13. package = {
  14. {
  15. name = "rg",
  16. url = "[BurntSushi/ripgrep](https://github.com/BurntSushi/ripgrep)",
  17. optional = false,
  18. },
  19. },
  20. },
  21. {
  22. finder_name = "find-files",
  23. package = {
  24. {
  25. name = "fd",
  26. binaries = { "fd", "fdfind" },
  27. url = "[sharkdp/fd](https://github.com/sharkdp/fd)",
  28. optional = true,
  29. },
  30. },
  31. },
  32. }
  33. local required_plugins = {
  34. { lib = "plenary", optional = false },
  35. {
  36. lib = "nvim-treesitter",
  37. optional = true,
  38. info = "",
  39. },
  40. }
  41. local check_binary_installed = function(package)
  42. local binaries = package.binaries or { package.name }
  43. for _, binary in ipairs(binaries) do
  44. if is_win then
  45. binary = binary .. ".exe"
  46. end
  47. if fn.executable(binary) == 1 then
  48. local handle = io.popen(binary .. " --version")
  49. local binary_version = handle:read "*a"
  50. handle:close()
  51. return true, binary_version
  52. end
  53. end
  54. end
  55. local function lualib_installed(lib_name)
  56. local res, _ = pcall(require, lib_name)
  57. return res
  58. end
  59. local M = {}
  60. M.check = function()
  61. -- Required lua libs
  62. health_start "Checking for required plugins"
  63. for _, plugin in ipairs(required_plugins) do
  64. if lualib_installed(plugin.lib) then
  65. health_ok(plugin.lib .. " installed.")
  66. else
  67. local lib_not_installed = plugin.lib .. " not found."
  68. if plugin.optional then
  69. health_warn(("%s %s"):format(lib_not_installed, plugin.info))
  70. else
  71. health_error(lib_not_installed)
  72. end
  73. end
  74. end
  75. -- external dependencies
  76. -- TODO: only perform checks if user has enabled dependency in their config
  77. health_start "Checking external dependencies"
  78. for _, opt_dep in pairs(optional_dependencies) do
  79. for _, package in ipairs(opt_dep.package) do
  80. local installed, version = check_binary_installed(package)
  81. if not installed then
  82. local err_msg = ("%s: not found."):format(package.name)
  83. if package.optional then
  84. health_warn(("%s %s"):format(err_msg, ("Install %s for extended capabilities"):format(package.url)))
  85. else
  86. health_error(
  87. ("%s %s"):format(
  88. err_msg,
  89. ("`%s` finder will not function without %s installed."):format(opt_dep.finder_name, package.url)
  90. )
  91. )
  92. end
  93. else
  94. local eol = version:find "\n"
  95. health_ok(("%s: found %s"):format(package.name, version:sub(0, eol - 1) or "(unknown version)"))
  96. end
  97. end
  98. end
  99. -- Extensions
  100. health_start "===== Installed extensions ====="
  101. local installed = {}
  102. for extension_name, _ in pairs(extension_info) do
  103. installed[#installed + 1] = extension_name
  104. end
  105. table.sort(installed)
  106. for _, installed_ext in ipairs(installed) do
  107. local extension_healthcheck = extension_module._health[installed_ext]
  108. health_start(string.format("Telescope Extension: `%s`", installed_ext))
  109. if extension_healthcheck then
  110. extension_healthcheck()
  111. else
  112. health_info "No healthcheck provided"
  113. end
  114. end
  115. end
  116. return M