Skip to content

Commit

Permalink
Updating formatting rules and engine to use the shared layer formatti…
Browse files Browse the repository at this point in the history
…ng options
  • Loading branch information
mavasani committed Mar 10, 2020
1 parent 762c09c commit 2848a31
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
_forceIndentation,
_indentation,
_indentationDelta,
_options.GetOption(FormattingOptions.UseTabs),
_options.GetOption(FormattingOptions.TabSize));
_options.GetOption(FormattingOptions2.UseTabs),
_options.GetOption(FormattingOptions2.TabSize));

if (triviaText == newTriviaText)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected override SyntaxTrivia CreateEndOfLine()
{
if (_newLine == default)
{
var text = this.Context.Options.GetOption(FormattingOptions.NewLine);
var text = this.Context.Options.GetOption(FormattingOptions2.NewLine);
_newLine = SyntaxFactory.EndOfLine(text);
}

Expand Down Expand Up @@ -200,9 +200,9 @@ private bool TryFormatMultiLineCommentTrivia(LineColumn lineColumn, SyntaxTrivia
false /* forceIndentation */,
indentation,
indentationDelta,
this.Options.GetOption(FormattingOptions.UseTabs),
this.Options.GetOption(FormattingOptions.TabSize),
this.Options.GetOption(FormattingOptions.NewLine));
this.Options.GetOption(FormattingOptions2.UseTabs),
this.Options.GetOption(FormattingOptions2.TabSize),
this.Options.GetOption(FormattingOptions2.NewLine));

var multilineCommentTrivia = SyntaxFactory.ParseLeadingTrivia(multiLineComment);
Contract.ThrowIfFalse(multilineCommentTrivia.Count == 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private bool OnWhitespace(SyntaxTrivia trivia)
return true;
}

_indentation += text.ConvertTabToSpace(_options.GetOption(FormattingOptions.TabSize), _indentation, text.Length);
_indentation += text.ConvertTabToSpace(_options.GetOption(FormattingOptions2.TabSize), _indentation, text.Length);

return false;
}
Expand Down Expand Up @@ -190,7 +190,7 @@ private bool OnComment(SyntaxTrivia trivia)

