Skip to content

Commit

Permalink
refactor(core)!: remove real/imaginary components of modules, impro…
Browse files Browse the repository at this point in the history
…ve startup time, remove `imports` from `module.setup`
  • Loading branch information
vhyrro committed Jun 4, 2023
1 parent 3caca5a commit 593e9b2
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 222 deletions.
4 changes: 2 additions & 2 deletions docgen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ for _, file in ipairs(docgen.aggregate_module_files()) do
goto continue
end

-- Make Neorg load the module, which also evaluates dependencies and imports
-- Make Neorg load the module, which also evaluates dependencies
neorg.modules.load_module(parsed_module.name)

-- Retrieve the module from the `loaded_modules` table.
parsed_module = neorg.modules.loaded_modules[parsed_module.name].real()
parsed_module = neorg.modules.loaded_modules[parsed_module.name]

modules[parsed_module.name] = {
top_comment_data = top_comment_data,
Expand Down
68 changes: 66 additions & 2 deletions lua/neorg/external/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
-- HELPER FUNCTIONS FOR NEORG
-- This file contains some simple helper functions to improve QOL
--]]

local version = vim.version()

neorg.utils = {
--- A version agnostic way to call the neovim treesitter query parser
--- @param language string # Language to use for the query
Expand Down Expand Up @@ -99,8 +102,6 @@ neorg.utils = {
---@param patch number #The patch number (in case you need it)
---@return boolean #Whether Neovim is running at the same or a higher version than the one given
is_minimum_version = function(major, minor, patch)
local version = vim.version()

return major <= version.major and minor <= version.minor and patch <= version.patch
end,
--- Parses a version string like "0.4.2" and provides back a table like { major = <number>, minor = <number>, patch = <number> }
Expand Down Expand Up @@ -579,6 +580,69 @@ neorg.lib = {

return wrapped_value
end,

--- Lazily copy a table-like object.
---@param to_copy table|any #The table to copy. If any other type is provided it will be copied immediately.
---@return table #The copied table
lazy_copy = function(to_copy)
if type(to_copy) ~= "table" then
return vim.deepcopy(to_copy)
end

local proxy = {
original = function()
return to_copy
end,

collect = function(self)
return vim.tbl_deep_extend("force", to_copy, self)
end,
}

return setmetatable(proxy, {
__index = function(_, key)
if not to_copy[key] then
return nil
end

if type(to_copy[key]) == "table" then
local copied = neorg.lib.lazy_copy(to_copy[key])

rawset(proxy, key, copied)

return copied
end

local copied = vim.deepcopy(to_copy[key])
rawset(proxy, key, copied)
return copied
end,

__pairs = function(tbl)
local function stateless_iter(_, key)
local value
key, value = next(to_copy, key)
if value ~= nil then
return key, neorg.lib.lazy_copy(value)
end
end

return stateless_iter, tbl, nil
end,

__ipairs = function(tbl)
local function stateless_iter(_, i)
i = i + 1
local value = to_copy[i]
if value ~= nil then
return i, neorg.lib.lazy_copy(value)
end
end

return stateless_iter, tbl, 0
end,
})
end,
}

return neorg.utils
37 changes: 6 additions & 31 deletions lua/neorg/modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ neorg.modules.loaded_modules = {}
--- Loads and enables a module
-- Loads a specified module. If the module subscribes to any events then they will be activated too.
---@param module table #The actual module to load
---@param parent string? #The name of a potential parent of the module
---@return boolean #Whether the module successfully loaded
function neorg.modules.load_module_from_table(module, parent)
function neorg.modules.load_module_from_table(module)
log.info("Loading module with name", module.name)

-- If our module is already loaded don't try loading it again
Expand All @@ -32,10 +31,6 @@ function neorg.modules.load_module_from_table(module, parent)
return true
end

if parent then
module = module:from(neorg.modules.loaded_modules[parent])
end

-- Invoke the setup function. This function returns whether or not the loading of the module was successful and some metadata.
local loaded_module = module.setup and module.setup()
or {
Expand All @@ -44,7 +39,6 @@ function neorg.modules.load_module_from_table(module, parent)
replace_merge = false,
requires = {},
wants = {},
imports = {},
}

-- We do not expect module.setup() to ever return nil, that's why this check is in place
Expand Down Expand Up @@ -193,24 +187,6 @@ function neorg.modules.load_module_from_table(module, parent)
module.replaced = true
end

if loaded_module.imports and not vim.tbl_isempty(loaded_module.imports) then
log.info("Module", module.name, "has imports. Including them...")

for _, import in ipairs(loaded_module.imports) do
if not neorg.modules.load_module(module.name .. "." .. import, module.name) then
log.error(
"Unable to load",
module.name,
"- the module specified an import (" .. import .. ") but that import could not be found under",
"neorg.modules." .. module.name .. "." .. import
)
return false
end

module = module:from(neorg.modules.loaded_modules[module.name .. "." .. import], "keep")
end
end

log.info("Successfully loaded module", module.name)

-- Keep track of the number of loaded modules
Expand Down Expand Up @@ -247,10 +223,9 @@ end
-- If the module cannot not be found, attempt to load it off of github (unimplemented). This function also applies user-defined configurations and keymaps to the modules themselves.
-- This is the recommended way of loading modules - `load_module_from_table()` should only really be used by neorg itself.
---@param module_name string #A path to a module on disk. A path seperator in neorg is '.', not '/'
---@param parent string? #The name of a potential parent of the module
---@param config table? #A configuration that reflects the structure of `neorg.configuration.user_configuration.load["module.name"].config`
---@return boolean #Whether the module was successfully loaded
function neorg.modules.load_module(module_name, parent, config)
function neorg.modules.load_module(module_name, config)
-- Don't bother loading the module from disk if it's already loaded
if neorg.modules.is_module_loaded(module_name) then
return true
Expand Down Expand Up @@ -302,7 +277,7 @@ function neorg.modules.load_module(module_name, parent, config)
end

-- Pass execution onto load_module_from_table() and let it handle the rest
return neorg.modules.load_module_from_table(module, parent)
return neorg.modules.load_module_from_table(module)
end

--- Has the same principle of operation as load_module_from_table(), except it then sets up the parent module's "required" table, allowing the parent to access the child as if it were a dependency.
Expand All @@ -323,7 +298,7 @@ end
---@param parent_module string #The name of the parent module. This is the module which the dependency will be attached to.
---@param config table #A configuration that reflects the structure of neorg.configuration.user_configuration.load["module.name"].config
function neorg.modules.load_module_as_dependency(module_name, parent_module, config)
if neorg.modules.load_module(module_name, nil, config) and neorg.modules.is_module_loaded(parent_module) then
if neorg.modules.load_module(module_name, config) and neorg.modules.is_module_loaded(parent_module) then
neorg.modules.loaded_modules[parent_module].required[module_name] = neorg.modules.get_module_config(module_name)
end
end
Expand Down Expand Up @@ -380,10 +355,10 @@ end

--- Executes `callback` once `module` is a valid and loaded module, else the callback gets instantly executed.
---@param module_name string #The name of the module to listen for.
---@param callback fun(public_module_table) #The callback to execute.
---@param callback fun(module_public_table: table) #The callback to execute.
function neorg.modules.await(module_name, callback)
if neorg.modules.is_module_loaded(module_name) then
callback(neorg.modules.get_module(module_name))
callback(assert(neorg.modules.get_module(module_name)))
return
end

Expand Down
Loading

0 comments on commit 593e9b2

Please sign in to comment.