Skip to content

Commit

Permalink
Adding support for csharpier-ignore-start in object initializers (#1345)
Browse files Browse the repository at this point in the history
closes #1342
  • Loading branch information
belav committed Sep 15, 2024
1 parent 8bca3ce commit 3fd7faa
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,53 @@ public class ClassName
var unformatted = true;
var unformatted = true;
}

void ObjectInitialize()
{
return new SomeClass
{
// csharpier-ignore-start
SomeProperty = someValue
// csharpier-ignore-end
};

return new SomeClass
{
SomeProperty1 = 1,

// csharpier-ignore-start
SomeProperty2 = 2,
// csharpier-ignore-end
SomeProperty3 = 3,
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,
SomeProperty2 = someValue
// csharpier-ignore-end
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,

SomeProperty2 = someValue
// csharpier-ignore-end
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,


SomeProperty2 = someValue
// csharpier-ignore-end
};
}
}

public class ClassName2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,52 @@ public class ClassName
var unformatted = true;
var unformatted = true;
}

void ObjectInitialize()
{
return new SomeClass
{
// csharpier-ignore-start
SomeProperty = someValue
// csharpier-ignore-end
};

return new SomeClass
{
SomeProperty1 = 1,
// csharpier-ignore-start
SomeProperty2 = 2,
// csharpier-ignore-end
SomeProperty3 = 3,
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,
SomeProperty2 = someValue
// csharpier-ignore-end
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,

SomeProperty2 = someValue
// csharpier-ignore-end
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,


SomeProperty2 = someValue
// csharpier-ignore-end
};
}
}

public class ClassName2
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static List<Doc> Print<T>(
{
result.Add(Doc.HardLine);
}
;

var unFormattedCode = new StringBuilder();
var printUnformatted = false;
var lastMemberForcedBlankLine = false;
Expand Down
37 changes: 37 additions & 0 deletions Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace CSharpier.SyntaxPrinter;

using System.Text;

internal static class SeparatedSyntaxList
{
public static Doc Print<T>(
Expand Down Expand Up @@ -40,8 +42,38 @@ private static Doc Print<T>(
where T : SyntaxNode
{
var docs = new List<Doc>();
var unFormattedCode = new StringBuilder();
var printUnformatted = false;
for (var x = startingIndex; x < list.Count; x++)
{
var member = list[x];

if (Token.HasLeadingCommentMatching(member, CSharpierIgnore.IgnoreEndRegex))
{
docs.Add(unFormattedCode.ToString().Trim());
unFormattedCode.Clear();
printUnformatted = false;
}
else if (Token.HasLeadingCommentMatching(member, CSharpierIgnore.IgnoreStartRegex))
{
if (!printUnformatted && x > 0)
{
docs.Add(Doc.HardLine);
}
printUnformatted = true;
}

if (printUnformatted)
{
unFormattedCode.Append(CSharpierIgnore.PrintWithoutFormatting(member, context));
if (x < list.SeparatorCount)
{
unFormattedCode.AppendLine(list.GetSeparator(x).Text);
}

continue;
}

docs.Add(printFunc(list[x], context));

// if the syntax tree doesn't have a trailing comma but we want want, then add it
Expand Down Expand Up @@ -86,6 +118,11 @@ private static Doc Print<T>(
}
}

if (unFormattedCode.Length > 0)
{
docs.Add(unFormattedCode.ToString().Trim());
}

return docs.Count == 0 ? Doc.Null : Doc.Concat(docs);
}
}
8 changes: 7 additions & 1 deletion docs/Ignore.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ClassName

```

Use a ranged ignore to exclude multiple lines from formatting. A range is valid around statements and members.
Use a ranged ignore to exclude multiple lines from formatting. A range is not valid in all contexts. Currently it works with statements, members and object initializer expressions.
```csharp
// csharpier-ignore-start
public class Unformatted1 { }
Expand Down Expand Up @@ -104,6 +104,12 @@ public class ClassName
var formatted = true;
}

return new SomeClass
{
// csharpier-ignore-start
SomeProperty = true
// csharpier-ignore-end
}
}
```
Expand Down

0 comments on commit 3fd7faa

Please sign in to comment.