init.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. local utils = {}
  2. local uv = vim.loop
  3. -- recursive Print (structure, limit, separator)
  4. local function r_inspect_settings(structure, limit, separator)
  5. limit = limit or 100 -- default item limit
  6. separator = separator or "." -- indent string
  7. if limit < 1 then
  8. print "ERROR: Item limit reached."
  9. return limit - 1
  10. end
  11. if structure == nil then
  12. io.write("-- O", separator:sub(2), " = nil\n")
  13. return limit - 1
  14. end
  15. local ts = type(structure)
  16. if ts == "table" then
  17. for k, v in pairs(structure) do
  18. -- replace non alpha keys with ["key"]
  19. if tostring(k):match "[^%a_]" then
  20. k = '["' .. tostring(k) .. '"]'
  21. end
  22. limit = r_inspect_settings(v, limit, separator .. "." .. tostring(k))
  23. if limit < 0 then
  24. break
  25. end
  26. end
  27. return limit
  28. end
  29. if ts == "string" then
  30. -- escape sequences
  31. structure = string.format("%q", structure)
  32. end
  33. separator = separator:gsub("%.%[", "%[")
  34. if type(structure) == "function" then
  35. -- don't print functions
  36. io.write("-- lvim", separator:sub(2), " = function ()\n")
  37. else
  38. io.write("lvim", separator:sub(2), " = ", tostring(structure), "\n")
  39. end
  40. return limit - 1
  41. end
  42. function utils.generate_settings()
  43. -- Opens a file in append mode
  44. local file = io.open("lv-settings.lua", "w")
  45. -- sets the default output file as test.lua
  46. io.output(file)
  47. -- write all `lvim` related settings to `lv-settings.lua` file
  48. r_inspect_settings(lvim, 10000, ".")
  49. -- closes the open file
  50. io.close(file)
  51. end
  52. function utils.unrequire(m)
  53. package.loaded[m] = nil
  54. _G[m] = nil
  55. end
  56. function utils.gsub_args(args)
  57. if args == nil or type(args) ~= "table" then
  58. return args
  59. end
  60. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  61. for i = 1, #args do
  62. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  63. end
  64. return args
  65. end
  66. --- Returns a table with the default values that are missing.
  67. --- either paramter can be empty.
  68. --@param config (table) table containing entries that take priority over defaults
  69. --@param default_config (table) table contatining default values if found
  70. function utils.apply_defaults(config, default_config)
  71. config = config or {}
  72. default_config = default_config or {}
  73. local new_config = vim.tbl_deep_extend("keep", vim.empty_dict(), config)
  74. new_config = vim.tbl_deep_extend("keep", new_config, default_config)
  75. return new_config
  76. end
  77. --- Checks whether a given path exists and is a file.
  78. --@param path (string) path to check
  79. --@returns (bool)
  80. function utils.is_file(path)
  81. local stat = uv.fs_stat(path)
  82. return stat and stat.type == "file" or false
  83. end
  84. --- Checks whether a given path exists and is a directory
  85. --@param path (string) path to check
  86. --@returns (bool)
  87. function utils.is_directory(path)
  88. local stat = uv.fs_stat(path)
  89. return stat and stat.type == "directory" or false
  90. end
  91. utils.join_paths = _G.join_paths
  92. function utils.write_file(path, txt, flag)
  93. uv.fs_open(path, flag, 438, function(open_err, fd)
  94. assert(not open_err, open_err)
  95. uv.fs_write(fd, txt, -1, function(write_err)
  96. assert(not write_err, write_err)
  97. uv.fs_close(fd, function(close_err)
  98. assert(not close_err, close_err)
  99. end)
  100. end)
  101. end)
  102. end
  103. function utils.debounce(ms, fn)
  104. local timer = vim.loop.new_timer()
  105. return function(...)
  106. local argv = { ... }
  107. timer:start(ms, 0, function()
  108. timer:stop()
  109. vim.schedule_wrap(fn)(unpack(argv))
  110. end)
  111. end
  112. end
  113. function utils.search_file(file, args)
  114. local Job = require "plenary.job"
  115. local stderr = {}
  116. local stdout, ret = Job
  117. :new({
  118. command = "grep",
  119. args = { args, file },
  120. cwd = get_cache_dir(),
  121. on_stderr = function(_, data)
  122. table.insert(stderr, data)
  123. end,
  124. })
  125. :sync()
  126. return stdout, ret, stderr
  127. end
  128. function utils.file_contains(file, query)
  129. local stdout, ret, stderr = utils.search_file(file, query)
  130. if ret == 0 then
  131. return true
  132. end
  133. if not vim.tbl_isempty(stderr) then
  134. error(vim.inspect(stderr))
  135. end
  136. if not vim.tbl_isempty(stdout) then
  137. error(vim.inspect(stdout))
  138. end
  139. return false
  140. end
  141. function utils.log_contains(query)
  142. local logfile = require("lvim.core.log"):get_path()
  143. local stdout, ret, stderr = utils.search_file(logfile, query)
  144. if ret == 0 then
  145. return true
  146. end
  147. if not vim.tbl_isempty(stderr) then
  148. error(vim.inspect(stderr))
  149. end
  150. if not vim.tbl_isempty(stdout) then
  151. error(vim.inspect(stdout))
  152. end
  153. if not vim.tbl_isempty(stderr) then
  154. error(vim.inspect(stderr))
  155. end
  156. return false
  157. end
  158. function utils.generate_plugins_sha(output)
  159. local list = {}
  160. output = output or "commits.lua"
  161. local function git_cmd(args)
  162. local Job = require "plenary.job"
  163. local stderr = {}
  164. local stdout, ret = Job
  165. :new({
  166. command = "git",
  167. args = args,
  168. on_stderr = function(_, data)
  169. table.insert(stderr, data)
  170. end,
  171. })
  172. :sync()
  173. return ret, stdout
  174. end
  175. local core_plugins = require "lvim.plugins"
  176. for _, plugin in pairs(core_plugins) do
  177. local name = plugin[1]:match "/(%S*)"
  178. local url = "https://github.com/" .. plugin[1]
  179. print("checking: " .. name .. ", at: " .. url)
  180. local retval, latest_sha = git_cmd { "ls-remote", url, "origin", "HEAD" }
  181. if retval == 0 then
  182. -- replace dashes, remove postfixes and use lowercase
  183. local normalize_name = (name:gsub("-", "_"):gsub("%.%S+", "")):lower()
  184. list[normalize_name] = latest_sha[1]:gsub("\tHEAD", "")
  185. end
  186. end
  187. utils.write_file(output, "local commit = " .. vim.inspect(list), "w")
  188. end
  189. return utils