private bool ApplyDynamicSpacesOperation(AdjustSpacesOperation operation, int pairIndex)
            {
                var triviaInfo = _tokenStream.GetTriviaData(pairIndex);

                if (triviaInfo.SecondTokenIsFirstTokenOnLine)
                {
                    return false;
                }

                Contract.ThrowIfFalse(triviaInfo.LineBreaks == 0);

                var indentation = _context.GetBaseIndentation(_tokenStream.GetToken(pairIndex + 1));

                var previousToken = _tokenStream.GetToken(pairIndex);

                bool multipleLines;
                int tokenLength;
                _tokenStream.GetTokenLength(previousToken, out tokenLength, out multipleLines);

                // get end column of previous token
                var endColumnOfPreviousToken = multipleLines ? tokenLength : _tokenStream.GetCurrentColumn(previousToken) + tokenLength;

                // check whether current position is less than indentation
                if (endColumnOfPreviousToken < indentation)
                {
                    _tokenStream.ApplyChange(pairIndex, triviaInfo.WithSpace(indentation - endColumnOfPreviousToken, _context, _formattingRules));
                    return true;
                }

                // delegate to normal singleline space applier
                return ApplySpaceIfSingleLine(operation, pairIndex);
            }
            public bool ApplyForceSpacesOperation(AdjustSpacesOperation operation, int pairIndex)
            {
                var triviaInfo = this.tokenStream.GetTriviaData(pairIndex);

                if (triviaInfo.LineBreaks == 0 && triviaInfo.Spaces == operation.Space)
                {
                    return false;
                }

                this.tokenStream.ApplyChange(pairIndex, triviaInfo.WithSpace(operation.Space, context, formattingRules));
                return true;
            }
            public bool Apply(AdjustSpacesOperation operation, int pairIndex)
            {
                if (operation.Option == AdjustSpacesOption.PreserveSpaces)
                {
                    return ApplyPreserveSpacesOperation(operation, pairIndex);
                }

                if (operation.Option == AdjustSpacesOption.ForceSpaces)
                {
                    return ApplyForceSpacesOperation(operation, pairIndex);
                }

                return ApplySpaceIfSingleLine(operation, pairIndex);
            }
Esempio n. 4
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;
        }
            private bool ApplySpaceIfSingleLine(AdjustSpacesOperation operation, int pairIndex)
            {
                var triviaInfo = this.tokenStream.GetTriviaData(pairIndex);
                var space = operation.Space;

                if (triviaInfo.SecondTokenIsFirstTokenOnLine)
                {
                    return false;
                }

                Contract.ThrowIfFalse(triviaInfo.LineBreaks == 0);

                if (triviaInfo.Spaces == space)
                {
                    return false;
                }

                this.tokenStream.ApplyChange(pairIndex, triviaInfo.WithSpace(space, context, formattingRules));
                return true;
            }
            private bool ApplyPreserveSpacesOperation(AdjustSpacesOperation operation, int pairIndex)
            {
                var triviaInfo = _tokenStream.GetTriviaData(pairIndex);
                var space = operation.Space;

                if (triviaInfo.SecondTokenIsFirstTokenOnLine)
                {
                    return false;
                }

                Contract.ThrowIfFalse(triviaInfo.LineBreaks == 0);

                if (space <= triviaInfo.Spaces)
                {
                    return false;
                }

                _tokenStream.ApplyChange(pairIndex, triviaInfo.WithSpace(space, _context, _formattingRules));
                return true;
            }