init.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. local utils = {}
  2. local Log = require "core.log"
  3. local uv = vim.loop
  4. -- recursive Print (structure, limit, separator)
  5. local function r_inspect_settings(structure, limit, separator)
  6. limit = limit or 100 -- default item limit
  7. separator = separator or "." -- indent string
  8. if limit < 1 then
  9. print "ERROR: Item limit reached."
  10. return limit - 1
  11. end
  12. if structure == nil then
  13. io.write("-- O", separator:sub(2), " = nil\n")
  14. return limit - 1
  15. end
  16. local ts = type(structure)
  17. if ts == "table" then
  18. for k, v in pairs(structure) do
  19. -- replace non alpha keys with ["key"]
  20. if tostring(k):match "[^%a_]" then
  21. k = '["' .. tostring(k) .. '"]'
  22. end
  23. limit = r_inspect_settings(v, limit, separator .. "." .. tostring(k))
  24. if limit < 0 then
  25. break
  26. end
  27. end
  28. return limit
  29. end
  30. if ts == "string" then
  31. -- escape sequences
  32. structure = string.format("%q", structure)
  33. end
  34. separator = separator:gsub("%.%[", "%[")
  35. if type(structure) == "function" then
  36. -- don't print functions
  37. io.write("-- lvim", separator:sub(2), " = function ()\n")
  38. else
  39. io.write("lvim", separator:sub(2), " = ", tostring(structure), "\n")
  40. end
  41. return limit - 1
  42. end
  43. function utils.generate_settings()
  44. -- Opens a file in append mode
  45. local file = io.open("lv-settings.lua", "w")
  46. -- sets the default output file as test.lua
  47. io.output(file)
  48. -- write all `lvim` related settings to `lv-settings.lua` file
  49. r_inspect_settings(lvim, 10000, ".")
  50. -- closes the open file
  51. io.close(file)
  52. end
  53. -- autoformat
  54. function utils.toggle_autoformat()
  55. if lvim.format_on_save then
  56. require("core.autocmds").define_augroups {
  57. autoformat = {
  58. {
  59. "BufWritePre",
  60. "*",
  61. ":silent lua vim.lsp.buf.formatting_sync()",
  62. },
  63. },
  64. }
  65. if Log:get_default() then
  66. Log:get_default().info "Format on save active"
  67. end
  68. end
  69. if not lvim.format_on_save then
  70. vim.cmd [[
  71. if exists('#autoformat#BufWritePre')
  72. :autocmd! autoformat
  73. endif
  74. ]]
  75. if Log:get_default() then
  76. Log:get_default().info "Format on save off"
  77. end
  78. end
  79. end
  80. function utils.reload_lv_config()
  81. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/settings.lua"
  82. vim.cmd("source " .. USER_CONFIG_PATH)
  83. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/plugins.lua"
  84. local plugins = require "plugins"
  85. local plugin_loader = require("plugin-loader").init()
  86. utils.toggle_autoformat()
  87. plugin_loader:load { plugins, lvim.plugins }
  88. vim.cmd ":PackerCompile"
  89. vim.cmd ":PackerInstall"
  90. require("keymappings").setup()
  91. -- vim.cmd ":PackerClean"
  92. Log:get_default().info "Reloaded configuration"
  93. end
  94. function utils.check_lsp_client_active(name)
  95. local clients = vim.lsp.get_active_clients()
  96. for _, client in pairs(clients) do
  97. if client.name == name then
  98. return true
  99. end
  100. end
  101. return false
  102. end
  103. function utils.get_active_client_by_ft(filetype)
  104. local clients = vim.lsp.get_active_clients()
  105. for _, client in pairs(clients) do
  106. if client.name == lvim.lang[filetype].lsp.provider then
  107. return client
  108. end
  109. end
  110. return nil
  111. end
  112. -- TODO: consider porting this logic to null-ls instead
  113. function utils.get_supported_linters_by_filetype(filetype)
  114. local null_ls = require "null-ls"
  115. local matches = {}
  116. for _, provider in pairs(null_ls.builtins.diagnostics) do
  117. if vim.tbl_contains(provider.filetypes, filetype) then
  118. local provider_name = provider.name
  119. table.insert(matches, provider_name)
  120. end
  121. end
  122. return matches
  123. end
  124. function utils.get_supported_formatters_by_filetype(filetype)
  125. local null_ls = require "null-ls"
  126. local matches = {}
  127. for _, provider in pairs(null_ls.builtins.formatting) do
  128. if provider.filetypes and vim.tbl_contains(provider.filetypes, filetype) then
  129. -- table.insert(matches, { provider.name, ft })
  130. table.insert(matches, provider.name)
  131. end
  132. end
  133. return matches
  134. end
  135. function utils.unrequire(m)
  136. package.loaded[m] = nil
  137. _G[m] = nil
  138. end
  139. function utils.gsub_args(args)
  140. if args == nil or type(args) ~= "table" then
  141. return args
  142. end
  143. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  144. for i = 1, #args do
  145. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  146. end
  147. return args
  148. end
  149. --- Checks whether a given path exists and is a file.
  150. --@param filename (string) path to check
  151. --@returns (bool)
  152. function utils.is_file(filename)
  153. local stat = uv.fs_stat(filename)
  154. return stat and stat.type == "file" or false
  155. end
  156. return utils
  157. -- TODO: find a new home for these autocommands