Skip to content

Commit

Permalink
Setting to remove braces from single-line blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Jun 29, 2024
1 parent 79d6eb1 commit e0becae
Show file tree
Hide file tree
Showing 47 changed files with 865 additions and 35 deletions.
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/IASTNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public interface IASTNode<T>
/// Prints this node using the provided printer.
/// </summary>
public void Print(ASTPrinter printer);

/// <summary>
/// Calculates and returns whether the node will require multiple lines when printed.
/// </summary>
public bool RequiresMultipleLines(ASTPrinter printer);
}
12 changes: 12 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/ArrayInitNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public void Print(ASTPrinter printer)
printer.Write(']');
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
for (int i = 0; i < Elements.Count; i++)
{
if (Elements[i].RequiresMultipleLines(printer))
{
return true;
}
}
return false;
}

public IExpressionNode ResolveMacroType(ASTCleaner cleaner, IMacroType type)
{
if (type is IMacroTypeArrayInit typeArrayInit)
Expand Down
9 changes: 8 additions & 1 deletion Underanalyzer/Decompiler/AST/Nodes/AssetReferenceNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Underanalyzer.Decompiler.AST;
using System.Xml.Linq;

namespace Underanalyzer.Decompiler.AST;

/// <summary>
/// Represents an asset reference in the AST.
Expand Down Expand Up @@ -33,6 +35,11 @@ public IExpressionNode Clean(ASTCleaner cleaner)
return this;
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}

public void Print(ASTPrinter printer)
{
string assetName = printer.Context.GameContext.GetAssetName(AssetType, AssetId);
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/AssignNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,9 @@ public void Print(ASTPrinter printer)
break;
}
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return Variable.RequiresMultipleLines(printer) || Value.RequiresMultipleLines(printer);
}
}
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/BinaryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public void Print(ASTPrinter printer)
}
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return Left.RequiresMultipleLines(printer) || Right.RequiresMultipleLines(printer);
}

public IExpressionNode ResolveMacroType(ASTCleaner cleaner, IMacroType type)
{
bool didAnything = false;
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/BlockLocalVarDeclNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public void Print(ASTPrinter printer)
}
}
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}
}
39 changes: 39 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/BlockNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,45 @@ public void Print(ASTPrinter printer)
printer.PopFragmentContext();
}

public void PrintSingleLine(ASTPrinter printer)
{
printer.PushFragmentContext(FragmentContext);

printer.EndLine();
printer.Indent();
printer.StartLine();
if (Children.Count != 1)
{
throw new DecompilerException("Expected only one child node when printing on single line");
}
Children[0].Print(printer);
if (Children[0].SemicolonAfter)
{
printer.Semicolon();
}
printer.Dedent();

printer.PopFragmentContext();
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
// If we have more than one child node, or zero child nodes, we need multiple lines
if (Children.Count > 1 || Children.Count == 0)
{
return true;
}

// If our single child needs multiple lines, so do we
if (Children[0].RequiresMultipleLines(printer))
{
return true;
}

// Other basic cases: all switch statements, and all struct initializations
return PartOfSwitch || printer.StructArguments is not null;
}

/// <summary>
/// Adds a block-level local variable declaration node to the top of this block.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/BooleanNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public void Print(ASTPrinter printer)
printer.Write(Value ? "true" : "false");
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}

public IExpressionNode ResolveMacroType(ASTCleaner cleaner, IMacroType type)
{
if (type is IMacroTypeConditional conditional)
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/BreakNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public void Print(ASTPrinter printer)
{
printer.Write("break");
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}
}
7 changes: 7 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/ConditionalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public void Print(ASTPrinter printer)
}
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return Condition.RequiresMultipleLines(printer) ||
True.RequiresMultipleLines(printer) ||
False.RequiresMultipleLines(printer);
}

