Exemple #1
0
            public bool ApplyPreserveLinesOperation(
                AdjustNewLinesOperation operation, int pairIndex, CancellationToken cancellationToken)
            {
                var triviaInfo = _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 = _context.GetBaseIndentation(_tokenStream.GetToken(pairIndex + 1));

                if (operation.Line > triviaInfo.LineBreaks)
                {
                    // alright force them
                    _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)
                {
                    _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);
            }
Exemple #2
0
            public bool ApplyPreserveLinesOperation(
                AdjustNewLinesOperation operation, int pairIndex, CancellationToken cancellationToken)
            {
                var triviaInfo = _context.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 = _context.GetBaseIndentation(_context.TokenStream.GetToken(pairIndex + 1));

                if (operation.Line > triviaInfo.LineBreaks)
                {
                    Debug.Assert(!_context.IsFormattingDisabled(pairIndex));

                    // alright force them
                    _context.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)
                {
                    // Formatting can only be disabled for entire lines. This block only modifies the line containing
                    // the second token of the current pair, so we only need to check for disabled formatting at the
                    // starting position of the second token of the pair.
                    Debug.Assert(!_context.IsFormattingDisabled(new TextSpan(_context.TokenStream.GetToken(pairIndex + 1).SpanStart, 0)));

                    _context.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);
            }
Exemple #3
0
            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.ForceLinesIfOnSingleLine);

                    // 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.TwoTokensOnSameLine(_tokenStream.GetToken(pairIndex),
                                                         _tokenStream.GetToken(pairIndex + 1)))
                    {
                        return(ApplyForceLinesOperation(operation, pairIndex, cancellationToken));
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
Exemple #4
0
            private bool ApplyForceLinesOperation(AdjustNewLinesOperation operation, int pairIndex, CancellationToken cancellationToken)
            {
                var triviaInfo = _tokenStream.GetTriviaData(pairIndex);

                var indentation = _context.GetBaseIndentation(_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
                _tokenStream.ApplyChange(pairIndex, triviaInfo.WithLine(operation.Line, indentation, _context, _formattingRules, cancellationToken));
                return(true);
            }
Exemple #5
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;
        }