Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
feat(core): Add requires_modules field to modules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
connorgmeean committed Sep 8, 2022
1 parent e65525b commit c475091
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
78 changes: 52 additions & 26 deletions lua/doom/core/modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/doom/modules/features/extra_snippets/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions lua/doom/modules/features/linter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ linter.settings = {
},
}

linter.requires_modules = { "features.lsp" }

linter.packages = {
["null-ls.nvim"] = {
"jose-elias-alvarez/null-ls.nvim",
Expand Down
2 changes: 2 additions & 0 deletions lua/doom/modules/features/projects/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c475091

Please sign in to comment.