Another fix for property type

This commit is contained in:
Robert Jelic
2025-03-04 14:33:33 +01:00
parent bc2d8d6a63
commit 4434989731
2 changed files with 36 additions and 13 deletions

View File

@@ -71,11 +71,11 @@ end})
---@property ignoreOffset boolean false Whether to ignore the parent's offset ---@property ignoreOffset boolean false Whether to ignore the parent's offset
VisualElement.defineProperty(VisualElement, "ignoreOffset", {default = false, type = "boolean"}) VisualElement.defineProperty(VisualElement, "ignoreOffset", {default = false, type = "boolean"})
---@combinedProperty position {x y} Combined x, y position ---@combinedProperty position {x number, y number} Combined x, y position
VisualElement.combineProperties(VisualElement, "position", "x", "y") VisualElement.combineProperties(VisualElement, "position", "x", "y")
---@combinedProperty size {width height} Combined width, height ---@combinedProperty size {width number, height number} Combined width, height
VisualElement.combineProperties(VisualElement, "size", "width", "height") VisualElement.combineProperties(VisualElement, "size", "width", "height")
---@combinedProperty color {foreground background} Combined foreground, background colors ---@combinedProperty color {foreground number, background number} Combined foreground, background colors
VisualElement.combineProperties(VisualElement, "color", "foreground", "background") VisualElement.combineProperties(VisualElement, "color", "foreground", "background")
---@event onClick {button, x, y} Fired on mouse click ---@event onClick {button, x, y} Fired on mouse click

View File

@@ -24,9 +24,13 @@ local function parseCombinedProperty(content)
local name, props, desc = line:match("%-%-%-@combinedProperty%s+(%w+)%s+{(.-)%}%s+(.*)") local name, props, desc = line:match("%-%-%-@combinedProperty%s+(%w+)%s+{(.-)%}%s+(.*)")
if name and props then if name and props then
local propList = {} local propList = {}
for prop in props:gmatch("(%w+)") do for prop, typ in props:gmatch("(%w+)%s*(%w*)")do
table.insert(propList, prop) table.insert(propList, {
name = prop,
type = typ ~= "" and typ or "any"
})
end end
combinedProperties[#combinedProperties + 1] = { combinedProperties[#combinedProperties + 1] = {
name = name, name = name,
properties = propList, properties = propList,
@@ -119,32 +123,51 @@ local function generateClassContent(className, properties, combinedProperties, e
table.insert(content, "") table.insert(content, "")
end end
for _, combinedProp in ipairs(combinedProperties)do for _, combinedProp in ipairs(combinedProperties) do
table.insert(content, string.format("--- Gets the %s", combinedProp.description)) table.insert(content, string.format("--- Gets the %s", combinedProp.description))
table.insert(content, string.format("---@generic Element: %s", className)) table.insert(content, string.format("---@generic Element: %s", className))
table.insert(content, "---@param self Element") table.insert(content, "---@param self Element")
table.insert(content, string.format("---@return %s", combinedProp.type)) local returns = {}
table.insert(content, string.format("function %s:get%s()", for _, prop in ipairs(combinedProp.properties) do
table.insert(returns, prop.type)
end
table.insert(content, string.format("---@return %s", table.concat(returns, " ")))
table.insert(content, string.format("function %s:get%s()",
className, className,
combinedProp.name:sub(1,1):upper() .. combinedProp.name:sub(2) combinedProp.name:sub(1,1):upper() .. combinedProp.name:sub(2)
)) ))
table.insert(content, string.format(" return self.%s", combinedProp.name)) local returnValues = {}
for _, prop in ipairs(combinedProp.properties) do
table.insert(returnValues, "self." .. prop.name)
end
table.insert(content, string.format(" return %s", table.concat(returnValues, ", ")))
table.insert(content, "end") table.insert(content, "end")
table.insert(content, "") table.insert(content, "")
table.insert(content, string.format("--- Sets the %s", combinedProp.description)) table.insert(content, string.format("--- Sets the %s", combinedProp.description))
table.insert(content, string.format("---@generic Element: %s", className)) table.insert(content, string.format("---@generic Element: %s", className))
table.insert(content, "---@param self Element") table.insert(content, "---@param self Element")
table.insert(content, string.format("---@param %s %s", combinedProp.name, combinedProp.type)) for _, prop in ipairs(combinedProp.properties) do
table.insert(content, string.format("---@param %s %s", prop.name, prop.type))
end
table.insert(content, "---@return Element") table.insert(content, "---@return Element")
table.insert(content, string.format("function %s:set%s(%s)",
local params = {}
for _, prop in ipairs(combinedProp.properties) do
table.insert(params, prop.name)
end
table.insert(content, string.format("function %s:set%s(%s)",
className, className,
combinedProp.name:sub(1,1):upper() .. combinedProp.name:sub(2), combinedProp.name:sub(1,1):upper() .. combinedProp.name:sub(2),
combinedProp.name table.concat(params, ", ")
)) ))
table.insert(content, string.format(" self.%s = %s", combinedProp.name, combinedProp.name))
for _, prop in ipairs(combinedProp.properties) do
table.insert(content, string.format(" self.%s = %s", prop.name, prop.name))
end
table.insert(content, " return self") table.insert(content, " return self")
table.insert(content, "end") table.insert(content, "end")
table.insert(content, "")
end end
for _, event in ipairs(events) do for _, event in ipairs(events) do