init.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.has_value(tab, val)
  42. for _, value in ipairs(tab) do
  43. if value == val then
  44. return true
  45. end
  46. end
  47. return false
  48. end
  49. function utils.generate_settings()
  50. -- Opens a file in append mode
  51. local file = io.open("lv-settings.lua", "w")
  52. -- sets the default output file as test.lua
  53. io.output(file)
  54. -- write all `lvim` related settings to `lv-settings.lua` file
  55. r_inspect_settings(lvim, 10000, ".")
  56. -- closes the open file
  57. io.close(file)
  58. end
  59. -- autoformat
  60. function utils.toggle_autoformat()
  61. if lvim.format_on_save then
  62. require("core.autocmds").define_augroups {
  63. autoformat = {
  64. {
  65. "BufWritePre",
  66. "*",
  67. ":silent lua vim.lsp.buf.formatting_sync()",
  68. },
  69. },
  70. }
  71. end
  72. if not lvim.format_on_save then
  73. vim.cmd [[
  74. if exists('#autoformat#BufWritePre')
  75. :autocmd! autoformat
  76. endif
  77. ]]
  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. -- vim.cmd ":PackerClean"
  91. end
  92. function utils.check_lsp_client_active(name)
  93. local clients = vim.lsp.get_active_clients()
  94. for _, client in pairs(clients) do
  95. if client.name == name then
  96. return true
  97. end
  98. end
  99. return false
  100. end
  101. --- Extends a list-like table with the unique values of another list-like table.
  102. ---
  103. --- NOTE: This mutates dst!
  104. ---
  105. --@see |vim.tbl_extend()|
  106. ---
  107. --@param dst list which will be modified and appended to.
  108. --@param src list from which values will be inserted.
  109. --@param start Start index on src. defaults to 1
  110. --@param finish Final index on src. defaults to #src
  111. --@returns dst
  112. function utils.list_extend_unique(dst, src, start, finish)
  113. vim.validate {
  114. dst = { dst, "t" },
  115. src = { src, "t" },
  116. start = { start, "n", true },
  117. finish = { finish, "n", true },
  118. }
  119. for i = start or 1, finish or #src do
  120. if not vim.tbl_contains(dst, src[i]) then
  121. table.insert(dst, src[i])
  122. end
  123. end
  124. return dst
  125. end
  126. function utils.unrequire(m)
  127. package.loaded[m] = nil
  128. _G[m] = nil
  129. end
  130. function utils.gsub_args(args)
  131. if args == nil or type(args) ~= "table" then
  132. return args
  133. end
  134. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  135. for i = 1, #args do
  136. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  137. end
  138. return args
  139. end
  140. function utils.lvim_log(msg)
  141. if lvim.debug then
  142. vim.notify(msg, vim.log.levels.DEBUG)
  143. end
  144. end
  145. return utils
  146. -- TODO: find a new home for these autocommands