From 60ae841ac590db1f101ad16ac4b549a596ee2f68 Mon Sep 17 00:00:00 2001 From: Dmitry Zhavoronkov Date: Wed, 20 Dec 2023 16:50:24 +0700 Subject: [PATCH] refactor: Use switches instead of if-else in DocFitter (#1080) * 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 --- Src/CSharpier/DocPrinter/DocFitter.cs | 115 +++++++++++--------------- 1 file changed, 48 insertions(+), 67 deletions(-) diff --git a/Src/CSharpier/DocPrinter/DocFitter.cs b/Src/CSharpier/DocPrinter/DocFitter.cs index 59f6ea166..3ff8f8cb5 100644 --- a/Src/CSharpier/DocPrinter/DocFitter.cs +++ b/Src/CSharpier/DocPrinter/DocFitter.cs @@ -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; @@ -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) @@ -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 }) @@ -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()); - } } }