예제 #1
0
        public TokenPairWithOperations(
            TokenStream tokenStream,
            int tokenPairIndex,
            AdjustSpacesOperation spaceOperations,
            AdjustNewLinesOperation lineOperations) :
            this()
        {
            Contract.ThrowIfNull(tokenStream);

            Contract.ThrowIfFalse(0 <= tokenPairIndex && tokenPairIndex < tokenStream.TokenCount - 1);

            this.tokenStream = tokenStream;
            this.PairIndex = tokenPairIndex;

            SpaceOperation = spaceOperations;
            LineOperation = lineOperations;
        }
            public bool ApplyPreserveLinesOperation(
                AdjustNewLinesOperation operation, int pairIndex, CancellationToken cancellationToken)
            {
                var triviaInfo = this.tokenStream.GetTriviaData(pairIndex);

                // okay, check whether there is line between token more than we want
                // check whether we should force it if it is less than given number
                var indentation = this.context.GetBaseIndentation(this.tokenStream.GetToken(pairIndex + 1));
                if (operation.Line > triviaInfo.LineBreaks)
                {
                    // alright force them
                    this.tokenStream.ApplyChange(pairIndex, triviaInfo.WithLine(operation.Line, indentation, context, formattingRules, cancellationToken));
                    return true;
                }

                // lines between tokens are as expected, but indentation is not right
                if (triviaInfo.SecondTokenIsFirstTokenOnLine &&
                    indentation != triviaInfo.Spaces)
                {
                    this.tokenStream.ApplyChange(pairIndex, triviaInfo.WithIndentation(indentation, context, formattingRules, cancellationToken));
                    return true;
                }

                // if PreserveLineOperation's line is set to 0, let space operation to override wrapping operation
                return operation.Line > 0;
            }
            private bool ApplyForceLinesOperation(AdjustNewLinesOperation operation, int pairIndex, CancellationToken cancellationToken)
            {
                var triviaInfo = this.tokenStream.GetTriviaData(pairIndex);

                var indentation = this.context.GetBaseIndentation(this.tokenStream.GetToken(pairIndex + 1));
                if (triviaInfo.LineBreaks == operation.Line && triviaInfo.Spaces == indentation && !triviaInfo.TreatAsElastic)
                {
                    // things are already in the shape we want, so we don't actually need to do
                    // anything but, conceptually, we handled this case
                    return true;
                }

                // well, force it regardless original content
                this.tokenStream.ApplyChange(pairIndex, triviaInfo.WithLine(operation.Line, indentation, context, formattingRules, cancellationToken));
                return true;
            }
            public bool Apply(AdjustNewLinesOperation operation, int pairIndex, CancellationToken cancellationToken)
            {
                if (operation.Option == AdjustNewLinesOption.PreserveLines)
                {
                    return ApplyPreserveLinesOperation(operation, pairIndex, cancellationToken);
                }
                else if (operation.Option == AdjustNewLinesOption.ForceLines)
                {
                    return ApplyForceLinesOperation(operation, pairIndex, cancellationToken);
                }
                else
                {
                    Debug.Assert(operation.Option == AdjustNewLinesOption.ForceIfSameLine);

                    // We force the tokens to the different line only they are on the same line
                    // else we leave the tokens as it is (Note: We should not preserve too. If we
                    // we do, then that will be counted as a line operation and the indentation of
                    // the second token will be modified)
                    if (tokenStream.TwoTokensOriginallyOnSameLine(tokenStream.GetToken(pairIndex),
                                                                  tokenStream.GetToken(pairIndex + 1)))
                    {
                        return ApplyForceLinesOperation(operation, pairIndex, cancellationToken);
                    }
                    else
                    {
                        return false;
                    }
                }
            }