// go deep down for single line documentation comment
if (trivia.IsSingleLineDocComment() &&
ShouldFormatSingleLineDocumentationComment(_indentation, _options.GetOption(FormattingOptions.TabSize), trivia))
ShouldFormatSingleLineDocumentationComment(_indentation, _options.GetOption(FormattingOptions2.TabSize), trivia))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ComplexTrivia(AnalyzerConfigOptions options, TreeData treeInfo, SyntaxTok

protected override void ExtractLineAndSpace(string text, out int lines, out int spaces)
{
text.ProcessTextBetweenTokens(this.TreeInfo, this.Token1, this.Options.GetOption(FormattingOptions.TabSize), out lines, out spaces);
text.ProcessTextBetweenTokens(this.TreeInfo, this.Token1, this.Options.GetOption(FormattingOptions2.TabSize), out lines, out spaces);
}

protected override TriviaData CreateComplexTrivia(int line, int space)
Expand Down Expand Up @@ -85,7 +85,7 @@ private bool ShouldFormat(FormattingContext context)

Debug.Assert(this.SecondTokenIsFirstTokenOnLine);

if (this.Options.GetOption(FormattingOptions.UseTabs))
if (this.Options.GetOption(FormattingOptions2.UseTabs))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ private TriviaData GetWhitespaceOnlyTriviaInfo(SyntaxToken token1, SyntaxToken t

private int CalculateSpaces(SyntaxToken token1, SyntaxToken token2)
{
var initialColumn = (token1.RawKind == 0) ? 0 : this.TreeInfo.GetOriginalColumn(this.Options.GetOption(FormattingOptions.TabSize), token1) + token1.Span.Length;
var initialColumn = (token1.RawKind == 0) ? 0 : this.TreeInfo.GetOriginalColumn(this.Options.GetOption(FormattingOptions2.TabSize), token1) + token1.Span.Length;
var textSnippet = this.TreeInfo.GetTextBetween(token1, token2);

return textSnippet.ConvertTabToSpace(this.Options.GetOption(FormattingOptions.TabSize), initialColumn, textSnippet.Length);
return textSnippet.ConvertTabToSpace(this.Options.GetOption(FormattingOptions2.TabSize), initialColumn, textSnippet.Length);
}

private (bool canUseTriviaAsItIs, int lineBreaks, int indentation) GetLineBreaksAndIndentation(Analyzer.AnalysisResult result)
{
Debug.Assert(result.Tab >= 0);
Debug.Assert(result.LineBreaks >= 0);

var indentation = result.Tab * this.Options.GetOption(FormattingOptions.TabSize) + result.Space;
var indentation = result.Tab * this.Options.GetOption(FormattingOptions2.TabSize) + result.Space;
if (result.HasTrailingSpace || result.HasUnknownWhitespace)
{
if (result.HasUnknownWhitespace && result.LineBreaks == 0 && indentation == 0)
Expand All @@ -154,7 +154,7 @@ private int CalculateSpaces(SyntaxToken token1, SyntaxToken token2)
return (canUseTriviaAsItIs: false, result.LineBreaks, indentation);
}

if (!this.Options.GetOption(FormattingOptions.UseTabs))
if (!this.Options.GetOption(FormattingOptions2.UseTabs))
{
if (result.Tab > 0)
{
Expand All @@ -164,21 +164,21 @@ private int CalculateSpaces(SyntaxToken token1, SyntaxToken token2)
return (canUseTriviaAsItIs: true, result.LineBreaks, indentation);
}

Debug.Assert(this.Options.GetOption(FormattingOptions.UseTabs));
Debug.Assert(this.Options.GetOption(FormattingOptions2.UseTabs));

// tab can only appear before space to be a valid tab for indentation
if (result.HasTabAfterSpace)
{
return (canUseTriviaAsItIs: false, result.LineBreaks, indentation);
}

if (result.Space >= this.Options.GetOption(FormattingOptions.TabSize))
if (result.Space >= this.Options.GetOption(FormattingOptions2.TabSize))
{
return (canUseTriviaAsItIs: false, result.LineBreaks, indentation);
}

Debug.Assert((indentation / this.Options.GetOption(FormattingOptions.TabSize)) == result.Tab);
Debug.Assert((indentation % this.Options.GetOption(FormattingOptions.TabSize)) == result.Space);
Debug.Assert((indentation / this.Options.GetOption(FormattingOptions2.TabSize)) == result.Tab);
Debug.Assert((indentation % this.Options.GetOption(FormattingOptions2.TabSize)) == result.Space);

return (canUseTriviaAsItIs: true, result.LineBreaks, indentation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ private void AddSwitchIndentationOperation(List<IndentBlockOperation> list, Synt
return;
}

var indentSwitchCase = options.GetOption(CSharpFormattingOptions.IndentSwitchCaseSection);
var indentSwitchCaseWhenBlock = options.GetOption(CSharpFormattingOptions.IndentSwitchCaseSectionWhenBlock);
var indentSwitchCase = options.GetOption(CSharpFormattingOptions2.IndentSwitchCaseSection);
var indentSwitchCaseWhenBlock = options.GetOption(CSharpFormattingOptions2.IndentSwitchCaseSectionWhenBlock);
if (!indentSwitchCase && !indentSwitchCaseWhenBlock)
{
// Never indent
Expand Down Expand Up @@ -115,7 +115,7 @@ private void AddLabelIndentationOperation(List<IndentBlockOperation> list, Synta
// label statement
if (node is LabeledStatementSyntax labeledStatement)
{
var labelPositioningOption = options.GetOption(CSharpFormattingOptions.LabelPositioning);
var labelPositioningOption = options.GetOption(CSharpFormattingOptions2.LabelPositioning);

if (labelPositioningOption == LabelPositionOptions.OneLess)
{
Expand Down Expand Up @@ -197,13 +197,13 @@ private void AddBlockIndentationOperation(List<IndentBlockOperation> list, Synta
AddAlignmentBlockOperationRelativeToFirstTokenOnBaseTokenLine(list, bracePair);
}

if (node is BlockSyntax && !options.GetOption(CSharpFormattingOptions.IndentBlock))
if (node is BlockSyntax && !options.GetOption(CSharpFormattingOptions2.IndentBlock))
{
// do not add indent operation for block
return;
}

if (node is SwitchStatementSyntax && !options.GetOption(CSharpFormattingOptions.IndentSwitchSection))
if (node is SwitchStatementSyntax && !options.GetOption(CSharpFormattingOptions2.IndentSwitchSection))
{
// do not add indent operation for switch statement
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void AddIndentBlockOperations(List<IndentBlockOperation> list, S
return;
}

if (options.GetOption(CSharpFormattingOptions.IndentBraces))
if (options.GetOption(CSharpFormattingOptions2.IndentBraces))
{
AddIndentBlockOperation(list, bracePair.Item1, bracePair.Item1, bracePair.Item1.Span);
AddIndentBlockOperation(list, bracePair.Item2, bracePair.Item2, bracePair.Item2.Span);
Expand Down
Loading

0 comments on commit 2848a31

Please sign in to comment.