Skip to content

Commit

Permalink
fix(base): fix bugs and optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pysan3 committed Jan 29, 2024
1 parent 0979cd1 commit 8fb9050
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
46 changes: 22 additions & 24 deletions lua/pathlib/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Path.__index = require("pathlib.utils.nuv").generate_index(Path)
---@param ... string | PathlibPath # List of string and Path objects
function Path:_init(...)
local run_resolve = false
local iswin = not utils.tables.is_type_of(self, const.path_module_enum.PathlibPosix)
for i, s in ipairs({ ... }) do
if utils.tables.is_path_module(s) then
---@cast s PathlibPath
Expand All @@ -37,7 +38,7 @@ function Path:_init(...)
self._raw_paths:extend(s._raw_paths)
end
elseif type(s) == "string" then
local path = require("pathlib.utils.paths").normalize(s, const.IS_WINDOWS, { collapse_slash = false })
local path = require("pathlib.utils.paths").normalize(s, iswin, { collapse_slash = false })
if i == 1 then
if path:sub(2, 2) == ":" then -- Windows C: etc
self.__windows_panic = true
Expand All @@ -51,7 +52,7 @@ function Path:_init(...)
path = path_start > 0 and path:sub(path_start) or "/"
end
end
local splits = vim.split(path:gsub("//", "/"), "/", { plain = true, trimempty = false })
local splits = vim.split(path:gsub("/+", "/"), "/", { plain = true, trimempty = false })
if #splits == 0 then
goto continue
elseif vim.tbl_contains(splits, "..") then -- deal with '../' later in `self:resolve()`
Expand Down Expand Up @@ -270,36 +271,35 @@ end
---Alias to `tostring(self)`
---@return string
function Path:tostring()
return tostring(self)
return self:__tostring()
end

---Return the group name of the file GID.
---Return the basename of `self`.
---Eg: foo/bar/baz.txt -> baz.txt
---@return string
function Path:basename()
return fs.basename(self:tostring())
return self._raw_paths[#self._raw_paths]
end

---Return the group name of the file GID. Same as `str(self) minus self:modify(":r")`.
---@return string # extension of path including the dot (`.`): `.py`, `.lua` etc
function Path:suffix()
local path_str = self:tostring()
local without_ext = vim.fn.fnamemodify(path_str, ":r")
return path_str:sub(without_ext:len() + 1) or ""
return self:basename():sub(self:stem():len() + 1)
end

---Return the group name of the file GID. Same as `self:modify(":t:r")`.
---@return string # stem of path. (src/version.c -> "version")
function Path:stem()
return vim.fn.fnamemodify(self:tostring(), ":t:r")
return (self:basename():gsub("([^.])%.[^.]+$", "%1", 1))
end

---Return parent directory of itself. If parent does not exist, returns nil.
---@return PathlibPath?
function Path:parent()
if #self._raw_paths >= 2 then
return self.new_from(self, 1)
else
return nil
if not self.__parent_cache and #self._raw_paths >= 2 then
self.__parent_cache = self.new_from(self, 1)
end
return self.__parent_cache
end

---Return iterator of parents.
Expand Down Expand Up @@ -520,12 +520,12 @@ end
---@param recursive boolean # if true, creates parent directories as well
---@return boolean? success
function Path:mkdir(mode, recursive)
if self:is_dir() then
if self:is_dir(true) then
return true
end
if recursive then
local parent = self:parent()
if parent and not parent:is_dir() then
if parent and not parent:is_dir(true) then
local success = parent:mkdir(mode, recursive)
if not success then
return success
Expand Down Expand Up @@ -595,7 +595,9 @@ function Path:move(target)
errs.assert_function("Path:move", function()
return utils.tables.is_path_module(target)
end, "target is not a Path object.")
target:unlink()
if target:exists() then
target:unlink()
end
return self.nuv.fs_rename(self:tostring(), target:tostring())
end

Expand Down Expand Up @@ -684,7 +686,7 @@ function Path:fs_open(flags, mode, ensure_dir)
if ensure_dir == true then
ensure_dir = const.o755
end
if type(ensure_dir) == "integer" then
if type(ensure_dir) == "number" then
self:parent():mkdir(ensure_dir, true)
end
return self.nuv.fs_open(self:tostring(), flags, mode or const.o644)
Expand All @@ -704,7 +706,7 @@ end
---@param offset integer?
---@return integer? bytes # number of bytes written
function Path:fs_write(data, offset)
local fd = self:fs_open("w")
local fd = self:fs_open("w", nil, true)
return fd and self.nuv.fs_write(fd, data, offset) --[[@as integer]]
end

Expand All @@ -713,13 +715,12 @@ end
---@param offset integer?
---@return integer? bytes # number of bytes written
function Path:fs_append(data, offset)
local fd = self:fs_open("a")
local fd = self:fs_open("a", nil, true)
return fd and self.nuv.fs_write(fd, data, offset) --[[@as integer]]
end

---Call `io.read`. Use `self:fs_read` to use with `nio.run` instead.
---@return string? data # whole file content
---@nodiscard
function Path:io_read()
local file, err_msg = io.open(self:tostring(), "r")
if not file then
Expand All @@ -736,7 +737,6 @@ end

---Call `io.read` with byte read mode.
---@return string? bytes # whole file content
---@nodiscard
function Path:io_read_bytes()
local file, err_msg = io.open(self:tostring(), "rb")
if not file then
Expand All @@ -754,7 +754,6 @@ end
---Call `io.write`. Use `self:fs_write` to use with `nio.run` instead. If failed, returns error message
---@param data string # content
---@return boolean # success
---@nodiscard
function Path:io_write(data)
local file, err_msg = io.open(self:tostring(), "w")
if not file then
Expand All @@ -773,7 +772,6 @@ end

---Call `io.write` with byte write mode.
---@param data string # content
---@nodiscard
function Path:io_write_bytes(data)
local file, err_msg = io.open(self:tostring(), "wb")
if not file then
Expand Down Expand Up @@ -831,7 +829,7 @@ function Path:iterdir_async(callback, on_error, on_exit)
break
end
counter = counter + 1
if callback(self:new_child_unpack(name), fs_type) == false then
if callback(self:child(name), fs_type) == false then
break
end
end
Expand Down
3 changes: 3 additions & 0 deletions lua/pathlib/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ end
---@param paths PathlibAbsPath[]
---@param git_root PathlibPath
function M.fill_git_ignore(paths, git_root)
if not git_root then
return
end
local cmd = { "git", "-C", git_root:tostring(), "check-ignore", "--stdin" }
local path_strs = {}
for _, path in ipairs(paths) do
Expand Down
2 changes: 1 addition & 1 deletion lua/pathlib/utils/paths.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ M.normalize_default_opts = {
function M.normalize(path, iswin, opts)
opts = vim.tbl_deep_extend("force", M.normalize_default_opts, opts or {})
if path:sub(1, 1) == "~" then
path = (vim.loop.os_homedir() or "~") .. "/" .. path
path = (vim.loop.os_homedir() or "~") .. "/" .. path:sub(2)
end
if opts.expand_env then
path = path:gsub("%$([%w_]+)", vim.loop.os_getenv)
Expand Down

0 comments on commit 8fb9050

Please sign in to comment.