-
Notifications
You must be signed in to change notification settings - Fork 270
feat(symlink_target): show relative path if in cwd #1609
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,9 +15,54 @@ local utils = require("neo-tree.utils") | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
local file_nesting = require("neo-tree.sources.common.file-nesting") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local container = require("neo-tree.sources.common.container") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local log = require("neo-tree.log") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local path = require("plenary.path") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local M = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- Workaround for https://github.com/nvim-lua/plenary.nvim/issues/411 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local relative_path = function(original_path, reference_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- Use plenary's make_relative to clean paths | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
original_path = path:new(original_path):make_relative(".") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
reference_path = path:new(reference_path):make_relative(".") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local o_path = path:new(original_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local ref_path = path:new(reference_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+18
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local parents = o_path:parents() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local ref_parents = ref_path:parents() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local path_elements = vim.split(o_path.filename, "/") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Windows uses backslashes, so this might not always work. to take that into account:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
table.insert(parents, 1, original_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
table.insert(ref_parents, 1, reference_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local result = "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for i, ref_parent in ipairs(ref_parents) do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for j, par in ipairs(parents) do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if ref_parent == par then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if i == 1 and j == 1 then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result = result .. table.concat(path_elements, "/", #path_elements - j + 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result = "../" .. result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+36
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a slightly faster algorithm, although I haven't tested it much, feel free to ignore:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local get_relative_target = function(node, state) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local cwd = path:new(state.path):absolute() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local target = path:new(node.link_to):absolute() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local node_dir = path:new(node.path):parent():absolute() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- If target is inside cwd, make it relative | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if target:find(cwd, 1, true) == 1 then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return relative_path(target, node_dir) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return node.link_to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local make_two_char = function(symbol) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if vim.fn.strchars(symbol) == 1 then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return symbol .. " " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -528,8 +573,9 @@ end | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
M.symlink_target = function(config, node, state) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you could move the local methods to just above symlink_target so it's more organized. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if node.is_link then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
local target = get_relative_target(node, state) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text = string.format(" ➛ %s", node.link_to), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text = string.format(" ➛ %s", target), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
highlight = config.highlight or highlights.SYMBOLIC_LINK_TARGET, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The paths are already absolute before you pass them in, so this seems unneeded (alternatively, you could make them absolute here, and then not bother having to make them absolute before passing them in)