Skip to content

Commit

Permalink
Fixing bug with range ignore + regions (#1216)
Browse files Browse the repository at this point in the history
  • Loading branch information
belav authored Apr 1, 2024
1 parent 8fe2310 commit e95599a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ public class ClassName
#endregion one
;
}

class ClassName
{
#region Region
// csharpier-ignore-start
public string Field;
// csharpier-ignore-end
#endregion
}
15 changes: 12 additions & 3 deletions Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class DocPrinter
protected readonly string EndOfLine;
protected readonly PrinterOptions PrinterOptions;
protected readonly Indenter Indenter;
protected Stack<Indent> RegionIndents = new();
protected readonly Stack<Indent> RegionIndents = new();

protected DocPrinter(Doc doc, PrinterOptions printerOptions, string endOfLine)
{
Expand Down Expand Up @@ -156,8 +156,17 @@ private void ProcessNextCommand()
{
if (region.IsEnd)
{
var regionIndent = this.RegionIndents.Pop();
this.Output.Append(regionIndent.Value);
// in the case where regions are combined with ignored ranges, the start region
// ends up printing inside the unformatted nodes, so we don't have a matching
// start region to go with this end region
if (this.RegionIndents.TryPop(out var regionIndent))
{
this.Output.Append(regionIndent.Value);
}
else
{
this.Output.Append(indent.Value);
}
}
else
{
Expand Down
22 changes: 22 additions & 0 deletions Src/CSharpier/Utilities/StackExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace CSharpier.Utilities;

using System.Diagnostics.CodeAnalysis;

internal static class StackExtensions
{
#if NETSTANDARD2_0
public static bool TryPop<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result)
{
if (stack.Count > 0)
{
result = stack.Pop()!;
return true;
}
else
{
result = default;
return false;
}
}
#endif
}

0 comments on commit e95599a

Please sign in to comment.