Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Src/CSharpier.Tests/CSharpier.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<SignAssembly>True</SignAssembly>
<!-- DiffEngine references older packages and vulnerabilities on tests isn't a big deal -->
<NuGetAuditMode>direct</NuGetAuditMode>
<LangVersion>13</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DiffEngine" />
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier/DocPrinter/DocFitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
case Region:
return false;
case Concat concat:
for (var i = concat.Contents.Count - 1; i >= 0; i--)
Push(concat.Contents[i], currentMode, currentIndent);
for (var i = concat.Count - 1; i >= 0; i--)
Push(concat[i], currentMode, currentIndent);
break;
case IndentDoc indent:
Push(indent.Contents, currentMode, indenter.IncreaseIndent(currentIndent));
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ private void ProcessNextCommand()
}
else if (doc is Concat concat)
{
for (var x = concat.Contents.Count - 1; x >= 0; x--)
for (var x = concat.Count - 1; x >= 0; x--)
{
this.Push(concat.Contents[x], mode, indent);
this.Push(concat[x], mode, indent);
}
}
else if (doc is IndentDoc indentDoc)
Expand Down
9 changes: 4 additions & 5 deletions Src/CSharpier/DocPrinter/PropagateBreaks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,14 @@ void OnExit(Doc doc)
if (doc is Concat concat)
{
// push onto stack in reverse order so they are processed in the original order
for (var x = concat.Contents.Count - 1; x >= 0; --x)
for (var x = concat.Count - 1; x >= 0; --x)
{
if (forceFlat > 0 && concat.Contents[x] is LineDoc { IsLiteral: false } lineDoc)
if (forceFlat > 0 && concat[x] is LineDoc { IsLiteral: false } lineDoc)
{
concat.Contents[x] =
lineDoc.Type == LineDoc.LineType.Soft ? string.Empty : " ";
concat[x] = lineDoc.Type == LineDoc.LineType.Soft ? string.Empty : " ";
}

docsStack.Push(concat.Contents[x]);
docsStack.Push(concat[x]);
}
}
else if (doc is IfBreak ifBreak)
Expand Down
8 changes: 4 additions & 4 deletions Src/CSharpier/DocSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ void AppendNextIndent()
{
AppendIndent();
result.Append("Doc.Concat(");
if (concat.Contents.Count > 0)
if (concat.Count > 0)
{
result.AppendLine();
}
}

