提交 b03a0623 编写于 作者: S Sam Harwell

Avoid passing options to AddIndentBlockOperations

上级 4a180e39
...@@ -54,7 +54,7 @@ private bool IsQuestionOrColonOfNewConditional(SyntaxToken token) ...@@ -54,7 +54,7 @@ private bool IsQuestionOrColonOfNewConditional(SyntaxToken token)
} }
public override void AddIndentBlockOperations( public override void AddIndentBlockOperations(
List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
if (node.HasAnnotation(SpecializedFormattingAnnotation) && if (node.HasAnnotation(SpecializedFormattingAnnotation) &&
node is ConditionalExpressionSyntax conditional) node is ConditionalExpressionSyntax conditional)
......
...@@ -50,7 +50,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseConditionalExpression ...@@ -50,7 +50,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseConditionalExpression
End Function End Function
Public Overrides Sub AddIndentBlockOperationsSlow( Public Overrides Sub AddIndentBlockOperationsSlow(
list As List(Of IndentBlockOperation), node As SyntaxNode, options As AnalyzerConfigOptions, ByRef nextOperation As NextIndentBlockOperationAction) list As List(Of IndentBlockOperation), node As SyntaxNode, ByRef nextOperation As NextIndentBlockOperationAction)
If node.HasAnnotation(UseConditionalExpressionCodeFixHelpers.SpecializedFormattingAnnotation) AndAlso If node.HasAnnotation(UseConditionalExpressionCodeFixHelpers.SpecializedFormattingAnnotation) AndAlso
TypeOf node Is TernaryConditionalExpressionSyntax Then TypeOf node Is TernaryConditionalExpressionSyntax Then
......
...@@ -22,7 +22,7 @@ internal sealed class ChangeSignatureFormattingRule : BaseFormattingRule ...@@ -22,7 +22,7 @@ internal sealed class ChangeSignatureFormattingRule : BaseFormattingRule
SyntaxKind.BracketedArgumentList, SyntaxKind.BracketedArgumentList,
SyntaxKind.AttributeArgumentList); SyntaxKind.AttributeArgumentList);
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
nextOperation.Invoke(); nextOperation.Invoke();
......
...@@ -45,7 +45,7 @@ public FormatLargeBinaryExpressionRule(ISyntaxFactsService syntaxFacts) ...@@ -45,7 +45,7 @@ public FormatLargeBinaryExpressionRule(ISyntaxFactsService syntaxFacts)
/// ... /// ...
/// </summary> /// </summary>
public override void AddIndentBlockOperations( public override void AddIndentBlockOperations(
List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
if (_syntaxFacts.IsReturnStatement(node)) if (_syntaxFacts.IsReturnStatement(node))
{ {
......
...@@ -33,10 +33,10 @@ public override sealed void AddAnchorIndentationOperations(List<AnchorIndentatio ...@@ -33,10 +33,10 @@ public override sealed void AddAnchorIndentationOperations(List<AnchorIndentatio
[Obsolete("Do not call this method directly (it will Stack Overflow).", error: true)] [Obsolete("Do not call this method directly (it will Stack Overflow).", error: true)]
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]
public override sealed void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) public override sealed void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
var nextOperationCopy = nextOperation; var nextOperationCopy = nextOperation;
AddIndentBlockOperationsSlow(list, node, options, ref nextOperationCopy); AddIndentBlockOperationsSlow(list, node, ref nextOperationCopy);
} }
[Obsolete("Do not call this method directly (it will Stack Overflow).", error: true)] [Obsolete("Do not call this method directly (it will Stack Overflow).", error: true)]
...@@ -80,8 +80,8 @@ public virtual void AddAnchorIndentationOperationsSlow(List<AnchorIndentationOpe ...@@ -80,8 +80,8 @@ public virtual void AddAnchorIndentationOperationsSlow(List<AnchorIndentationOpe
/// <summary> /// <summary>
/// returns IndentBlockOperations under a node either by itself or by filtering/replacing operations returned by NextOperation /// returns IndentBlockOperations under a node either by itself or by filtering/replacing operations returned by NextOperation
/// </summary> /// </summary>
public virtual void AddIndentBlockOperationsSlow(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, ref NextIndentBlockOperationAction nextOperation) public virtual void AddIndentBlockOperationsSlow(List<IndentBlockOperation> list, SyntaxNode node, ref NextIndentBlockOperationAction nextOperation)
=> base.AddIndentBlockOperations(list, node, options, in nextOperation); => base.AddIndentBlockOperations(list, node, in nextOperation);
/// <summary> /// <summary>
/// returns AlignTokensOperations under a node either by itself or by filtering/replacing operations returned by NextOperation /// returns AlignTokensOperations under a node either by itself or by filtering/replacing operations returned by NextOperation
......
...@@ -11,7 +11,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ChangeSignature ...@@ -11,7 +11,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ChangeSignature
Friend NotInheritable Class ChangeSignatureFormattingRule Friend NotInheritable Class ChangeSignatureFormattingRule
Inherits BaseFormattingRule Inherits BaseFormattingRule
Public Overrides Sub AddIndentBlockOperationsSlow(list As List(Of IndentBlockOperation), node As SyntaxNode, options As AnalyzerConfigOptions, ByRef nextOperation As NextIndentBlockOperationAction) Public Overrides Sub AddIndentBlockOperationsSlow(list As List(Of IndentBlockOperation), node As SyntaxNode, ByRef nextOperation As NextIndentBlockOperationAction)
nextOperation.Invoke() nextOperation.Invoke()
If node.IsKind(SyntaxKind.ParameterList) OrElse node.IsKind(SyntaxKind.ArgumentList) Then If node.IsKind(SyntaxKind.ParameterList) OrElse node.IsKind(SyntaxKind.ArgumentList) Then
......
...@@ -138,7 +138,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic.Venus ...@@ -138,7 +138,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic.Venus
Public Shared Shadows Instance As AbstractFormattingRule = New VisualBasicHelperFormattingRule() Public Shared Shadows Instance As AbstractFormattingRule = New VisualBasicHelperFormattingRule()
Public Overrides Sub AddIndentBlockOperationsSlow(list As List(Of IndentBlockOperation), node As SyntaxNode, options As AnalyzerConfigOptions, ByRef nextOperation As NextIndentBlockOperationAction) Public Overrides Sub AddIndentBlockOperationsSlow(list As List(Of IndentBlockOperation), node As SyntaxNode, ByRef nextOperation As NextIndentBlockOperationAction)
' we need special behavior for VB due to @Helper code generation weird-ness. ' we need special behavior for VB due to @Helper code generation weird-ness.
' this will looking for code gen specific style to make it not so expansive ' this will looking for code gen specific style to make it not so expansive
If IsEndHelperPattern(node) Then If IsEndHelperPattern(node) Then
...@@ -150,7 +150,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic.Venus ...@@ -150,7 +150,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic.Venus
Return Return
End If End If
MyBase.AddIndentBlockOperationsSlow(list, node, options, nextOperation) MyBase.AddIndentBlockOperationsSlow(list, node, nextOperation)
End Sub End Sub
Private Shared Function IsHelperSubLambda(multiLineLambda As MultiLineLambdaExpressionSyntax) As Boolean Private Shared Function IsHelperSubLambda(multiLineLambda As MultiLineLambdaExpressionSyntax) As Boolean
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules; using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Host.Mef;
...@@ -107,7 +106,7 @@ private static bool IsInvalidToken(SyntaxToken token) ...@@ -107,7 +106,7 @@ private static bool IsInvalidToken(SyntaxToken token)
private class FormattingRule : AbstractFormattingRule private class FormattingRule : AbstractFormattingRule
{ {
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
// these nodes should be from syntax tree from ITextSnapshot. // these nodes should be from syntax tree from ITextSnapshot.
Debug.Assert(node.SyntaxTree != null); Debug.Assert(node.SyntaxTree != null);
......
...@@ -30,7 +30,7 @@ public override void AddAnchorIndentationOperations(List<AnchorIndentationOperat ...@@ -30,7 +30,7 @@ public override void AddAnchorIndentationOperations(List<AnchorIndentationOperat
{ {
} }
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
} }
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting.Rules; using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities; using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
...@@ -18,21 +19,45 @@ ...@@ -18,21 +19,45 @@
namespace Microsoft.CodeAnalysis.CSharp.Formatting namespace Microsoft.CodeAnalysis.CSharp.Formatting
{ {
internal class IndentBlockFormattingRule : BaseFormattingRule internal sealed class IndentBlockFormattingRule : BaseFormattingRule
{ {
internal const string Name = "CSharp IndentBlock Formatting Rule"; internal const string Name = "CSharp IndentBlock Formatting Rule";
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) private readonly CachedOptions _options;
public IndentBlockFormattingRule()
: this(new CachedOptions(null))
{
}
private IndentBlockFormattingRule(CachedOptions options)
{
_options = options;
}
public override AbstractFormattingRule WithOptions(AnalyzerConfigOptions options)
{
var cachedOptions = new CachedOptions(options);
if (cachedOptions == _options)
{
return this;
}
return new IndentBlockFormattingRule(cachedOptions);
}
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
nextOperation.Invoke(); nextOperation.Invoke();
AddAlignmentBlockOperation(list, node); AddAlignmentBlockOperation(list, node);
AddBlockIndentationOperation(list, node, options); AddBlockIndentationOperation(list, node);
AddLabelIndentationOperation(list, node, options); AddLabelIndentationOperation(list, node);
AddSwitchIndentationOperation(list, node, options); AddSwitchIndentationOperation(list, node);
AddEmbeddedStatementsIndentationOperation(list, node); AddEmbeddedStatementsIndentationOperation(list, node);
...@@ -48,7 +73,7 @@ private void AddTypeParameterConstraintClauseOperation(List<IndentBlockOperation ...@@ -48,7 +73,7 @@ private void AddTypeParameterConstraintClauseOperation(List<IndentBlockOperation
} }
} }
private void AddSwitchIndentationOperation(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options) private void AddSwitchIndentationOperation(List<IndentBlockOperation> list, SyntaxNode node)
{ {
if (!(node is SwitchSectionSyntax section)) if (!(node is SwitchSectionSyntax section))
{ {
...@@ -62,25 +87,23 @@ private void AddSwitchIndentationOperation(List<IndentBlockOperation> list, Synt ...@@ -62,25 +87,23 @@ private void AddSwitchIndentationOperation(List<IndentBlockOperation> list, Synt
return; return;
} }
var indentSwitchCase = options.GetOption(CSharpFormattingOptions2.IndentSwitchCaseSection); if (!_options.IndentSwitchCaseSection && !_options.IndentSwitchCaseSectionWhenBlock)
var indentSwitchCaseWhenBlock = options.GetOption(CSharpFormattingOptions2.IndentSwitchCaseSectionWhenBlock);
if (!indentSwitchCase && !indentSwitchCaseWhenBlock)
{ {
// Never indent // Never indent
return; return;
} }
var alwaysIndent = indentSwitchCase && indentSwitchCaseWhenBlock; var alwaysIndent = _options.IndentSwitchCaseSection && _options.IndentSwitchCaseSectionWhenBlock;
if (!alwaysIndent) if (!alwaysIndent)
{ {
// Only one of these values can be true at this point. // Only one of these values can be true at this point.
Debug.Assert(indentSwitchCase != indentSwitchCaseWhenBlock); Debug.Assert(_options.IndentSwitchCaseSection != _options.IndentSwitchCaseSectionWhenBlock);
var firstStatementIsBlock = var firstStatementIsBlock =
section.Statements.Count > 0 && section.Statements.Count > 0 &&
section.Statements[0].IsKind(SyntaxKind.Block); section.Statements[0].IsKind(SyntaxKind.Block);
if (indentSwitchCaseWhenBlock != firstStatementIsBlock) if (_options.IndentSwitchCaseSectionWhenBlock != firstStatementIsBlock)
{ {
return; return;
} }
...@@ -113,18 +136,16 @@ private void AddSwitchIndentationOperation(List<IndentBlockOperation> list, Synt ...@@ -113,18 +136,16 @@ private void AddSwitchIndentationOperation(List<IndentBlockOperation> list, Synt
AddIndentBlockOperation(list, startToken, endToken, span); AddIndentBlockOperation(list, startToken, endToken, span);
} }
private void AddLabelIndentationOperation(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options) private void AddLabelIndentationOperation(List<IndentBlockOperation> list, SyntaxNode node)
{ {
// label statement // label statement
if (node is LabeledStatementSyntax labeledStatement) if (node is LabeledStatementSyntax labeledStatement)
{ {
var labelPositioningOption = options.GetOption(CSharpFormattingOptions2.LabelPositioning); if (_options.LabelPositioning == LabelPositionOptions.OneLess)
if (labelPositioningOption == LabelPositionOptions.OneLess)
{ {
AddUnindentBlockOperation(list, labeledStatement.Identifier, labeledStatement.ColonToken); AddUnindentBlockOperation(list, labeledStatement.Identifier, labeledStatement.ColonToken);
} }
else if (labelPositioningOption == LabelPositionOptions.LeftMost) else if (_options.LabelPositioning == LabelPositionOptions.LeftMost)
{ {
AddAbsoluteZeroIndentBlockOperation(list, labeledStatement.Identifier, labeledStatement.ColonToken); AddAbsoluteZeroIndentBlockOperation(list, labeledStatement.Identifier, labeledStatement.ColonToken);
} }
...@@ -178,7 +199,7 @@ private void SetAlignmentBlockOperation(List<IndentBlockOperation> list, SyntaxN ...@@ -178,7 +199,7 @@ private void SetAlignmentBlockOperation(List<IndentBlockOperation> list, SyntaxN
SetAlignmentBlockOperation(list, baseToken, firstToken, lastToken, option); SetAlignmentBlockOperation(list, baseToken, firstToken, lastToken, option);
} }
private void AddBlockIndentationOperation(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options) private void AddBlockIndentationOperation(List<IndentBlockOperation> list, SyntaxNode node)
{ {
var bracePair = node.GetBracePair(); var bracePair = node.GetBracePair();
...@@ -200,13 +221,13 @@ private void AddBlockIndentationOperation(List<IndentBlockOperation> list, Synta ...@@ -200,13 +221,13 @@ private void AddBlockIndentationOperation(List<IndentBlockOperation> list, Synta
AddAlignmentBlockOperationRelativeToFirstTokenOnBaseTokenLine(list, bracePair); AddAlignmentBlockOperationRelativeToFirstTokenOnBaseTokenLine(list, bracePair);
} }
if (node is BlockSyntax && !options.GetOption(CSharpFormattingOptions2.IndentBlock)) if (node is BlockSyntax && !_options.IndentBlock)
{ {
// do not add indent operation for block // do not add indent operation for block
return; return;
} }
if (node is SwitchStatementSyntax && !options.GetOption(CSharpFormattingOptions2.IndentSwitchSection)) if (node is SwitchStatementSyntax && !_options.IndentSwitchSection)
{ {
// do not add indent operation for switch statement // do not add indent operation for switch statement
return; return;
...@@ -299,5 +320,60 @@ private void AddEmbeddedStatementsIndentationOperation(List<IndentBlockOperation ...@@ -299,5 +320,60 @@ private void AddEmbeddedStatementsIndentationOperation(List<IndentBlockOperation
AddIndentBlockOperation(list, firstToken, lastToken, TextSpan.FromBounds(firstToken.FullSpan.Start, lastToken.FullSpan.End)); AddIndentBlockOperation(list, firstToken, lastToken, TextSpan.FromBounds(firstToken.FullSpan.Start, lastToken.FullSpan.End));
} }
} }
private readonly struct CachedOptions : IEquatable<CachedOptions>
{
public readonly LabelPositionOptions LabelPositioning;
public readonly bool IndentBlock;
public readonly bool IndentSwitchCaseSection;
public readonly bool IndentSwitchCaseSectionWhenBlock;
public readonly bool IndentSwitchSection;
public CachedOptions(AnalyzerConfigOptions? options)
{
LabelPositioning = GetOptionOrDefault(options, CSharpFormattingOptions2.LabelPositioning);
IndentBlock = GetOptionOrDefault(options, CSharpFormattingOptions2.IndentBlock);
IndentSwitchCaseSection = GetOptionOrDefault(options, CSharpFormattingOptions2.IndentSwitchCaseSection);
IndentSwitchCaseSectionWhenBlock = GetOptionOrDefault(options, CSharpFormattingOptions2.IndentSwitchCaseSectionWhenBlock);
IndentSwitchSection = GetOptionOrDefault(options, CSharpFormattingOptions2.IndentSwitchSection);
}
public static bool operator ==(CachedOptions left, CachedOptions right)
=> left.Equals(right);
public static bool operator !=(CachedOptions left, CachedOptions right)
=> !(left == right);
private static T GetOptionOrDefault<T>(AnalyzerConfigOptions? options, Option2<T> option)
{
if (options is null)
return option.DefaultValue;
return options.GetOption(option);
}
public override bool Equals(object? obj)
=> obj is CachedOptions options && Equals(options);
public bool Equals(CachedOptions other)
{
return LabelPositioning == other.LabelPositioning
&& IndentBlock == other.IndentBlock
&& IndentSwitchCaseSection == other.IndentSwitchCaseSection
&& IndentSwitchCaseSectionWhenBlock == other.IndentSwitchCaseSectionWhenBlock
&& IndentSwitchSection == other.IndentSwitchSection;
}
public override int GetHashCode()
{
var hashCode = 0;
hashCode = (hashCode << 2) + (int)LabelPositioning;
hashCode = (hashCode << 1) + (IndentBlock ? 1 : 0);
hashCode = (hashCode << 1) + (IndentSwitchCaseSection ? 1 : 0);
hashCode = (hashCode << 1) + (IndentSwitchCaseSectionWhenBlock ? 1 : 0);
hashCode = (hashCode << 1) + (IndentSwitchSection ? 1 : 0);
return hashCode;
}
}
} }
} }
...@@ -4,15 +4,41 @@ ...@@ -4,15 +4,41 @@
#nullable enable #nullable enable
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting.Rules; using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Options;
namespace Microsoft.CodeAnalysis.CSharp.Formatting namespace Microsoft.CodeAnalysis.CSharp.Formatting
{ {
internal class IndentUserSettingsFormattingRule : BaseFormattingRule internal sealed class IndentUserSettingsFormattingRule : BaseFormattingRule
{ {
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) private readonly CachedOptions _options;
public IndentUserSettingsFormattingRule()
: this(new CachedOptions(null))
{
}
private IndentUserSettingsFormattingRule(CachedOptions options)
{
_options = options;
}
public override AbstractFormattingRule WithOptions(AnalyzerConfigOptions options)
{
var cachedOptions = new CachedOptions(options);
if (cachedOptions == _options)
{
return this;
}
return new IndentUserSettingsFormattingRule(cachedOptions);
}
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
nextOperation.Invoke(); nextOperation.Invoke();
...@@ -24,11 +50,50 @@ public override void AddIndentBlockOperations(List<IndentBlockOperation> list, S ...@@ -24,11 +50,50 @@ public override void AddIndentBlockOperations(List<IndentBlockOperation> list, S
return; return;
} }
if (options.GetOption(CSharpFormattingOptions2.IndentBraces)) if (_options.IndentBraces)
{ {
AddIndentBlockOperation(list, bracePair.Item1, bracePair.Item1, bracePair.Item1.Span); AddIndentBlockOperation(list, bracePair.Item1, bracePair.Item1, bracePair.Item1.Span);
AddIndentBlockOperation(list, bracePair.Item2, bracePair.Item2, bracePair.Item2.Span); AddIndentBlockOperation(list, bracePair.Item2, bracePair.Item2, bracePair.Item2.Span);
} }
} }
private readonly struct CachedOptions : IEquatable<CachedOptions>
{
public readonly bool IndentBraces;
public CachedOptions(AnalyzerConfigOptions? options)
{
IndentBraces = GetOptionOrDefault(options, CSharpFormattingOptions2.IndentBraces);
}
public static bool operator ==(CachedOptions left, CachedOptions right)
=> left.Equals(right);
public static bool operator !=(CachedOptions left, CachedOptions right)
=> !(left == right);
private static T GetOptionOrDefault<T>(AnalyzerConfigOptions? options, Option2<T> option)
{
if (options is null)
return option.DefaultValue;
return options.GetOption(option);
}
public override bool Equals(object? obj)
=> obj is CachedOptions options && Equals(options);
public bool Equals(CachedOptions other)
{
return IndentBraces == other.IndentBraces;
}
public override int GetHashCode()
{
var hashCode = 0;
hashCode = (hashCode << 1) + (IndentBraces ? 1 : 0);
return hashCode;
}
}
} }
} }
...@@ -56,7 +56,7 @@ private void AddIndentBlockOperationsForFromClause(List<IndentBlockOperation> li ...@@ -56,7 +56,7 @@ private void AddIndentBlockOperationsForFromClause(List<IndentBlockOperation> li
AddIndentBlockOperation(list, baseToken, startToken, endToken); AddIndentBlockOperation(list, baseToken, startToken, endToken);
} }
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
nextOperation.Invoke(); nextOperation.Invoke();
......
...@@ -60,7 +60,7 @@ public void AddAnchorIndentationOperations(List<AnchorIndentationOperation> list ...@@ -60,7 +60,7 @@ public void AddAnchorIndentationOperations(List<AnchorIndentationOperation> list
public void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode currentNode) public void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode currentNode)
{ {
var action = new NextIndentBlockOperationAction(_addIndentBlockOperationsRules, index: 0, currentNode, _options, list); var action = new NextIndentBlockOperationAction(_addIndentBlockOperationsRules, index: 0, currentNode, list);
action.Invoke(); action.Invoke();
} }
......
...@@ -34,7 +34,7 @@ public virtual void AddAnchorIndentationOperations(List<AnchorIndentationOperati ...@@ -34,7 +34,7 @@ public virtual void AddAnchorIndentationOperations(List<AnchorIndentationOperati
/// <summary> /// <summary>
/// returns IndentBlockOperations under a node either by itself or by filtering/replacing operations returned by NextOperation /// returns IndentBlockOperations under a node either by itself or by filtering/replacing operations returned by NextOperation
/// </summary> /// </summary>
public virtual void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) public virtual void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
=> nextOperation.Invoke(); => nextOperation.Invoke();
/// <summary> /// <summary>
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities; using Roslyn.Utilities;
...@@ -31,7 +30,7 @@ public BaseIndentationFormattingRule(SyntaxNode root, TextSpan span, int baseInd ...@@ -31,7 +30,7 @@ public BaseIndentationFormattingRule(SyntaxNode root, TextSpan span, int baseInd
_vbHelperFormattingRule = vbHelperFormattingRule; _vbHelperFormattingRule = vbHelperFormattingRule;
} }
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
// for the common node itself, return absolute indentation // for the common node itself, return absolute indentation
if (_commonNode == node) if (_commonNode == node)
...@@ -48,21 +47,21 @@ public override void AddIndentBlockOperations(List<IndentBlockOperation> list, S ...@@ -48,21 +47,21 @@ public override void AddIndentBlockOperations(List<IndentBlockOperation> list, S
} }
// Add everything to the list. // Add everything to the list.
AddNextIndentBlockOperations(list, node, options, in nextOperation); AddNextIndentBlockOperations(list, node, in nextOperation);
// Filter out everything that encompasses our span. // Filter out everything that encompasses our span.
AdjustIndentBlockOperation(list); AdjustIndentBlockOperation(list);
} }
private void AddNextIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) private void AddNextIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
if (_vbHelperFormattingRule == null) if (_vbHelperFormattingRule == null)
{ {
base.AddIndentBlockOperations(list, node, options, in nextOperation); base.AddIndentBlockOperations(list, node, in nextOperation);
return; return;
} }
_vbHelperFormattingRule.AddIndentBlockOperations(list, node, options, in nextOperation); _vbHelperFormattingRule.AddIndentBlockOperations(list, node, in nextOperation);
} }
private void AdjustIndentBlockOperation(List<IndentBlockOperation> list) private void AdjustIndentBlockOperation(List<IndentBlockOperation> list)
......
...@@ -32,10 +32,10 @@ public override sealed void AddAnchorIndentationOperations(List<AnchorIndentatio ...@@ -32,10 +32,10 @@ public override sealed void AddAnchorIndentationOperations(List<AnchorIndentatio
[Obsolete("Do not call this method directly (it will Stack Overflow).", error: true)] [Obsolete("Do not call this method directly (it will Stack Overflow).", error: true)]
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]
public override sealed void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, in NextIndentBlockOperationAction nextOperation) public override sealed void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation)
{ {
var nextOperationCopy = nextOperation; var nextOperationCopy = nextOperation;
AddIndentBlockOperationsSlow(list, node, options, ref nextOperationCopy); AddIndentBlockOperationsSlow(list, node, ref nextOperationCopy);
} }
[Obsolete("Do not call this method directly (it will Stack Overflow).", error: true)] [Obsolete("Do not call this method directly (it will Stack Overflow).", error: true)]
...@@ -79,8 +79,8 @@ public virtual void AddAnchorIndentationOperationsSlow(List<AnchorIndentationOpe ...@@ -79,8 +79,8 @@ public virtual void AddAnchorIndentationOperationsSlow(List<AnchorIndentationOpe
/// <summary> /// <summary>
/// returns IndentBlockOperations under a node either by itself or by filtering/replacing operations returned by NextOperation /// returns IndentBlockOperations under a node either by itself or by filtering/replacing operations returned by NextOperation
/// </summary> /// </summary>
public virtual void AddIndentBlockOperationsSlow(List<IndentBlockOperation> list, SyntaxNode node, AnalyzerConfigOptions options, ref NextIndentBlockOperationAction nextOperation) public virtual void AddIndentBlockOperationsSlow(List<IndentBlockOperation> list, SyntaxNode node, ref NextIndentBlockOperationAction nextOperation)
=> base.AddIndentBlockOperations(list, node, options, in nextOperation); => base.AddIndentBlockOperations(list, node, in nextOperation);
/// <summary> /// <summary>
/// returns AlignTokensOperations under a node either by itself or by filtering/replacing operations returned by NextOperation /// returns AlignTokensOperations under a node either by itself or by filtering/replacing operations returned by NextOperation
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Utilities; using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Formatting.Rules namespace Microsoft.CodeAnalysis.Formatting.Rules
...@@ -17,25 +16,22 @@ namespace Microsoft.CodeAnalysis.Formatting.Rules ...@@ -17,25 +16,22 @@ namespace Microsoft.CodeAnalysis.Formatting.Rules
private readonly ImmutableArray<AbstractFormattingRule> _formattingRules; private readonly ImmutableArray<AbstractFormattingRule> _formattingRules;
private readonly int _index; private readonly int _index;
private readonly SyntaxNode _node; private readonly SyntaxNode _node;
private readonly AnalyzerConfigOptions _options;
private readonly List<IndentBlockOperation> _list; private readonly List<IndentBlockOperation> _list;
public NextIndentBlockOperationAction( public NextIndentBlockOperationAction(
ImmutableArray<AbstractFormattingRule> formattingRules, ImmutableArray<AbstractFormattingRule> formattingRules,
int index, int index,
SyntaxNode node, SyntaxNode node,
AnalyzerConfigOptions options,
List<IndentBlockOperation> list) List<IndentBlockOperation> list)
{ {
_formattingRules = formattingRules; _formattingRules = formattingRules;
_index = index; _index = index;
_node = node; _node = node;
_options = options;
_list = list; _list = list;
} }
private NextIndentBlockOperationAction NextAction private NextIndentBlockOperationAction NextAction
=> new NextIndentBlockOperationAction(_formattingRules, _index + 1, _node, _options, _list); => new NextIndentBlockOperationAction(_formattingRules, _index + 1, _node, _list);
public void Invoke() public void Invoke()
{ {
...@@ -47,7 +43,7 @@ public void Invoke() ...@@ -47,7 +43,7 @@ public void Invoke()
else else
{ {
// Call the handler at the index, passing a continuation that will come back to here with index + 1 // Call the handler at the index, passing a continuation that will come back to here with index + 1
_formattingRules[_index].AddIndentBlockOperations(_list, _node, _options, NextAction); _formattingRules[_index].AddIndentBlockOperations(_list, _node, NextAction);
return; return;
} }
} }
......
...@@ -28,7 +28,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Formatting ...@@ -28,7 +28,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Formatting
Public Overrides Sub AddAnchorIndentationOperationsSlow(operations As List(Of AnchorIndentationOperation), node As SyntaxNode, ByRef nextAction As NextAnchorIndentationOperationAction) Public Overrides Sub AddAnchorIndentationOperationsSlow(operations As List(Of AnchorIndentationOperation), node As SyntaxNode, ByRef nextAction As NextAnchorIndentationOperationAction)
End Sub End Sub
Public Overrides Sub AddIndentBlockOperationsSlow(operations As List(Of IndentBlockOperation), node As SyntaxNode, options As AnalyzerConfigOptions, ByRef nextAction As NextIndentBlockOperationAction) Public Overrides Sub AddIndentBlockOperationsSlow(operations As List(Of IndentBlockOperation), node As SyntaxNode, ByRef nextAction As NextIndentBlockOperationAction)
End Sub End Sub
Public Overrides Sub AddAlignTokensOperationsSlow(operations As List(Of AlignTokensOperation), node As SyntaxNode, options As AnalyzerConfigOptions, ByRef nextAction As NextAlignTokensOperationAction) Public Overrides Sub AddAlignTokensOperationsSlow(operations As List(Of AlignTokensOperation), node As SyntaxNode, options As AnalyzerConfigOptions, ByRef nextAction As NextAlignTokensOperationAction)
......
...@@ -19,7 +19,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Formatting ...@@ -19,7 +19,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Formatting
nextOperation.Invoke() nextOperation.Invoke()
End Sub End Sub
Public Overrides Sub AddIndentBlockOperationsSlow(list As List(Of IndentBlockOperation), node As SyntaxNode, options As AnalyzerConfigOptions, ByRef nextOperation As NextIndentBlockOperationAction) Public Overrides Sub AddIndentBlockOperationsSlow(list As List(Of IndentBlockOperation), node As SyntaxNode, ByRef nextOperation As NextIndentBlockOperationAction)
nextOperation.Invoke() nextOperation.Invoke()
If node.Kind = SyntaxKind.ObjectMemberInitializer Then If node.Kind = SyntaxKind.ObjectMemberInitializer Then
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
' See the LICENSE file in the project root for more information. ' See the LICENSE file in the project root for more information.
Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.Formatting.Rules Imports Microsoft.CodeAnalysis.Formatting.Rules
Imports Microsoft.CodeAnalysis.Text Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
...@@ -49,7 +48,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Formatting ...@@ -49,7 +48,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Formatting
Public Overrides Sub AddIndentBlockOperationsSlow(operations As List(Of IndentBlockOperation), Public Overrides Sub AddIndentBlockOperationsSlow(operations As List(Of IndentBlockOperation),
node As SyntaxNode, node As SyntaxNode,
options As AnalyzerConfigOptions,
ByRef nextOperation As NextIndentBlockOperationAction) ByRef nextOperation As NextIndentBlockOperationAction)
nextOperation.Invoke() nextOperation.Invoke()
......
...@@ -54,7 +54,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Indentation ...@@ -54,7 +54,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Indentation
Return spaceOperation Return spaceOperation
End Function End Function
Public Overrides Sub AddIndentBlockOperationsSlow(list As List(Of IndentBlockOperation), node As SyntaxNode, options As AnalyzerConfigOptions, ByRef nextOperation As NextIndentBlockOperationAction) Public Overrides Sub AddIndentBlockOperationsSlow(list As List(Of IndentBlockOperation), node As SyntaxNode, ByRef nextOperation As NextIndentBlockOperationAction)
nextOperation.Invoke() nextOperation.Invoke()
Dim singleLineLambdaFunction = TryCast(node, SingleLineLambdaExpressionSyntax) Dim singleLineLambdaFunction = TryCast(node, SingleLineLambdaExpressionSyntax)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册