Skip to content

Commit

Permalink
cleanup(neorg.lua): remove vim.filetype logic in setup as it is alr…
Browse files Browse the repository at this point in the history
…eady covered by the ftdetect file

FYI: ftdetect directory is going to be deleted once Neovim v0.10 release is stable as I've added support for norg filetype in core
  • Loading branch information
NTBBloodbath committed May 26, 2024
1 parent bd9ee52 commit 9699dbc
Showing 1 changed file with 20 additions and 94 deletions.
114 changes: 20 additions & 94 deletions lua/neorg/init.lua
Original file line number Diff line number Diff line change
@@ -1,84 +1,32 @@
--- @brief [[
--- This file marks the beginning of the entire plugin. It's here that everything fires up and starts pumping.
--- @brief ]]
--[[
-- ROOT NEORG FILE
-- This file is the beginning of the entire plugin. It's here that everything fires up and starts pumping.
--]]

-- Require the most important modules
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
--- This function takes in a user config, parses it, initializes everything and launches neorg if inside a .norg or .org file
---@param cfg table #A table that reflects the structure of config.user_config
function neorg.setup(cfg)
-- If the user supplied no configuration then generate a default one (assume the user wants the defaults)
cfg = cfg or {
load = {
["core.defaults"] = {},
},
}

-- If no `load` table was passed whatsoever then assume the user wants the default ones.
-- If the user explicitly sets `load = {}` in their configs then that means they do not want
-- any modules loaded.
--
-- We check for nil specifically because some users might think `load = false` is a valid thing.
-- With the explicit check `load = false` will issue an error.
if cfg.load == nil then
cfg.load = {
["core.defaults"] = {},
}
end

if not (pcall(require, "lua-utils")) then
vim.notify(
"Warning [neorg]: lua-utils not found. If you're just installing the plugin, ignore this message, when in doubt run `:Lazy build neorg`. If you're not on lazy please rerun the build scripts.",
vim.log.levels.WARN
)

-- Allow neorg to be reloaded once more.
package.loaded.neorg = nil
config.user_config = vim.tbl_deep_extend("force", config.user_config, cfg or {})

user_configuration = cfg
return
end

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

-- Create a new global instance of the neorg logger.
-- Create a new global instance of the neorg logger
log.new(config.user_config.logger or log.get_default_config(), true)

-- TODO(vhyrro): Remove this after Neovim 0.10, where `norg` files will be
-- detected automatically.
vim.filetype.add({
extension = {
norg = "norg",
},
})

local ok, lua_utils = pcall(require, "lua-utils")
assert(ok, "unable to find lua-utils dependency. Perhaps try restarting Neovim?")

neorg.lib = lua_utils

-- If the file we have entered has a `.norg` extension:
-- 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.
-- Then boot up the environment
neorg.org_file_entered(false)
else
-- 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, {})
-- Else listen for a BufReadPost event 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>)
]])

vim.api.nvim_create_autocmd("BufAdd", {
vim.api.nvim_create_autocmd("FileType", {
pattern = "norg",
callback = function()
neorg.org_file_entered(false)
Expand All @@ -87,28 +35,9 @@ 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

package.loaded["lua-utils"] = nil

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

neorg.lib = lua_utils

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".
---@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"
function neorg.org_file_entered(manual, arguments)
-- Extract the module list from the user config
local module_list = config.user_config and config.user_config.load or {}
Expand Down Expand Up @@ -177,9 +106,6 @@ function neorg.org_file_entered(manual, arguments)
referrer = "core",
line_content = "",
broadcast = true,
buffer = vim.api.nvim_get_current_buf(),
window = vim.api.nvim_get_current_win(),
mode = vim.fn.mode(),
})

-- Sometimes external plugins prefer hooking in to an autocommand
Expand All @@ -189,7 +115,7 @@ function neorg.org_file_entered(manual, arguments)
end

--- Returns whether or not Neorg is loaded
--- @return boolean
---@return boolean
function neorg.is_loaded()
return config.started
end
Expand Down

0 comments on commit 9699dbc

Please sign in to comment.