init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. local utils = {}
  2. -- recursive Print (structure, limit, separator)
  3. local function r_inspect_settings(structure, limit, separator)
  4. limit = limit or 100 -- default item limit
  5. separator = separator or "." -- indent string
  6. if limit < 1 then
  7. print "ERROR: Item limit reached."
  8. return limit - 1
  9. end
  10. if structure == nil then
  11. io.write("-- O", separator:sub(2), " = nil\n")
  12. return limit - 1
  13. end
  14. local ts = type(structure)
  15. if ts == "table" then
  16. for k, v in pairs(structure) do
  17. -- replace non alpha keys wih ["key"]
  18. if tostring(k):match "[^%a_]" then
  19. k = '["' .. tostring(k) .. '"]'
  20. end
  21. limit = r_inspect_settings(v, limit, separator .. "." .. tostring(k))
  22. if limit < 0 then
  23. break
  24. end
  25. end
  26. return limit
  27. end
  28. if ts == "string" then
  29. -- escape sequences
  30. structure = string.format("%q", structure)
  31. end
  32. separator = separator:gsub("%.%[", "%[")
  33. if type(structure) == "function" then
  34. -- don't print functions
  35. io.write("-- lvim", separator:sub(2), " = function ()\n")
  36. else
  37. io.write("lvim", separator:sub(2), " = ", tostring(structure), "\n")
  38. end
  39. return limit - 1
  40. end
  41. function utils.generate_settings()
  42. -- Opens a file in append mode
  43. local file = io.open("lv-settings.lua", "w")
  44. -- sets the default output file as test.lua
  45. io.output(file)
  46. -- write all `lvim` related settings to `lv-settings.lua` file
  47. r_inspect_settings(lvim, 10000, ".")
  48. -- closes the open file
  49. io.close(file)
  50. end
  51. -- autoformat
  52. function utils.toggle_autoformat()
  53. if lvim.format_on_save then
  54. require("core.autocmds").define_augroups {
  55. autoformat = {
  56. {
  57. "BufWritePre",
  58. "*",
  59. ":silent lua vim.lsp.buf.formatting_sync()",
  60. },
  61. },
  62. }
  63. end
  64. if not lvim.format_on_save then
  65. vim.cmd [[
  66. if exists('#autoformat#BufWritePre')
  67. :autocmd! autoformat
  68. endif
  69. ]]
  70. end
  71. end
  72. function utils.reload_lv_config()
  73. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/settings.lua"
  74. vim.cmd("source " .. USER_CONFIG_PATH)
  75. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/plugins.lua"
  76. local plugins = require "plugins"
  77. local plugin_loader = require("plugin-loader").init()
  78. utils.toggle_autoformat()
  79. plugin_loader:load { plugins, lvim.plugins }
  80. vim.cmd ":PackerCompile"
  81. vim.cmd ":PackerInstall"
  82. -- vim.cmd ":PackerClean"
  83. end
  84. function utils.check_lsp_client_active(name)
  85. local clients = vim.lsp.get_active_clients()
  86. for _, client in pairs(clients) do
  87. if client.name == name then
  88. return true
  89. end
  90. end
  91. return false
  92. end
  93. --- Extends a list-like table with the unique values of another list-like table.
  94. ---
  95. --- NOTE: This mutates dst!
  96. ---
  97. --@see |vim.tbl_extend()|
  98. ---
  99. --@param dst list which will be modified and appended to.
  100. --@param src list from which values will be inserted.
  101. --@param start Start index on src. defaults to 1
  102. --@param finish Final index on src. defaults to #src
  103. --@returns dst
  104. function utils.list_extend_unique(dst, src, start, finish)
  105. vim.validate {
  106. dst = { dst, "t" },
  107. src = { src, "t" },
  108. start = { start, "n", true },
  109. finish = { finish, "n", true },
  110. }
  111. for i = start or 1, finish or #src do
  112. if not vim.tbl_contains(dst, src[i]) then
  113. table.insert(dst, src[i])
  114. end
  115. end
  116. return dst
  117. end
  118. function utils.unrequire(m)
  119. package.loaded[m] = nil
  120. _G[m] = nil
  121. end
  122. function utils.gsub_args(args)
  123. if args == nil or type(args) ~= "table" then
  124. return args
  125. end
  126. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  127. for i = 1, #args do
  128. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  129. end
  130. return args
  131. end
  132. function utils.lvim_log(msg)
  133. if lvim.debug then
  134. vim.notify(msg, vim.log.levels.DEBUG)
  135. end
  136. end
  137. return utils
  138. -- TODO: find a new home for these autocommands