From 5310697d3f23e62b659bfc6338960651371b19c4 Mon Sep 17 00:00:00 2001 From: Macoy Madson Date: Thu, 24 Sep 2020 08:31:10 -0700 Subject: [PATCH] Fixed Writer formatting Both K&R One True Brace and Allman are looking pretty good now --- runtime/HotReloading.cake | 3 ++- runtime/TextAdventure.cake | 4 ++++ src/Generators.cpp | 4 ++-- src/Writer.cpp | 44 ++++++++++++++++++++++++++++---------- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/runtime/HotReloading.cake b/runtime/HotReloading.cake index 07fde25..7dfd3e3 100644 --- a/runtime/HotReloading.cake +++ b/runtime/HotReloading.cake @@ -34,12 +34,13 @@ (unless current-lib (return false)) - (for-in function-referent-it FunctionReferenceMapPair registered-functions + (for-in function-referent-it (& FunctionReferenceMapPair) registered-functions (var loaded-symbol (* void) (getSymbolFromDynamicLibrary current-lib (on-call (path function-referent-it . first) c_str))) (unless loaded-symbol (return false)) + ;; TODO: What will happen once modules are unloaded? We can't store pointers to their static memory (for-in function-pointer (* (* void)) (path function-referent-it . second) (set (deref function-pointer) loaded-symbol))) (return true)) diff --git a/runtime/TextAdventure.cake b/runtime/TextAdventure.cake index f3406f7..61e4a3c 100644 --- a/runtime/TextAdventure.cake +++ b/runtime/TextAdventure.cake @@ -1,3 +1,7 @@ +;; Cake Adventure +;; This is meant to be a dead simple "game" which you can modify while it is running. +;; This is to test hot-reloading + ;; (import &comptime-only "Macros.cake") (c-import "" diff --git a/src/Generators.cpp b/src/Generators.cpp index eaf9d9d..c003b2d 100644 --- a/src/Generators.cpp +++ b/src/Generators.cpp @@ -934,9 +934,9 @@ static void tokenizeGenerateStringTokenize(const char* outputVarName, const Toke // TODO Need to gensym error, or replace with a function call char tokenizeLineBuffer[2048] = {0}; PrintfBuffer(tokenizeLineBuffer, - "if (!tokenizeLinePrintError(\"%s\", \"%s\", %d, %s)) {return false;}\n", + "if (!tokenizeLinePrintError(\"%s\", \"%s\", %d, %s)) {return false;}", stringToTokenize, triggerToken.source, triggerToken.lineNumber, outputVarName); - addStringOutput(output.source, tokenizeLineBuffer, StringOutMod_None, &triggerToken); + addStringOutput(output.source, tokenizeLineBuffer, StringOutMod_NewlineAfter, &triggerToken); } bool TokenizePushGenerator(EvaluatorEnvironment& environment, const EvaluatorContext& context, diff --git a/src/Writer.cpp b/src/Writer.cpp index 882abbc..e681bc6 100644 --- a/src/Writer.cpp +++ b/src/Writer.cpp @@ -165,6 +165,7 @@ struct StringOutputState // For determining character offsets to pass to e.g. Emacs' goto-char int numCharsOutput; int currentLine; + int lastLineIndented; FILE* fileOut; }; @@ -186,8 +187,13 @@ static void Writer_Writef(StringOutputState& state, const char* format, ...) static void printIndentation(const WriterFormatSettings& formatSettings, StringOutputState& state) { - // TODO: Good indentation - return; + // Only indent at beginning of line. This will of course break if there are newlines added in + // strings being output + if (state.currentLine == state.lastLineIndented) + return; + + state.lastLineIndented = state.currentLine; + if (formatSettings.indentStyle == WriterFormatIndentType_Tabs) { for (int i = 0; i < state.blockDepth; ++i) @@ -205,12 +211,9 @@ static void printIndentation(const WriterFormatSettings& formatSettings, StringO } } -static void writeStringOutput(const NameStyleSettings& nameSettings, - const WriterFormatSettings& formatSettings, - const StringOutput& outputOperation, StringOutputState& state) +static void updateDepthDoIndentation(const WriterFormatSettings& formatSettings, + const StringOutput& outputOperation, StringOutputState& state) { - // First, handle indentation - printIndentation(formatSettings, state); if (outputOperation.modifiers & StringOutMod_OpenBlock) { state.blockDepth++; @@ -225,6 +228,15 @@ static void writeStringOutput(const NameStyleSettings& nameSettings, } } + printIndentation(formatSettings, state); +} + +static void writeStringOutput(const NameStyleSettings& nameSettings, + const WriterFormatSettings& formatSettings, + const StringOutput& outputOperation, StringOutputState& state) +{ + updateDepthDoIndentation(formatSettings, outputOperation, state); + if (outputOperation.modifiers & StringOutMod_SpaceBefore) Writer_Writef(state, " "); @@ -246,12 +258,22 @@ static void writeStringOutput(const NameStyleSettings& nameSettings, Writer_Writef(state, "{"); else if (formatSettings.braceStyle == WriterFormatBraceStyle_Allman) { - Writer_Writef(state, "\n{\n"); - state.currentLine += 2; + Writer_Writef(state, "\n"); + state.currentLine += 1; + + // Allman brackets are not as deep as their contents, but this bracket was already + // counted by updateDepthDoIndentation() above. Take it back one so indentation is + // correct for the bracket, then go back to the proper block indentation + state.blockDepth -= 1; + printIndentation(formatSettings, state); + state.blockDepth += 1; + + Writer_Writef(state, "{\n"); + state.currentLine += 1; } else if (formatSettings.braceStyle == WriterFormatBraceStyle_KandR_1TBS) { - Writer_Writef(state, "{\n"); + Writer_Writef(state, " {\n"); state.currentLine += 1; } } @@ -283,7 +305,7 @@ static void writeStringOutput(const NameStyleSettings& nameSettings, ++state.currentLine; } - printIndentation(formatSettings, state); + // printIndentation(/*isCloseBlockLine=*/false, formatSettings, state); } else if (outputOperation.modifiers & StringOutMod_ListSeparator) Writer_Writef(state, ", ");