2021-07-26 19:14:23 +02:00
|
|
|
local utils = {}
|
2021-08-09 19:02:37 +02:00
|
|
|
local uv = vim.loop
|
2021-03-17 05:17:41 +01:00
|
|
|
|
2021-07-18 19:57:05 +02:00
|
|
|
-- recursive Print (structure, limit, separator)
|
|
|
|
local function r_inspect_settings(structure, limit, separator)
|
|
|
|
limit = limit or 100 -- default item limit
|
|
|
|
separator = separator or "." -- indent string
|
|
|
|
if limit < 1 then
|
|
|
|
print "ERROR: Item limit reached."
|
|
|
|
return limit - 1
|
|
|
|
end
|
|
|
|
if structure == nil then
|
|
|
|
io.write("-- O", separator:sub(2), " = nil\n")
|
|
|
|
return limit - 1
|
|
|
|
end
|
|
|
|
local ts = type(structure)
|
|
|
|
|
|
|
|
if ts == "table" then
|
|
|
|
for k, v in pairs(structure) do
|
2021-08-06 13:20:25 +02:00
|
|
|
-- replace non alpha keys with ["key"]
|
2021-07-18 19:57:05 +02:00
|
|
|
if tostring(k):match "[^%a_]" then
|
|
|
|
k = '["' .. tostring(k) .. '"]'
|
|
|
|
end
|
|
|
|
limit = r_inspect_settings(v, limit, separator .. "." .. tostring(k))
|
|
|
|
if limit < 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return limit
|
|
|
|
end
|
|
|
|
|
|
|
|
if ts == "string" then
|
|
|
|
-- escape sequences
|
|
|
|
structure = string.format("%q", structure)
|
|
|
|
end
|
|
|
|
separator = separator:gsub("%.%[", "%[")
|
|
|
|
if type(structure) == "function" then
|
|
|
|
-- don't print functions
|
2021-07-25 07:46:09 +02:00
|
|
|
io.write("-- lvim", separator:sub(2), " = function ()\n")
|
2021-07-18 19:57:05 +02:00
|
|
|
else
|
2021-07-25 07:46:09 +02:00
|
|
|
io.write("lvim", separator:sub(2), " = ", tostring(structure), "\n")
|
2021-07-18 19:57:05 +02:00
|
|
|
end
|
|
|
|
return limit - 1
|
|
|
|
end
|
|
|
|
|
2021-07-26 19:14:23 +02:00
|
|
|
function utils.generate_settings()
|
2021-07-18 19:57:05 +02:00
|
|
|
-- Opens a file in append mode
|
|
|
|
local file = io.open("lv-settings.lua", "w")
|
|
|
|
|
|
|
|
-- sets the default output file as test.lua
|
|
|
|
io.output(file)
|
|
|
|
|
2021-07-25 07:45:08 +02:00
|
|
|
-- write all `lvim` related settings to `lv-settings.lua` file
|
|
|
|
r_inspect_settings(lvim, 10000, ".")
|
2021-07-18 19:57:05 +02:00
|
|
|
|
|
|
|
-- closes the open file
|
|
|
|
io.close(file)
|
|
|
|
end
|
|
|
|
|
2021-07-26 19:14:23 +02:00
|
|
|
function utils.unrequire(m)
|
2021-07-13 06:48:37 +02:00
|
|
|
package.loaded[m] = nil
|
|
|
|
_G[m] = nil
|
|
|
|
end
|
|
|
|
|
2021-07-26 19:14:23 +02:00
|
|
|
function utils.gsub_args(args)
|
2021-07-18 20:36:08 +02:00
|
|
|
if args == nil or type(args) ~= "table" then
|
|
|
|
return args
|
|
|
|
end
|
|
|
|
local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
|
|
|
|
for i = 1, #args do
|
2021-07-19 18:12:59 +02:00
|
|
|
args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
|
2021-07-18 20:36:08 +02:00
|
|
|
end
|
|
|
|
return args
|
|
|
|
end
|
|
|
|
|
2021-09-16 09:58:32 +02:00
|
|
|
--- Returns a table with the default values that are missing.
|
|
|
|
--- either paramter can be empty.
|
|
|
|
--@param config (table) table containing entries that take priority over defaults
|
|
|
|
--@param default_config (table) table contatining default values if found
|
|
|
|
function utils.apply_defaults(config, default_config)
|
|
|
|
config = config or {}
|
|
|
|
default_config = default_config or {}
|
|
|
|
local new_config = vim.tbl_deep_extend("keep", vim.empty_dict(), config)
|
|
|
|
new_config = vim.tbl_deep_extend("keep", new_config, default_config)
|
|
|
|
return new_config
|
|
|
|
end
|
|
|
|
|
2021-08-09 19:02:37 +02:00
|
|
|
--- Checks whether a given path exists and is a file.
|
2021-10-03 16:13:46 +02:00
|
|
|
--@param path (string) path to check
|
2021-08-09 19:02:37 +02:00
|
|
|
--@returns (bool)
|
2021-10-03 16:13:46 +02:00
|
|
|
function utils.is_file(path)
|
|
|
|
local stat = uv.fs_stat(path)
|
2021-08-09 19:02:37 +02:00
|
|
|
return stat and stat.type == "file" or false
|
2021-07-31 06:06:08 +02:00
|
|
|
end
|
|
|
|
|
2021-10-03 16:13:46 +02:00
|
|
|
--- Checks whether a given path exists and is a directory
|
|
|
|
--@param path (string) path to check
|
|
|
|
--@returns (bool)
|
|
|
|
function utils.is_directory(path)
|
|
|
|
local stat = uv.fs_stat(path)
|
|
|
|
return stat and stat.type == "directory" or false
|
|
|
|
end
|
|
|
|
|
2021-10-01 13:27:06 +02:00
|
|
|
utils.join_paths = _G.join_paths
|
2021-09-16 09:58:32 +02:00
|
|
|
|
2021-10-03 16:13:46 +02:00
|
|
|
function utils.write_file(path, txt, flag)
|
|
|
|
uv.fs_open(path, flag, 438, function(open_err, fd)
|
|
|
|
assert(not open_err, open_err)
|
|
|
|
uv.fs_write(fd, txt, -1, function(write_err)
|
|
|
|
assert(not write_err, write_err)
|
|
|
|
uv.fs_close(fd, function(close_err)
|
|
|
|
assert(not close_err, close_err)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.debounce(ms, fn)
|
|
|
|
local timer = vim.loop.new_timer()
|
|
|
|
return function(...)
|
|
|
|
local argv = { ... }
|
|
|
|
timer:start(ms, 0, function()
|
|
|
|
timer:stop()
|
|
|
|
vim.schedule_wrap(fn)(unpack(argv))
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.search_file(file, args)
|
|
|
|
local Job = require "plenary.job"
|
|
|
|
local stderr = {}
|
|
|
|
local stdout, ret = Job
|
|
|
|
:new({
|
|
|
|
command = "grep",
|
|
|
|
args = { args, file },
|
|
|
|
cwd = get_cache_dir(),
|
|
|
|
on_stderr = function(_, data)
|
|
|
|
table.insert(stderr, data)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
:sync()
|
|
|
|
return stdout, ret, stderr
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.file_contains(file, query)
|
|
|
|
local stdout, ret, stderr = utils.search_file(file, query)
|
|
|
|
if ret == 0 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if not vim.tbl_isempty(stderr) then
|
|
|
|
error(vim.inspect(stderr))
|
|
|
|
end
|
|
|
|
if not vim.tbl_isempty(stdout) then
|
|
|
|
error(vim.inspect(stdout))
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.log_contains(query)
|
2021-10-10 21:07:41 +02:00
|
|
|
local logfile = require("lvim.core.log"):get_path()
|
2021-10-03 16:13:46 +02:00
|
|
|
local stdout, ret, stderr = utils.search_file(logfile, query)
|
|
|
|
if ret == 0 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if not vim.tbl_isempty(stderr) then
|
|
|
|
error(vim.inspect(stderr))
|
|
|
|
end
|
|
|
|
if not vim.tbl_isempty(stdout) then
|
|
|
|
error(vim.inspect(stdout))
|
|
|
|
end
|
|
|
|
if not vim.tbl_isempty(stderr) then
|
|
|
|
error(vim.inspect(stderr))
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-11-27 15:22:43 +01:00
|
|
|
function utils.generate_plugins_sha(output)
|
|
|
|
local list = {}
|
|
|
|
output = output or "commits.lua"
|
|
|
|
|
|
|
|
local function git_cmd(args)
|
|
|
|
local Job = require "plenary.job"
|
|
|
|
local stderr = {}
|
|
|
|
local stdout, ret = Job
|
|
|
|
:new({
|
|
|
|
command = "git",
|
|
|
|
args = args,
|
|
|
|
on_stderr = function(_, data)
|
|
|
|
table.insert(stderr, data)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
:sync()
|
|
|
|
return ret, stdout
|
|
|
|
end
|
2021-07-05 20:49:07 +02:00
|
|
|
|
2021-11-29 09:12:13 +01:00
|
|
|
local core_plugins = require "lvim.plugins"
|
|
|
|
for _, plugin in pairs(core_plugins) do
|
|
|
|
local name = plugin[1]:match "/(%S*)"
|
|
|
|
local url = "https://github.com/" .. plugin[1]
|
|
|
|
print("checking: " .. name .. ", at: " .. url)
|
|
|
|
local retval, latest_sha = git_cmd { "ls-remote", url, "origin", "HEAD" }
|
2021-11-27 15:22:43 +01:00
|
|
|
if retval == 0 then
|
|
|
|
-- replace dashes, remove postfixes and use lowercase
|
|
|
|
local normalize_name = (name:gsub("-", "_"):gsub("%.%S+", "")):lower()
|
2021-11-29 09:12:13 +01:00
|
|
|
list[normalize_name] = latest_sha[1]:gsub("\tHEAD", "")
|
2021-11-27 15:22:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
utils.write_file(output, "local commits = " .. vim.inspect(list), "w")
|
|
|
|
end
|
|
|
|
|
|
|
|
return utils
|