Skip to content

Commit

Permalink
Removing the Align doc type. It wasn't used and added unnecessary com… (
Browse files Browse the repository at this point in the history
#1516)

…plexity to the Indent logic.
  • Loading branch information
belav authored Feb 28, 2025
1 parent e292c4f commit f359fbd
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 227 deletions.
60 changes: 0 additions & 60 deletions Src/CSharpier.Tests/DocPrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,66 +712,6 @@ public void Conditional_Group_Prints_Initial_Group_If_It_Fit()
PrintedDocShouldBe(doc, "(1)(1)", 10);
}

[Test]
public void Align_Should_Print_Basic_Case()
{
var doc = Doc.Concat("+ ", Doc.Align(2, Doc.Group("1", Doc.HardLine, "2")));
PrintedDocShouldBe(doc, "+ 1\n 2");
}

[Test]
public void Align_Should_Convert_Non_Trailing_Spaces_To_Tabs()
{
var doc = Doc.Concat(
"+ ",
Doc.Align(
2,
Doc.Indent(Doc.Concat("+ ", Doc.Align(2, Doc.Group("1", Doc.HardLine, "2"))))
)
);
PrintedDocShouldBe(doc, "+ + 1\n\t\t 2", useTabs: true);
}

[Test]
public void Align_Should_Merge()
{
var doc = Doc.Group(
Doc.Concat("return firstCondition"),
Doc.Align(
2,
Doc.HardLine,
Doc.Concat("? "),
Doc.Align(2, "firstValue"),
Doc.HardLine,
Doc.Concat(": "),
Doc.Align(
2,
"secondCondition",
Doc.Align(
2,
Doc.Line,
Doc.Concat("? "),
Doc.Align(2, "secondValue"),
Doc.Line,
Doc.Concat(": "),
Doc.Align(2, "thirdCondition;")
)
)
)
);
PrintedDocShouldBe(
doc,
$"""
return firstCondition
? firstValue
: secondCondition
{'\t'} ? secondValue
{'\t'} : thirdCondition;
""",
useTabs: true
);
}

[TestCase(0, "\n")]
[TestCase(0, "\r\n")]
[TestCase(1, "\n")]
Expand Down
17 changes: 0 additions & 17 deletions Src/CSharpier.Tests/DocSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,6 @@ public void Should_Print_ConditionalGroup()
);
}

[Test]
public void Should_Print_Align()
{
var doc = Doc.Align(2, Doc.Null, Doc.Null);

var actual = DocSerializer.Serialize(doc);

ActualShouldBe(
actual,
@"Doc.Align(
2,
Doc.Null,
Doc.Null
)"
);
}

[Test]
public void Should_Print_ForceFlat()
{
Expand Down
7 changes: 0 additions & 7 deletions Src/CSharpier/DocPrinter/DocFitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
break;
case BreakParent:
break;
case Align align:
Push(
align.Contents,
currentMode,
indenter.AddAlign(currentIndent, align.Width)
);
break;
case AlwaysFits:
break;
default:
Expand Down
4 changes: 0 additions & 4 deletions Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ private void ProcessNextCommand()
{
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 if (doc is Region region)
{
if (region.IsEnd)
Expand Down
110 changes: 0 additions & 110 deletions Src/CSharpier/DocPrinter/Indent.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
using System.Diagnostics;
using System.Text;

namespace CSharpier.DocPrinter;

internal class Indent
{
public string Value = string.Empty;
public int Length;
public IList<IIndentType>? TypesForTabs;
}

internal interface IIndentType { }

internal class IndentType : IIndentType
{
protected IndentType() { }

public static IndentType Instance = new();
}

internal class AlignType : IIndentType
{
public int Width { get; init; }
}

internal class Indenter(PrinterOptions printerOptions)
Expand All @@ -36,11 +18,6 @@ public static Indent GenerateRoot()

public Indent IncreaseIndent(Indent indent)
{
if (indent.TypesForTabs != null && this.PrinterOptions.UseTabs)
{
return this.MakeIndentWithTypesForTabs(indent, IndentType.Instance);
}

if (IncreaseIndentCache.TryGetValue(indent.Value, out var increasedIndent))
{
return increasedIndent;
Expand All @@ -61,91 +38,4 @@ public Indent IncreaseIndent(Indent indent)
IncreaseIndentCache[indent.Value] = nextIndent;
return nextIndent;
}

public Indent AddAlign(Indent indent, int alignment)
{
if (this.PrinterOptions.UseTabs)
{
return this.MakeIndentWithTypesForTabs(indent, new AlignType { Width = alignment });
}
else
{
return new Indent
{
Value = indent.Value.PadRight(indent.Value.Length + alignment),
Length = indent.Length + alignment,
};
}
}

// when using tabs we need to sometimes replace the spaces from align with tabs
// trailing aligns stay as spaces, but any aligns before a tab get converted to a single tab
// see https://github.com/prettier/prettier/blob/main/commands.md#align
private Indent MakeIndentWithTypesForTabs(Indent indent, IIndentType nextIndentType)
{
List<IIndentType> types;

// if it doesn't exist yet, then all values on it are regular indents, not aligns
if (indent.TypesForTabs == null)
{
types = [];
for (var x = 0; x < indent.Value.Length; x++)
{
types.Add(IndentType.Instance);
}

types.Add(nextIndentType);
}
else
{
var placeTab = false;
types = [.. indent.TypesForTabs, nextIndentType];
for (var x = types.Count - 1; x >= 0; x--)
{
if (types[x] == IndentType.Instance)
{
placeTab = true;
}

if (placeTab)
{
types[x] = IndentType.Instance;
}
}

// merge back to back aligns into tabs
for (var x = 0; x < types.Count - 1; x++)
{
if (types[x] is AlignType && types[x + 1] is AlignType)
{
types[x] = IndentType.Instance;
types.RemoveAt(x + 1);
x -= 1;
}
}
}

var length = 0;
var value = new StringBuilder();
foreach (var indentType in types)
{
if (indentType is AlignType alignType)
{
value.Append(' ', alignType.Width);
length += alignType.Width;
}
else
{
value.Append('\t');
length += this.PrinterOptions.IndentSize;
}
}

return new Indent
{
Length = length,
Value = value.ToString(),
TypesForTabs = types,
};
}
}
5 changes: 0 additions & 5 deletions Src/CSharpier/DocSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ void AppendNextIndent()
CultureInfo.InvariantCulture,
$"Doc.{(doc is IndentDoc ? "Indent" : doc.GetType().Name)}("
);
if (doc is Align align)
{
AppendNextIndent();
result.AppendLine(align.Width + ",");
}

Serialize(hasContents.Contents, result, indent + 1, doc);
result.AppendLine();
Expand Down
18 changes: 0 additions & 18 deletions Src/CSharpier/DocTypes/Align.cs

This file was deleted.

6 changes: 0 additions & 6 deletions Src/CSharpier/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@ public static IndentIfBreak IndentIfBreak(Doc contents, string groupId) =>

public static ConditionalGroup ConditionalGroup(params Doc[] options) => new(options);

// prevents allocating an array if there is only a single parameter
public static Align Align(int alignment, Doc contents) => new(alignment, contents);

public static Align Align(int alignment, params Doc[] contents) =>
new(alignment, Concat(contents));

public static AlwaysFits AlwaysFits(Doc printedTrivia)
{
return new AlwaysFits(printedTrivia);
Expand Down

0 comments on commit f359fbd

Please sign in to comment.