Skip to content

Commit

Permalink
Improve IndentationAnalysis
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Oct 29, 2022
1 parent 41816ef commit 3d9775d
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/CSharp/CSharp/IndentationAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@ namespace Roslynator.CSharp
[DebuggerDisplay("{DebuggerDisplay,nq}")]
internal readonly struct IndentationAnalysis
{
private readonly SyntaxTrivia _indentation;
private readonly int _indentSize;
private readonly int? _indentSize;
private readonly SyntaxTrivia? _singleIndentation;

private IndentationAnalysis(SyntaxTrivia indentation, int indentSize)
private IndentationAnalysis(SyntaxTrivia indentation, int? indentSize, SyntaxTrivia? singleIndentation)
{
_indentation = indentation;
Indentation = indentation;
_indentSize = indentSize;
_singleIndentation = singleIndentation;
}

public SyntaxTrivia Indentation => (_indentSize == -1) ? default : _indentation;
public SyntaxTrivia Indentation { get; }

public int IndentSize => (_indentSize == -1) ? 0 : _indentSize;
public int IndentSize => _indentSize ?? _singleIndentation?.Span.Length ?? 0;

public int IndentationLength => (_indentSize == -1) ? 0 : Indentation.Span.Length;
public int IndentationLength => Indentation.Span.Length;

public int IncreasedIndentationLength => (_indentSize == -1) ? 0 : (Indentation.Span.Length + _indentSize);
public int IncreasedIndentationLength => (IndentSize > 0) ? Indentation.Span.Length + IndentSize : 0;

public bool IsDefault => _indentation == default && _indentSize == 0;
public bool IsDefault
{
get
{
return Indentation.IsKind(SyntaxKind.None)
&& _indentSize is null
&& _singleIndentation is null;
}
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"Length = {Indentation.Span.Length} {nameof(IndentSize)} = {IndentSize}";
Expand All @@ -41,17 +50,17 @@ public static IndentationAnalysis Create(SyntaxNode node, CancellationToken canc

if (isFromCompilationUnit)
{
return new IndentationAnalysis(indentation, trivia1.Span.Length - trivia2.Span.Length);
return new IndentationAnalysis(indentation, trivia1.Span.Length - trivia2.Span.Length, null);
}
else if (indentation.Span.Length > 0)
{
return (trivia1.Span.Length > 0)
? new IndentationAnalysis(indentation, trivia1.Span.Length)
: new IndentationAnalysis(indentation, -1);
? new IndentationAnalysis(indentation, null, trivia1)
: new IndentationAnalysis(indentation, null, null);
}
else if (trivia1.Span.Length > 0)
{
return new IndentationAnalysis(trivia1, -1);
return new IndentationAnalysis(indentation, null, trivia1);
}
else
{
Expand All @@ -78,13 +87,16 @@ public SyntaxTriviaList GetIncreasedIndentationTriviaList()

public string GetSingleIndentation()
{
if (_singleIndentation is not null)
return _singleIndentation.ToString();

if (_indentSize == -1)
return _indentation.ToString();
return Indentation.ToString();

if (_indentation.Span.Length == 0)
if (Indentation.Span.Length == 0)
return "";

string indentation = _indentation.ToString();
string indentation = Indentation.ToString();

if (indentation[indentation.Length - 1] == '\t')
return "\t";
Expand Down

0 comments on commit 3d9775d

Please sign in to comment.