Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dirman): dynamically set default workspace #1623

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lua/neorg/modules/core/dirman/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ 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.'
)
module.public.set_last_workspace()
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

Expand All @@ -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.
Expand Down Expand Up @@ -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()
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
Expand Down Expand Up @@ -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)
Expand Down
Loading