Skip to content

Commit

Permalink
ditch ugly switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Jan 30, 2023
1 parent 6ec3ae6 commit d90fb8a
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 153 deletions.
167 changes: 86 additions & 81 deletions Src/CSharpier/DocPrinter/DocFitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,94 +61,99 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
}
else if (currentDoc != Doc.Null)
{
switch (currentDoc)
if (currentDoc is LeadingComment or TrailingComment)
{
case LeadingComment:
case TrailingComment:
if (output.Length > 0 && currentMode is not PrintMode.ForceFlat)
{
returnFalseIfMoreStringsFound = true;
}
break;
case Concat concat:
for (var i = concat.Contents.Count - 1; i >= 0; i--)
{
Push(concat.Contents[i], currentMode, currentIndent);
}
break;
case IndentDoc indent:
Push(indent.Contents, currentMode, indenter.IncreaseIndent(currentIndent));
break;
case Trim:
remainingWidth += output.TrimTrailingWhitespace();
break;
case Group group:
var groupMode = group.Break ? PrintMode.Break : currentMode;

// when determining if something fits, use the last option from a conditionalGroup, which should be the most expanded one
var groupContents =
groupMode == PrintMode.Break
&& group is ConditionalGroup conditionalGroup
? conditionalGroup.Options.Last()
: group.Contents;
Push(groupContents, groupMode, currentIndent);

if (group.GroupId != null)
if (output.Length > 0 && currentMode is not PrintMode.ForceFlat)
{
returnFalseIfMoreStringsFound = true;
}
}
else if (currentDoc is Concat concat)
{
for (var i = concat.Contents.Count - 1; i >= 0; i--)
{
Push(concat.Contents[i], currentMode, currentIndent);
}
}
else if (currentDoc is IndentDoc indent)
{
Push(indent.Contents, currentMode, indenter.IncreaseIndent(currentIndent));
}
else if (currentDoc is Trim)
{
remainingWidth += output.TrimTrailingWhitespace();
}
else if (currentDoc is Group group)
{
var groupMode = group.Break ? PrintMode.Break : currentMode;

// when determining if something fits, use the last option from a conditionalGroup, which should be the most expanded one
var groupContents =
groupMode == PrintMode.Break && group is ConditionalGroup conditionalGroup
? conditionalGroup.Options.Last()
: group.Contents;
Push(groupContents, groupMode, currentIndent);

if (group.GroupId != null)
{
groupModeMap![group.GroupId] = groupMode;
}
}
else if (currentDoc is IfBreak ifBreak)
{
var ifBreakMode =
ifBreak.GroupId != null && groupModeMap!.ContainsKey(ifBreak.GroupId)
? groupModeMap[ifBreak.GroupId]
: currentMode;

var contents =
ifBreakMode == PrintMode.Break
? ifBreak.BreakContents
: ifBreak.FlatContents;

Push(contents, currentMode, currentIndent);
}
else if (currentDoc is LineDoc line)
{
if (currentMode is PrintMode.Flat or PrintMode.ForceFlat)
{
if (currentDoc is HardLine { SkipBreakIfFirstInGroup: true })
{
groupModeMap![group.GroupId] = groupMode;
returnFalseIfMoreStringsFound = false;
}
break;
case IfBreak ifBreak:
var ifBreakMode =
ifBreak.GroupId != null && groupModeMap!.ContainsKey(ifBreak.GroupId)
? groupModeMap[ifBreak.GroupId]
: currentMode;

var contents =
ifBreakMode == PrintMode.Break
? ifBreak.BreakContents
: ifBreak.FlatContents;

Push(contents, currentMode, currentIndent);
break;
case LineDoc line:
if (currentMode is PrintMode.Flat or PrintMode.ForceFlat)
else if (line.Type == LineDoc.LineType.Hard)
{
if (currentDoc is HardLine { SkipBreakIfFirstInGroup: true })
{
returnFalseIfMoreStringsFound = false;
}
else if (line.Type == LineDoc.LineType.Hard)
{
return true;
}

if (line.Type != LineDoc.LineType.Soft)
{
output.Append(' ');

remainingWidth -= 1;
}
return true;
}
else

if (line.Type != LineDoc.LineType.Soft)
{
return true;
output.Append(' ');

remainingWidth -= 1;
}
break;
case ForceFlat flat:
Push(flat.Contents, PrintMode.ForceFlat, currentIndent);
break;
case BreakParent:
break;
case Align align:
Push(
align.Contents,
currentMode,
indenter.AddAlign(currentIndent, align.Width)
);
break;
default:
throw new Exception("Can't handle " + currentDoc.GetType());
}
else
{
return true;
}
}
else if (currentDoc is ForceFlat flat)
{
Push(flat.Contents, PrintMode.ForceFlat, currentIndent);
}
else if (currentDoc is BreakParent) { }
else if (currentDoc is Align align)
{
Push(
align.Contents,
currentMode,
indenter.AddAlign(currentIndent, align.Width)
);
}
else
{
throw new Exception("Can't handle " + currentDoc.GetType());
}
}
}
Expand Down
141 changes: 69 additions & 72 deletions Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,89 +74,86 @@ private void ProcessNextCommand()
return;
}

