Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix directive indentation issue #761

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 7 additions & 1 deletion Src/CSharpier/DocPrinter/PropagateBreaks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/Align.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public Align(int width, Doc contents)
this.Width = width;
this.Contents = contents;
}

public override bool ContainsDirective()
{
return this.Contents.ContainsDirective();
}
}
8 changes: 7 additions & 1 deletion Src/CSharpier/DocTypes/BreakParent.cs
Original file line number Diff line number Diff line change
@@ -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 { }
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/Concat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public Concat(IList<Doc> contents)
{
this.Contents = contents;
}

public override bool ContainsDirective()
{
return this.Contents.Any(o => o.ContainsDirective());
}
}
2 changes: 2 additions & 0 deletions Src/CSharpier/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion Src/CSharpier/DocTypes/ForceFlat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
11 changes: 8 additions & 3 deletions Src/CSharpier/DocTypes/IfBreak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/IndentDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/LeadingComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/LineDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/NullDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ internal class NullDoc : Doc
public static NullDoc Instance { get; } = new();

private NullDoc() { }

public override bool ContainsDirective()
{
return false;
}
}
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/StringDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public StringDoc(string value, bool isDirective = false)
this.Value = value;
this.IsDirective = isDirective;
}

public override bool ContainsDirective()
{
return this.IsDirective;
}
}
4 changes: 4 additions & 0 deletions Src/CSharpier/DocTypes/TrailingComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 7 additions & 1 deletion Src/CSharpier/DocTypes/Trim.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
namespace CSharpier.DocTypes;

internal class Trim : Doc { }
internal class Trim : Doc
{
public override bool ContainsDirective()
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down