public IExpressionNode ResolveMacroType(ASTCleaner cleaner, IMacroType type)
{
if (type is IMacroTypeConditional conditional)
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/ContinueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public void Print(ASTPrinter printer)
{
printer.Write("continue");
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}
}
28 changes: 21 additions & 7 deletions Underanalyzer/Decompiler/AST/Nodes/DoUntilLoopNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,34 @@ public IStatementNode Clean(ASTCleaner cleaner)
public void Print(ASTPrinter printer)
{
printer.Write("do");
if (printer.Context.Settings.OpenBlockBraceOnSameLine)
if (printer.Context.Settings.RemoveSingleLineBlockBraces && !Body.RequiresMultipleLines(printer))
{
printer.Write(' ');
Body.Print(printer);
printer.Write(' ');
Body.PrintSingleLine(printer);
printer.EndLine();
printer.StartLine();
}
else
{
Body.Print(printer);
printer.EndLine();
printer.StartLine();
if (printer.Context.Settings.OpenBlockBraceOnSameLine)
{
printer.Write(' ');
Body.Print(printer);
printer.Write(' ');
}
else
{
Body.Print(printer);
printer.EndLine();
printer.StartLine();
}
}
printer.Write("until (");
Condition.Print(printer);
printer.Write(')');
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return true;
}
}
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/DoubleNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public void Print(ASTPrinter printer)
printer.Write(resultStr);
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}

public IExpressionNode ResolveMacroType(ASTCleaner cleaner, IMacroType type)
{
if (type is IMacroTypeConditional conditional)
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/EnumDeclNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public void Print(ASTPrinter printer)
printer.CloseBlock();
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return true;
}

/// <summary>
/// Generates enum declarations for the given context, returning a modified AST.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/EnumValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public void Print(ASTPrinter printer)
}
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}

public IExpressionNode ResolveMacroType(ASTCleaner cleaner, IMacroType type)
{
if (type is IMacroTypeInt64 type64)
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/ExitNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public void Print(ASTPrinter printer)
// TODO: check if we're inside of a function (or script in GMS2) and use "return" instead
printer.Write("exit");
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}
}
18 changes: 15 additions & 3 deletions Underanalyzer/Decompiler/AST/Nodes/ForLoopNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,22 @@ public void Print(ASTPrinter printer)
}
printer.Write(')');

if (printer.Context.Settings.OpenBlockBraceOnSameLine)
if (printer.Context.Settings.RemoveSingleLineBlockBraces && !Body.RequiresMultipleLines(printer))
{
printer.Write(' ');
Body.PrintSingleLine(printer);
}
Body.Print(printer);
else
{
if (printer.Context.Settings.OpenBlockBraceOnSameLine)
{
printer.Write(' ');
}
Body.Print(printer);
}
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return true;
}
}
12 changes: 12 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/FunctionCallNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ public void Print(ASTPrinter printer)
printer.Write(')');
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
foreach (IExpressionNode arg in Arguments)
{
if (arg.RequiresMultipleLines(printer))
{
return true;
}
}
return false;
}

public IMacroType GetExpressionMacroType(ASTCleaner cleaner)
{
return cleaner.GlobalMacroResolver.ResolveReturnValueType(cleaner, Function.Name.Content);
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/FunctionDeclNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ public void Print(ASTPrinter printer)
Body.Print(printer);
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return true;
}

public IExpressionNode ResolveMacroType(ASTCleaner cleaner, IMacroType type)
{
if (type is IMacroTypeConditional conditional)
Expand Down
5 changes: 5 additions & 0 deletions Underanalyzer/Decompiler/AST/Nodes/FunctionReferenceNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public void Print(ASTPrinter printer)
printer.Write(printer.LookupFunction(Function));
}

public bool RequiresMultipleLines(ASTPrinter printer)
{
return false;
}

public IExpressionNode ResolveMacroType(ASTCleaner cleaner, IMacroType type)
{
if (type is IMacroTypeConditional conditional)
Expand Down
Loading

0 comments on commit e0becae

Please sign in to comment.