]> git.tdb.fi Git - libs/gl.git/commitdiff
Fix formatting issues with else ifs
authorMikko Rasa <tdb@tdb.fi>
Sun, 13 Nov 2016 22:35:55 +0000 (00:35 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 13 Nov 2016 22:35:55 +0000 (00:35 +0200)
source/programcompiler.cpp
source/programcompiler.h

index fdbc83c7d4be404d7f07fe5a7c4757ac74596926..2428ab796e4756ea171cd026d2954a49b505c35b 100644 (file)
@@ -171,7 +171,7 @@ void ProgramCompiler::Visitor::apply(Stage &s)
 ProgramCompiler::Formatter::Formatter():
        indent(0),
        parameter_list(false),
-       else_if(false)
+       else_if(0)
 { }
 
 void ProgramCompiler::Formatter::visit(Literal &literal)
@@ -241,31 +241,26 @@ void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
 
 void ProgramCompiler::Formatter::visit(Block &block)
 {
+       if(else_if)
+               --else_if;
+
+       unsigned brace_indent = indent;
        if(block.use_braces)
-       {
-               if(else_if)
-               {
-                       formatted += '\n';
-                       else_if = false;
-               }
-               formatted += format("%s{\n", string(indent*2, ' '));
-       }
+               formatted += format("%s{\n", string(brace_indent*2, ' '));
 
-       bool change_indent = (!formatted.empty() && !else_if);
-       indent += change_indent;
+       SetForScope<unsigned> set(indent, indent+!formatted.empty());
        string spaces(indent*2, ' ');
        for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
        {
                if(i!=block.body.begin())
                        formatted += '\n';
-               if(!else_if)
-                       formatted += spaces;
+               formatted += spaces;
                (*i)->visit(*this);
+               else_if = 0;
        }
-       indent -= change_indent;
 
        if(block.use_braces)
-               formatted += format("\n%s}", string(indent*2, ' '));
+               formatted += format("\n%s}", string(brace_indent*2, ' '));
 }
 
 void ProgramCompiler::Formatter::visit(Layout &layout)
@@ -345,10 +340,9 @@ void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
 void ProgramCompiler::Formatter::visit(Conditional &cond)
 {
        if(else_if)
-       {
-               formatted += ' ';
-               else_if = false;
-       }
+               formatted.replace(formatted.rfind('\n'), string::npos, 1, ' ');
+
+       indent -= else_if;
 
        formatted += "if(";
        cond.condition->visit(*this);
@@ -357,8 +351,8 @@ void ProgramCompiler::Formatter::visit(Conditional &cond)
        cond.body.visit(*this);
        if(!cond.else_body.body.empty())
        {
-               formatted += format("\n%selse", string(indent*2, ' '));
-               SetFlag set(else_if);
+               formatted += format("\n%selse\n", string(indent*2, ' '));
+               SetForScope<unsigned> set(else_if, 2);
                cond.else_body.visit(*this);
        }
 }
index 1c76d63f8ccb4201da8b6429abfad5f37e636b4b..a2dd9401256a34c7659800765f8cf25ca56a8313 100644 (file)
@@ -27,7 +27,7 @@ private:
                std::string formatted;
                unsigned indent;
                bool parameter_list;
-               bool else_if;
+               unsigned else_if;
                std::string block_interface;
 
                Formatter();