From fa736697a9c51dfc27b536adcef23715635b1d36 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Thu, 14 Nov 2019 18:18:07 -0800 Subject: [PATCH] Introduce early Lua formatting library inspired by Rust --- plugin/default.project.json | 3 +++ plugin/fmt/init.lua | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 plugin/fmt/init.lua diff --git a/plugin/default.project.json b/plugin/default.project.json index bdccf678..36ee57f1 100644 --- a/plugin/default.project.json +++ b/plugin/default.project.json @@ -11,6 +11,9 @@ "Http": { "$path": "http" }, + "Fmt": { + "$path": "fmt" + }, "Roact": { "$path": "modules/roact/src" }, diff --git a/plugin/fmt/init.lua b/plugin/fmt/init.lua new file mode 100644 index 00000000..387802c6 --- /dev/null +++ b/plugin/fmt/init.lua @@ -0,0 +1,54 @@ +local function debugOutputBuffer() + local buffer = {} + local indentLevel = 0 + local indentation = "" + + function buffer:push(template, ...) + local value = string.format(template, ...) + + if #indentation > 0 then + value = indentation .. value:gsub("\n", "\n" .. indentation) + end + + table.insert(self, value) + end + + function buffer:indent() + indentLevel = indentLevel + 1 + indentation = string.rep(" ", indentLevel) + end + + function buffer:unindent() + indentLevel = math.max(0, indentLevel - 1) + indentation = string.rep(" ", indentLevel) + end + + function buffer:finish() + return table.concat(self, "\n") + end + + return buffer +end + +local function debugInner(value) + local valueType = typeof(value) + + if valueType == "string" then + return string.format("%q", value) + elseif valueType == "number" then + return tostring(value) + elseif valueType == "table" then + local debugImpl = getmetatable(value).__fmtDebug + + if debugImpl ~= nil then + return debugImpl() + else + -- TODO: Nicer default debug implementation? + return tostring(value) + end + end +end + +return { + debugOutputBuffer = debugOutputBuffer, +} \ No newline at end of file