From 29993a7bb8279ffa0ba473a3f393daa28c645825 Mon Sep 17 00:00:00 2001 From: Mauricio Trajano Date: Sun, 12 Jan 2025 16:04:20 -0300 Subject: [PATCH] feat(dirman): dynamically set default workspace (#1623) --- 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 0a8a542af..033f93194 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -100,7 +100,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.' ) @@ -108,12 +108,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 @@ -131,6 +131,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. @@ -166,6 +167,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() + 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 @@ -361,7 +371,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)