diff --git a/src/awesomewm.d.tl/entity/Function_Info.tl b/src/awesomewm.d.tl/entity/Function_Info.tl index f856539..6b8c821 100644 --- a/src/awesomewm.d.tl/entity/Function_Info.tl +++ b/src/awesomewm.d.tl/entity/Function_Info.tl @@ -1,4 +1,5 @@ local List = require "pl.List" +local Type_Info = require "entity.Type_Info" local record Function_Info metamethod __call: function(Function_Info): Function_Info @@ -6,7 +7,7 @@ local record Function_Info Function_Info: Function_Info record Parameter name: string - types: List + types: List end name: string @@ -27,7 +28,7 @@ local __Function_Info: metatable = { end, } -function Function_Info:append_parameter(name: string, types: List) +function Function_Info:append_parameter(name: string, types: List) self.parameters:append { name = name, types = types, diff --git a/src/awesomewm.d.tl/entity/Module_Doc.tl b/src/awesomewm.d.tl/entity/Module_Doc.tl index bb50903..484bd75 100644 --- a/src/awesomewm.d.tl/entity/Module_Doc.tl +++ b/src/awesomewm.d.tl/entity/Module_Doc.tl @@ -9,7 +9,9 @@ local record Module_Doc record_name: string - constructors: List + constructors: List -- Translates to a list of methods + constructor_param_record: List -- Constructor with the __call metamethod and named parameters pattern + methods: List properties: List static_functions: List @@ -20,6 +22,7 @@ local __Module_Doc: metatable = { __call = function(_: Module_Doc): Module_Doc return { constructors = List(), + constructor_param_record = List(), methods = List(), properties = List(), static_functions = List(), diff --git a/src/awesomewm.d.tl/entity/Type_Info.tl b/src/awesomewm.d.tl/entity/Type_Info.tl new file mode 100644 index 0000000..d7eb14d --- /dev/null +++ b/src/awesomewm.d.tl/entity/Type_Info.tl @@ -0,0 +1,31 @@ +local Map = require "pl.Map" + +local record Type_Info + metamethod __call: function(Type_Info, record_name: string): Type_Info + + Type_Info: Type_Info + + name: string + + -- Map : name -> type + -- We can't use Variable_Info here because it's a circular dependency. + record_entries: Map | nil +end + +local __Type_Info: metatable = { + __call = function(_self: Type_Info, record_name: string): Type_Info + if record_name ~= nil then + return { + name = record_name, + record_entries = Map() + } + end + return { + name = "", + record_entries = nil, + } + end, +} + +return setmetatable({} as Type_Info, __Type_Info) + diff --git a/src/awesomewm.d.tl/entity/Variable_Info.tl b/src/awesomewm.d.tl/entity/Variable_Info.tl index f376458..48e4ce0 100644 --- a/src/awesomewm.d.tl/entity/Variable_Info.tl +++ b/src/awesomewm.d.tl/entity/Variable_Info.tl @@ -1,4 +1,5 @@ local List = require "pl.List" +local Type_Info = require "entity.Type_Info" local record Variable_Info metamethod __call: function(Variable_Info): Variable_Info @@ -6,7 +7,7 @@ local record Variable_Info Variable_Info: Variable_Info name: string - types: List + types: List constraints: List end diff --git a/src/awesomewm.d.tl/generator/snippets.tl b/src/awesomewm.d.tl/generator/snippets.tl index 510a945..96043ea 100644 --- a/src/awesomewm.d.tl/generator/snippets.tl +++ b/src/awesomewm.d.tl/generator/snippets.tl @@ -2,6 +2,7 @@ local Function_Info = require "entity.Function_Info" local List = require "pl.List" local stringx = require "pl.stringx" local template = require "pl.template" +local Type_Info = require "entity.Type_Info" local utils = require "utils" local Variable_Info = require "entity.Variable_Info" @@ -12,13 +13,13 @@ function snippets.indent(str: string, level: number): string return stringx.rstrip(stringx.indent(str, level, string.rep(" ", 3))) end -function snippets.render_typed_variable(name: string, types: List): string +function snippets.render_typed_variable(name: string, types: List): string local tmpl = [[$(name): $(types)]] local tmpl_args = { name = name, - types = types:concat(" | "), + types = types:map(function(t: Type_Info.Type_Info): string return t.name end):concat(" | "), } return utils.do_or_fail(template.substitute, tmpl, tmpl_args) @@ -70,10 +71,11 @@ function snippets.render_record_properties(items: List +local function parse_parameter_types(parameter_type: string): List if parameter_type == "" then - return List({ "any" }) + local type_info: Type_Info.Type_Info = { name = "any" } + return List({ type_info }) end - return stringx.split(parameter_type, " or "):map(utils.sanitize_string) + return stringx.split(parameter_type, " or "):map( + function(type_name: string): Type_Info.Type_Info + return { name = utils.sanitize_string(type_name) } + end + ) end local function extract_item_name(item_name_node: scan.HTMLNode): string @@ -50,12 +56,12 @@ local function extract_function_parameters(function_parameters_node: scan.HTMLNo local name = extract_node_text(nodes[query_selectors.name] as scan.HTMLNode) local types = parse_parameter_types(extract_node_text(nodes[query_selectors.types] as scan.HTMLNode)) - if types == List({ "table" }) then + if #types == 1 and types[1].name == "table" then local record_name = utils.capitalize(name) current_record_parameter = record_name return { name = name, - types = List({ record_name }), + types = List({ Type_Info(record_name) }), } end @@ -132,7 +138,8 @@ local function extract_section_variables(dl: string): { Variable_Info.Variable_I variable_info.name = extract_item_name(nodes[query_selectors.variable_name]) variable_info.types = parse_parameter_types(extract_node_text(nodes[query_selectors.variable_summary_type])) - if variable_info.types:contains("string") then + if #variable_info.types == 1 and variable_info.types[1].name == "string" then + log:debug("extract variable string with constraints, this is an enum") variable_info.constraints = List(extract_property_constraints(nodes[query_selectors.variable_property_constraint])):map( function(constraint: string): string return (constraint:gsub(""", "")) @@ -166,6 +173,12 @@ function module.get_doc_from_page(html: string, module_name: string): Module_Doc local module_doc = Module_Doc() module_doc.record_name = utils.capitalize((module_name:gsub(".*%.", ""))) + local self_type: Type_Info.Type_Info = { name = module_doc.record_name } + local self_parameter: Function_Info.Parameter = { + name = "self", + types = List({ self_type }), + } + for i = 1, #nodes:get("h2.section-header") do local h2 = nodes:get("h2.section-header")[i] local section_name = utils.sanitize_string(h2:inner_text()) @@ -181,10 +194,7 @@ function module.get_doc_from_page(html: string, module_name: string): Module_Doc log:warn("Not implemented: Deprecated object properties") elseif section_name == "Object methods" then module_doc.methods = List(extract_section_functions(dl_html)):map(function(method: Function_Info.Function_Info): Function_Info.Function_Info - method.parameters:insert(1, { - name = "self", - types = List({ module_doc.record_name }), - }) + method.parameters:insert(1, self_parameter) return method end) elseif section_name == "Signals" then