Skip to content

Commit 80d580d

Browse files
epheph
eph
authored and
eph
committed
fix #2127 and #2803
1 parent 3c3100b commit 80d580d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lua/nvim-tree.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,11 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
584584
default_yes = false,
585585
},
586586
},
587-
experimental = {},
587+
experimental = {
588+
open = {
589+
relative_path = false,
590+
},
591+
},
588592
log = {
589593
enable = false,
590594
truncate = false,

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,29 @@ local function open_file_in_tab(filename)
190190
if M.quit_on_open then
191191
view.close()
192192
end
193+
if require('nvim-tree').config.experimental.open.relative_path then
194+
filename = utils.path_relative(filename, vim.fn.getcwd())
195+
end
193196
vim.cmd("tabe " .. vim.fn.fnameescape(filename))
194197
end
195198

196199
local function drop(filename)
197200
if M.quit_on_open then
198201
view.close()
199202
end
203+
if require('nvim-tree').config.experimental.open.relative_path then
204+
filename = utils.path_relative(filename, vim.fn.getcwd())
205+
end
200206
vim.cmd("drop " .. vim.fn.fnameescape(filename))
201207
end
202208

203209
local function tab_drop(filename)
204210
if M.quit_on_open then
205211
view.close()
206212
end
213+
if require('nvim-tree').config.experimental.open.relative_path then
214+
filename = utils.path_relative(filename, vim.fn.getcwd())
215+
end
207216
vim.cmd("tab :drop " .. vim.fn.fnameescape(filename))
208217
end
209218

@@ -310,8 +319,12 @@ local function open_in_new_window(filename, mode)
310319
end
311320
end
312321

313-
-- TODO: nvim-tree 会改变当前目录, 调用结束前恢复
314-
local fname = utils.escape_special_chars(vim.fn.fnameescape(vim.fn.fnamemodify(filename, ':.')))
322+
local fname
323+
if require('nvim-tree').config.experimental.open.relative_path then
324+
fname = utils.escape_special_chars(vim.fn.fnameescape(utils.path_relative(filename, vim.fn.getcwd())))
325+
else
326+
fname = utils.escape_special_chars(vim.fn.fnameescape(filename))
327+
end
315328

316329
local command
317330
if create_new_window then
@@ -347,6 +360,9 @@ end
347360

348361
local function edit_in_current_buf(filename)
349362
require("nvim-tree.view").abandon_current_window()
363+
if require('nvim-tree').config.experimental.open.relative_path then
364+
filename = utils.path_relative(filename, vim.fn.getcwd())
365+
end
350366
vim.cmd("keepalt keepjumps edit " .. vim.fn.fnameescape(filename))
351367
end
352368

0 commit comments

Comments
 (0)