Skip to content

Commit c7fc90f

Browse files
committed
feat: add type annotations for plenary.enum
1 parent bc87fa6 commit c7fc90f

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

lua/plenary/enum.lua

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@
2525
--- In case of name or value clashing, the call will fail. For this reason, it's
2626
--- best if you define the members in ascending order.
2727
---@brief ]]
28-
local Enum = {}
28+
---@class PlenaryEnum
29+
---@field is_enum fun(tbl: table): boolean
30+
---@field make_enum fun(tbl: PlenaryEnumRawTable): PlenaryEnumEnum
31+
---@overload fun(tbl: PlenaryEnumRawTable): PlenaryEnumEnum
2932

30-
---@class Enum
33+
---@alias PlenaryEnumRawTable (string|{ [1]: string, [2]: integer })[]
3134

32-
---@class Variant
35+
---@class PlenaryEnumEnum
36+
---@field [integer] string
37+
---@field [string] PlenaryVariant
38+
local Enum = {}
3339

40+
---@param name string
41+
---@return string
3442
local function validate_member_name(name)
3543
if #name > 0 and name:sub(1, 1):match "%u" then
3644
return name
@@ -46,32 +54,46 @@ end
4654
--- {'Qux', 10}
4755
--- }
4856
--- </pre>
49-
--- @return Enum: A new enum
57+
---@param tbl PlenaryEnumRawTable
58+
---@return PlenaryEnumEnum: A new enum
5059
local function make_enum(tbl)
60+
---@type PlenaryEnumEnum
5161
local enum = {}
5262

63+
---@class PlenaryVariant
64+
---@field value integer
5365
local Variant = {}
5466
Variant.__index = Variant
5567

68+
---@param i integer
69+
---@return PlenaryVariant
5670
local function newVariant(i)
5771
return setmetatable({ value = i }, Variant)
5872
end
5973

6074
-- we don't need __eq because the __eq metamethod will only ever be
6175
-- invoked when they both have the same metatable
6276

77+
---@param o PlenaryVariant
78+
---@return boolean
6379
function Variant:__lt(o)
6480
return self.value < o.value
6581
end
6682

83+
---@param o PlenaryVariant
84+
---@return boolean
6785
function Variant:__gt(o)
6886
return self.value > o.value
6987
end
7088

89+
---@return string
7190
function Variant:__tostring()
7291
return tostring(self.value)
7392
end
7493

94+
---@param e PlenaryEnumEnum
95+
---@param i integer
96+
---@return integer
7597
local function find_next_idx(e, i)
7698
local newI = i + 1
7799
if not e[newI] then
@@ -112,6 +134,9 @@ local function make_enum(tbl)
112134
return require("plenary.tbl").freeze(setmetatable(enum, Enum))
113135
end
114136

137+
---@param _ PlenaryEnumEnum
138+
---@param key string|integer
139+
---@return string|PlenaryVariant
115140
Enum.__index = function(_, key)
116141
if Enum[key] then
117142
return Enum[key]
@@ -120,8 +145,8 @@ Enum.__index = function(_, key)
120145
end
121146

122147
--- Checks whether the enum has a member with the given name
123-
--- @param key string: The element to check for
124-
--- @return boolean: True if key is present
148+
---@param key string The element to check for
149+
---@return boolean has_key True if key is present
125150
function Enum:has_key(key)
126151
if rawget(getmetatable(self).__index, key) then
127152
return true
@@ -130,17 +155,17 @@ function Enum:has_key(key)
130155
end
131156

132157
--- If there is a member named 'key', return it, otherwise return nil
133-
--- @param key string: The element to check for
134-
--- @return Variant: The element named by key, or nil if not present
158+
---@param key string The element to check for
159+
---@return PlenaryVariant? variant The element named by key, or nil if not present
135160
function Enum:from_str(key)
136161
if self:has_key(key) then
137162
return self[key]
138163
end
139164
end
140165

141166
--- If there is a member of value 'num', return it, otherwise return nil
142-
--- @param num number: The value of the element to check for
143-
--- @return Variant: The element whose value is num
167+
--- @param num integer The value of the element to check for
168+
--- @return PlenaryVariant? variant The element whose value is num
144169
function Enum:from_num(num)
145170
local key = self[num]
146171
if key then
@@ -149,8 +174,8 @@ function Enum:from_num(num)
149174
end
150175

151176
--- Checks whether the given object corresponds to an instance of Enum
152-
--- @param tbl table: The object to be checked
153-
--- @return boolean: True if tbl is an Enum
177+
---@param tbl table The object to be checked
178+
---@return boolean is_enum True if tbl is an Enum
154179
local function is_enum(tbl)
155180
return getmetatable(getmetatable(tbl).__index) == Enum
156181
end
@@ -159,4 +184,4 @@ return setmetatable({ is_enum = is_enum, make_enum = make_enum }, {
159184
__call = function(_, tbl)
160185
return make_enum(tbl)
161186
end,
162-
})
187+
}) --[[@as PlenaryEnum]]

lua/plenary/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
---@field class PlenaryClass
55
---@field context_manager PlenaryContextManager
66
---@field curl PlenaryCurl
7+
---@field enum PlenaryEnum
78
---@field functional PlenaryFunctional
89
---@field job PlenaryJob
910
---@field json PlenaryJson

0 commit comments

Comments
 (0)