Skip to content

Commit db9e70f

Browse files
authored
open file with relative path when possible (#487)
1 parent fd7f60e commit db9e70f

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lua/nvim-tree.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ local keypress_funcs = {
8080
close = function() M.close() end,
8181
preview = function(node)
8282
if node.entries ~= nil or node.name == '..' then return end
83-
return lib.open_file('preview', node.absolute_path)
83+
return lib.open_file('preview', node.relative_path)
8484
end,
8585
}
8686

@@ -102,11 +102,11 @@ function M.on_keypress(mode)
102102
end
103103

104104
if node.link_to and not node.entries then
105-
lib.open_file(mode, node.link_to)
105+
lib.open_file(mode, node.relative_path)
106106
elseif node.entries ~= nil then
107107
lib.unroll_dir(node)
108108
else
109-
lib.open_file(mode, node.absolute_path)
109+
lib.open_file(mode, node.relative_path)
110110
end
111111
end
112112

lua/nvim-tree/populate.lua

+5
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ end
4141

4242
local function file_new(cwd, name)
4343
local absolute_path = utils.path_join({cwd, name})
44+
local relative_path = utils.path_relative(absolute_path, luv.cwd())
4445
local is_exec = luv.fs_access(absolute_path, 'X')
4546
return {
4647
name = name,
4748
absolute_path = absolute_path,
49+
relative_path = relative_path,
4850
executable = is_exec,
4951
extension = string.match(name, ".?[^.]+%.(.*)") or "",
5052
match_name = path_to_matching_str(name),
@@ -62,6 +64,8 @@ local function link_new(cwd, name)
6264
--- I dont know if this is needed, because in my understanding, there isnt hard links in windows, but just to be sure i changed it.
6365
local absolute_path = utils.path_join({ cwd, name })
6466
local link_to = luv.fs_realpath(absolute_path)
67+
-- if links to a file outside cwd, relative_path equals absolute_path
68+
local relative_path = utils.path_relative(link_to, luv.cwd())
6569
local stat = luv.fs_stat(absolute_path)
6670
local open, entries
6771
if (link_to ~= nil) and luv.fs_stat(link_to).type == 'directory' then
@@ -77,6 +81,7 @@ local function link_new(cwd, name)
7781
return {
7882
name = name,
7983
absolute_path = absolute_path,
84+
relative_path = relative_path,
8085
link_to = link_to,
8186
last_modified = last_modified,
8287
open = open,

0 commit comments

Comments
 (0)