init.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. Log:debug "Format on save active"
  66. end
  67. if not lvim.format_on_save then
  68. vim.cmd [[
  69. if exists('#autoformat#BufWritePre')
  70. :autocmd! autoformat
  71. endif
  72. ]]
  73. Log:debug "Format on save off"
  74. end
  75. end
  76. function utils.reload_lv_config()
  77. require("core.lualine").config()
  78. local config = require "config"
  79. config:load()
  80. require("keymappings").setup() -- this should be done before loading the plugins
  81. vim.cmd("source " .. utils.join_paths(get_runtime_dir(), "lvim", "lua", "plugins.lua"))
  82. local plugins = require "plugins"
  83. utils.toggle_autoformat()
  84. local plugin_loader = require "plugin-loader"
  85. plugin_loader:cache_reset()
  86. plugin_loader:load { plugins, lvim.plugins }
  87. vim.cmd ":PackerInstall"
  88. vim.cmd ":PackerCompile"
  89. -- vim.cmd ":PackerClean"
  90. require("lsp").setup()
  91. Log:info "Reloaded configuration"
  92. end
  93. function utils.unrequire(m)
  94. package.loaded[m] = nil
  95. _G[m] = nil
  96. end
  97. function utils.gsub_args(args)
  98. if args == nil or type(args) ~= "table" then
  99. return args
  100. end
  101. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  102. for i = 1, #args do
  103. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  104. end
  105. return args
  106. end
  107. --- Returns a table with the default values that are missing.
  108. --- either paramter can be empty.
  109. --@param config (table) table containing entries that take priority over defaults
  110. --@param default_config (table) table contatining default values if found
  111. function utils.apply_defaults(config, default_config)
  112. config = config or {}
  113. default_config = default_config or {}
  114. local new_config = vim.tbl_deep_extend("keep", vim.empty_dict(), config)
  115. new_config = vim.tbl_deep_extend("keep", new_config, default_config)
  116. return new_config
  117. end
  118. --- Checks whether a given path exists and is a file.
  119. --@param path (string) path to check
  120. --@returns (bool)
  121. function utils.is_file(path)
  122. local stat = uv.fs_stat(path)
  123. return stat and stat.type == "file" or false
  124. end
  125. --- Checks whether a given path exists and is a directory
  126. --@param path (string) path to check
  127. --@returns (bool)
  128. function utils.is_directory(path)
  129. local stat = uv.fs_stat(path)
  130. return stat and stat.type == "directory" or false
  131. end
  132. function utils.write_file(path, txt, flag)
  133. uv.fs_open(path, flag, 438, function(open_err, fd)
  134. assert(not open_err, open_err)
  135. uv.fs_write(fd, txt, -1, function(write_err)
  136. assert(not write_err, write_err)
  137. uv.fs_close(fd, function(close_err)
  138. assert(not close_err, close_err)
  139. end)
  140. end)
  141. end)
  142. end
  143. utils.join_paths = _G.join_paths
  144. function utils.write_file(path, txt, flag)
  145. uv.fs_open(path, flag, 438, function(open_err, fd)
  146. assert(not open_err, open_err)
  147. uv.fs_write(fd, txt, -1, function(write_err)
  148. assert(not write_err, write_err)
  149. uv.fs_close(fd, function(close_err)
  150. assert(not close_err, close_err)
  151. end)
  152. end)
  153. end)
  154. end
  155. function utils.debounce(ms, fn)
  156. local timer = vim.loop.new_timer()
  157. return function(...)
  158. local argv = { ... }
  159. timer:start(ms, 0, function()
  160. timer:stop()
  161. vim.schedule_wrap(fn)(unpack(argv))
  162. end)
  163. end
  164. end
  165. function utils.search_file(file, args)
  166. local Job = require "plenary.job"
  167. local stderr = {}
  168. local stdout, ret = Job
  169. :new({
  170. command = "grep",
  171. args = { args, file },
  172. cwd = get_cache_dir(),
  173. on_stderr = function(_, data)
  174. table.insert(stderr, data)
  175. end,
  176. })
  177. :sync()
  178. return stdout, ret, stderr
  179. end
  180. function utils.file_contains(file, query)
  181. local stdout, ret, stderr = utils.search_file(file, query)
  182. if ret == 0 then
  183. return true
  184. end
  185. if not vim.tbl_isempty(stderr) then
  186. error(vim.inspect(stderr))
  187. end
  188. if not vim.tbl_isempty(stdout) then
  189. error(vim.inspect(stdout))
  190. end
  191. return false
  192. end
  193. function utils.log_contains(query)
  194. local logfile = require("core.log"):get_path()
  195. local stdout, ret, stderr = utils.search_file(logfile, query)
  196. if ret == 0 then
  197. return true
  198. end
  199. if not vim.tbl_isempty(stderr) then
  200. error(vim.inspect(stderr))
  201. end
  202. if not vim.tbl_isempty(stdout) then
  203. error(vim.inspect(stdout))
  204. end
  205. if not vim.tbl_isempty(stderr) then
  206. error(vim.inspect(stderr))
  207. end
  208. return false
  209. end
  210. return utils
  211. -- TODO: find a new home for these autocommands