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:
qualious
2021-09-12 22:14:00 +03:00
committed by GitHub
parent 978366ba46
commit 1d17cc4abc
3 changed files with 49 additions and 0 deletions

View File

@@ -201,6 +201,8 @@ telescope.setup({opts}) *telescope.setup()*
- "hidden" hide file names - "hidden" hide file names
- "tail" only display the file name, and not the path - "tail" only display the file name, and not the path
- "absolute" display absolute paths - "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 - "shorten" only display the first character of each directory in
the path 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, {jump_type} (string) how to goto definition if there is only one,
values: "tab", "split", "vsplit", "never" values: "tab", "split", "vsplit", "never"
builtin.lsp_implementations({opts}) *builtin.lsp_implementations()* builtin.lsp_implementations({opts}) *builtin.lsp_implementations()*
Goto the implementation of the word under the cursor if there's only one, Goto the implementation of the word under the cursor if there's only one,
otherwise show all options in Telescope otherwise show all options in Telescope

View File

@@ -201,6 +201,8 @@ local telescope_defaults = {
- "hidden" hide file names - "hidden" hide file names
- "tail" only display the file name, and not the path - "tail" only display the file name, and not the path
- "absolute" display absolute paths - "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 - "shorten" only display the first character of each directory in
the path the path

View File

@@ -261,6 +261,48 @@ utils.path_shorten = function(filename, len)
return Path:new(filename):shorten(len) return Path:new(filename):shorten(len)
end 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() utils.path_tail = (function()
local os_sep = utils.get_separator() local os_sep = utils.get_separator()
local match_string = "[^" .. os_sep .. "]*$" local match_string = "[^" .. os_sep .. "]*$"
@@ -300,6 +342,8 @@ utils.transform_path = function(opts, path)
elseif type(path_display) == "table" then elseif type(path_display) == "table" then
if vim.tbl_contains(path_display, "tail") or path_display.tail then if vim.tbl_contains(path_display, "tail") or path_display.tail then
transformed_path = utils.path_tail(transformed_path) 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 else
if not vim.tbl_contains(path_display, "absolute") or path_display.absolute == false then if not vim.tbl_contains(path_display, "absolute") or path_display.absolute == false then
local cwd local cwd