Skip to content

Commit

Permalink
Better Indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Weinert committed Oct 26, 2021
1 parent 51cf7d4 commit d9a43e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
35 changes: 26 additions & 9 deletions src/Razor2Liquid/CodeTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ private void WriteIf(IfStatementSyntax ifSyntax, bool code = true)

{
_context.Liquid.AppendLine("");
_context.Liquid.AddIndent(_context.Inner.Count);
StartCode();
_context.Inner.Push("if");
_context.Liquid.Append("if ");
Expand Down Expand Up @@ -369,8 +368,7 @@ void WriteElseClause(ElseClauseSyntax elseClause)
if (elseClause.Statement is IfStatementSyntax ifStatement)
{
_context.Liquid.AppendLine("");
_context.Liquid.AddIndent(_context.Inner.Count - 1);
StartCode();
StartCode(elseClause);
_context.Liquid.Append("elsif ");
WriteIf(ifStatement, false);
statement = true;
Expand All @@ -380,8 +378,7 @@ void WriteElseClause(ElseClauseSyntax elseClause)
else
{
_context.Liquid.AppendLine("");
_context.Liquid.AddIndent(_context.Inner.Count - 1);
StartCode();
StartCode(elseClause);
_context.Liquid.Append("else");
EndCode();
_context.Liquid.AppendLine("");
Expand All @@ -403,11 +400,11 @@ private void WriteForEach(ForEachStatementSyntax node)
_context.Liquid.Append(" in ");
TransformExpression(node.Expression);
EndCode();
_context.Inner.Push("for");
_context.Liquid.AppendLine();
TransformCSharpSyntax(node.Statement);
_context.Liquid.AppendLine();
_context.Hint = oldHint;
_context.Inner.Push("for");
}

private void HandleLocalDeclaration(LocalDeclarationStatementSyntax node)
Expand All @@ -431,16 +428,35 @@ private void StartBars()
_context.BarsCounter++;
}

private void StartCode()
private void StartCode(CSharpSyntaxNode expressionSyntax = null)
{
if (_context.CodeCounter == 0 && _context.CodeCounter == 0)
{
var indent = _context.Inner.Count;
if (SumIndent(expressionSyntax))
{
indent--;
}
_context.Liquid.AddIndent(indent);
_context.Model.Liquid.Append("{% ");
}

_context.CodeCounter++;
}

private bool SumIndent(CSharpSyntaxNode expressionSyntax)
{

if (expressionSyntax is ElseClauseSyntax )
{
return true;
}

return false;


}

private void EndBars()
{
if (_context.Hint == ReadingHint.Expression)
Expand Down Expand Up @@ -499,11 +515,12 @@ void WriteAsComment(string value, Type getType = null, [CallerMemberName] string
void WriteAssignmentExpression(AssignmentExpressionSyntax ae)
{
_context.Liquid.AppendLine();
_context.Liquid.Append("{% assign ");
StartCode(ae);
_context.Liquid.Append("assign ");
TransformExpression(ae.Left);
_context.Liquid.Append(" = ");
TransformExpression(ae.Right);
_context.Liquid.Append(" %}");
EndCode();
}

bool ShouldAsComment(SyntaxNode node)
Expand Down
8 changes: 4 additions & 4 deletions tests/RazorLiquid.Tests/ControlFlowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void If_assign()
{% assign priceWidthInPercent = 40 %}
{% if course.IsBundleItem %}
{% assign priceWidthInPercent = 30 %}
{% assign priceWidthInPercent = 30 %}
<ding>{{ priceWidthInPercent }}</ding>
{% endif %}
Expand Down Expand Up @@ -117,11 +117,11 @@ public void IfThenElse()
var expected = @"
{% assign deliveryOptionLabelLocalizationId = """" %}
{% if course.IsDigital %}
{% assign deliveryOptionLabelLocalizationId = LocalizationKeys.OrderConfirmationEmail.DeliveryOption1Label_Text %}
{% assign deliveryOptionLabelLocalizationId = LocalizationKeys.OrderConfirmationEmail.DeliveryOption1Label_Text %}
{% else %}
{% assign deliveryOptionLabelLocalizationId = LocalizationKeys.OrderConfirmationEmail.DeliveryOption2Label_Text %}
{% assign deliveryOptionLabelLocalizationId = LocalizationKeys.OrderConfirmationEmail.DeliveryOption2Label_Text %}
{% endif %}
";

Expand All @@ -142,7 +142,7 @@ public void SimpleForEachWithAnAssign()
}";
var expected = @"
{% for course in Model.CurrentCart.SortedCourseItems %}
{% assign a = course %}
{% assign a = course %}
{% endfor %}
";
Expand Down

0 comments on commit d9a43e4

Please sign in to comment.