summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorqualious <denizsydgl@gmail.com>2021-09-12 22:14:00 +0300
committerGitHub <noreply@github.com>2021-09-12 21:14:00 +0200
commit1d17cc4abc493d20b4aba36569e8805c2382ea7b (patch)
treeb3ada5bb78027ef6fe67bbd243aa785f1f541bb9 /lua
parent978366ba46e2306b60ef64bd9e51a6c758fe48f0 (diff)
feat: smart path (#914)
path_display = { "smart" } Co-authored-by: Deniz <deniz@admentum.se> Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/config.lua2
-rw-r--r--lua/telescope/utils.lua44
2 files changed, 46 insertions, 0 deletions
diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua
index d58b94e..c66278f 100644
--- a/lua/telescope/config.lua
+++ b/lua/telescope/config.lua
@@ -201,6 +201,8 @@ local telescope_defaults = {
- "hidden" hide file names
- "tail" only display the file name, and not the path
- "absolute" display absolute paths
+ - "smart" remove as much from the path as possible to only show
+ the difference between the displayed paths
- "shorten" only display the first character of each directory in
the path
diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua
index b5be5ef..cddeea5 100644
--- a/lua/telescope/utils.lua
+++ b/lua/telescope/utils.lua
@@ -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