Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(make_entry): ordinal & display for higher depth search #373

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/telescope/_extensions/file_browser/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ end
local function newly_created_root(path, cwd)
local idx
local parents = path:parents()
cwd = fb_utils.trim_right_os_sep(cwd)
cwd = fb_utils.sanitize_path_str(cwd)
for i, p in ipairs(parents) do
if p == cwd then
idx = i
Expand Down
25 changes: 12 additions & 13 deletions lua/telescope/_extensions/file_browser/make_entry.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local fb_utils = require "telescope._extensions.file_browser.utils"
local Path = require "plenary.path"
local action_state = require "telescope.actions.state"
local entry_display = require "telescope.pickers.entry_display"
local fb_git = require "telescope._extensions.file_browser.git"
local fb_utils = require "telescope._extensions.file_browser.utils"
local fb_make_entry_utils = require "telescope._extensions.file_browser.make_entry_utils"
local fs_stat = require "telescope._extensions.file_browser.fs_stat"
local utils = require "telescope.utils"
local log = require "telescope.log"
local entry_display = require "telescope.pickers.entry_display"
local action_state = require "telescope.actions.state"
local os_sep = Path.path.sep
local state = require "telescope.state"
local strings = require "plenary.strings"
local Path = require "plenary.path"
local os_sep = Path.path.sep
local os_sep_len = #os_sep
local utils = require "telescope.utils"

local sep = " "

Expand Down Expand Up @@ -114,9 +114,9 @@ local make_entry = function(opts)
})

-- needed since Path:make_relative does not resolve parent dirs
local parent_dir = Path:new(opts.cwd):parent():absolute()
local parent_dir = fb_utils.sanitize_path_str(Path:new(opts.cwd):parent():absolute())
local mt = {}
mt.cwd = opts.cwd
mt.cwd = fb_utils.sanitize_path_str(opts.cwd)

-- TODO(fdschmidt93): handle VimResized with due to variable width
mt.display = function(entry)
Expand All @@ -125,7 +125,7 @@ local make_entry = function(opts)
local display_array = {}
local icon, icon_hl

local tail = fb_utils.trim_right_os_sep(entry.ordinal)
local tail = fb_utils.sanitize_path_str(entry.ordinal)
local path_display = utils.transform_path(opts, tail)

if entry.is_dir then
Expand Down Expand Up @@ -235,14 +235,13 @@ local make_entry = function(opts)
end

return function(absolute_path)
absolute_path = fb_utils.trim_right_os_sep(absolute_path)
absolute_path = fb_utils.sanitize_path_str(absolute_path)
local path = Path:new(absolute_path)
local is_dir = path:is_dir()

local e = setmetatable({
absolute_path,
ordinal = (absolute_path == opts.cwd and ".")
or (absolute_path == parent_dir and ".." or vim.fs.basename(absolute_path)),
ordinal = fb_make_entry_utils.get_ordinal_path(absolute_path, opts.cwd, parent_dir),
Path = path,
path = absolute_path,
is_dir = is_dir,
Expand Down
30 changes: 30 additions & 0 deletions lua/telescope/_extensions/file_browser/make_entry_utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local Path = require "plenary.path"
local fb_utils = require "telescope._extensions.file_browser.utils"

local os_sep = Path.path.sep
local os_sep_len = #os_sep

local M = {}

--- compute ordinal path
--- accounts for `auto_depth` option
---@param path string
---@param cwd string
---@param parent string
---@return string
M.get_ordinal_path = function(path, cwd, parent)
path = fb_utils.sanitize_path_str(path)
if path == cwd then
return "."
elseif path == parent then
return ".."
end

local cwd_substr = #cwd + 1
print(cwd_substr)
cwd_substr = cwd:sub(-1, -1) ~= os_sep and cwd_substr + os_sep_len or cwd_substr

return path:sub(cwd_substr, -1)
end

return M
23 changes: 18 additions & 5 deletions lua/telescope/_extensions/file_browser/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ local truncate = require("plenary.strings").truncate

local fb_utils = {}

local iswin = vim.loop.os_uname().sysname == "Windows_NT"

fb_utils.is_dir = function(path)
if Path.is_path(path) then
return path:is_dir()
Expand Down Expand Up @@ -181,17 +183,28 @@ fb_utils.notify = function(funname, opts)
end
end

-- trim the right most os separator from a path string
fb_utils.trim_right_os_sep = function(path)
return path:sub(-1, -1) ~= os_sep and path or path:sub(1, -1 - #os_sep)
--- de-dupe os_sep and right trim os_sep nearly everywhere
--- exception for root dir path (`/` or `C:\`)
---@param path string
---@return string
fb_utils.sanitize_path_str = function(path)
path = path:gsub(os_sep .. os_sep, os_sep)
if iswin then
if path:match "^%w:\\$" then
return path
else
return (path:gsub("(.)\\$", "%1"))
end
end
return (path:gsub("(.)/$", "%1"))
end

local _get_selection_index = function(path, dir, results)
local path_dir = Path:new(path):parent():absolute()
path = fb_utils.trim_right_os_sep(path)
path = fb_utils.sanitize_path_str(path)
if dir == path_dir then
for i, path_entry in ipairs(results) do
if fb_utils.trim_right_os_sep(path_entry.value) == path then
if fb_utils.sanitize_path_str(path_entry.value) == path then
return i
end
end
Expand Down
24 changes: 24 additions & 0 deletions lua/tests/make_entry_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local me_utils = require "telescope._extensions.file_browser.make_entry_utils"

describe("get_ordinal_path", function()
it("shows '.' for cwd", function()
assert.are.same(".", me_utils.get_ordinal_path("/home/a/b/c", "/home/a/b/c", "/home/a/b"))
assert.are.same(".", me_utils.get_ordinal_path("/home/a/b/c", "/home/a/b/c", "/home/a/b"))
end)

it("shows '..' for parent path", function()
assert.are.same("..", me_utils.get_ordinal_path("/home/a/b", "/home/a/b/c", "/home/a/b"))
end)

it("shows basename for cwd file", function()
assert.are.same("file.txt", me_utils.get_ordinal_path("/home/a/b/c/file.txt", "/home/a/b/c", "/home/a/b"))
end)

it("handles depths greater than 1", function()
assert.are.same("d/file.txt", me_utils.get_ordinal_path("/home/a/b/c/d/file.txt", "/home/a/b/c", "/home/a/b"))
end)

it("handles duplicate os_sep", function()
assert.are.same("file.txt", me_utils.get_ordinal_path("/home/a/b/c//file.txt", "/home/a/b/c", "/home/a/b"))
end)
end)
Loading