Skip to content

Commit

Permalink
fix: shorten_path handles optional trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Oct 17, 2023
1 parent 098eb7f commit eeb8976
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lua/oil/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,19 @@ end
local home_dir = assert(uv.os_homedir())

---@param path string
---@param relative_to? string Shorten relative to this path (default cwd)
---@return string
M.shorten_path = function(path)
local cwd = vim.fn.getcwd()
if M.is_subpath(cwd, path) then
local relative = path:sub(cwd:len() + 2)
M.shorten_path = function(path, relative_to)
if not relative_to then
relative_to = vim.fn.getcwd()
end
if M.is_subpath(relative_to, path) then
local idx = relative_to:len() + 1
-- Trim the dividing slash if it's not included in relative_to
if not vim.endswith(relative_to, "/") and not vim.endswith(relative_to, "\\") then
idx = idx + 1
end
local relative = path:sub(idx)
if relative == "" then
relative = "."
end
Expand Down

0 comments on commit eeb8976

Please sign in to comment.