Skip to content

Commit b8789ab

Browse files
committed
enum
1 parent bc87fa6 commit b8789ab

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

lua/plenary/enum.lua

+35-14
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
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+
---@class PlenaryEnumEnum
29+
---@field [integer] string
30+
---@field [string] PlenaryVariant
2831
local Enum = {}
2932

30-
---@class Enum
31-
32-
---@class Variant
33-
33+
---@param name string
34+
---@return string
3435
local function validate_member_name(name)
3536
if #name > 0 and name:sub(1, 1):match "%u" then
3637
return name
@@ -46,32 +47,46 @@ end
4647
--- {'Qux', 10}
4748
--- }
4849
--- </pre>
49-
--- @return Enum: A new enum
50+
---@param tbl (string|{ [1]: string, [2]: integer })[]
51+
---@return PlenaryEnumEnum: A new enum
5052
local function make_enum(tbl)
53+
---@type PlenaryEnumEnum
5154
local enum = {}
5255

56+
---@class PlenaryVariant
57+
---@field private value integer
5358
local Variant = {}
5459
Variant.__index = Variant
5560

61+
---@param i integer
62+
---@return PlenaryVariant
5663
local function newVariant(i)
5764
return setmetatable({ value = i }, Variant)
5865
end
5966

6067
-- we don't need __eq because the __eq metamethod will only ever be
6168
-- invoked when they both have the same metatable
6269

70+
---@param o PlenaryVariant
71+
---@return boolean
6372
function Variant:__lt(o)
6473
return self.value < o.value
6574
end
6675

76+
---@param o PlenaryVariant
77+
---@return boolean
6778
function Variant:__gt(o)
6879
return self.value > o.value
6980
end
7081

82+
---@return string
7183
function Variant:__tostring()
7284
return tostring(self.value)
7385
end
7486

87+
---@param e PlenaryEnumEnum
88+
---@param i integer
89+
---@return integer
7590
local function find_next_idx(e, i)
7691
local newI = i + 1
7792
if not e[newI] then
@@ -112,6 +127,9 @@ local function make_enum(tbl)
112127
return require("plenary.tbl").freeze(setmetatable(enum, Enum))
113128
end
114129

130+
---@param _ PlenaryEnumEnum
131+
---@param key string|integer
132+
---@return string|PlenaryVariant
115133
Enum.__index = function(_, key)
116134
if Enum[key] then
117135
return Enum[key]
@@ -120,8 +138,8 @@ Enum.__index = function(_, key)
120138
end
121139

122140
--- 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
141+
---@param key string The element to check for
142+
---@return boolean has_key True if key is present
125143
function Enum:has_key(key)
126144
if rawget(getmetatable(self).__index, key) then
127145
return true
@@ -130,17 +148,17 @@ function Enum:has_key(key)
130148
end
131149

132150
--- 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
151+
---@param key string The element to check for
152+
---@return PlenaryVariant? variant The element named by key, or nil if not present
135153
function Enum:from_str(key)
136154
if self:has_key(key) then
137155
return self[key]
138156
end
139157
end
140158

141159
--- 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
160+
--- @param num integer The value of the element to check for
161+
--- @return string? name The element whose value is num
144162
function Enum:from_num(num)
145163
local key = self[num]
146164
if key then
@@ -149,14 +167,17 @@ function Enum:from_num(num)
149167
end
150168

151169
--- 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
170+
---@param tbl table The object to be checked
171+
---@return boolean is_enum True if tbl is an Enum
154172
local function is_enum(tbl)
155173
return getmetatable(getmetatable(tbl).__index) == Enum
156174
end
157175

176+
---@class PlenaryEnum
177+
---@field is_enum `is_enum`
178+
158179
return setmetatable({ is_enum = is_enum, make_enum = make_enum }, {
159180
__call = function(_, tbl)
160181
return make_enum(tbl)
161182
end,
162-
})
183+
}) --[[@as PlenaryEnum]]

0 commit comments

Comments
 (0)