Example #1
0
            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 single-line space applier
                return(ApplySpaceIfSingleLine(operation, pairIndex));
            }
Example #2
0
            private bool ApplySpaceIfSingleLine(AdjustSpacesOperation operation, int pairIndex)
            {
                var triviaInfo = _context.TokenStream.GetTriviaData(pairIndex);
                var space      = operation.Space;

                if (triviaInfo.SecondTokenIsFirstTokenOnLine)
                {
                    return(false);
                }

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

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

                Debug.Assert(!_context.IsFormattingDisabled(pairIndex));

                _context.TokenStream.ApplyChange(
                    pairIndex,
                    triviaInfo.WithSpace(space, _context, _formattingRules)
                    );
                return(true);
            }
Example #3
0
            public bool ApplyForceSpacesOperation(AdjustSpacesOperation operation, int pairIndex)
            {
                var triviaInfo = _tokenStream.GetTriviaData(pairIndex);

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

                _tokenStream.ApplyChange(pairIndex, triviaInfo.WithSpace(operation.Space, _context, _formattingRules));
                return(true);
            }
Example #4
0
            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));
            }
Example #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;
        }
Example #6
0
            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);
            }