FormatLargeBinaryExpressionRule.cs 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.GenerateEqualsAndGetHashCodeFromMembers
{
    internal partial class GenerateEqualsAndGetHashCodeFromMembersCodeRefactoringProvider
    {
        private partial class GenerateEqualsAndGetHashCodeAction : CodeAction
        {
            private class FormatLargeBinaryExpressionRule : AbstractFormattingRule
            {
                private ISyntaxFactsService _syntaxFacts;

                public FormatLargeBinaryExpressionRule(ISyntaxFactsService syntaxFacts)
                {
                    _syntaxFacts = syntaxFacts;
                }

                public override AdjustNewLinesOperation GetAdjustNewLinesOperation(
                    SyntaxToken previousToken, SyntaxToken currentToken, OptionSet optionSet, NextOperation<AdjustNewLinesOperation> nextOperation)
                {
                    if (_syntaxFacts.IsLogicalAndExpression(previousToken.Parent))
                    {
                        return FormattingOperations.CreateAdjustNewLinesOperation(1, AdjustNewLinesOption.PreserveLines);
                    }

                    return nextOperation.Invoke();
                }

                public override void AddIndentBlockOperations(
                    List<IndentBlockOperation> list, SyntaxNode node, OptionSet optionSet, NextAction<IndentBlockOperation> nextOperation)
                {
                    if (_syntaxFacts.IsReturnStatement(node))
                    {
                        list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
                            node.GetFirstToken(),
                            node.GetFirstToken().GetNextToken(),
                            node.GetLastToken(),
                            indentationDelta: 1,
                            option: IndentBlockOption.RelativePosition));
                        return;
                    }

                    nextOperation.Invoke(list);
                }
            }
        }
    }
}