Skip to content

Commit 29993a7

Browse files
authored
feat(dirman): dynamically set default workspace (#1623)
1 parent 3548a34 commit 29993a7

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lua/neorg/modules/core/dirman/module.lua

+15-5
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,20 @@ module.load = function()
100100

101101
if module.config.public.open_last_workspace and vim.fn.argc(-1) == 0 then
102102
if module.config.public.open_last_workspace == "default" then
103-
if not module.config.public.default_workspace then
103+
if not module.public.get_default_workspace() then
104104
log.warn(
105105
'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.'
106106
)
107107
module.public.set_last_workspace()
108108
return
109109
end
110110

111-
module.public.open_workspace(module.config.public.default_workspace)
111+
module.public.open_workspace(module.public.get_default_workspace())
112112
else
113113
module.public.set_last_workspace()
114114
end
115-
elseif module.config.public.default_workspace then
116-
module.public.set_workspace(module.config.public.default_workspace)
115+
elseif module.public.get_default_workspace() then
116+
module.public.set_workspace(module.public.get_default_workspace())
117117
end
118118
end
119119

@@ -131,6 +131,7 @@ module.config.public = {
131131
-- The index file is the "entry point" for all of your notes.
132132
index = "index.norg",
133133
-- The default workspace to set whenever Neovim starts.
134+
-- If a function, will be called with the current workspace and should resolve to a valid workspace name
134135
default_workspace = nil,
135136
-- Whether to open the last workspace's index file when `nvim` is executed
136137
-- without arguments.
@@ -166,6 +167,15 @@ module.public = {
166167
get_current_workspace = function()
167168
return module.private.current_workspace
168169
end,
170+
--- The default workspace, may be set dynamically based on cwd
171+
---@return string? # Should evaluate to a valid workspace name
172+
get_default_workspace = function()
173+
if type(module.config.public.default_workspace) == "function" then
174+
return module.config.public.default_workspace()
175+
end
176+
177+
return module.config.public.default_workspace
178+
end,
169179
--- Sets the workspace to the one specified (if it exists) and broadcasts the workspace_changed event
170180
---@param ws_name string #The name of a valid namespace we want to switch to
171181
---@return boolean #True if the workspace is set correctly, false otherwise
@@ -361,7 +371,7 @@ module.public = {
361371

362372
local last_workspace = storage.retrieve("last_workspace")
363373
last_workspace = type(last_workspace) == "string" and last_workspace
364-
or module.config.public.default_workspace
374+
or module.public.get_default_workspace()
365375
or ""
366376

367377
local workspace_path = module.public.get_workspace(last_workspace)

0 commit comments

Comments
 (0)