for (var x = 0; x < concat.Contents.Count; x++)
for (var x = 0; x < concat.Count; x++)
{
Serialize(concat.Contents[x], result, skipConcat ? indent : indent + 1);
Serialize(concat[x], result, skipConcat ? indent : indent + 1);

if (x < concat.Contents.Count - 1)
if (x < concat.Count - 1)
{
result.AppendLine(",");
}
Expand Down
74 changes: 74 additions & 0 deletions Src/CSharpier/DocTypes/Concat.WithFourChildren.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace CSharpier.DocTypes;

internal abstract partial class Concat
{
internal sealed class WithFourChildren(Doc child0, Doc child1, Doc child2, Doc child3) : Concat
{
private Doc _child0 = child0;
private Doc _child1 = child1;
private Doc _child2 = child2;
private Doc _child3 = child3;

public override int Count => 4;

public override Doc this[int index]
{
get =>
index switch
{
0 => _child0,
1 => _child1,
2 => _child2,
3 => _child3,
_ => throw new IndexOutOfRangeException(nameof(index)),
};
set
{
switch (index)
{
case 0:
_child0 = value;
break;
case 1:
_child1 = value;
break;
case 2:
_child2 = value;
break;
case 3:
_child3 = value;
break;
default:
throw new IndexOutOfRangeException(nameof(index));
}
}
}

public override void RemoveAt(int index)
{
switch (index)
{
case 0:
_child0 = _child1;
_child1 = _child2;
_child2 = _child3;
_child3 = Doc.Null;
break;
case 1:
_child1 = _child2;
_child2 = _child3;
_child3 = Doc.Null;
break;
case 2:
_child2 = _child3;
_child3 = Doc.Null;
break;
case 3:
_child3 = Doc.Null;
break;
default:
throw new IndexOutOfRangeException(nameof(index));
}
}
}
}
17 changes: 17 additions & 0 deletions Src/CSharpier/DocTypes/Concat.WithManyChildren.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace CSharpier.DocTypes;

internal abstract partial class Concat
{
internal sealed class WithManyChildren(IList<Doc> content) : Concat
{
public override int Count => content.Count;

public override Doc this[int index]
{
get => content[index];
set => content[index] = value;
}

public override void RemoveAt(int index) => content.RemoveAt(index);
}
}
63 changes: 63 additions & 0 deletions Src/CSharpier/DocTypes/Concat.WithThreeChildren.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace CSharpier.DocTypes;

internal abstract partial class Concat
{
internal sealed class WithThreeChildren(Doc child0, Doc child1, Doc child2) : Concat
{
private Doc _child0 = child0;
private Doc _child1 = child1;
private Doc _child2 = child2;

public override int Count => 3;

public override Doc this[int index]
{
get =>
index switch
{
0 => _child0,
1 => _child1,
2 => _child2,
_ => throw new IndexOutOfRangeException(nameof(index)),
};
set
{
switch (index)
{
case 0:
_child0 = value;
break;
case 1:
_child1 = value;
break;
case 2:
_child2 = value;
break;
default:
throw new IndexOutOfRangeException(nameof(index));
}
}
}

public override void RemoveAt(int index)
{
switch (index)
{
case 0:
_child0 = _child1;
_child1 = _child2;
_child2 = Doc.Null;
break;
case 1:
_child1 = _child2;
_child2 = Doc.Null;
break;
case 2:
_child2 = Doc.Null;
break;
default:
throw new IndexOutOfRangeException(nameof(index));
}
}
}
}
52 changes: 52 additions & 0 deletions Src/CSharpier/DocTypes/Concat.WithTwoChildren.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace CSharpier.DocTypes;

internal abstract partial class Concat
{
internal sealed class WithTwoChildren(Doc child0, Doc child1) : Concat
{
private Doc _child0 = child0;
private Doc _child1 = child1;
public override int Count => 2;

public override Doc this[int index]
{
get =>
index switch
{
0 => _child0,
1 => _child1,
_ => throw new IndexOutOfRangeException(nameof(index)),
};
set
{
switch (index)
{
case 0:
_child0 = value;
break;
case 1:
_child1 = value;
break;
default:
throw new IndexOutOfRangeException(nameof(index));
}
}
}

public override void RemoveAt(int index)
{
switch (index)
{
case 0:
_child0 = _child1;
_child1 = Doc.Null;
break;
case 1:
_child1 = Doc.Null;
break;
default:
throw new IndexOutOfRangeException(nameof(index));
}
}
}
}
36 changes: 34 additions & 2 deletions Src/CSharpier/DocTypes/Concat.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
namespace CSharpier.DocTypes;

internal sealed class Concat(IList<Doc> contents) : Doc
internal abstract partial class Concat : Doc
{
public IList<Doc> Contents { get; set; } = contents;
public abstract int Count { get; }

public abstract Doc this[int index] { get; set; }

public abstract void RemoveAt(int index);

public bool Any(Func<Doc, bool> predicate)
{
for (var i = 0; i < Count; i++)
{
if (predicate(this[i]))
{
return true;
}
}

return false;
}

public static Concat Create(IList<Doc> collection) => new WithManyChildren(collection);

public static Doc Create(ReadOnlySpan<Doc> collection)
{
return collection.Length switch
{
0 => Doc.Null,
1 => collection[0],
2 => new WithTwoChildren(collection[0], collection[1]),
3 => new WithThreeChildren(collection[0], collection[1], collection[2]),
4 => new WithFourChildren(collection[0], collection[1], collection[2], collection[3]),
_ => new WithManyChildren(collection.ToArray()),
};
}
}
14 changes: 8 additions & 6 deletions Src/CSharpier/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public static TrailingComment TrailingComment(string comment, CommentType commen
new() { Type = commentType, Comment = comment };

public static Doc Concat(List<Doc> contents) =>
contents.Count == 1 ? contents[0] : new Concat(contents);
contents.Count == 1 ? contents[0] : DocTypes.Concat.Create(contents);

// prevents allocating an array if there is only a single parameter
public static Doc Concat(Doc contents) => contents;

public static Doc Concat(params Doc[] contents) => new Concat(contents);
public static Doc Concat(Doc[] contents) => DocTypes.Concat.Create((IList<Doc>)contents);

public static Doc Concat(params ReadOnlySpan<Doc> contents) => DocTypes.Concat.Create(contents);

public static Doc Join(Doc separator, IEnumerable<Doc> array)
{
Expand Down Expand Up @@ -83,7 +85,7 @@ public static Doc Join(Doc separator, IEnumerable<Doc> array)
public static ForceFlat ForceFlat(List<Doc> contents) =>
new() { Contents = contents.Count == 0 ? contents[0] : Concat(contents) };

public static ForceFlat ForceFlat(params Doc[] contents) =>
public static ForceFlat ForceFlat(params ReadOnlySpan<Doc> contents) =>
new() { Contents = contents.Length == 0 ? contents[0] : Concat(contents) };

public static Group Group(List<Doc> contents) =>
Expand All @@ -104,7 +106,7 @@ public static Group GroupWithId(string groupId, Doc contents)
return group;
}

public static Group GroupWithId(string groupId, params Doc[] contents)
public static Group GroupWithId(string groupId, params ReadOnlySpan<Doc> contents)
{
var group = Group(contents);
group.GroupId = groupId;
Expand All @@ -114,12 +116,12 @@ public static Group GroupWithId(string groupId, params Doc[] contents)
// prevents allocating an array if there is only a single parameter
public static Group Group(Doc contents) => new() { Contents = contents };

public static Group Group(params Doc[] contents) => new() { Contents = Concat(contents) };
public static Group Group(params ReadOnlySpan<Doc> contents) => new() { Contents = Concat(contents) };

// prevents allocating an array if there is only a single parameter
public static IndentDoc Indent(Doc contents) => new() { Contents = contents };

public static IndentDoc Indent(params Doc[] contents) => new() { Contents = Concat(contents) };
public static IndentDoc Indent(params ReadOnlySpan<Doc> contents) => new() { Contents = Concat(contents) };

public static IndentDoc Indent(List<Doc> contents) => new() { Contents = Concat(contents) };

Expand Down
Loading
Loading