From bfc10e60341578c7a6fa209277c0259002d84847 Mon Sep 17 00:00:00 2001 From: Morten Olsen Date: Tue, 24 Aug 2021 21:45:59 +0200 Subject: [PATCH] feat: Add custom scripts and hook based setup This commit introduces a hook system that allows the user to add custom modules which can use these hooks to invoke function af specific NvChad events to allow for extending og functionality --- lua/core/custom.lua | 22 ++++++++++++++++++++++ lua/core/hooks.lua | 38 ++++++++++++++++++++++++++++++++++++++ lua/core/init.lua | 5 +++++ lua/core/mappings.lua | 3 ++- lua/custom/init.lua | 2 ++ lua/plugins/init.lua | 2 ++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 lua/core/custom.lua create mode 100644 lua/core/hooks.lua create mode 100644 lua/custom/init.lua diff --git a/lua/core/custom.lua b/lua/core/custom.lua new file mode 100644 index 0000000..05a7329 --- /dev/null +++ b/lua/core/custom.lua @@ -0,0 +1,22 @@ +local function isModuleAvailable(name) + if package.loaded[name] then + return true + else + for _, searcher in ipairs(package.searchers or package.loaders) do + local loader = searcher(name) + if type(loader) == 'function' then + package.preload[name] = loader + return true + end + end + return false + end +end + +local loadIfExists = function (module) + if isModuleAvailable(module) then + require(module) + end +end + +loadIfExists('custom') diff --git a/lua/core/hooks.lua b/lua/core/hooks.lua new file mode 100644 index 0000000..3353d71 --- /dev/null +++ b/lua/core/hooks.lua @@ -0,0 +1,38 @@ +local hooks, M = {}, {}; +local allowed_hooks = { + "install_plugins", + "setup_mappings", + "ready", +} + +local function has_value (tab, val) + for _, value in ipairs(tab) do + if value == val then + return true + end + end + + return false +end + +M.add = function(name, fn) + if not(has_value(allowed_hooks, name)) then + error("Custom lua uses unallowed hook " .. name) + end + if hooks[name] == nil then + hooks[name] = {} + end + table.insert(hooks[name], fn); +end + +M.run = function(name, args) + if hooks[name] == nil then + return; + end + + for _, hook in pairs(hooks[name]) do + hook(args) + end +end + +return M; diff --git a/lua/core/init.lua b/lua/core/init.lua index 999576f..819bc51 100644 --- a/lua/core/init.lua +++ b/lua/core/init.lua @@ -1,9 +1,12 @@ local core_modules = { + "core.custom", "core.options", "core.autocmds", "core.mappings", } +local hooks = require('core.hooks'); + for _, module in ipairs(core_modules) do local ok, err = pcall(require, module) if not ok then @@ -13,3 +16,5 @@ end -- set all the non plugin mappings require("core.mappings").misc() + +hooks.run("ready") diff --git a/lua/core/mappings.lua b/lua/core/mappings.lua index 2e86198..3a41a83 100644 --- a/lua/core/mappings.lua +++ b/lua/core/mappings.lua @@ -1,8 +1,8 @@ local utils = require "core.utils" +local hooks = require "core.hooks" local config = utils.load_config() local map = utils.map - local maps = config.mappings local plugin_maps = maps.plugin @@ -116,6 +116,7 @@ M.misc = function() optional_mappings() required_mappings() user_config_mappings() + hooks.run("setup_mappings", map) end -- below are all plugin related mappings diff --git a/lua/custom/init.lua b/lua/custom/init.lua new file mode 100644 index 0000000..56508a9 --- /dev/null +++ b/lua/custom/init.lua @@ -0,0 +1,2 @@ +-- This is where you custom modules and plugins goes. +-- See the wiki for a guide on how to extend NvChad diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index e9e23a1..fbdb606 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -337,4 +337,6 @@ return packer.startup(function() require("core.mappings").vim_fugitive() end, } + + require("core.hooks").run("install_plugins", use) end)