init.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. require("keymappings").setup()
  91. -- vim.cmd ":PackerClean"
  92. end
  93. function utils.check_lsp_client_active(name)
  94. local clients = vim.lsp.get_active_clients()
  95. for _, client in pairs(clients) do
  96. if client.name == name then
  97. return true
  98. end
  99. end
  100. return false
  101. end
  102. function utils.get_active_client_by_ft(filetype)
  103. local clients = vim.lsp.get_active_clients()
  104. for _, client in pairs(clients) do
  105. if client.name == lvim.lang[filetype].lsp.provider then
  106. return client
  107. end
  108. end
  109. return nil
  110. end
  111. --- Extends a list-like table with the unique values of another list-like table.
  112. ---
  113. --- NOTE: This mutates dst!
  114. ---
  115. --@see |vim.tbl_extend()|
  116. ---
  117. --@param dst list which will be modified and appended to.
  118. --@param src list from which values will be inserted.
  119. --@param start Start index on src. defaults to 1
  120. --@param finish Final index on src. defaults to #src
  121. --@returns dst
  122. function utils.list_extend_unique(dst, src, start, finish)
  123. vim.validate {
  124. dst = { dst, "t" },
  125. src = { src, "t" },
  126. start = { start, "n", true },
  127. finish = { finish, "n", true },
  128. }
  129. for i = start or 1, finish or #src do
  130. if not vim.tbl_contains(dst, src[i]) then
  131. table.insert(dst, src[i])
  132. end
  133. end
  134. return dst
  135. end
  136. function utils.unrequire(m)
  137. package.loaded[m] = nil
  138. _G[m] = nil
  139. end
  140. function utils.gsub_args(args)
  141. if args == nil or type(args) ~= "table" then
  142. return args
  143. end
  144. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  145. for i = 1, #args do
  146. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  147. end
  148. return args
  149. end
  150. function utils.lvim_log(msg)
  151. if lvim.debug then
  152. vim.notify(msg, vim.log.levels.DEBUG)
  153. end
  154. end
  155. return utils
  156. -- TODO: find a new home for these autocommands