From dc8698270b73bcb2130ea6d29b16ec517e8801c0 Mon Sep 17 00:00:00 2001 From: Aire-One Date: Mon, 19 Dec 2022 19:39:34 +0100 Subject: [PATCH] feat(Module_Doc): manage requires --- src/awesomewm.d.tl/entity/Module_Doc.tl | 65 ++++++++++++++++++- src/awesomewm.d.tl/entity/Type_Info.tl | 8 ++- src/awesomewm.d.tl/generator/snippets.tl | 17 +++++ .../generator/teal_type_definitions.tl | 4 ++ src/awesomewm.d.tl/init.tl | 1 + 5 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/awesomewm.d.tl/entity/Module_Doc.tl b/src/awesomewm.d.tl/entity/Module_Doc.tl index 02cf186..612df1b 100644 --- a/src/awesomewm.d.tl/entity/Module_Doc.tl +++ b/src/awesomewm.d.tl/entity/Module_Doc.tl @@ -1,7 +1,14 @@ local Function_Info = require "entity.Function_Info" local List = require "pl.List" +local Map = require "pl.Map" local Variable_Info = require "entity.Variable_Info" +local module_to_require : Map = Map({ + Shape = "gears.shape", + Surface = "gears.surface", + Widget = "wibox.widget", +}) + local record Module_Doc metamethod __call: function(Module_Doc): Module_Doc @@ -16,7 +23,10 @@ local record Module_Doc static_functions: List signals: List + requires: Map + fixup: function(Module_Doc) + populate_requires: function(Module_Doc) end local __Module_Doc: metatable = { @@ -27,6 +37,7 @@ local __Module_Doc: metatable = { properties = List(), static_functions = List(), signals = List(), + requires = Map(), fixup = function(self: Module_Doc) for c in self.constructors:iter() do @@ -44,8 +55,60 @@ local __Module_Doc: metatable = { for s in self.static_functions:iter() do s:fixup() end - end, + populate_requires = function(self: Module_Doc) + -- TODO : Move this to other Entities. Can be a little tricky because we populate a map + for c in self.constructors:iter() do + for p in c.parameters:iter() do + for t in p.types:iter() do + local mod = module_to_require:get(t.name) + if mod then + self.requires:set(t.name, mod) + end + end + end + end + for m in self.methods:iter() do + for p in m.parameters:iter() do + for t in p.types:iter() do + local mod = module_to_require:get(t.name) + if mod then + self.requires:set(t.name, mod) + end + end + end + for t in m.return_types:iter() do + local mod = module_to_require:get(t.name) + if mod then + self.requires:set(t.name, mod) + end + end + end + for p in self.properties:iter() do + for t in p.types:iter() do + local mod = module_to_require:get(t.name) + if mod then + self.requires:set(t.name, mod) + end + end + end + for s in self.static_functions:iter() do + for p in s.parameters:iter() do + for t in p.types:iter() do + local mod = module_to_require:get(t.name) + if mod then + self.requires:set(t.name, mod) + end + end + end + for t in s.return_types:iter() do + local mod = module_to_require:get(t.name) + if mod then + self.requires:set(t.name, mod) + end + end + end + end } end, } diff --git a/src/awesomewm.d.tl/entity/Type_Info.tl b/src/awesomewm.d.tl/entity/Type_Info.tl index 7a6ba90..819a0a6 100644 --- a/src/awesomewm.d.tl/entity/Type_Info.tl +++ b/src/awesomewm.d.tl/entity/Type_Info.tl @@ -1,13 +1,19 @@ local List = require "pl.List" local Map = require "pl.Map" -local type_fix: Map = Map({ +local type_fix : Map = Map({ bool = "boolean", client = "Client", + ["gears.shape"] = "Shape", + ["gears.surface"] = "Surface", image = "Image", int = "integer", screen = "Screen", + shape = "Shape", + surface = "Surface", tag = "Tag", + ["wibox.widget"] = "Widget", + widget = "Widget", }) local record Type_Info diff --git a/src/awesomewm.d.tl/generator/snippets.tl b/src/awesomewm.d.tl/generator/snippets.tl index 1a2c4e2..7d3209b 100644 --- a/src/awesomewm.d.tl/generator/snippets.tl +++ b/src/awesomewm.d.tl/generator/snippets.tl @@ -9,6 +9,7 @@ local Variable_Info = require "entity.Variable_Info" local record Module indent: function(str: string, level: number): string + render_requires: function(requires: Map): string render_typed_variable: function(name: string, types: List): string render_anonymous_function_signature: function(item: Function_Info.Function_Info): string render_record_functions: function(items: List): string @@ -26,6 +27,22 @@ function snippets.indent(str: string, level: number): string return stringx.rstrip(stringx.indent(str, level, string.rep(" ", 3))) end +function snippets.render_requires(requires: Map): string + local tmpl = [[local $(name) = require "$(path)"]] + + local require_statements = List() + for name, path in requires:iter() do + local tmpl_args = { + name = name, + path = path, + } + + require_statements:append(utils.do_or_fail(template.substitute, tmpl, tmpl_args)) + end + + return require_statements:concat("\n") +end + function snippets.render_typed_variable(name: string, types: List): string local tmpl = [[$(name): $(types)]] diff --git a/src/awesomewm.d.tl/generator/teal_type_definitions.tl b/src/awesomewm.d.tl/generator/teal_type_definitions.tl index 1697d0b..0a1d31a 100644 --- a/src/awesomewm.d.tl/generator/teal_type_definitions.tl +++ b/src/awesomewm.d.tl/generator/teal_type_definitions.tl @@ -8,6 +8,10 @@ local snippets = require "generator.snippets" local tmpl = [[ -- Auto generated file (Do not manually edit this file!) +# if module.requires:len() ~= 0 then +$(snippets.render_requires(module.requires)) +# end -- /requires + local record $(module.record_name) # if #module.signals ~= 0 then $(snippets.indent(snippets.render_enum("Signal", module.signals))) diff --git a/src/awesomewm.d.tl/init.tl b/src/awesomewm.d.tl/init.tl index 42e638d..e5156ae 100644 --- a/src/awesomewm.d.tl/init.tl +++ b/src/awesomewm.d.tl/init.tl @@ -28,6 +28,7 @@ local function do_one_file(url: string, module_name: string, output: string) local html = crawler.fetch(url) local module_doc = scraper.module_doc.get_doc_from_page(html, module_name) module_doc:fixup() + module_doc:populate_requires() filesystem.file_writer.write( generator.teal_type_definitions.generate_teal(module_doc), output