switch (doc)
if (doc is StringDoc stringDoc)
{
case StringDoc stringDoc:
this.ProcessString(stringDoc, indent);
break;
case Concat concat:
this.ProcessString(stringDoc, indent);
}
else if (doc is Concat concat)
{
for (var x = concat.Contents.Count - 1; x >= 0; x--)
{
for (var x = concat.Contents.Count - 1; x >= 0; x--)
{
this.Push(concat.Contents[x], mode, indent);
}
break;
this.Push(concat.Contents[x], mode, indent);
}
case IndentDoc indentDoc:
this.Push(indentDoc.Contents, mode, this.Indenter.IncreaseIndent(indent));
break;
case Trim:
this.CurrentWidth -= this.Output.TrimTrailingWhitespace();
this.NewLineNextStringValue = false;
break;
case Group group:
this.ProcessGroup(@group, mode, indent);
break;
case IfBreak ifBreak:
}
else if (doc is IndentDoc indentDoc)
{
this.Push(indentDoc.Contents, mode, this.Indenter.IncreaseIndent(indent));
}
else if (doc is Trim)
{
this.CurrentWidth -= this.Output.TrimTrailingWhitespace();
this.NewLineNextStringValue = false;
}
else if (doc is Group group)
{
this.ProcessGroup(group, mode, indent);
}
else if (doc is IfBreak ifBreak)
{
var groupMode = mode;
if (ifBreak.GroupId != null)
{
var groupMode = mode;
if (ifBreak.GroupId != null)
if (!this.GroupModeMap.TryGetValue(ifBreak.GroupId, out groupMode))
{
if (!this.GroupModeMap.TryGetValue(ifBreak.GroupId, out groupMode))
{
throw new Exception(
"You cannot use an ifBreak before the group it targets."
);
}
throw new Exception("You cannot use an ifBreak before the group it targets.");
}

var contents =
groupMode == PrintMode.Break ? ifBreak.BreakContents : ifBreak.FlatContents;
this.Push(contents, mode, indent);
break;
}
case LineDoc line:
this.ProcessLine(line, mode, indent);
break;
case BreakParent:
break;
case LeadingComment leadingComment:

var contents =
groupMode == PrintMode.Break ? ifBreak.BreakContents : ifBreak.FlatContents;
this.Push(contents, mode, indent);
}
else if (doc is LineDoc line)
{
this.ProcessLine(line, mode, indent);
}
else if (doc is BreakParent) { }
else if (doc is LeadingComment leadingComment)
{
this.Output.TrimTrailingWhitespace();
if ((this.Output.Length != 0 && this.Output[^1] != '\n') || this.NewLineNextStringValue)
{
this.Output.TrimTrailingWhitespace();
if (
(this.Output.Length != 0 && this.Output[^1] != '\n')
|| this.NewLineNextStringValue
)
{
this.Output.Append(this.EndOfLine);
}
this.Output.Append(this.EndOfLine);
}

this.AppendComment(leadingComment, indent);
this.AppendComment(leadingComment, indent);

this.CurrentWidth = indent.Length;
this.NewLineNextStringValue = false;
this.SkipNextNewLine = false;
break;
this.CurrentWidth = indent.Length;
this.NewLineNextStringValue = false;
this.SkipNextNewLine = false;
}
else if (doc is TrailingComment trailingComment)
{
this.Output.TrimTrailingWhitespace();
this.Output.Append(' ').Append(trailingComment.Comment);
this.CurrentWidth = indent.Length;
if (mode != PrintMode.ForceFlat)
{
this.NewLineNextStringValue = true;
this.SkipNextNewLine = true;
}
case TrailingComment trailingComment:
this.Output.TrimTrailingWhitespace();
this.Output.Append(' ').Append(trailingComment.Comment);
this.CurrentWidth = indent.Length;
if (mode != PrintMode.ForceFlat)
{
this.NewLineNextStringValue = true;
this.SkipNextNewLine = true;
}

break;
case ForceFlat forceFlat:
this.Push(forceFlat.Contents, PrintMode.ForceFlat, indent);
break;
case Align align:
this.Push(align.Contents, mode, this.Indenter.AddAlign(indent, align.Width));
break;
default:
throw new Exception("didn't handle " + doc);
}
else if (doc is ForceFlat forceFlat)
{
this.Push(forceFlat.Contents, PrintMode.ForceFlat, indent);
}
else if (doc is Align align)
{
this.Push(align.Contents, mode, this.Indenter.AddAlign(indent, align.Width));
}
else
{
throw new Exception("didn't handle " + doc);
}
}

Expand Down

0 comments on commit d90fb8a

Please sign in to comment.