Skip to content

Commit 5c79f56

Browse files
committed
Preserve local directory. This is an issue for me with nvim-tree
1 parent 67ed2bd commit 5c79f56

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,19 @@ module.public = {
8686
--- - "split" - open in horizontal split
8787
--- - "tab-drop" - open in new tab or switch to existing tab
8888
--- - "drop" - switch to existing buffer or open in current window
89+
---@param opts.preserve_cwd boolean? If true, preserves the current working directory after opening (default: true for directories)
8990
edit_file = function(path, opts)
9091
opts = opts or {}
9192
local mode = opts.mode or "edit"
9293

94+
-- Check if path is a directory
95+
local is_directory = vim.fn.isdirectory(path) == 1
96+
-- Preserve cwd by default for directories, unless explicitly disabled
97+
local should_preserve_cwd = opts.preserve_cwd ~= nil and opts.preserve_cwd or is_directory
98+
99+
-- Save current working directory if we need to preserve it
100+
local original_cwd = should_preserve_cwd and vim.fn.getcwd() or nil
101+
93102
local ok, err
94103
-- Special handling for tab-drop and drop commands
95104
if mode == "tab-drop" or mode == "drop" then
@@ -100,6 +109,11 @@ module.public = {
100109
ok, err = pcall(vim.cmd[mode], path)
101110
end
102111

112+
-- Restore working directory if it was changed
113+
if should_preserve_cwd and original_cwd then
114+
vim.cmd.cd(original_cwd)
115+
end
116+
103117
if not ok then
104118
-- Vim:E325 is the swap file error, in which case, a lengthy message already shows to
105119
-- the user, and we don't have to crash out of this function (which creates a long and

0 commit comments

Comments
 (0)