Skip to content

Commit cadba46

Browse files
committed
feat(symlink_target): show relative path if in cwd
1 parent 42caaf5 commit cadba46

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

lua/neo-tree/sources/common/components.lua

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,54 @@ local utils = require("neo-tree.utils")
1515
local file_nesting = require("neo-tree.sources.common.file-nesting")
1616
local container = require("neo-tree.sources.common.container")
1717
local log = require("neo-tree.log")
18+
local path = require("plenary.path")
1819

1920
local M = {}
2021

22+
-- Workaround for https://github.com/nvim-lua/plenary.nvim/issues/411
23+
local relative_path = function(original_path, reference_path)
24+
-- Use plenary's make_relative to clean paths
25+
original_path = path:new(original_path):make_relative(".")
26+
reference_path = path:new(reference_path):make_relative(".")
27+
local o_path = path:new(original_path)
28+
local ref_path = path:new(reference_path)
29+
local parents = o_path:parents()
30+
local ref_parents = ref_path:parents()
31+
32+
local path_elements = vim.split(o_path.filename, "/")
33+
table.insert(parents, 1, original_path)
34+
table.insert(ref_parents, 1, reference_path)
35+
36+
local result = ""
37+
for i, ref_parent in ipairs(ref_parents) do
38+
for j, par in ipairs(parents) do
39+
if ref_parent == par then
40+
if i == 1 and j == 1 then
41+
return ""
42+
end
43+
44+
result = result .. table.concat(path_elements, "/", #path_elements - j + 2)
45+
return result
46+
end
47+
end
48+
49+
result = "../" .. result
50+
end
51+
end
52+
53+
local get_relative_target = function(node, state)
54+
local cwd = path:new(state.path):absolute()
55+
local target = path:new(node.link_to):absolute()
56+
local node_dir = path:new(node.path):parent():absolute()
57+
58+
-- If target is inside cwd, make it relative
59+
if target:find(cwd, 1, true) == 1 then
60+
return relative_path(target, node_dir)
61+
end
62+
63+
return node.link_to
64+
end
65+
2166
local make_two_char = function(symbol)
2267
if vim.fn.strchars(symbol) == 1 then
2368
return symbol .. " "
@@ -528,8 +573,9 @@ end
528573

529574
M.symlink_target = function(config, node, state)
530575
if node.is_link then
576+
local target = get_relative_target(node, state)
531577
return {
532-
text = string.format(" ➛ %s", node.link_to),
578+
text = string.format(" ➛ %s", target),
533579
highlight = config.highlight or highlights.SYMBOLIC_LINK_TARGET,
534580
}
535581
else

0 commit comments

Comments
 (0)