Skip to content

Commit

Permalink
add missing functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 committed Oct 10, 2023
1 parent 9028187 commit add2a3d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lua/luasnip/util/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ function Path.expand(filepath)
return uv.fs_realpath(expanded)
end

function Path.expand_nonexisting(filepath)
local normalized = filepath
-- replace multiple slashes by one.
:gsub(sep .. sep .. "+", sep)
-- remove trailing slash.
:gsub("/$", "")

normalized
-- replace ~ with home-directory.
:gsub("^~", vim.env.HOME)
-- replace ./ or .\ with config-directory (likely ~/.config/nvim)
:gsub("^[.][/\\]", MYCONFIG_ROOT .. sep)

-- if not yet absolute, prepend path to current directory.
if not normalized:match("^[/\\]") then
normalized = Path.join(vim.fn.getcwd(), normalized)
end

return normalized
end

---Return files and directories in path as a list
---@param root string
---@return string[] files, string[] directories
Expand Down Expand Up @@ -132,6 +153,15 @@ function Path.components(path)
return vim.split(path, sep, {plain=true, trimempty=true})
end

function Path.parent(path)
local last_component = path:match("%" .. sep .."[^" .. sep .. "]+$")
if not last_component then
return nil
end

return path:sub(1, #path - #last_component)
end

-- returns nil if the file does not exist!
Path.normalize = uv.fs_realpath

Expand Down

0 comments on commit add2a3d

Please sign in to comment.