From fe845004668049d742fec8ac6c8a3d702b24a7e3 Mon Sep 17 00:00:00 2001 From: belav Date: Mon, 7 Jun 2021 17:13:57 -0500 Subject: [PATCH 1/4] Implementing align doc type + using it for classes closes #276 closes #275 --- Src/CSharpier.Tests/DocPrinterTests.cs | 35 +++++- .../ClassDeclaration/ClassDeclarations.cst | 33 +++-- .../RecordDeclaration/RecordDeclarations.cst | 4 +- Src/CSharpier/DocPrinter/DocFitter.cs | 13 +- Src/CSharpier/DocPrinter/DocPrinter.cs | 13 +- Src/CSharpier/DocPrinter/Indent.cs | 113 ++++++++++++++++-- Src/CSharpier/DocSerializer.cs | 1 + Src/CSharpier/DocTypes/Align.cs | 21 ++++ Src/CSharpier/DocTypes/Doc.cs | 2 + .../SyntaxNodePrinters/BaseList.cs | 12 +- 10 files changed, 218 insertions(+), 29 deletions(-) create mode 100644 Src/CSharpier/DocTypes/Align.cs diff --git a/Src/CSharpier.Tests/DocPrinterTests.cs b/Src/CSharpier.Tests/DocPrinterTests.cs index 3df06b808..632995cf4 100644 --- a/Src/CSharpier.Tests/DocPrinterTests.cs +++ b/Src/CSharpier.Tests/DocPrinterTests.cs @@ -616,6 +616,26 @@ public void Conditional_Group_Does_Not_Propagate_Breaks_To_Parent() PrintedDocShouldBe(doc, "1 2", 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{NewLine} 2"); + } + + [Test] + public void Align_Should_Convert_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{NewLine}\t\t 2", useTabs: true); + } + [Test] public void Scratch() { @@ -627,9 +647,10 @@ private static void PrintedDocShouldBe( Doc doc, string expected, int width = PrinterOptions.WidthUsedByTests, - bool trimInitialLines = false + bool trimInitialLines = false, + bool useTabs = false ) { - var result = Print(doc, width, trimInitialLines); + var result = Print(doc, width, trimInitialLines, useTabs); result.Should().Be(expected); } @@ -637,11 +658,17 @@ private static void PrintedDocShouldBe( private static string Print( Doc doc, int width = PrinterOptions.WidthUsedByTests, - bool trimInitialLines = false + bool trimInitialLines = false, + bool useTabs = false ) { return DocPrinter.DocPrinter.Print( doc, - new PrinterOptions { Width = width, TrimInitialLines = trimInitialLines, }, + new PrinterOptions + { + Width = width, + TrimInitialLines = trimInitialLines, + UseTabs = useTabs + }, Environment.NewLine ) .TrimEnd('\r', '\n'); diff --git a/Src/CSharpier.Tests/TestFiles/ClassDeclaration/ClassDeclarations.cst b/Src/CSharpier.Tests/TestFiles/ClassDeclaration/ClassDeclarations.cst index 135828c0a..d043add19 100644 --- a/Src/CSharpier.Tests/TestFiles/ClassDeclaration/ClassDeclarations.cst +++ b/Src/CSharpier.Tests/TestFiles/ClassDeclaration/ClassDeclarations.cst @@ -6,13 +6,13 @@ class NoModifiers { } public class WithInterface : IInterface { } -public class WithReallyLongNameInterface : - IReallyLongNameLetsMakeThisBreak___________________________ { } +public class WithReallyLongNameInterface + : IReallyLongNameLetsMakeThisBreak___________________________ { } -public class ThisIsSomeLongNameAndItShouldFormatWell1 : - AnotherLongClassName, - AndYetAnotherLongClassName, - AndStillOneMore { } +public class ThisIsSomeLongNameAndItShouldFormatWell1 + : AnotherLongClassName, + AndYetAnotherLongClassName, + AndStillOneMore { } public class SimpleGeneric where T : new() { } @@ -36,9 +36,24 @@ public class LongerClassNameWithLotsOfGenerics< public class SimpleGeneric : BaseClass where T : new() { } -public class ThisIsSomeLongNameAndItShouldFormatWell2 : - AnotherLongClassName, - AnotherClassName +public class ThisIsSomeLongNameAndItShouldFormatWell2 + : AnotherLongClassName, + AnotherClassName where T : new(), AnotherTypeConstraint where T2 : new() where T3 : new() { } + +public class IdentityDbContext + : IdentityDbContext< + TUser, + TRole, + TKey, + IdentityUserClaim, + IdentityUserRole, + IdentityUserLogin, + IdentityRoleClaim, + IdentityUserToken + > + where TUser : IdentityUser + where TRole : IdentityRole + where TKey : IEquatable { } diff --git a/Src/CSharpier.Tests/TestFiles/RecordDeclaration/RecordDeclarations.cst b/Src/CSharpier.Tests/TestFiles/RecordDeclaration/RecordDeclarations.cst index 209e1d82f..2d8466d7e 100644 --- a/Src/CSharpier.Tests/TestFiles/RecordDeclaration/RecordDeclarations.cst +++ b/Src/CSharpier.Tests/TestFiles/RecordDeclaration/RecordDeclarations.cst @@ -17,7 +17,7 @@ record PC(string x) : PrimaryConstructor(x) { } record RecordWithoutBody(string property); -record LongerRecordNameWhatHappens_________________________________________(string x) : - R4(x) { } +record LongerRecordNameWhatHappens_________________________________________(string x) + : R4(x) { } record GenericRecord(T Result); diff --git a/Src/CSharpier/DocPrinter/DocFitter.cs b/Src/CSharpier/DocPrinter/DocFitter.cs index 3488fa2fa..19abce2a1 100644 --- a/Src/CSharpier/DocPrinter/DocFitter.cs +++ b/Src/CSharpier/DocPrinter/DocFitter.cs @@ -83,7 +83,7 @@ void Push(Doc doc, PrintMode printMode, Indent indent) Push( indent.Contents, currentMode, - IndentBuilder.Make(currentIndent, printerOptions) + IndentBuilder.MakeIndent(currentIndent, printerOptions) ); break; case Trim: @@ -146,6 +146,17 @@ is ConditionalGroup conditionalGroup break; case BreakParent: break; + case Align align: + Push( + align.Contents, + currentMode, + IndentBuilder.MakeAlign( + currentIndent, + align.Alignment, + printerOptions + ) + ); + break; default: throw new Exception("Can't handle " + currentDoc.GetType()); } diff --git a/Src/CSharpier/DocPrinter/DocPrinter.cs b/Src/CSharpier/DocPrinter/DocPrinter.cs index 2e7d3d9ab..435635c38 100644 --- a/Src/CSharpier/DocPrinter/DocPrinter.cs +++ b/Src/CSharpier/DocPrinter/DocPrinter.cs @@ -78,7 +78,11 @@ private void ProcessNextCommand() break; } case IndentDoc indentDoc: - Push(indentDoc.Contents, mode, IndentBuilder.Make(indent, PrinterOptions)); + Push( + indentDoc.Contents, + mode, + IndentBuilder.MakeIndent(indent, PrinterOptions) + ); break; case Trim: CurrentWidth -= Output.TrimTrailingWhitespace(); @@ -128,6 +132,13 @@ private void ProcessNextCommand() case ForceFlat forceFlat: Push(forceFlat.Contents, PrintMode.Flat, indent); break; + case Align align: + Push( + align.Contents, + mode, + IndentBuilder.MakeAlign(indent, align.Alignment, PrinterOptions) + ); + break; default: throw new Exception("didn't handle " + doc); } diff --git a/Src/CSharpier/DocPrinter/Indent.cs b/Src/CSharpier/DocPrinter/Indent.cs index 00dcc0b83..aaaa85a33 100644 --- a/Src/CSharpier/DocPrinter/Indent.cs +++ b/Src/CSharpier/DocPrinter/Indent.cs @@ -4,33 +4,130 @@ namespace CSharpier.DocPrinter { - public record Indent(string Value, int Length); + public class Indent + { + public string Value = string.Empty; + public int Length; + public IList? TypesForTabs; + } + + public class IndentType + { + public bool IsAlign { get; set; } + public int AlignWidth { get; set; } + } public static class IndentBuilder { public static Indent MakeRoot() { - return new Indent(string.Empty, 0); + return new(); } - public static Indent Make(Indent indent, PrinterOptions printerOptions) + public static Indent MakeIndent(Indent indent, PrinterOptions printerOptions) { return GenerateIndent(indent, printerOptions); } private static Indent GenerateIndent(Indent indent, PrinterOptions printerOptions) + { + if (!printerOptions.UseTabs) + { + return new Indent + { + Value = indent.Value + new string(' ', printerOptions.TabWidth), + Length = indent.Length + printerOptions.TabWidth + }; + } + + if (indent.TypesForTabs != null) + { + return MakeIndentWithTypesForTabs(indent, new IndentType(), printerOptions); + } + + return new Indent + { + Value = indent.Value + "\t", + Length = indent.Length + printerOptions.TabWidth + }; + } + + public static Indent MakeAlign(Indent indent, int alignment, PrinterOptions printerOptions) { if (printerOptions.UseTabs) { - return new Indent(indent.Value + "\t", indent.Length + printerOptions.TabWidth); + return MakeIndentWithTypesForTabs( + indent, + new IndentType { IsAlign = true, AlignWidth = alignment }, + printerOptions + ); } else { - return new Indent( - indent.Value + new string(' ', printerOptions.TabWidth), - indent.Length + printerOptions.TabWidth - ); + return new Indent + { + Value = indent.Value + new string(' ', 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 static Indent MakeIndentWithTypesForTabs( + Indent indent, + IndentType nextIndentType, + PrinterOptions printerOptions + ) { + List types; + + // if it doesn't exist yet, then all values on it are regular indents, not aligns + if (indent.TypesForTabs == null) + { + types = new List(); + for (var x = 0; x < indent.Value.Length; x++) + { + types.Add(new IndentType()); + } } + else + { + var placeTab = false; + types = new List(indent.TypesForTabs); + for (var x = types.Count - 1; x >= 0; x--) + { + if (!types[x].IsAlign) + { + placeTab = true; + } + + if (placeTab) + { + types[x] = new IndentType(); + } + } + } + + types.Add(nextIndentType); + + var length = 0; + var value = new StringBuilder(); + foreach (var indentType in types) + { + if (indentType.IsAlign) + { + value.Append(' ', indentType.AlignWidth); + length += indentType.AlignWidth; + } + else + { + value.Append('\t'); + length += printerOptions.TabWidth; + } + } + + return new Indent { Length = length, Value = value.ToString(), TypesForTabs = types }; } } } diff --git a/Src/CSharpier/DocSerializer.cs b/Src/CSharpier/DocSerializer.cs index d21ed59ad..691482a0a 100644 --- a/Src/CSharpier/DocSerializer.cs +++ b/Src/CSharpier/DocSerializer.cs @@ -61,6 +61,7 @@ string PrintConcat(Concat concatToPrint) LineDoc lineDoc => indent + (lineDoc.Type == LineDoc.LineType.Normal ? "Doc.Line" : "Doc.SoftLine"), BreakParent => "", + Align align => $"{indent}Doc.Align({align.Alignment}, {PrintIndentedDocTree(align.Contents)})", Trim => $"{indent}Doc.Trim", ForceFlat forceFlat => $"{indent}Doc.ForceFlat({newLine}{PrintIndentedDocTree(forceFlat.Contents)})", IndentDoc indentDoc => $"{indent}Doc.Indent({newLine}{PrintIndentedDocTree(indentDoc.Contents)}{newLine}{indent})", diff --git a/Src/CSharpier/DocTypes/Align.cs b/Src/CSharpier/DocTypes/Align.cs new file mode 100644 index 000000000..d8126af22 --- /dev/null +++ b/Src/CSharpier/DocTypes/Align.cs @@ -0,0 +1,21 @@ +using System; + +namespace CSharpier.DocTypes +{ + public class Align : Doc + { + public int Alignment { get; } + public Doc Contents { get; } + + public Align(int alignment, Doc contents) + { + if (alignment < 1) + { + throw new Exception($"{nameof(alignment)} must be >= 1"); + } + + this.Alignment = alignment; + this.Contents = contents; + } + } +} diff --git a/Src/CSharpier/DocTypes/Doc.cs b/Src/CSharpier/DocTypes/Doc.cs index 13f754a9f..5f6b57e41 100644 --- a/Src/CSharpier/DocTypes/Doc.cs +++ b/Src/CSharpier/DocTypes/Doc.cs @@ -107,6 +107,8 @@ public static IndentIfBreak IndentIfBreak(Doc contents, string groupId) => public static Doc Directive(string value) => new StringDoc(value, true); public static ConditionalGroup ConditionalGroup(params Doc[] options) => new(options); + + public static Align Align(int alignment, Doc contents) => new(alignment, contents); } public enum CommentType diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseList.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseList.cs index 9dfc1ca41..b6ff0639d 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseList.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseList.cs @@ -7,11 +7,15 @@ public static class BaseList { public static Doc Print(BaseListSyntax node) { - return Doc.Concat( - " ", - Token.Print(node.ColonToken), + return Doc.Group( Doc.Indent( - Doc.Group(Doc.Line, SeparatedSyntaxList.Print(node.Types, Node.Print, Doc.Line)) + Doc.Line, + Token.Print(node.ColonToken), + " ", + Doc.Align( + 2, + Doc.Concat(SeparatedSyntaxList.Print(node.Types, Node.Print, Doc.Line)) + ) ) ); } From 18ae797289fa8146cda0715164e7190bce7e3f8c Mon Sep 17 00:00:00 2001 From: belav Date: Wed, 9 Jun 2021 19:05:20 -0500 Subject: [PATCH 2/4] Some refactoring --- Src/CSharpier/DocPrinter/DocFitter.cs | 31 +++++---- Src/CSharpier/DocPrinter/DocPrinter.cs | 24 +++---- Src/CSharpier/DocPrinter/Indent.cs | 94 ++++++++++++++------------ Src/CSharpier/DocSerializer.cs | 2 +- Src/CSharpier/DocTypes/Align.cs | 10 +-- 5 files changed, 80 insertions(+), 81 deletions(-) diff --git a/Src/CSharpier/DocPrinter/DocFitter.cs b/Src/CSharpier/DocPrinter/DocFitter.cs index 19abce2a1..45e549924 100644 --- a/Src/CSharpier/DocPrinter/DocFitter.cs +++ b/Src/CSharpier/DocPrinter/DocFitter.cs @@ -7,14 +7,21 @@ namespace CSharpier.DocPrinter { - public static class DocFitter + public class DocFitter { - public static bool Fits( + protected readonly Dictionary GroupModeMap; + protected readonly Indenter Indenter; + + public DocFitter(Dictionary groupModeMap, Indenter indenter) + { + GroupModeMap = groupModeMap; + Indenter = indenter; + } + + public bool Fits( PrintCommand nextCommand, Stack remainingCommands, - int remainingWidth, - PrinterOptions printerOptions, - Dictionary groupModeMap + int remainingWidth ) { var returnFalseIfMoreStringsFound = false; var newCommands = new Stack(); @@ -83,7 +90,7 @@ void Push(Doc doc, PrintMode printMode, Indent indent) Push( indent.Contents, currentMode, - IndentBuilder.MakeIndent(currentIndent, printerOptions) + Indenter.IncreaseIndent(currentIndent) ); break; case Trim: @@ -102,13 +109,13 @@ is ConditionalGroup conditionalGroup if (group.GroupId != null) { - groupModeMap![group.GroupId] = groupMode; + GroupModeMap![group.GroupId] = groupMode; } break; case IfBreak ifBreak: var ifBreakMode = ifBreak.GroupId != null - && groupModeMap!.ContainsKey(ifBreak.GroupId) - ? groupModeMap[ifBreak.GroupId] + && GroupModeMap!.ContainsKey(ifBreak.GroupId) + ? GroupModeMap[ifBreak.GroupId] : currentMode; var contents = ifBreakMode == PrintMode.Break @@ -150,11 +157,7 @@ is ConditionalGroup conditionalGroup Push( align.Contents, currentMode, - IndentBuilder.MakeAlign( - currentIndent, - align.Alignment, - printerOptions - ) + Indenter.AddAlign(currentIndent, align.Width) ); break; default: diff --git a/Src/CSharpier/DocPrinter/DocPrinter.cs b/Src/CSharpier/DocPrinter/DocPrinter.cs index 435635c38..12c712401 100644 --- a/Src/CSharpier/DocPrinter/DocPrinter.cs +++ b/Src/CSharpier/DocPrinter/DocPrinter.cs @@ -18,14 +18,16 @@ public class DocPrinter protected bool SkipNextNewLine; protected readonly string EndOfLine; protected readonly PrinterOptions PrinterOptions; + protected readonly DocFitter DocFitter; + protected readonly Indenter Indenter; protected DocPrinter(Doc doc, PrinterOptions printerOptions, string endOfLine) { EndOfLine = endOfLine; PrinterOptions = printerOptions; - RemainingCommands.Push( - new PrintCommand(IndentBuilder.MakeRoot(), PrintMode.Break, doc) - ); + Indenter = new Indenter(printerOptions); + DocFitter = new DocFitter(this.GroupModeMap, Indenter); + RemainingCommands.Push(new PrintCommand(Indenter.GenerateRoot(), PrintMode.Break, doc)); } public static string Print(Doc document, PrinterOptions printerOptions, string endOfLine) @@ -78,11 +80,7 @@ private void ProcessNextCommand() break; } case IndentDoc indentDoc: - Push( - indentDoc.Contents, - mode, - IndentBuilder.MakeIndent(indent, PrinterOptions) - ); + Push(indentDoc.Contents, mode, Indenter.IncreaseIndent(indent)); break; case Trim: CurrentWidth -= Output.TrimTrailingWhitespace(); @@ -133,11 +131,7 @@ private void ProcessNextCommand() Push(forceFlat.Contents, PrintMode.Flat, indent); break; case Align align: - Push( - align.Contents, - mode, - IndentBuilder.MakeAlign(indent, align.Alignment, PrinterOptions) - ); + Push(align.Contents, mode, Indenter.AddAlign(indent, align.Width)); break; default: throw new Exception("didn't handle " + doc); @@ -281,9 +275,7 @@ private bool Fits(PrintCommand possibleCommand) return DocFitter.Fits( possibleCommand, RemainingCommands, - PrinterOptions.Width - CurrentWidth, - PrinterOptions, - GroupModeMap + PrinterOptions.Width - CurrentWidth ); } diff --git a/Src/CSharpier/DocPrinter/Indent.cs b/Src/CSharpier/DocPrinter/Indent.cs index aaaa85a33..a40558d98 100644 --- a/Src/CSharpier/DocPrinter/Indent.cs +++ b/Src/CSharpier/DocPrinter/Indent.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Text; @@ -8,59 +7,67 @@ public class Indent { public string Value = string.Empty; public int Length; - public IList? TypesForTabs; + public IList? TypesForTabs; } - public class IndentType + public interface IIndentType { } + + public class IndentType : IIndentType + { + protected IndentType() { } + + public static IndentType Instance = new(); + } + + public class AlignType : IIndentType { - public bool IsAlign { get; set; } - public int AlignWidth { get; set; } + public int Width { get; init; } } - public static class IndentBuilder + public class Indenter { - public static Indent MakeRoot() + protected readonly PrinterOptions PrinterOptions; + + public Indenter(PrinterOptions printerOptions) { - return new(); + PrinterOptions = printerOptions; } - public static Indent MakeIndent(Indent indent, PrinterOptions printerOptions) + public Indent GenerateRoot() { - return GenerateIndent(indent, printerOptions); + return new(); } - private static Indent GenerateIndent(Indent indent, PrinterOptions printerOptions) + public Indent IncreaseIndent(Indent indent) { - if (!printerOptions.UseTabs) + if (PrinterOptions.UseTabs) { + if (indent.TypesForTabs != null) + { + return MakeIndentWithTypesForTabs(indent, IndentType.Instance); + } + return new Indent { - Value = indent.Value + new string(' ', printerOptions.TabWidth), - Length = indent.Length + printerOptions.TabWidth + Value = indent.Value + "\t", + Length = indent.Length + PrinterOptions.TabWidth }; } - - if (indent.TypesForTabs != null) + else { - return MakeIndentWithTypesForTabs(indent, new IndentType(), printerOptions); + return new Indent + { + Value = indent.Value + new string(' ', PrinterOptions.TabWidth), + Length = indent.Length + PrinterOptions.TabWidth + }; } - - return new Indent - { - Value = indent.Value + "\t", - Length = indent.Length + printerOptions.TabWidth - }; } - public static Indent MakeAlign(Indent indent, int alignment, PrinterOptions printerOptions) + public Indent AddAlign(Indent indent, int alignment) { - if (printerOptions.UseTabs) + if (PrinterOptions.UseTabs) { - return MakeIndentWithTypesForTabs( - indent, - new IndentType { IsAlign = true, AlignWidth = alignment }, - printerOptions - ); + return MakeIndentWithTypesForTabs(indent, new AlignType { Width = alignment }); } else { @@ -75,36 +82,33 @@ public static Indent MakeAlign(Indent indent, int alignment, PrinterOptions prin // 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 static Indent MakeIndentWithTypesForTabs( - Indent indent, - IndentType nextIndentType, - PrinterOptions printerOptions - ) { - List types; + private Indent MakeIndentWithTypesForTabs(Indent indent, IIndentType nextIndentType) + { + List types; // if it doesn't exist yet, then all values on it are regular indents, not aligns if (indent.TypesForTabs == null) { - types = new List(); + types = new List(); for (var x = 0; x < indent.Value.Length; x++) { - types.Add(new IndentType()); + types.Add(IndentType.Instance); } } else { var placeTab = false; - types = new List(indent.TypesForTabs); + types = new List(indent.TypesForTabs); for (var x = types.Count - 1; x >= 0; x--) { - if (!types[x].IsAlign) + if (types[x] == IndentType.Instance) { placeTab = true; } if (placeTab) { - types[x] = new IndentType(); + types[x] = IndentType.Instance; } } } @@ -115,15 +119,15 @@ PrinterOptions printerOptions var value = new StringBuilder(); foreach (var indentType in types) { - if (indentType.IsAlign) + if (indentType is AlignType alignType) { - value.Append(' ', indentType.AlignWidth); - length += indentType.AlignWidth; + value.Append(' ', alignType.Width); + length += alignType.Width; } else { value.Append('\t'); - length += printerOptions.TabWidth; + length += PrinterOptions.TabWidth; } } diff --git a/Src/CSharpier/DocSerializer.cs b/Src/CSharpier/DocSerializer.cs index 691482a0a..bfa480364 100644 --- a/Src/CSharpier/DocSerializer.cs +++ b/Src/CSharpier/DocSerializer.cs @@ -61,7 +61,7 @@ string PrintConcat(Concat concatToPrint) LineDoc lineDoc => indent + (lineDoc.Type == LineDoc.LineType.Normal ? "Doc.Line" : "Doc.SoftLine"), BreakParent => "", - Align align => $"{indent}Doc.Align({align.Alignment}, {PrintIndentedDocTree(align.Contents)})", + Align align => $"{indent}Doc.Align({align.Width}, {PrintIndentedDocTree(align.Contents)})", Trim => $"{indent}Doc.Trim", ForceFlat forceFlat => $"{indent}Doc.ForceFlat({newLine}{PrintIndentedDocTree(forceFlat.Contents)})", IndentDoc indentDoc => $"{indent}Doc.Indent({newLine}{PrintIndentedDocTree(indentDoc.Contents)}{newLine}{indent})", diff --git a/Src/CSharpier/DocTypes/Align.cs b/Src/CSharpier/DocTypes/Align.cs index d8126af22..8ff486138 100644 --- a/Src/CSharpier/DocTypes/Align.cs +++ b/Src/CSharpier/DocTypes/Align.cs @@ -4,17 +4,17 @@ namespace CSharpier.DocTypes { public class Align : Doc { - public int Alignment { get; } + public int Width { get; } public Doc Contents { get; } - public Align(int alignment, Doc contents) + public Align(int width, Doc contents) { - if (alignment < 1) + if (width < 1) { - throw new Exception($"{nameof(alignment)} must be >= 1"); + throw new Exception($"{nameof(width)} must be >= 1"); } - this.Alignment = alignment; + this.Width = width; this.Contents = contents; } } From f85e9466690737c3267ea74a9937e9b8be5b1227 Mon Sep 17 00:00:00 2001 From: belav Date: Wed, 9 Jun 2021 19:32:04 -0500 Subject: [PATCH 3/4] a little more refactoring(defactoring?) --- Src/CSharpier/DocPrinter/DocFitter.cs | 27 ++++++++++---------------- Src/CSharpier/DocPrinter/DocPrinter.cs | 6 +++--- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/Src/CSharpier/DocPrinter/DocFitter.cs b/Src/CSharpier/DocPrinter/DocFitter.cs index 45e549924..43653dbed 100644 --- a/Src/CSharpier/DocPrinter/DocFitter.cs +++ b/Src/CSharpier/DocPrinter/DocFitter.cs @@ -7,21 +7,14 @@ namespace CSharpier.DocPrinter { - public class DocFitter + public static class DocFitter { - protected readonly Dictionary GroupModeMap; - protected readonly Indenter Indenter; - - public DocFitter(Dictionary groupModeMap, Indenter indenter) - { - GroupModeMap = groupModeMap; - Indenter = indenter; - } - - public bool Fits( + public static bool Fits( PrintCommand nextCommand, Stack remainingCommands, - int remainingWidth + int remainingWidth, + Dictionary groupModeMap, + Indenter indenter ) { var returnFalseIfMoreStringsFound = false; var newCommands = new Stack(); @@ -90,7 +83,7 @@ void Push(Doc doc, PrintMode printMode, Indent indent) Push( indent.Contents, currentMode, - Indenter.IncreaseIndent(currentIndent) + indenter.IncreaseIndent(currentIndent) ); break; case Trim: @@ -109,13 +102,13 @@ is ConditionalGroup conditionalGroup if (group.GroupId != null) { - GroupModeMap![group.GroupId] = groupMode; + groupModeMap![group.GroupId] = groupMode; } break; case IfBreak ifBreak: var ifBreakMode = ifBreak.GroupId != null - && GroupModeMap!.ContainsKey(ifBreak.GroupId) - ? GroupModeMap[ifBreak.GroupId] + && groupModeMap!.ContainsKey(ifBreak.GroupId) + ? groupModeMap[ifBreak.GroupId] : currentMode; var contents = ifBreakMode == PrintMode.Break @@ -157,7 +150,7 @@ is ConditionalGroup conditionalGroup Push( align.Contents, currentMode, - Indenter.AddAlign(currentIndent, align.Width) + indenter.AddAlign(currentIndent, align.Width) ); break; default: diff --git a/Src/CSharpier/DocPrinter/DocPrinter.cs b/Src/CSharpier/DocPrinter/DocPrinter.cs index 12c712401..5bb50670e 100644 --- a/Src/CSharpier/DocPrinter/DocPrinter.cs +++ b/Src/CSharpier/DocPrinter/DocPrinter.cs @@ -18,7 +18,6 @@ public class DocPrinter protected bool SkipNextNewLine; protected readonly string EndOfLine; protected readonly PrinterOptions PrinterOptions; - protected readonly DocFitter DocFitter; protected readonly Indenter Indenter; protected DocPrinter(Doc doc, PrinterOptions printerOptions, string endOfLine) @@ -26,7 +25,6 @@ protected DocPrinter(Doc doc, PrinterOptions printerOptions, string endOfLine) EndOfLine = endOfLine; PrinterOptions = printerOptions; Indenter = new Indenter(printerOptions); - DocFitter = new DocFitter(this.GroupModeMap, Indenter); RemainingCommands.Push(new PrintCommand(Indenter.GenerateRoot(), PrintMode.Break, doc)); } @@ -275,7 +273,9 @@ private bool Fits(PrintCommand possibleCommand) return DocFitter.Fits( possibleCommand, RemainingCommands, - PrinterOptions.Width - CurrentWidth + PrinterOptions.Width - CurrentWidth, + GroupModeMap, + Indenter ); } From a40c683786a7524f0e4e7b2bd2d7bc0d12177938 Mon Sep 17 00:00:00 2001 From: belav Date: Wed, 9 Jun 2021 19:51:00 -0500 Subject: [PATCH 4/4] A little more refactoring --- Src/CSharpier.Tests/DocPrinterTests.cs | 2 +- Src/CSharpier/DocTypes/Align.cs | 2 +- Src/CSharpier/DocTypes/Doc.cs | 2 +- Src/CSharpier/DocTypes/StringDoc.cs | 4 ++-- Src/CSharpier/DocTypes/Trim.cs | 1 - 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Src/CSharpier.Tests/DocPrinterTests.cs b/Src/CSharpier.Tests/DocPrinterTests.cs index 632995cf4..d43a368da 100644 --- a/Src/CSharpier.Tests/DocPrinterTests.cs +++ b/Src/CSharpier.Tests/DocPrinterTests.cs @@ -624,7 +624,7 @@ public void Align_Should_Print_Basic_Case() } [Test] - public void Align_Should_Convert_Spaces_To_Tabs() + public void Align_Should_Convert_Non_Trailing_Spaces_To_Tabs() { var doc = Doc.Concat( "+ ", diff --git a/Src/CSharpier/DocTypes/Align.cs b/Src/CSharpier/DocTypes/Align.cs index 8ff486138..881ddc4ea 100644 --- a/Src/CSharpier/DocTypes/Align.cs +++ b/Src/CSharpier/DocTypes/Align.cs @@ -2,7 +2,7 @@ namespace CSharpier.DocTypes { - public class Align : Doc + public class Align : Doc, IHasContents { public int Width { get; } public Doc Contents { get; } diff --git a/Src/CSharpier/DocTypes/Doc.cs b/Src/CSharpier/DocTypes/Doc.cs index 5f6b57e41..50983d74f 100644 --- a/Src/CSharpier/DocTypes/Doc.cs +++ b/Src/CSharpier/DocTypes/Doc.cs @@ -119,6 +119,6 @@ public enum CommentType interface IHasContents { - Doc Contents { get; set; } + Doc Contents { get; } } } diff --git a/Src/CSharpier/DocTypes/StringDoc.cs b/Src/CSharpier/DocTypes/StringDoc.cs index 9a0dbeb3c..c77ef23f7 100644 --- a/Src/CSharpier/DocTypes/StringDoc.cs +++ b/Src/CSharpier/DocTypes/StringDoc.cs @@ -2,8 +2,8 @@ namespace CSharpier.DocTypes { public class StringDoc : Doc { - public string Value { get; set; } - public bool IsDirective { get; set; } + public string Value { get; } + public bool IsDirective { get; } public StringDoc(string value, bool isDirective = false) { diff --git a/Src/CSharpier/DocTypes/Trim.cs b/Src/CSharpier/DocTypes/Trim.cs index 9c0136079..67b0015a2 100644 --- a/Src/CSharpier/DocTypes/Trim.cs +++ b/Src/CSharpier/DocTypes/Trim.cs @@ -2,6 +2,5 @@ namespace CSharpier.DocTypes { public class Trim : Doc { - public Trim() { } } }