コード例 #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));
            }
コード例 #2
0
ファイル: FormattingContext.cs プロジェクト: belav/roslyn
        public void Initialize(
            ChainedFormattingRules formattingRules,
            SyntaxToken startToken,
            SyntaxToken endToken,
            CancellationToken cancellationToken
            )
        {
            var rootNode = this.TreeData.Root;

            if (_tokenStream.IsFormattingWholeDocument)
            {
                // if we are trying to format whole document, there is no reason to get initial context. just set
                // initial indentation.
                var data = new RootIndentationData(rootNode);
                _indentationTree.AddIntervalInPlace(data);
                _indentationMap.Add(data.TextSpan);
                return;
            }

            var initialContextFinder = new InitialContextFinder(
                _tokenStream,
                formattingRules,
                rootNode
                );

            var(indentOperations, suppressOperations) = initialContextFinder.Do(
                startToken,
                endToken
                );

            if (indentOperations != null)
            {
                var indentationOperations = indentOperations;

                var initialOperation      = indentationOperations[0];
                var baseIndentationFinder = new BottomUpBaseIndentationFinder(
                    formattingRules,
                    this.Options.GetOption(FormattingOptions2.TabSize),
                    this.Options.GetOption(FormattingOptions2.IndentationSize),
                    _tokenStream,
                    _engine.SyntaxFacts
                    );
                var initialIndentation = baseIndentationFinder.GetIndentationOfCurrentPosition(
                    rootNode,
                    initialOperation,
                    t => _tokenStream.GetCurrentColumn(t),
                    cancellationToken
                    );

                var data = new SimpleIndentationData(initialOperation.TextSpan, initialIndentation);
                _indentationTree.AddIntervalInPlace(data);
                _indentationMap.Add(data.TextSpan);

                // hold onto initial operations
                _initialIndentBlockOperations = indentationOperations;
            }

            suppressOperations?.Do(o => this.AddInitialSuppressOperation(o));
        }