init.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ~/.config/lvim/lv-config.lua"
  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. function utils.is_table(t)
  94. return type(t) == "table"
  95. end
  96. function utils.is_string(t)
  97. return type(t) == "string"
  98. end
  99. --- Extends a list-like table with the unique values of another list-like table.
  100. ---
  101. --- NOTE: This mutates dst!
  102. ---
  103. --@see |vim.tbl_extend()|
  104. ---
  105. --@param dst list which will be modified and appended to.
  106. --@param src list from which values will be inserted.
  107. --@param start Start index on src. defaults to 1
  108. --@param finish Final index on src. defaults to #src
  109. --@returns dst
  110. function utils.list_extend_unique(dst, src, start, finish)
  111. vim.validate {
  112. dst = { dst, "t" },
  113. src = { src, "t" },
  114. start = { start, "n", true },
  115. finish = { finish, "n", true },
  116. }
  117. for i = start or 1, finish or #src do
  118. if not vim.tbl_contains(dst, src[i]) then
  119. table.insert(dst, src[i])
  120. end
  121. end
  122. return dst
  123. end
  124. function utils.unrequire(m)
  125. package.loaded[m] = nil
  126. _G[m] = nil
  127. end
  128. function utils.gsub_args(args)
  129. if args == nil or type(args) ~= "table" then
  130. return args
  131. end
  132. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  133. for i = 1, #args do
  134. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  135. end
  136. return args
  137. end
  138. function utils.lvim_log(msg)
  139. if lvim.debug then
  140. vim.notify(msg, vim.log.levels.DEBUG)
  141. end
  142. end
  143. return utils
  144. -- TODO: find a new home for these autocommands