feat: smart path (#914)
path_display = { "smart" }
Co-authored-by: Deniz <deniz@admentum.se>
Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
This commit is contained in:
@@ -261,6 +261,48 @@ utils.path_shorten = function(filename, len)
|
||||
return Path:new(filename):shorten(len)
|
||||
end
|
||||
|
||||
utils.path_smart = (function()
|
||||
local paths = {}
|
||||
return function(filepath)
|
||||
local final = filepath
|
||||
if #paths ~= 0 then
|
||||
local dirs = vim.split(filepath, "/")
|
||||
local max = 1
|
||||
for _, p in pairs(paths) do
|
||||
if #p > 0 and p ~= filepath then
|
||||
local _dirs = vim.split(p, "/")
|
||||
for i = 1, math.min(#dirs, #_dirs) do
|
||||
if (dirs[i] ~= _dirs[i]) and i > max then
|
||||
max = i
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if #dirs ~= 0 then
|
||||
if max == 1 and #dirs >= 2 then
|
||||
max = #dirs - 2
|
||||
end
|
||||
final = ""
|
||||
for k, v in pairs(dirs) do
|
||||
if k >= max - 1 then
|
||||
final = final .. (#final > 0 and "/" or "") .. v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if not paths[filepath] then
|
||||
paths[filepath] = ""
|
||||
table.insert(paths, filepath)
|
||||
end
|
||||
if final and final ~= filepath then
|
||||
return "../" .. final
|
||||
else
|
||||
return filepath
|
||||
end
|
||||
end
|
||||
end)()
|
||||
|
||||
utils.path_tail = (function()
|
||||
local os_sep = utils.get_separator()
|
||||
local match_string = "[^" .. os_sep .. "]*$"
|
||||
@@ -300,6 +342,8 @@ utils.transform_path = function(opts, path)
|
||||
elseif type(path_display) == "table" then
|
||||
if vim.tbl_contains(path_display, "tail") or path_display.tail then
|
||||
transformed_path = utils.path_tail(transformed_path)
|
||||
elseif vim.tbl_contains(path_display, "smart") or path_display.smart then
|
||||
transformed_path = utils.path_smart(transformed_path)
|
||||
else
|
||||
if not vim.tbl_contains(path_display, "absolute") or path_display.absolute == false then
|
||||
local cwd
|
||||
|
||||
Reference in New Issue
Block a user