Skip to content

Commit

Permalink
feat: name/path transforms using custom user functions
Browse files Browse the repository at this point in the history
  • Loading branch information
keevan committed Jul 24, 2023
1 parent 8c6bad7 commit 61fa02d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ use {
-- * win
scope_chdir = 'global',

-- Custom callback to allow the name to be altered before display.
-- Displays the tail component of the path by default.
-- * function(path) return vim.fn.fnamemodify(path, ":t") end
transform_name = nil,

-- Custom callback to allow the path to be altered before display.
-- Displays an absolute path by default.
-- Ex: to shorten /home/me/projects/my-project into ~/projects/my-project, you can use:
-- * function(path) return vim.fn.fnamemodify(path, ":~") end
transform_path = nil,

-- Path where project.nvim will store the project history for use in
-- telescope
datapath = vim.fn.stdpath("data"),
Expand Down
11 changes: 11 additions & 0 deletions lua/project_nvim/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ M.defaults = {
-- * win
scope_chdir = 'global',

-- Custom callback to allow the name to be altered before display.
-- Displays the tail component of the path by default.
-- * function(path) return vim.fn.fnamemodify(path, ":t") end
transform_name = nil,

-- Custom callback to allow the path to be altered before display.
-- Displays an absolute path by default.
-- Ex: to shorten /home/me/projects/my-project into ~/projects/my-project, you can use:
-- * function(path) return vim.fn.fnamemodify(path, ":~") end
transform_path = nil,

-- Path where project.nvim will store the project history for use in
-- telescope
datapath = vim.fn.stdpath("data"),
Expand Down
13 changes: 12 additions & 1 deletion lua/telescope/_extensions/projects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,27 @@ local function create_finder()
})

local function make_display(entry)
return displayer({ entry.name, { entry.value, "Comment" } })
return displayer({ entry.name, { entry.display_path, "Comment" } })
end

return finders.new_table({
results = results,
entry_maker = function(entry)
local name = vim.fn.fnamemodify(entry, ":t")
local display_path = entry

if config.options.transform_name ~= nil then
name = config.options.transform_name(entry)
end

if config.options.transform_path ~= nil then
display_path = config.options.transform_path(entry)
end

return {
display = make_display,
name = name,
display_path = display_path,
value = entry,
ordinal = name .. " " .. entry,
}
Expand Down

0 comments on commit 61fa02d

Please sign in to comment.