From 1d17cc4abc493d20b4aba36569e8805c2382ea7b Mon Sep 17 00:00:00 2001 From: qualious Date: Sun, 12 Sep 2021 22:14:00 +0300 Subject: [PATCH] feat: smart path (#914) path_display = { "smart" } Co-authored-by: Deniz Co-authored-by: Simon Hauser --- doc/telescope.txt | 3 +++ lua/telescope/config.lua | 2 ++ lua/telescope/utils.lua | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/doc/telescope.txt b/doc/telescope.txt index da2415b..cdcfe56 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -201,6 +201,8 @@ telescope.setup({opts}) *telescope.setup()* - "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 @@ -917,6 +919,7 @@ builtin.lsp_type_definitions({opts}) *builtin.lsp_type_definitions()* {jump_type} (string) how to goto definition if there is only one, values: "tab", "split", "vsplit", "never" + builtin.lsp_implementations({opts}) *builtin.lsp_implementations()* Goto the implementation of the word under the cursor if there's only one, otherwise show all options in Telescope 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