feat(entry_maker): add 'filename_first' option for path_display (#3010)
* Initial commit * Fixes issue with find_files where devicons where disabled * Fixes issue with vimgrep where devicons where disabled * Fixes trailing space for path with only a file name * Adds test for reverse path_display * Refactors reverse to filename_first * Adds tests * Fixes highlighting * Fixes linting issues * Uses trim function * Fixes issue with highlighting * Moves local function to utils * Removes redundant code * Adds highlighting for gen_from_buffer * fix formatting --------- Co-authored-by: alycklama <> Co-authored-by: James Trew <j.trew10@gmail.com>
This commit is contained in:
@@ -270,23 +270,24 @@ end
|
||||
--- not be addressed by us
|
||||
---@param opts table: The opts the users passed into the picker. Might contains a path_display key
|
||||
---@param path string|nil: The path that should be formatted
|
||||
---@return string: The transformed path ready to be displayed
|
||||
---@return string, table: The transformed path ready to be displayed with the styling (or nil)
|
||||
utils.transform_path = function(opts, path)
|
||||
if path == nil then
|
||||
return ""
|
||||
return "", {}
|
||||
end
|
||||
if utils.is_uri(path) then
|
||||
return path
|
||||
return path, {}
|
||||
end
|
||||
|
||||
local path_display = vim.F.if_nil(opts.path_display, require("telescope.config").values.path_display)
|
||||
|
||||
local transformed_path = path
|
||||
local path_style = {}
|
||||
|
||||
if type(path_display) == "function" then
|
||||
return path_display(opts, transformed_path)
|
||||
elseif utils.is_path_hidden(nil, path_display) then
|
||||
return ""
|
||||
return "", path_style
|
||||
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)
|
||||
@@ -306,6 +307,37 @@ utils.transform_path = function(opts, path)
|
||||
transformed_path = Path:new(transformed_path):make_relative(cwd)
|
||||
end
|
||||
|
||||
if vim.tbl_contains(path_display, "filename_first") or path_display["filename_first"] ~= nil then
|
||||
local reverse_directories = false
|
||||
|
||||
if type(path_display["filename_first"]) == "table" then
|
||||
local filename_first_opts = path_display["filename_first"]
|
||||
|
||||
if filename_first_opts.reverse_directories == nil or filename_first_opts.reverse_directories == false then
|
||||
reverse_directories = false
|
||||
else
|
||||
reverse_directories = filename_first_opts.reverse_directories
|
||||
end
|
||||
end
|
||||
|
||||
local dirs = vim.split(transformed_path, utils.get_separator())
|
||||
local filename
|
||||
|
||||
if reverse_directories then
|
||||
dirs = utils.reverse_table(dirs)
|
||||
filename = table.remove(dirs, 1)
|
||||
else
|
||||
filename = table.remove(dirs, #dirs)
|
||||
end
|
||||
|
||||
local tail = table.concat(dirs, utils.get_separator())
|
||||
|
||||
-- Prevents a toplevel filename to have a trailing whitespace
|
||||
transformed_path = vim.trim(filename .. " " .. tail)
|
||||
|
||||
path_style = { { { #filename, #transformed_path }, "TelescopeResultsComment" } }
|
||||
end
|
||||
|
||||
if vim.tbl_contains(path_display, "shorten") or path_display["shorten"] ~= nil then
|
||||
if type(path_display["shorten"]) == "table" then
|
||||
local shorten = path_display["shorten"]
|
||||
@@ -315,6 +347,7 @@ utils.transform_path = function(opts, path)
|
||||
transformed_path = Path:new(transformed_path):shorten(length)
|
||||
end
|
||||
end
|
||||
|
||||
if vim.tbl_contains(path_display, "truncate") or path_display.truncate then
|
||||
if opts.__length == nil then
|
||||
opts.__length = calc_result_length(path_display.truncate)
|
||||
@@ -326,10 +359,10 @@ utils.transform_path = function(opts, path)
|
||||
end
|
||||
end
|
||||
|
||||
return transformed_path
|
||||
return transformed_path, path_style
|
||||
else
|
||||
log.warn("`path_display` must be either a function or a table.", "See `:help telescope.defaults.path_display.")
|
||||
return transformed_path
|
||||
return transformed_path, path_style
|
||||
end
|
||||
end
|
||||
|
||||
@@ -661,4 +694,25 @@ utils.__separate_file_path_location = function(path)
|
||||
return path, nil, nil
|
||||
end
|
||||
|
||||
utils.merge_styles = function(style1, style2, offset)
|
||||
local function addOffset(i, obj)
|
||||
return { obj[1] + i, obj[2] + i }
|
||||
end
|
||||
|
||||
for _, item in ipairs(style2) do
|
||||
item[1] = addOffset(offset, item[1])
|
||||
table.insert(style1, item)
|
||||
end
|
||||
|
||||
return style1
|
||||
end
|
||||
|
||||
utils.reverse_table = function(input_table)
|
||||
local temp_table = {}
|
||||
for index = 0, #input_table do
|
||||
temp_table[#input_table - index] = input_table[index + 1] -- Reverses the order
|
||||
end
|
||||
return temp_table
|
||||
end
|
||||
|
||||
return utils
|
||||
|
||||
Reference in New Issue
Block a user