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

open file with relative path when possible #2805

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,11 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
default_yes = false,
},
},
experimental = {},
experimental = {
open = {
relative_path = false,
},
},
log = {
enable = false,
truncate = false,
Expand Down
20 changes: 19 additions & 1 deletion lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,29 @@ local function open_file_in_tab(filename)
if M.quit_on_open then
view.close()
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
end
vim.cmd("tabe " .. vim.fn.fnameescape(filename))
end

local function drop(filename)
if M.quit_on_open then
view.close()
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
end
vim.cmd("drop " .. vim.fn.fnameescape(filename))
end

local function tab_drop(filename)
if M.quit_on_open then
view.close()
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
end
vim.cmd("tab :drop " .. vim.fn.fnameescape(filename))
end

Expand Down Expand Up @@ -310,7 +319,12 @@ local function open_in_new_window(filename, mode)
end
end

local fname = utils.escape_special_chars(vim.fn.fnameescape(filename))
local fname
if M.relative_path then
fname = utils.escape_special_chars(vim.fn.fnameescape(utils.path_relative(filename, vim.fn.getcwd())))
else
fname = utils.escape_special_chars(vim.fn.fnameescape(filename))
end

local command
if create_new_window then
Expand Down Expand Up @@ -346,6 +360,9 @@ end

local function edit_in_current_buf(filename)
require("nvim-tree.view").abandon_current_window()
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
end
vim.cmd("keepalt keepjumps edit " .. vim.fn.fnameescape(filename))
end

Expand Down Expand Up @@ -402,6 +419,7 @@ end
function M.setup(opts)
M.quit_on_open = opts.actions.open_file.quit_on_open
M.resize_window = opts.actions.open_file.resize_window
M.relative_path = opts.experimental.open.relative_path
if opts.actions.open_file.window_picker.chars then
opts.actions.open_file.window_picker.chars = tostring(opts.actions.open_file.window_picker.chars):upper()
end
Expand Down