Skip to content

Commit

Permalink
feat: fix build.lua process (maybe once and for all?)
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Mar 24, 2024
1 parent fb45f83 commit eea6263
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 2 additions & 0 deletions build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ vim.schedule(function()
"nvim-nio ~> 1.7",
"lua-utils.nvim == 1.0.2",
})

require("neorg").setup_after_build()
end)
39 changes: 33 additions & 6 deletions lua/neorg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
local neorg = require("neorg.core")
local config, log, modules = neorg.config, neorg.log, neorg.modules

-- HACK(vhyrro): This variable is here to prevent issues with lazy's build.lua script loading.

---@type neorg.configuration.user?
local user_configuration

--- @module "neorg.core.config"

--- Initializes Neorg. Parses the supplied user configuration, initializes all selected modules and adds filetype checking for `.norg`.
--- @param cfg neorg.configuration.user A table that reflects the structure of `config.user_config`.
--- @see config.user_config
--- @see neorg.configuration.user
function neorg.setup(cfg)
if not (pcall(require, "lua-utils")) then
user_configuration = cfg or {}
return
end

config.user_config = vim.tbl_deep_extend("force", config.user_config, cfg or {})

-- Create a new global instance of the neorg logger.
Expand All @@ -25,18 +35,20 @@ function neorg.setup(cfg)
},
})

neorg.lib = assert(require("lua-utils"), "unable to find lua-utils dependency. Perhaps try restarting Neovim?")

-- If the file we have entered has a `.norg` extension:
if vim.fn.expand("%:e") == "norg" or not config.user_config.lazy_loading then
-- Then boot up the environment.
neorg.org_file_entered(false)
else
-- Else listen for a BufReadPost event for `.norg` files and fire up the Neorg environment.
vim.cmd([[
autocmd BufAdd *.norg ++once :lua require('neorg').org_file_entered(false)
command! -nargs=* NeorgStart delcommand NeorgStart | lua require('neorg').org_file_entered(true, <q-args>)
]])
-- Else listen for a BufAdd event for `.norg` files and fire up the Neorg environment.
vim.api.nvim_create_user_command("NeorgStart", function()
vim.cmd.delcommand("NeorgStart")
neorg.org_file_entered(true)
end, {})

vim.api.nvim_create_autocmd("FileType", {
vim.api.nvim_create_autocmd("BufAdd", {
pattern = "norg",
callback = function()
neorg.org_file_entered(false)
Expand All @@ -45,6 +57,21 @@ function neorg.setup(cfg)
end
end

--- Equivalent of `setup()`, but is executed by Lazy.nvim's build.lua script.
--- It attempts to pull the configuration options provided by the user when setup()
--- first ran, and relays those configuration options to the actual Neorg runtime.
function neorg.setup_after_build()
if not user_configuration then
return
end

-- HACK(vhyrro): Please do this elsewhere.
neorg.lib =
assert((pcall(require, "lua-utils")), "unable to find lua-utils dependency. Perhaps try restarting Neovim?")

neorg.setup(user_configuration)
end

--- This function gets called upon entering a .norg file and loads all of the user-defined modules.
--- @param manual boolean If true then the environment was kickstarted manually by the user.
--- @param arguments string? A list of arguments in the format of "key=value other_key=other_value".
Expand Down
1 change: 0 additions & 1 deletion lua/neorg/core/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local neorg = {
callbacks = require("neorg.core.callbacks"),
config = require("neorg.core.config"),
lib = assert(require("lua-utils"), "unable to find lua-utils dependency. Perhaps try restarting Neovim?"),
log = require("neorg.core.log"),
modules = require("neorg.core.modules"),
utils = require("neorg.core.utils"),
Expand Down

0 comments on commit eea6263

Please sign in to comment.