예제 #1
0
        public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previousToken, SyntaxToken currentToken, OptionSet optionSet, NextOperation <AdjustSpacesOperation> nextOperation)
        {
            var operation = nextOperation.Invoke();

            if (operation == null)
            {
                return(null);
            }

            // if operation is already forced, return as it is.
            if (operation.Option == AdjustSpacesOption.ForceSpaces)
            {
                return(operation);
            }

            if (CommonFormattingHelpers.HasAnyWhitespaceElasticTrivia(previousToken, currentToken))
            {
                // current implementation of engine gives higher priority on new line operations over space operations if
                // two are conflicting.
                // ex) new line operation says add 1 line between tokens, and
                //     space operation says give 1 space between two tokens (basically means remove new lines)
                //     then, engine will pick new line operation and ignore space operation

                // make every operation forced
                return(CreateAdjustSpacesOperation(Math.Max(0, operation.Space), AdjustSpacesOption.ForceSpaces));
            }

            return(operation);
        }
        public override AdjustNewLinesOperation GetAdjustNewLinesOperation(SyntaxToken previousToken, SyntaxToken currentToken, OptionSet optionSet, NextOperation <AdjustNewLinesOperation> nextOperation)
        {
            var operation = nextOperation.Invoke();

            if (operation == null)
            {
                // If there are more than one Type Parameter Constraint Clause then each go in separate line
                if (CommonFormattingHelpers.HasAnyWhitespaceElasticTrivia(previousToken, currentToken) &&
                    currentToken.IsKind(SyntaxKind.WhereKeyword) &&
                    currentToken.Parent.IsKind(SyntaxKind.TypeParameterConstraintClause))
                {
                    // Check if there is another TypeParameterConstraintClause before
                    if (previousToken.Parent.Ancestors().OfType <TypeParameterConstraintClauseSyntax>().Any())
                    {
                        return(CreateAdjustNewLinesOperation(1, AdjustNewLinesOption.PreserveLines));
                    }

                    // Check if there is another TypeParameterConstraintClause after
                    var firstTokenAfterTypeConstraint = currentToken.Parent.GetLastToken().GetNextToken();
                    var lastTokenForTypeConstraint    = currentToken.Parent.GetLastToken().GetNextToken();
                    if (CommonFormattingHelpers.HasAnyWhitespaceElasticTrivia(lastTokenForTypeConstraint, firstTokenAfterTypeConstraint) &&
                        firstTokenAfterTypeConstraint.IsKind(SyntaxKind.WhereKeyword) &&
                        firstTokenAfterTypeConstraint.Parent.IsKind(SyntaxKind.TypeParameterConstraintClause))
                    {
                        return(CreateAdjustNewLinesOperation(1, AdjustNewLinesOption.PreserveLines));
                    }
                }

                return(null);
            }

            // if operation is already forced, return as it is.
            if (operation.Option == AdjustNewLinesOption.ForceLines)
            {
                return(operation);
            }

            if (!CommonFormattingHelpers.HasAnyWhitespaceElasticTrivia(previousToken, currentToken))
            {
                return(operation);
            }

            var betweenMemberOperation = GetAdjustNewLinesOperationBetweenMembers(previousToken, currentToken);

            if (betweenMemberOperation != null)
            {
                return(betweenMemberOperation);
            }

            var line = Math.Max(LineBreaksAfter(previousToken, currentToken), operation.Line);

            if (line == 0)
            {
                return(CreateAdjustNewLinesOperation(0, AdjustNewLinesOption.PreserveLines));
            }

            return(CreateAdjustNewLinesOperation(line, AdjustNewLinesOption.ForceLines));
        }
            public AbstractComplexTrivia(OptionSet optionSet, TreeData treeInfo, SyntaxToken token1, SyntaxToken token2)
                : base(optionSet, token1.Language)
            {
                Contract.ThrowIfNull(treeInfo);

                _token1 = token1;
                _token2 = token2;

                _treatAsElastic = CommonFormattingHelpers.HasAnyWhitespaceElasticTrivia(token1, token2);

                this.TreeInfo       = treeInfo;
                this.OriginalString = this.TreeInfo.GetTextBetween(token1, token2);
                ExtractLineAndSpace(this.OriginalString, out var lineBreaks, out var spaces);

                this.LineBreaks = lineBreaks;
                this.Spaces     = spaces;
            }