From: Mikko Rasa Date: Sun, 13 Nov 2016 22:35:55 +0000 (+0200) Subject: Fix formatting issues with else ifs X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=a048354f5be808c97d92504f9e618f6f5558ce1e;p=libs%2Fgl.git Fix formatting issues with else ifs --- diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index fdbc83c7..2428ab79 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -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 set(indent, indent+!formatted.empty()); string spaces(indent*2, ' '); for(list >::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 set(else_if, 2); cond.else_body.visit(*this); } } diff --git a/source/programcompiler.h b/source/programcompiler.h index 1c76d63f..a2dd9401 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -27,7 +27,7 @@ private: std::string formatted; unsigned indent; bool parameter_list; - bool else_if; + unsigned else_if; std::string block_interface; Formatter();