From c475091eeba684459dfb9412fa8b782dfc997d26 Mon Sep 17 00:00:00 2001 From: connorgmeean Date: Thu, 8 Sep 2022 12:56:39 +1000 Subject: [PATCH] feat(core): Add `requires_modules` field to modules Allow devs to specify module dependencies and throw an error / safely disable that a module if the dependency is not enabled. I.e. extra_snippets depends on lsp and will print an error if `lsp` is not enabled. --- lua/doom/core/modules.lua | 78 ++++++++++++------- .../modules/features/extra_snippets/init.lua | 1 + lua/doom/modules/features/linter/init.lua | 2 + lua/doom/modules/features/projects/init.lua | 2 + 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/lua/doom/core/modules.lua b/lua/doom/core/modules.lua index 4312ced36..afa447775 100644 --- a/lua/doom/core/modules.lua +++ b/lua/doom/core/modules.lua @@ -113,42 +113,68 @@ local keymaps_service = require("doom.services.keymaps") --- Applies commands, autocommands, packages from enabled modules (`modules.lua`). modules.load_modules = function() local use = require("packer").use + local logger = require("doom.utils.logging") -- Handle the Modules for section_name, _ in pairs(doom.modules) do - for module_name, module in pairs(doom[section_name]) do - -- Import dependencies with packer from module.packages - if module.packages then - for dependency_name, packer_spec in pairs(module.packages) do - -- Set packer_spec to configure function - if module.configs and module.configs[dependency_name] then - packer_spec.config = module.configs[dependency_name] + for module_name, module in pairs(doom.modules[section_name]) do + -- Flag to continue enabling module + local should_enable_module = true + + -- Check module has necessary dependencies + if module.requires_modules then + for _, dependent_module in ipairs(module.requires_modules) do + local dep_section_name, dep_module_name = unpack(vim.split(dependent_module, "%.")) + + if not doom.modules[dep_section_name][dep_module_name] then + should_enable_module = false + logger.error( + ("Doom module \"%s.%s\" depends on a module that is not enabled \"%s.%s\". Please enable the %s module."):format( + section_name, + module_name, + dep_section_name, + dep_module_name, + dep_module_name + ) + ) end + end + end + + if should_enable_module then + -- Import dependencies with packer from module.packages + if module.packages then + for dependency_name, packer_spec in pairs(module.packages) do + -- Set packer_spec to configure function + if module.configs and module.configs[dependency_name] then + packer_spec.config = module.configs[dependency_name] + end - -- Set/unset frozen packer dependencies - packer_spec.commit = doom.freeze_dependencies and packer_spec.commit or nil + -- Set/unset frozen packer dependencies + packer_spec.commit = doom.freeze_dependencies and packer_spec.commit or nil - -- Initialise packer - use(packer_spec) + -- Initialise packer + use(packer_spec) + end end - end - -- Setup package autogroups - if module.autocmds then - local autocmds = type(module.autocmds) == "function" and module.autocmds() - or module.autocmds - utils.make_augroup(module_name, autocmds) - end + -- Setup package autogroups + if module.autocmds then + local autocmds = type(module.autocmds) == "function" and module.autocmds() + or module.autocmds + utils.make_augroup(module_name, autocmds) + end - if module.cmds then - for _, cmd_spec in ipairs(module.cmds) do - utils.make_cmd(cmd_spec[1], cmd_spec[2]) + if module.cmds then + for _, cmd_spec in ipairs(module.cmds) do + utils.make_cmd(cmd_spec[1], cmd_spec[2]) + end end - end - if module.binds then - keymaps_service.applyKeymaps( - type(module.binds) == "function" and module.binds() or module.binds - ) + if module.binds then + keymaps_service.applyKeymaps( + type(module.binds) == "function" and module.binds() or module.binds + ) + end end end end diff --git a/lua/doom/modules/features/extra_snippets/init.lua b/lua/doom/modules/features/extra_snippets/init.lua index 5ba6bac6a..b52751b50 100644 --- a/lua/doom/modules/features/extra_snippets/init.lua +++ b/lua/doom/modules/features/extra_snippets/init.lua @@ -9,6 +9,7 @@ extra_snippets.packages = { }, } +extra_snippets.requires_modules = { "features.lsp" } extra_snippets.configs = {} extra_snippets.configs["friendly-snippets"] = function() require("luasnip.loaders.from_vscode").lazy_load() diff --git a/lua/doom/modules/features/linter/init.lua b/lua/doom/modules/features/linter/init.lua index da6c1e1bf..0a2a7ab0e 100644 --- a/lua/doom/modules/features/linter/init.lua +++ b/lua/doom/modules/features/linter/init.lua @@ -7,6 +7,8 @@ linter.settings = { }, } +linter.requires_modules = { "features.lsp" } + linter.packages = { ["null-ls.nvim"] = { "jose-elias-alvarez/null-ls.nvim", diff --git a/lua/doom/modules/features/projects/init.lua b/lua/doom/modules/features/projects/init.lua index 5b72460df..704ed8423 100644 --- a/lua/doom/modules/features/projects/init.lua +++ b/lua/doom/modules/features/projects/init.lua @@ -43,6 +43,8 @@ projects.packages = { }, } +projects.requires_modules = { "features.telescope" } + projects.configs = {} projects.configs["project.nvim"] = function() require("project_nvim").setup(doom.features.projects.settings)