| File: GenerateEqualsAndGetHashCodeFromMembers\FormatLargeBinaryExpressionRule.cs | Web Access |
| Project: src\roslyn\src\Features\Core\Portable\Microsoft.CodeAnalysis.Features.csproj (Microsoft.CodeAnalysis.Features) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using Microsoft.CodeAnalysis.Formatting.Rules; using Microsoft.CodeAnalysis.LanguageService; namespace Microsoft.CodeAnalysis.GenerateEqualsAndGetHashCodeFromMembers; internal abstract partial class AbstractGenerateEqualsAndGetHashCodeService { /// <summary> /// Specialized formatter for the "return a == obj.a && b == obj.b && c == obj.c && ... /// code that we spit out. /// </summary> private sealed class FormatLargeBinaryExpressionRule(ISyntaxFactsService syntaxFacts) : AbstractFormattingRule { private readonly ISyntaxFactsService _syntaxFacts = syntaxFacts; /// <summary> /// Wrap the large && expression after every && token. /// </summary> public override AdjustNewLinesOperation? GetAdjustNewLinesOperation( in SyntaxToken previousToken, in SyntaxToken currentToken, in NextGetAdjustNewLinesOperation nextOperation) { if (_syntaxFacts.IsLogicalAndExpression(previousToken.Parent)) { return FormattingOperations.CreateAdjustNewLinesOperation(1, AdjustNewLinesOption.PreserveLines); } return nextOperation.Invoke(in previousToken, in currentToken); } /// <summary> /// Align all the wrapped parts of the expression with the token after 'return'. /// That way we get: /// /// return a == obj.a && /// b == obj.b && /// ... /// </summary> public override void AddIndentBlockOperations( List<IndentBlockOperation> list, SyntaxNode node, in NextIndentBlockOperationAction nextOperation) { if (_syntaxFacts.IsReturnStatement(node)) { var expr = _syntaxFacts.GetExpressionOfReturnStatement(node); if (expr?.ChildNodesAndTokens().Count > 1) { list.Add(FormattingOperations.CreateRelativeIndentBlockOperation( expr.GetFirstToken(), expr.GetFirstToken().GetNextToken(), node.GetLastToken(), indentationDelta: 0, option: IndentBlockOption.RelativePosition)); return; } } nextOperation.Invoke(); } } }