From 42f091a8d377b647c7e32131b16bd6de43d7b417 Mon Sep 17 00:00:00 2001 From: Mauricio Trajano Date: Mon, 23 Dec 2024 16:53:08 -0500 Subject: [PATCH] feat(dirman): dynamically set default workspace --- lua/neorg/modules/core/dirman/module.lua | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index c43fd33b5..16fa93556 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -92,7 +92,7 @@ module.load = function() if module.config.public.open_last_workspace and vim.fn.argc(-1) == 0 then if module.config.public.open_last_workspace == "default" then - if not module.config.public.default_workspace then + if not module.public.get_default_workspace() then log.warn( 'Configuration error in `core.dirman`: the `open_last_workspace` option is set to "default", but no default workspace is provided in the `default_workspace` configuration variable. Defaulting to opening the last known workspace.' ) @@ -100,12 +100,12 @@ module.load = function() return end - module.public.open_workspace(module.config.public.default_workspace) + module.public.open_workspace(module.public.get_default_workspace()) else module.public.set_last_workspace() end - elseif module.config.public.default_workspace then - module.public.set_workspace(module.config.public.default_workspace) + elseif module.public.get_default_workspace() then + module.public.set_workspace(module.public.get_default_workspace()) end end @@ -123,6 +123,7 @@ module.config.public = { -- The index file is the "entry point" for all of your notes. index = "index.norg", -- The default workspace to set whenever Neovim starts. + -- If a function, will be called with the current workspace and should resolve to a valid workspace name default_workspace = nil, -- Whether to open the last workspace's index file when `nvim` is executed -- without arguments. @@ -158,6 +159,15 @@ module.public = { get_current_workspace = function() return module.private.current_workspace end, + --- The default workspace, may be set dynamically based on cwd + ---@return string? # Should evaluate to a valid workspace name + get_default_workspace = function() + if type(module.config.public.default_workspace) == "function" then + return module.config.public.default_workspace(Path.cwd()) + end + + return module.config.public.default_workspace + end, --- Sets the workspace to the one specified (if it exists) and broadcasts the workspace_changed event ---@param ws_name string #The name of a valid namespace we want to switch to ---@return boolean #True if the workspace is set correctly, false otherwise @@ -353,7 +363,7 @@ module.public = { local last_workspace = storage.retrieve("last_workspace") last_workspace = type(last_workspace) == "string" and last_workspace - or module.config.public.default_workspace + or module.public.get_default_workspace() or "" local workspace_path = module.public.get_workspace(last_workspace)