Skip to content

Commit

Permalink
fix(types): add type annotations for operator overload
Browse files Browse the repository at this point in the history
  • Loading branch information
pysan3 committed Mar 21, 2024
1 parent f8b8281 commit d194dc2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
9 changes: 5 additions & 4 deletions lua/pathlib/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ local watcher = require("pathlib.utils.watcher")
---@field public __string_cache string|nil # Cache result of `tostring(self)`.
---@field public __parent_cache PathlibPath|nil # Cache reference to parent object.
---@field public __concat_dir_mode boolean|nil # If the object was created with `Path() .. "/"`, next string concat will append as a file.
---@operator div(PathlibPath|string): PathlibPath
---@operator concat(PathlibPath|string): PathlibPath
local Path = setmetatable({
mytype = const.path_module_enum.PathlibPath,
sep_str = "/",
Expand Down Expand Up @@ -876,7 +878,7 @@ end
---Iterate dir with `luv.fs_scandir`.
---@param follow_symlinks boolean|nil # If true, resolves hyperlinks and go into the linked directory.
---@param depth integer|nil # How deep the traverse. If nil or <1, scans everything.
---@param skip_dir (fun(dir: PathlibPath): boolean)|nil # Function to decide whether to dig in a directory.
---@param skip_dir nil|fun(dir: PathlibPath): boolean # Function to decide whether to dig in a directory.
function Path:fs_iterdir(follow_symlinks, depth, skip_dir)
depth = depth or -1
---@type uv_fs_t|nil
Expand Down Expand Up @@ -1203,8 +1205,7 @@ function Path:iterdir(opts)
return function()
local name, fs_type = generator()
if name ~= nil then
return self:new_descendant(unpack(vim.split(name:gsub("\\", "/"), "/", { plain = true, trimempty = false }))),
fs_type
return self:descendant(unpack(vim.split(name:gsub("\\", "/"), "/", { plain = true, trimempty = false }))), fs_type
end
end
end
Expand All @@ -1216,7 +1217,7 @@ end
---@return function
function Path:fs_opendir(follow_symlinks, depth)
depth = depth or -1
---@type (uv.aliases.fs_readdir_entries[]|nil)
---@type uv.aliases.fs_readdir_entries[]|nil
local entries = nil
---@type luv_dir_t|nil
local handler = nil
Expand Down
7 changes: 4 additions & 3 deletions lua/pathlib/posix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ local const = require("pathlib.const")
local err = require("pathlib.utils.errors")

---@class PathlibPosixPath : PathlibPath
---@overload fun(...: string|PathlibPath): PathlibPosixPath
local PosixPath = setmetatable({ ---@diagnostic disable-line
---@operator div(PathlibPosixPath|string): PathlibPosixPath
---@operator concat(PathlibPosixPath|string): PathlibPosixPath
local PosixPath = setmetatable({
mytype = const.path_module_enum.PathlibPosixPath,
}, {
__index = Path,
Expand Down Expand Up @@ -39,7 +40,7 @@ end

function PosixPath.new_empty()
---@type PathlibPosixPath
local self = setmetatable({}, PosixPath) ---@diagnostic disable-line
local self = setmetatable({}, PosixPath)
self:to_empty()
return self
end
Expand Down
14 changes: 7 additions & 7 deletions lua/pathlib/utils/scheduler.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
local nio = require("nio")

---@class PathlibEventStorage
---@field items PathlibPath[]
---@field future nio.control.Future
---@field start integer
---@field public items PathlibPath[]
---@field public future nio.control.Future
---@field public start integer

---@alias PathlibScheduler.monitor fun(elapsed_ms: integer, item_len: integer, key: string): integer # A function to decide whether to trigger the debounce_fn. Triggers if value is negative.
---@alias PathlibScheduler.executor fun(items: PathlibPath[], key: string): (boolean, string|nil) # Executed when should_run_fn returns true. Return values are (success, error_msg?).

---@class PathlibScheduler
---@field storage table<string, PathlibEventStorage>
---@field minimum_debounce_ms integer
---@field monitor PathlibScheduler.monitor
---@field executor PathlibScheduler.executor
---@field public storage table<string, PathlibEventStorage>
---@field public minimum_debounce_ms integer
---@field public monitor PathlibScheduler.monitor
---@field public executor PathlibScheduler.executor
local _Scheduler = setmetatable({}, {
__call = function(cls, ...)
local self = setmetatable({
Expand Down
7 changes: 4 additions & 3 deletions lua/pathlib/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ local function load_winapi()
end

---@class PathlibWindowsPath : PathlibPath
---@overload fun(...: string|PathlibPath): PathlibWindowsPath
local WindowsPath = setmetatable({ ---@diagnostic disable-line
---@operator div(PathlibWindowsPath|string): PathlibWindowsPath
---@operator concat(PathlibWindowsPath|string): PathlibWindowsPath
local WindowsPath = setmetatable({
mytype = const.path_module_enum.PathlibWindows,
sep_str = "\\",
}, {
Expand Down Expand Up @@ -40,7 +41,7 @@ end

function WindowsPath.new_empty()
---@type PathlibWindowsPath
local self = setmetatable({}, WindowsPath) ---@diagnostic disable-line
local self = setmetatable({}, WindowsPath)
self:to_empty()
return self
end
Expand Down

0 comments on commit d194dc2

Please sign in to comment.