Skip to content

Commit

Permalink
refactor: Use switches instead of if-else in DocFitter (#1080)
Browse files Browse the repository at this point in the history
* refactor: use switch instead of chained if-else in DocFitter

In a chained if-else the debugger in Rider stops on a condition of each branch, but in a switch statement the debugger jumps right to the executed branch and it speed up debugging

* refactor: Use switch expression instead of if-else

It speeds up debugging because the debugger doesn't stops on the curly braces of the branches

* style: Remove unnecessary braces in DocFitter

---------

Co-authored-by: Lasath Fernando <[email protected]>
  • Loading branch information
Rudomitori and shocklateboy92 authored Dec 20, 2023
1 parent 43ba8d7 commit 60ae841
Showing 1 changed file with 48 additions and 67 deletions.
115 changes: 48 additions & 67 deletions Src/CSharpier/DocPrinter/DocFitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,64 +30,46 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
return false;
}

PrintCommand command;
if (newCommands.Count > 0)
var (currentIndent, currentMode, currentDoc) = newCommands switch
{
command = newCommands.Pop();
}
else
{
command = remainingCommands.ElementAt(x);
x++;
}
{ Count: > 0 } => newCommands.Pop(),
_ => remainingCommands.ElementAt(x++)
};

var (currentIndent, currentMode, currentDoc) = command;

if (currentDoc is StringDoc stringDoc)
switch (currentDoc)
{
// directives should not be considered when calculating if something fits
if (stringDoc.Value == null || stringDoc.IsDirective)
{
continue;
}

if (returnFalseIfMoreStringsFound)
{
return false;
}

output.Append(stringDoc.Value);
remainingWidth -= stringDoc.Value.GetPrintedWidth();
}
else if (currentDoc != Doc.Null)
{
if (currentDoc is LeadingComment or TrailingComment)
{
case NullDoc:
break;
case StringDoc stringDoc:
// directives should not be considered when calculating if something fits
if (stringDoc.Value == null || stringDoc.IsDirective)
continue;

if (returnFalseIfMoreStringsFound)
return false;

output.Append(stringDoc.Value);
remainingWidth -= stringDoc.Value.GetPrintedWidth();
break;
case LeadingComment
or TrailingComment:
if (output.Length > 0 && currentMode is not PrintMode.ForceFlat)
{
returnFalseIfMoreStringsFound = true;
}
}
else if (currentDoc is Region)
{

break;
case Region:
return false;
}
else if (currentDoc is Concat concat)
{
case Concat concat:
for (var i = concat.Contents.Count - 1; i >= 0; i--)
{
Push(concat.Contents[i], currentMode, currentIndent);
}
}
else if (currentDoc is IndentDoc indent)
{
break;
case IndentDoc indent:
Push(indent.Contents, currentMode, indenter.IncreaseIndent(currentIndent));
}
else if (currentDoc is Trim)
{
break;
case Trim:
remainingWidth += output.TrimTrailingWhitespace();
}
else if (currentDoc is Group group)
break;
case Group group:
{
var groupMode = group.Break ? PrintMode.Break : currentMode;

Expand All @@ -102,8 +84,10 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
{
groupModeMap![group.GroupId] = groupMode;
}

break;
}
else if (currentDoc is IfBreak ifBreak)
case IfBreak ifBreak:
{
var ifBreakMode =
ifBreak.GroupId != null && groupModeMap!.ContainsKey(ifBreak.GroupId)
Expand All @@ -116,9 +100,9 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
: ifBreak.FlatContents;

Push(contents, currentMode, currentIndent);
break;
}
else if (currentDoc is LineDoc line)
{
case LineDoc line:
if (currentMode is PrintMode.Flat or PrintMode.ForceFlat)
{
if (currentDoc is HardLine { SkipBreakIfFirstInGroup: true })
Expand All @@ -136,30 +120,27 @@ void Push(Doc doc, PrintMode printMode, Indent indent)

remainingWidth -= 1;
}

break;
}
else
{
return true;
}
}
else if (currentDoc is ForceFlat flat)
{

return true;
case ForceFlat flat:
Push(flat.Contents, PrintMode.ForceFlat, currentIndent);
}
else if (currentDoc is BreakParent) { }
else if (currentDoc is Align align)
{
break;
case BreakParent:
break;
case Align align:
Push(
align.Contents,
currentMode,
indenter.AddAlign(currentIndent, align.Width)
);
}
else if (currentDoc is AlwaysFits) { }
else
{
break;
case AlwaysFits:
break;
default:
throw new Exception("Can't handle " + currentDoc.GetType());
}
}
}

Expand Down

0 comments on commit 60ae841

Please sign in to comment.