From 93001ade7f42fb239c0b9ad2430357bc341fef86 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 21 Nov 2022 11:27:29 -0600 Subject: [PATCH] Potential fix for indentation issue closes #415 --- Src/CSharpier/DocPrinter/PropagateBreaks.cs | 8 +++++++- Src/CSharpier/DocTypes/Align.cs | 5 +++++ Src/CSharpier/DocTypes/BreakParent.cs | 8 +++++++- Src/CSharpier/DocTypes/Concat.cs | 5 +++++ Src/CSharpier/DocTypes/Doc.cs | 2 ++ Src/CSharpier/DocTypes/ForceFlat.cs | 7 ++++++- Src/CSharpier/DocTypes/Group.cs | 5 +++++ Src/CSharpier/DocTypes/IfBreak.cs | 11 +++++++--- Src/CSharpier/DocTypes/IndentDoc.cs | 5 +++++ Src/CSharpier/DocTypes/LeadingComment.cs | 5 +++++ Src/CSharpier/DocTypes/LineDoc.cs | 5 +++++ Src/CSharpier/DocTypes/NullDoc.cs | 5 +++++ Src/CSharpier/DocTypes/StringDoc.cs | 5 +++++ Src/CSharpier/DocTypes/TrailingComment.cs | 4 ++++ Src/CSharpier/DocTypes/Trim.cs | 8 +++++++- .../InvocationExpression.cs | 20 ++++++++++++++++++- 16 files changed, 100 insertions(+), 8 deletions(-) diff --git a/Src/CSharpier/DocPrinter/PropagateBreaks.cs b/Src/CSharpier/DocPrinter/PropagateBreaks.cs index 92e26ef5f..bc0b8198b 100644 --- a/Src/CSharpier/DocPrinter/PropagateBreaks.cs +++ b/Src/CSharpier/DocPrinter/PropagateBreaks.cs @@ -2,7 +2,13 @@ namespace CSharpier.DocPrinter; internal static class PropagateBreaks { - private class MarkerDoc : Doc { } + private class MarkerDoc : Doc + { + public override bool ContainsDirective() + { + return false; + } + } private static readonly MarkerDoc TraverseDocOnExitStackMarker = new(); diff --git a/Src/CSharpier/DocTypes/Align.cs b/Src/CSharpier/DocTypes/Align.cs index b019f1abd..4ca65c683 100644 --- a/Src/CSharpier/DocTypes/Align.cs +++ b/Src/CSharpier/DocTypes/Align.cs @@ -15,4 +15,9 @@ public Align(int width, Doc contents) this.Width = width; this.Contents = contents; } + + public override bool ContainsDirective() + { + return this.Contents.ContainsDirective(); + } } diff --git a/Src/CSharpier/DocTypes/BreakParent.cs b/Src/CSharpier/DocTypes/BreakParent.cs index 69aab1ff5..e830a019e 100644 --- a/Src/CSharpier/DocTypes/BreakParent.cs +++ b/Src/CSharpier/DocTypes/BreakParent.cs @@ -1,5 +1,11 @@ namespace CSharpier.DocTypes; -internal class BreakParent : Doc, IBreakParent { } +internal class BreakParent : Doc, IBreakParent +{ + public override bool ContainsDirective() + { + return false; + } +} internal interface IBreakParent { } diff --git a/Src/CSharpier/DocTypes/Concat.cs b/Src/CSharpier/DocTypes/Concat.cs index 9ee46c3ba..a2ac9ef5e 100644 --- a/Src/CSharpier/DocTypes/Concat.cs +++ b/Src/CSharpier/DocTypes/Concat.cs @@ -8,4 +8,9 @@ public Concat(IList contents) { this.Contents = contents; } + + public override bool ContainsDirective() + { + return this.Contents.Any(o => o.ContainsDirective()); + } } diff --git a/Src/CSharpier/DocTypes/Doc.cs b/Src/CSharpier/DocTypes/Doc.cs index 7916bd46c..2b1820ff0 100644 --- a/Src/CSharpier/DocTypes/Doc.cs +++ b/Src/CSharpier/DocTypes/Doc.cs @@ -139,6 +139,8 @@ public static IndentIfBreak IndentIfBreak(Doc contents, string groupId) => public static Align Align(int alignment, params Doc[] contents) => new(alignment, Concat(contents)); + + public abstract bool ContainsDirective(); } internal enum CommentType diff --git a/Src/CSharpier/DocTypes/ForceFlat.cs b/Src/CSharpier/DocTypes/ForceFlat.cs index 572877c2f..50763b866 100644 --- a/Src/CSharpier/DocTypes/ForceFlat.cs +++ b/Src/CSharpier/DocTypes/ForceFlat.cs @@ -2,5 +2,10 @@ namespace CSharpier.DocTypes; internal class ForceFlat : Doc, IHasContents { - public Doc Contents { get; set; } = Null; + public Doc Contents { get; init; } = Null; + + public override bool ContainsDirective() + { + return this.Contents.ContainsDirective(); + } } diff --git a/Src/CSharpier/DocTypes/Group.cs b/Src/CSharpier/DocTypes/Group.cs index d0bce5c17..f9cb34889 100644 --- a/Src/CSharpier/DocTypes/Group.cs +++ b/Src/CSharpier/DocTypes/Group.cs @@ -5,4 +5,9 @@ internal class Group : Doc, IHasContents public Doc Contents { get; set; } = Null; public bool Break { get; set; } public string? GroupId { get; set; } + + public override bool ContainsDirective() + { + return this.Contents.ContainsDirective(); + } } diff --git a/Src/CSharpier/DocTypes/IfBreak.cs b/Src/CSharpier/DocTypes/IfBreak.cs index 175a90a49..4ed5b2fb2 100644 --- a/Src/CSharpier/DocTypes/IfBreak.cs +++ b/Src/CSharpier/DocTypes/IfBreak.cs @@ -2,9 +2,14 @@ namespace CSharpier.DocTypes; internal class IfBreak : Doc { - public Doc FlatContents { get; set; } = Null; - public Doc BreakContents { get; set; } = Null; - public string? GroupId { get; set; } + public Doc FlatContents { get; init; } = Null; + public Doc BreakContents { get; init; } = Null; + public string? GroupId { get; init; } + + public override bool ContainsDirective() + { + return this.FlatContents.ContainsDirective() || this.BreakContents.ContainsDirective(); + } } internal class IndentIfBreak : IfBreak diff --git a/Src/CSharpier/DocTypes/IndentDoc.cs b/Src/CSharpier/DocTypes/IndentDoc.cs index 7979aa1bb..52c4f3acb 100644 --- a/Src/CSharpier/DocTypes/IndentDoc.cs +++ b/Src/CSharpier/DocTypes/IndentDoc.cs @@ -3,4 +3,9 @@ namespace CSharpier.DocTypes; internal class IndentDoc : Doc, IHasContents { public Doc Contents { get; set; } = Null; + + public override bool ContainsDirective() + { + return this.Contents.ContainsDirective(); + } } diff --git a/Src/CSharpier/DocTypes/LeadingComment.cs b/Src/CSharpier/DocTypes/LeadingComment.cs index 6ffd6b434..3fda15d0b 100644 --- a/Src/CSharpier/DocTypes/LeadingComment.cs +++ b/Src/CSharpier/DocTypes/LeadingComment.cs @@ -4,4 +4,9 @@ internal class LeadingComment : Doc { public CommentType Type { get; init; } public string Comment { get; init; } = string.Empty; + + public override bool ContainsDirective() + { + return false; + } } diff --git a/Src/CSharpier/DocTypes/LineDoc.cs b/Src/CSharpier/DocTypes/LineDoc.cs index 47eed4754..1e268af3a 100644 --- a/Src/CSharpier/DocTypes/LineDoc.cs +++ b/Src/CSharpier/DocTypes/LineDoc.cs @@ -12,4 +12,9 @@ public enum LineType public LineType Type { get; set; } public bool IsLiteral { get; set; } public bool Squash { get; set; } + + public override bool ContainsDirective() + { + return false; + } } diff --git a/Src/CSharpier/DocTypes/NullDoc.cs b/Src/CSharpier/DocTypes/NullDoc.cs index 4ca379c35..9805cdfd3 100644 --- a/Src/CSharpier/DocTypes/NullDoc.cs +++ b/Src/CSharpier/DocTypes/NullDoc.cs @@ -5,4 +5,9 @@ internal class NullDoc : Doc public static NullDoc Instance { get; } = new(); private NullDoc() { } + + public override bool ContainsDirective() + { + return false; + } } diff --git a/Src/CSharpier/DocTypes/StringDoc.cs b/Src/CSharpier/DocTypes/StringDoc.cs index 80cb08844..36f8d3be0 100644 --- a/Src/CSharpier/DocTypes/StringDoc.cs +++ b/Src/CSharpier/DocTypes/StringDoc.cs @@ -10,4 +10,9 @@ public StringDoc(string value, bool isDirective = false) this.Value = value; this.IsDirective = isDirective; } + + public override bool ContainsDirective() + { + return this.IsDirective; + } } diff --git a/Src/CSharpier/DocTypes/TrailingComment.cs b/Src/CSharpier/DocTypes/TrailingComment.cs index fd163bb01..0a21567fe 100644 --- a/Src/CSharpier/DocTypes/TrailingComment.cs +++ b/Src/CSharpier/DocTypes/TrailingComment.cs @@ -4,4 +4,8 @@ internal class TrailingComment : Doc { public CommentType Type { get; set; } public string Comment { get; set; } = string.Empty; + public override bool ContainsDirective() + { + return false; + } } diff --git a/Src/CSharpier/DocTypes/Trim.cs b/Src/CSharpier/DocTypes/Trim.cs index d4871eb3d..2d5cc4055 100644 --- a/Src/CSharpier/DocTypes/Trim.cs +++ b/Src/CSharpier/DocTypes/Trim.cs @@ -1,3 +1,9 @@ namespace CSharpier.DocTypes; -internal class Trim : Doc { } +internal class Trim : Doc +{ + public override bool ContainsDirective() + { + return false; + } +} diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs index 72cf8e4f9..2f6dd1060 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs @@ -32,8 +32,26 @@ public static Doc PrintMemberChain(ExpressionSyntax node, FormattingContext cont var cutoff = shouldMergeFirstTwoGroups ? 3 : 2; + var hasDirective = printedNodes.Any(o => o.Doc.ContainsDirective()); + + /* + this kinda fixes it, but we still leave this empty line and the semicolon does not indent + also curious how it performs + + var query = _context.Products +#if OLD_FROM_SQL + .FromSql(sql) +#else + .FromSqlRaw(sql) +#endif + + ; + + */ + var forceOneLine = - groups.Count <= cutoff + !hasDirective + && groups.Count <= cutoff && ( groups .Skip(shouldMergeFirstTwoGroups ? 1 : 0)