From b7d026b98ea9e8497c86031cd9189b5585ab380f Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Thu, 14 Nov 2019 18:43:51 -0800 Subject: [PATCH] Partial implementation of better formatting machinery --- plugin/fmt/init.lua | 77 +++++++++++++++++++++++++++++++++++-- plugin/src/ServeSession.lua | 2 +- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/plugin/fmt/init.lua b/plugin/fmt/init.lua index 387802c6..88b4044f 100644 --- a/plugin/fmt/init.lua +++ b/plugin/fmt/init.lua @@ -1,18 +1,85 @@ +local function writeFmt(buffer, template, ...) + local currentArg = 0 + local i = 1 + local len = #template + + while i < len do + local openBrace = template:find("{", i) + + if openBrace == nil then + buffer:writeRaw(template:sub(i)) + break + else + if openBrace - i > 0 then + buffer:writeRaw(template:sub(i, openBrace - 1)) + end + + local charAfterBrace = template:sub(openBrace + 1, openBrace + 1) + if charAfterBrace == "{" then + buffer:writeRaw(template:sub(i, openBrace)) + i = openBrace + 2 + else + local closeBrace = template:find("}", openBrace + 1) + assert(closeBrace ~= nil, "Unclosed formatting specifier. Use '{{' to write an open brace.") + + local formatSpecifier = template:sub(openBrace + 1, closeBrace - 1) + + currentArg = currentArg + 1 + + if formatSpecifier == "" then + local arg = select(currentArg, ...) + buffer:writeRaw(tostring(arg)) + else + error("unsupported format specifier " .. formatSpecifier, 2) + end + + i = closeBrace + 1 + end + end + end +end + +local function writeLineFmt(buffer, template, ...) + writeFmt(buffer, template, ...) + table.insert(buffer, "\n") +end + local function debugOutputBuffer() local buffer = {} local indentLevel = 0 local indentation = "" - function buffer:push(template, ...) - local value = string.format(template, ...) + function buffer:writeLine(template, ...) + return writeLineFmt(self, template, ...) + end + function buffer:write(template, ...) + return writeFmt(self, template, ...) + end + + function buffer:writeRaw(value) if #indentation > 0 then - value = indentation .. value:gsub("\n", "\n" .. indentation) + value = value:gsub("\n", "\n" .. indentation) end table.insert(self, value) end + function buffer:writeLineRaw(piece) + if #indentation > 0 then + self:writeRaw(indentation) + end + + self:writeRaw(piece) + table.insert(self, "\n") + end + + function buffer:push(template, ...) + local value = string.format(template, ...) + + self:writeLineRaw(value) + end + function buffer:indent() indentLevel = indentLevel + 1 indentation = string.rep(" ", indentLevel) @@ -24,7 +91,7 @@ local function debugOutputBuffer() end function buffer:finish() - return table.concat(self, "\n") + return table.concat(self, "") end return buffer @@ -51,4 +118,6 @@ end return { debugOutputBuffer = debugOutputBuffer, + writeFmt = writeFmt, + writeLineFmt = writeLineFmt, } \ No newline at end of file diff --git a/plugin/src/ServeSession.lua b/plugin/src/ServeSession.lua index c9f0a9e5..c5263b6e 100644 --- a/plugin/src/ServeSession.lua +++ b/plugin/src/ServeSession.lua @@ -20,7 +20,7 @@ local function DEBUG_showPatch(patch) output:indent() for removed in ipairs(patch.removed) do - output:push("Remove ID %s", removed) + output:writeLine("Remove ID {}", removed) end for id, added in pairs(patch.added) do