Пример #1
0
 private bool Myself(IndentBlockOperation operation)
 {
     return(operation.TextSpan == _span &&
            operation.StartToken == _token1 &&
            operation.EndToken == _token2 &&
            operation.IndentationDeltaOrPosition == _baseIndentation &&
            operation.Option == IndentBlockOption.AbsolutePosition);
 }
Пример #2
0
        private IndentBlockOperation CloneAndAdjustFormattingOperation(IndentBlockOperation operation)
        {
            switch (operation.Option)
            {
            case IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine:
                return(FormattingOperations.CreateRelativeIndentBlockOperation(operation.BaseToken, operation.StartToken, operation.EndToken, AdjustTextSpan(operation.TextSpan), operation.IndentationDeltaOrPosition, operation.Option));

            case IndentBlockOption.RelativePosition:
            case IndentBlockOption.AbsolutePosition:
                return(FormattingOperations.CreateIndentBlockOperation(operation.StartToken, operation.EndToken, AdjustTextSpan(operation.TextSpan), operation.IndentationDeltaOrPosition, operation.Option));

            default:
                throw ExceptionUtilities.Unreachable;
            }
        }
Пример #3
0
        public void AddIndentBlockOperation(IndentBlockOperation operation)
        {
            var intervalTreeSpan = operation.TextSpan;

            // don't add stuff if it is empty
            if (intervalTreeSpan.IsEmpty ||
                _indentationMap.Contains(intervalTreeSpan))
            {
                return;
            }

            // relative indentation case where indentation depends on other token
            if (operation.IsRelativeIndentation)
            {
                var inseparableRegionStartingPosition = operation.Option.IsOn(IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine) ? _tokenStream.FirstTokenOfBaseTokenLine(operation.BaseToken).FullSpan.Start : operation.BaseToken.FullSpan.Start;
                var relativeIndentationGetter = new Lazy<int>(() =>
                {
                    var indentationDelta = operation.IndentationDeltaOrPosition * this.OptionSet.GetOption(FormattingOptions.IndentationSize, _language);

                    // baseIndentation is calculated for the adjusted token if option is RelativeToFirstTokenOnBaseTokenLine
                    var baseIndentation = _tokenStream.GetCurrentColumn(operation.Option.IsOn(IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine) ?
                                                                            _tokenStream.FirstTokenOfBaseTokenLine(operation.BaseToken) :
                                                                            operation.BaseToken);

                    return baseIndentation + indentationDelta;
                }, isThreadSafe: true);

                // set new indentation
                var relativeIndentationData = new RelativeIndentationData(inseparableRegionStartingPosition, intervalTreeSpan, operation, relativeIndentationGetter);

                _indentationTree.AddIntervalInPlace(relativeIndentationData);
                _relativeIndentationTree.AddIntervalInPlace(relativeIndentationData);
                _indentationMap.Add(intervalTreeSpan);

                return;
            }

            // absolute position case
            if (operation.Option.IsOn(IndentBlockOption.AbsolutePosition))
            {
                _indentationTree.AddIntervalInPlace(new SimpleIndentationData(intervalTreeSpan, operation.IndentationDeltaOrPosition));
                _indentationMap.Add(intervalTreeSpan);
                return;
            }

            // regular indentation case where indentation is based on its previous indentation
            var indentationData = _indentationTree.GetSmallestContainingInterval(operation.TextSpan.Start, 0);
            if (indentationData == null)
            {
                // no previous indentation
                var indentation = operation.IndentationDeltaOrPosition * this.OptionSet.GetOption(FormattingOptions.IndentationSize, _language);
                _indentationTree.AddIntervalInPlace(new SimpleIndentationData(intervalTreeSpan, indentation));
                _indentationMap.Add(intervalTreeSpan);
                return;
            }

            // get indentation based on its previous indentation
            var indentationGetter = new Lazy<int>(() =>
            {
                var indentationDelta = operation.IndentationDeltaOrPosition * this.OptionSet.GetOption(FormattingOptions.IndentationSize, _language);

                return indentationData.Indentation + indentationDelta;
            }, isThreadSafe: true);

            // set new indentation
            _indentationTree.AddIntervalInPlace(new LazyIndentationData(intervalTreeSpan, indentationGetter));
            _indentationMap.Add(intervalTreeSpan);
        }
        public int GetIndentationOfCurrentPosition(
            SyntaxNode root,
            IndentBlockOperation startingOperation,
            Func<SyntaxToken, int> tokenColumnGetter,
            CancellationToken cancellationToken)
        {
            var token = startingOperation.StartToken;

            // gather all indent operations 
            var list = GetParentIndentBlockOperations(token);

            // remove one that is smaller than current one
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (CommonFormattingHelpers.IndentBlockOperationComparer(startingOperation, list[i]) < 0)
                {
                    list.RemoveAt(i);
                }
                else
                {
                    break;
                }
            }

            return GetIndentationOfCurrentPosition(root, token, list, token.SpanStart, /* extraSpaces */ 0, tokenColumnGetter, cancellationToken);
        }
 private IndentBlockOperation CloneAndAdjustFormattingOperation(IndentBlockOperation operation)
 {
     switch (operation.Option)
     {
         case IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine:
             return FormattingOperations.CreateRelativeIndentBlockOperation(operation.BaseToken, operation.StartToken, operation.EndToken, AdjustTextSpan(operation.TextSpan), operation.IndentationDeltaOrPosition, operation.Option);
         case IndentBlockOption.RelativePosition:
         case IndentBlockOption.AbsolutePosition:
             return FormattingOperations.CreateIndentBlockOperation(operation.StartToken, operation.EndToken, AdjustTextSpan(operation.TextSpan), operation.IndentationDeltaOrPosition, operation.Option);
         default:
             throw ExceptionUtilities.Unreachable;
     }
 }
 private bool Myself(IndentBlockOperation operation)
 {
     return operation.TextSpan == _span &&
            operation.StartToken == _token1 &&
            operation.EndToken == _token2 &&
            operation.IndentationDeltaOrPosition == _baseIndentation &&
            operation.Option == IndentBlockOption.AbsolutePosition;
 }