diff --git a/src/main.lua b/src/main.lua index 52de918..c7f3a06 100644 --- a/src/main.lua +++ b/src/main.lua @@ -244,7 +244,7 @@ function basalt.run(isActive) end end ---- Returns an element class +--- Returns an element's class without creating a instance --- @shortDescription Returns an element class --- @param name string The name of the element --- @return table Element The element class diff --git a/tools/annotationParser.lua b/tools/annotationParser.lua index 5c6e9cc..16b3524 100644 --- a/tools/annotationParser.lua +++ b/tools/annotationParser.lua @@ -1,98 +1,42 @@ -local function parseProperty(line) - local name, type, default, description = line:match("%-%-%-@property%s+(%w+)%s+(%w+)%s+(.-)%s+(.*)") - - if name and type then - - local fieldDef = string.format("---@field %s %s\n", name, type) - - local getterDoc = string.format([[ ---- Gets the %s ----@generic T: %s ----@param self T ----@return %s -]], description, "VisualElement", type) - - local getterFunc = string.format([[ -function VisualElement:get%s() - return self.%s -end -]], name:sub(1,1):upper() .. name:sub(2), name) - - local setterDoc = string.format([[ ---- Sets the %s ----@generic T: %s ----@param self T ----@param %s %s ----@return T -]], description, "VisualElement", name, type) - - local setterFunc = string.format([[ -function VisualElement:set%s(%s) - self.%s = %s - return self -end -]], name:sub(1,1):upper() .. name:sub(2), name, name, name) - - return fieldDef .. getterDoc .. getterFunc .. setterDoc .. setterFunc - end -end - -local function parseCombinedProperty(line) - local name, props, description = line:match("%-%-%-@combinedProperty%s+(%w+)%s+{(.-)%}%s+(.*)") - if name and props then - local propList = {} - for prop in props:gmatch("(%w+)") do - table.insert(propList, prop) - end - - local paramList = table.concat(propList, ", ") - local returnList = paramList - local assignList = {} - for _, prop in ipairs(propList) do - table.insert(assignList, string.format(" self.%s = %s", prop, prop)) - end - - local getterDoc = string.format([[ ---- Gets the %s ----@generic T: %s ----@param self T ----@return %s -]], description, "VisualElement", table.concat(propList, " ")) - - local getterFunc = string.format([[ -function VisualElement:get%s() - return self.%s -end -]], name:sub(1,1):upper() .. name:sub(2), returnList) - - local setterDoc = string.format([[ ---- Sets the %s ----@generic T: %s ----@param self T -%s ----@return T -]], description, "VisualElement", -table.concat(propList:map(function(prop) - return string.format("---@param %s any", prop) -end), "\n")) - - local setterFunc = string.format([[ -function VisualElement:set%s(%s) -%s - return self -end -]], name:sub(1,1):upper() .. name:sub(2), - paramList, - table.concat(assignList, "\n")) - - return getterDoc .. getterFunc .. setterDoc .. setterFunc - end -end - local function findClassName(content) return content:match("%-%-%-@class%s+(%w+)") end +local function parseProperties(content) + local properties = {} + for line in content:gmatch("[^\r\n]+") do + local name, type, default, desc = line:match("%-%-%-@property%s+(%w+)%s+(%w+)%s+(.-)%s+(.*)") + if name and type then + properties[#properties + 1] = { + name = name, + type = type, + default = default, + description = desc + } + end + end + return properties +end + +local function parseCombinedProperty(content) + local combinedProperties = {} + for line in content:gmatch("[^\r\n]+") do + local name, props, desc = line:match("%-%-%-@combinedProperty%s+(%w+)%s+{(.-)%}%s+(.*)") + if name and props then + local propList = {} + for prop in props:gmatch("(%w+)") do + table.insert(propList, prop) + end + combinedProperties[#combinedProperties + 1] = { + name = name, + properties = propList, + description = desc + } + end + end + return combinedProperties +end + local function parseEvents(content) local events = {} for line in content:gmatch("[^\r\n]+") do @@ -175,43 +119,32 @@ local function generateClassContent(className, properties, combinedProperties, e table.insert(content, "") end - for _, prop in ipairs(combinedProperties) do - local paramList = table.concat(prop.properties, ", ") - - table.insert(content, string.format("--- Gets the %s", prop.description)) + for _, combinedProp in ipairs(combinedProperties)do + table.insert(content, string.format("--- Gets the %s", combinedProp.description)) table.insert(content, string.format("---@generic Element: %s", className)) table.insert(content, "---@param self Element") - table.insert(content, string.format("---@return %s", table.concat(prop.properties, " "))) + table.insert(content, string.format("---@return %s", combinedProp.type)) table.insert(content, string.format("function %s:get%s()", className, - prop.name:sub(1,1):upper() .. prop.name:sub(2) - )) - table.insert(content, string.format(" return %s", - table.concat(prop.properties:map(function(p) - return "self." .. p - end), ", ") + combinedProp.name:sub(1,1):upper() .. combinedProp.name:sub(2) )) + table.insert(content, string.format(" return self.%s", combinedProp.name)) table.insert(content, "end") table.insert(content, "") - table.insert(content, string.format("--- Sets the %s", prop.description)) + table.insert(content, string.format("--- Sets the %s", combinedProp.description)) table.insert(content, string.format("---@generic Element: %s", className)) table.insert(content, "---@param self Element") - for _, p in ipairs(prop.properties) do - table.insert(content, string.format("---@param %s any", p)) - end + table.insert(content, string.format("---@param %s %s", combinedProp.name, combinedProp.type)) table.insert(content, "---@return Element") table.insert(content, string.format("function %s:set%s(%s)", className, - prop.name:sub(1,1):upper() .. prop.name:sub(2), - paramList + combinedProp.name:sub(1,1):upper() .. combinedProp.name:sub(2), + combinedProp.name )) - for _, p in ipairs(prop.properties) do - table.insert(content, string.format(" self.%s = %s", p, p)) - end + table.insert(content, string.format(" self.%s = %s", combinedProp.name, combinedProp.name)) table.insert(content, " return self") table.insert(content, "end") - table.insert(content, "") end for _, event in ipairs(events) do @@ -265,22 +198,8 @@ local function parseFolder(folder, destinationFile) local className = findClassName(content) if className then - local properties = {} - local combinedProperties = {} - - for line in content:gmatch("[^\r\n]+") do - - local propResult = parseProperty(line) - if propResult then - table.insert(properties, propResult) - end - - local combResult = parseCombinedProperty(line) - if combResult then - table.insert(combinedProperties, combResult) - end - end - + local properties = parseProperties(content) + local combinedProperties = parseCombinedProperty(content) local events = parseEvents(content) local classContent = generateClassContent(className, properties, combinedProperties, events, allClasses) if classContent then