Module:Enum

local api = require('Module:API') local member_link = require('Module:MemberLink') local member_tags = require('Module:Member tags') local utils = require('Module:Utils')

local p = {}

function p._enum_item_count(args) local enum_name = args.enum local enum_data = api.get_enum_data(enum_name) if not enum_data then return error("Unknown enum: " .. enum_name) end local item_count = 0 for _, item in pairs(enum_data.Items) do       item_count = item_count + 1 end return tostring(item_count) end

p.enum_item_count = utils.make_wrapper_function(p._enum_item_count)

function p._enum_reference_list(args) local enum_name = args.enum local reference_list = {} local classes = require("Module:APIClasses") for _, class in ipairs(classes) do       for _, member in ipairs(class.Members) do            if member.ValueType and member.ValueType.Category == "Enum" and member.ValueType.Name == enum_name or member.ReturnType and member.ReturnType.Category == "Enum" and member.ReturnType.Name == enum_name then table.insert(reference_list, {class.Name, member}) end if member.Parameters then local parameters = {} for _, parameter in ipairs(member.Parameters) do                   if parameter.Type and parameter.Type.Category == "Enum" and parameter.Type.Name == enum_name then table.insert(parameters, parameter.Name) end end if #parameters > 0 then table.insert(reference_list, {class.Name, member, parameters}) end end end end local output = "" if #reference_list > 0 then for _, reference in ipairs(reference_list) do           local class_name, member, parameter_names = reference[1], reference[2], reference[3] output = output .. "* " .. member_link.make_member_link_with_icon(class_name, member) if parameter_names then output = output .. " ("               for _, parameter_name in ipairs(parameter_names) do                    output = output .. " " .. parameter_name .. ", "                end                output = output:sub(1, -3) -- Remove the last comma and space.                output = output .. ")" end output = output .. "\n" end else output = "The " .. enum_name .. " enum is not referenced by any members in the API." end -- Remove last newline. if output:sub(-1) == "\n" then output = output:sub(1, -2) end return output end

p.enum_reference_list = utils.make_wrapper_function(p._enum_reference_list)

function p._generate_enum_table(args) local enum_name = args.enum local enum_data = api.get_enum_data(enum_name) if enum_data then -- Determine if at least one enum item has tags. local has_item_tags = false for _, item in pairs(enum_data.Items) do           if item.Tags then has_item_tags = true break end end

-- Get item descriptions local rmd_summaries = require("Module:ReflectionMetadataHelpers").get_enum_item_summaries(enum_name) local item_descriptions = {} local has_item_descriptions = false for _, item in pairs(enum_data.Items) do           if args[item.Name] then has_item_descriptions = true item_descriptions[item.Name] = args[item.Name] elseif rmd_summaries then local rmd_summary = rmd_summaries[item.Name] if rmd_summary and not rmd_summary:find("<") then has_item_descriptions = true item_descriptions[item.Name] = rmd_summary end end end

local t = '{| class="article-table"\n' .. '|+ Enum items\n' .. '! Name\n' .. '! Value\n' if has_item_tags then t = t .. '! Tags\n' end if has_item_descriptions then t = t .. '! Description\n' end t = t .. '|-\n' for _, item in pairs(enum_data.Items) do           t = t .. "| " .. item.Name .. "\n" .. "| " .. item.Value if has_item_tags then t = t .. "\n| " if item.Tags then local formatted_tags = {} for _, tag in pairs(item.Tags) do                       table.insert(formatted_tags, member_tags.make_tag{name=tag}) end t = t .. table.concat(formatted_tags, ", ") end end if has_item_descriptions then t = t .. "\n| " local item_description = item_descriptions[item.Name] if item_description then t = t .. item_description end end t = t .. "\n|-\n" end t = t .. "|}"       return t    else return error("Unknown enum: " .. enum_name) end end

p.generate_enum_table = utils.make_wrapper_function(p._generate_enum_table)

return p