diff options
Diffstat (limited to 'mac/.config/LunarVim/lua/lvim/utils')
| -rw-r--r-- | mac/.config/LunarVim/lua/lvim/utils/git.lua | 152 | ||||
| -rw-r--r-- | mac/.config/LunarVim/lua/lvim/utils/hooks.lua | 76 | ||||
| -rw-r--r-- | mac/.config/LunarVim/lua/lvim/utils/modules.lua | 122 | ||||
| -rw-r--r-- | mac/.config/LunarVim/lua/lvim/utils/table.lua | 24 |
4 files changed, 374 insertions, 0 deletions
diff --git a/mac/.config/LunarVim/lua/lvim/utils/git.lua b/mac/.config/LunarVim/lua/lvim/utils/git.lua new file mode 100644 index 0000000..d5f1d92 --- /dev/null +++ b/mac/.config/LunarVim/lua/lvim/utils/git.lua @@ -0,0 +1,152 @@ +local M = {} + +local Log = require "lvim.core.log" +local fmt = string.format +local if_nil = vim.F.if_nil + +local function git_cmd(opts) + local plenary_loaded, Job = pcall(require, "plenary.job") + if not plenary_loaded then + return 1, { "" } + end + + opts = opts or {} + opts.cwd = opts.cwd or get_lvim_base_dir() + + local stderr = {} + local stdout, ret = Job:new({ + command = "git", + args = opts.args, + cwd = opts.cwd, + on_stderr = function(_, data) + table.insert(stderr, data) + end, + }):sync(10000) + + if not vim.tbl_isempty(stderr) then + Log:debug(stderr) + end + + if not vim.tbl_isempty(stdout) then + Log:debug(stdout) + end + + return ret, stdout, stderr +end + +local function safe_deep_fetch() + local ret, result, error = git_cmd { args = { "rev-parse", "--is-shallow-repository" } } + if ret ~= 0 then + Log:error(vim.inspect(error)) + return + end + -- git fetch --unshallow will cause an error on a complete clone + local fetch_mode = result[1] == "true" and "--unshallow" or "--all" + ret = git_cmd { args = { "fetch", fetch_mode } } + if ret ~= 0 then + Log:error(fmt "Git fetch %s failed! Please pull the changes manually in %s", fetch_mode, get_lvim_base_dir()) + return + end + if fetch_mode == "--unshallow" then + ret = git_cmd { args = { "remote", "set-branches", "origin", "*" } } + if ret ~= 0 then + Log:error(fmt "Git fetch %s failed! Please pull the changes manually in %s", fetch_mode, get_lvim_base_dir()) + return + end + end + return true +end + +---pulls the latest changes from github +function M.update_base_lvim() + Log:info "Checking for updates" + + if not vim.loop.fs_access(get_lvim_base_dir(), "w") then + Log:warn(fmt("Lunarvim update aborted! cannot write to %s", get_lvim_base_dir())) + return + end + + if not safe_deep_fetch() then + return + end + + local ret + + ret = git_cmd { args = { "diff", "--quiet", "@{upstream}" } } + if ret == 0 then + Log:info "LunarVim is already up-to-date" + return + end + + ret = git_cmd { args = { "merge", "--ff-only", "--progress" } } + if ret ~= 0 then + Log:error("Update failed! Please pull the changes manually in " .. get_lvim_base_dir()) + return + end + + return true +end + +---Switch Lunarvim to the specified development branch +---@param branch string +function M.switch_lvim_branch(branch) + if not safe_deep_fetch() then + return + end + local args = { "switch", branch } + + if branch:match "^[0-9]" then + -- avoids producing an error for tags + vim.list_extend(args, { "--detach" }) + end + + local ret = git_cmd { args = args } + if ret ~= 0 then + Log:error "Unable to switch branches! Check the log for further information" + return + end + return true +end + +---Get the current Lunarvim development branch +---@return string|nil +function M.get_lvim_branch() + local _, results = git_cmd { args = { "rev-parse", "--abbrev-ref", "HEAD" } } + local branch = if_nil(results[1], "") + return branch +end + +---Get currently checked-out tag of Lunarvim +---@return string +function M.get_lvim_tag() + local args = { "describe", "--tags", "--abbrev=0" } + + local _, results = git_cmd { args = args } + local tag = if_nil(results[1], "") + return tag +end + +---Get the description of currently checked-out commit of Lunarvim +---@return string|nil +function M.get_lvim_description() + local _, results = git_cmd { args = { "describe", "--dirty", "--always" } } + + local description = if_nil(results[1], M.get_lvim_branch()) + return description +end + +---Get currently running version of Lunarvim +---@return string +function M.get_lvim_version() + local current_branch = M.get_lvim_branch() + + local lvim_version + if current_branch ~= "HEAD" or "" then + lvim_version = current_branch .. "-" .. M.get_lvim_description() + else + lvim_version = "v" .. M.get_lvim_tag() + end + return lvim_version +end + +return M diff --git a/mac/.config/LunarVim/lua/lvim/utils/hooks.lua b/mac/.config/LunarVim/lua/lvim/utils/hooks.lua new file mode 100644 index 0000000..2bf2ff0 --- /dev/null +++ b/mac/.config/LunarVim/lua/lvim/utils/hooks.lua @@ -0,0 +1,76 @@ +local M = {} + +local Log = require "lvim.core.log" +local in_headless = #vim.api.nvim_list_uis() == 0 +local plugin_loader = require "lvim.plugin-loader" + +function M.run_pre_update() + Log:debug "Starting pre-update hook" +end + +function M.run_pre_reload() + Log:debug "Starting pre-reload hook" +end + +-- TODO: convert to lazy.nvim +-- function M.run_on_packer_complete() +-- -- FIXME(kylo252): nvim-tree.lua/lua/nvim-tree/view.lua:442: Invalid window id +-- vim.g.colors_name = lvim.colorscheme +-- pcall(vim.cmd.colorscheme, lvim.colorscheme) +-- end + +function M.run_post_reload() + Log:debug "Starting post-reload hook" +end + +---Reset any startup cache files used by lazy.nvim +---It also forces regenerating any template ftplugin files +---Tip: Useful for clearing any outdated settings +function M.reset_cache() + local lvim_modules = {} + for module, _ in pairs(package.loaded) do + if module:match "lvim.core" or module:match "lvim.lsp" then + package.loaded[module] = nil + table.insert(lvim_modules, module) + end + end + Log:trace(string.format("Cache invalidated for core modules: { %s }", table.concat(lvim_modules, ", "))) + require("lvim.lsp.templates").generate_templates() +end + +function M.run_post_update() + Log:debug "Starting post-update hook" + + if vim.fn.has "nvim-0.9" ~= 1 then + local compat_tag = "1.2.0" + vim.notify( + "Please upgrade your Neovim base installation. Newer version of Lunarvim requires v0.9+", + vim.log.levels.WARN + ) + vim.wait(1000) + local ret = reload("lvim.utils.git").switch_lvim_branch(compat_tag) + if ret then + vim.notify("Reverted to the last known compatible version: " .. compat_tag, vim.log.levels.WARN) + end + return + end + + M.reset_cache() + + Log:debug "Syncing core plugins" + plugin_loader.reload { reload "lvim.plugins", lvim.plugins } + plugin_loader.sync_core_plugins() + M.reset_cache() -- force cache clear and templates regen once more + + if not in_headless then + vim.schedule(function() + if package.loaded["nvim-treesitter"] then + vim.cmd [[ TSUpdateSync ]] + end + -- TODO: add a changelog + vim.notify("Update complete", vim.log.levels.INFO) + end) + end +end + +return M diff --git a/mac/.config/LunarVim/lua/lvim/utils/modules.lua b/mac/.config/LunarVim/lua/lvim/utils/modules.lua new file mode 100644 index 0000000..45cacfa --- /dev/null +++ b/mac/.config/LunarVim/lua/lvim/utils/modules.lua @@ -0,0 +1,122 @@ +local M = {} + +local Log = require "lvim.core.log" +-- revisit this +-- function prequire(package) +-- local status, lib = pcall(require, package) +-- if status then +-- return lib +-- else +-- vim.notify("Failed to require '" .. package .. "' from " .. debug.getinfo(2).source) +-- return nil +-- end +-- end + +local function _assign(old, new, k) + local otype = type(old[k]) + local ntype = type(new[k]) + -- print("hi") + if (otype == "thread" or otype == "userdata") or (ntype == "thread" or ntype == "userdata") then + vim.notify(string.format("warning: old or new attr %s type be thread or userdata", k)) + end + old[k] = new[k] +end + +local function _replace(old, new, repeat_tbl) + if repeat_tbl[old] then + return + end + repeat_tbl[old] = true + + local dellist = {} + for k, _ in pairs(old) do + if not new[k] then + table.insert(dellist, k) + end + end + for _, v in ipairs(dellist) do + old[v] = nil + end + + for k, _ in pairs(new) do + if not old[k] then + old[k] = new[k] + else + if type(old[k]) ~= type(new[k]) then + Log:debug( + string.format("Reloader: mismatch between old [%s] and new [%s] type for [%s]", type(old[k]), type(new[k]), k) + ) + _assign(old, new, k) + else + if type(old[k]) == "table" then + _replace(old[k], new[k], repeat_tbl) + else + _assign(old, new, k) + end + end + end + end +end + +M.require_clean = function(m) + package.loaded[m] = nil + _G[m] = nil + local _, module = pcall(require, m) + return module +end + +M.require_safe = function(mod) + local status_ok, module = pcall(require, mod) + if not status_ok then + local trace = debug.getinfo(2, "SL") + local shorter_src = trace.short_src + local lineinfo = shorter_src .. ":" .. (trace.currentline or trace.linedefined) + local msg = string.format("%s : skipped loading [%s]", lineinfo, mod) + Log:debug(msg) + end + return module +end + +M.reload = function(mod) + if not package.loaded[mod] then + return M.require_safe(mod) + end + + local old = package.loaded[mod] + package.loaded[mod] = nil + local new = M.require_safe(mod) + + if type(old) == "table" and type(new) == "table" then + local repeat_tbl = {} + _replace(old, new, repeat_tbl) + end + + package.loaded[mod] = old + return old +end + +-- code from <https://github.com/tjdevries/lazy-require.nvim/blob/bb626818ebc175b8c595846925fd96902b1ce02b/lua/lazy-require.lua#L25> +function M.require_on_index(require_path) + return setmetatable({}, { + __index = function(_, key) + return require(require_path)[key] + end, + + __newindex = function(_, key, value) + require(require_path)[key] = value + end, + }) +end + +-- code from <https://github.com/tjdevries/lazy-require.nvim/blob/bb626818ebc175b8c595846925fd96902b1ce02b/lua/lazy-require.lua#L25> +function M.require_on_exported_call(require_path) + return setmetatable({}, { + __index = function(_, k) + return function(...) + return require(require_path)[k](...) + end + end, + }) +end + +return M diff --git a/mac/.config/LunarVim/lua/lvim/utils/table.lua b/mac/.config/LunarVim/lua/lvim/utils/table.lua new file mode 100644 index 0000000..1ac5949 --- /dev/null +++ b/mac/.config/LunarVim/lua/lvim/utils/table.lua @@ -0,0 +1,24 @@ +local Table = {} + +--- Find the first entry for which the predicate returns true. +-- @param t The table +-- @param predicate The function called for each entry of t +-- @return The entry for which the predicate returned True or nil +function Table.find_first(t, predicate) + for _, entry in pairs(t) do + if predicate(entry) then + return entry + end + end + return nil +end + +--- Check if the predicate returns True for at least one entry of the table. +-- @param t The table +-- @param predicate The function called for each entry of t +-- @return True if predicate returned True at least once, false otherwise +function Table.contains(t, predicate) + return Table.find_first(t, predicate) ~= nil +end + +return Table |
