public override void Format(
                FormattingContext context,
                ChainedFormattingRules formattingRules,
                Action<int, TriviaData> formattingResultApplier,
                CancellationToken cancellationToken,
                int tokenPairIndex = TokenPairIndexNotNeeded)
            {
                Contract.ThrowIfFalse(this.SecondTokenIsFirstTokenOnLine);

                var token1 = _original.Token1;
                var token2 = _original.Token2;

                var triviaList = new TriviaList(token1.TrailingTrivia, token2.LeadingTrivia);
                Contract.ThrowIfFalse(triviaList.Count > 0);

                // okay, now, check whether we need or are able to format noisy tokens
                if (CodeShapeAnalyzer.ContainsSkippedTokensOrText(triviaList))
                {
                    return;
                }

                formattingResultApplier(tokenPairIndex,
                    new FormattedComplexTrivia(
                        context,
                        formattingRules,
                        _original.Token1,
                        _original.Token2,
                        this.LineBreaks,
                        this.Spaces,
                        _original.OriginalString,
                        cancellationToken));
            }
            private bool ShouldFormat(FormattingContext context)
            {
                var commonToken1 = this.Token1;
                var commonToken2 = this.Token2;

                var triviaList = new TriviaList(commonToken1.TrailingTrivia, commonToken2.LeadingTrivia);

                Contract.ThrowIfFalse(triviaList.Count > 0);

                // okay, now, check whether we need or are able to format noisy tokens
                if (ContainsSkippedTokensOrText(triviaList))
                {
                    return(false);
                }

                if (!this.SecondTokenIsFirstTokenOnLine)
                {
                    return(CodeShapeAnalyzer.ShouldFormatSingleLine(triviaList));
                }

                Debug.Assert(this.SecondTokenIsFirstTokenOnLine);

                if (this.OptionSet.GetOption(FormattingOptions.UseTabs, LanguageNames.CSharp))
                {
                    return(true);
                }

                var firstTriviaInTree = this.Token1.CSharpKind() == SyntaxKind.None;

                return(CodeShapeAnalyzer.ShouldFormatMultiLine(context, firstTriviaInTree, triviaList));
            }
            public override void Format(
                FormattingContext context,
                ChainedFormattingRules formattingRules,
                Action <int, TokenStream, TriviaData> formattingResultApplier,
                CancellationToken cancellationToken,
                int tokenPairIndex = TokenPairIndexNotNeeded)
            {
                Contract.ThrowIfFalse(this.SecondTokenIsFirstTokenOnLine);

                var token1 = _original.Token1;
                var token2 = _original.Token2;

                var triviaList = new TriviaList(token1.TrailingTrivia, token2.LeadingTrivia);

                Contract.ThrowIfFalse(triviaList.Count > 0);

                // okay, now, check whether we need or are able to format noisy tokens
                if (CodeShapeAnalyzer.ContainsSkippedTokensOrText(triviaList))
                {
                    return;
                }

                formattingResultApplier(tokenPairIndex,
                                        context.TokenStream,
                                        new FormattedComplexTrivia(
                                            context,
                                            formattingRules,
                                            _original.Token1,
                                            _original.Token2,
                                            this.LineBreaks,
                                            this.Spaces,
                                            _original.OriginalString,
                                            cancellationToken));
            }
Beispiel #4
0
            internal Enumerator(TriviaList triviaList)
            {
                _list1 = triviaList._list1;
                _list2 = triviaList._list2;

                _index      = -1;
                _enumerator = _list1.GetEnumerator();
            }
            public static bool ShouldFormatMultiLine(
                FormattingContext context,
                bool firstTriviaInTree,
                TriviaList triviaList
                )
            {
                var analyzer = new CodeShapeAnalyzer(context, firstTriviaInTree, triviaList);

                return(analyzer.ShouldFormat());
            }
Beispiel #6
0
            private CodeShapeAnalyzer(FormattingContext context, bool firstTriviaInTree, TriviaList triviaList)
            {
                _context    = context;
                _options    = context.Options;
                _triviaList = triviaList;

                _indentation        = 0;
                _hasTrailingSpace   = false;
                _lastLineBreakIndex = firstTriviaInTree ? 0 : -1;
                _touchedNoisyCharacterOnCurrentLine = false;
            }
Beispiel #7
0
            private CodeShapeAnalyzer(FormattingContext context, bool firstTriviaInTree, TriviaList triviaList)
            {
                this.context    = context;
                this.optionSet  = context.OptionSet;
                this.triviaList = triviaList;

                this.indentation        = 0;
                this.hasTrailingSpace   = false;
                this.lastLineBreakIndex = firstTriviaInTree ? 0 : -1;
                this.touchedNoisyCharacterOnCurrentLine = false;
            }
Beispiel #8
0
            public static bool ContainsSkippedTokensOrText(TriviaList list)
            {
                foreach (var trivia in list)
                {
                    if (trivia.Kind() == SyntaxKind.SkippedTokensTrivia ||
                        trivia.Kind() == SyntaxKind.PreprocessingMessageTrivia)
                    {
                        return(true);
                    }
                }

                return(false);
            }
            public static bool ContainsSkippedTokensOrText(TriviaList list)
            {
                foreach (var trivia in list)
                {
                    if (trivia.Kind() == SyntaxKind.SkippedTokensTrivia ||
                        trivia.Kind() == SyntaxKind.PreprocessingMessageTrivia)
                    {
                        return true;
                    }
                }

                return false;
            }
            private bool ShouldFormat(FormattingContext context)
            {
                var commonToken1 = this.Token1;
                var commonToken2 = this.Token2;

                var formatSpanEnd =
                    commonToken2.Kind() == SyntaxKind.None
                        ? commonToken1.Span.End
                        : commonToken2.Span.Start;
                var span = TextSpan.FromBounds(commonToken1.Span.End, formatSpanEnd);

                if (context.IsSpacingSuppressed(span, TreatAsElastic))
                {
                    return(false);
                }

                var triviaList = new TriviaList(
                    commonToken1.TrailingTrivia,
                    commonToken2.LeadingTrivia
                    );

                Contract.ThrowIfFalse(triviaList.Count > 0);

                // okay, now, check whether we need or are able to format noisy tokens
                if (ContainsSkippedTokensOrText(triviaList))
                {
                    return(false);
                }

                if (!this.SecondTokenIsFirstTokenOnLine)
                {
                    return(CodeShapeAnalyzer.ShouldFormatSingleLine(triviaList));
                }

                Debug.Assert(this.SecondTokenIsFirstTokenOnLine);

                if (this.Options.GetOption(FormattingOptions2.UseTabs))
                {
                    return(true);
                }

                var firstTriviaInTree = this.Token1.Kind() == SyntaxKind.None;

                return(CodeShapeAnalyzer.ShouldFormatMultiLine(
                           context,
                           firstTriviaInTree,
                           triviaList
                           ));
            }
            public static bool ShouldFormatSingleLine(TriviaList list)
            {
                foreach (var commonTrivia in list)
                {
                    var trivia = (SyntaxTrivia)commonTrivia;

                    Contract.ThrowIfTrue(trivia.Kind() == SyntaxKind.EndOfLineTrivia);
                    Contract.ThrowIfTrue(trivia.Kind() == SyntaxKind.SkippedTokensTrivia);
                    Contract.ThrowIfTrue(trivia.Kind() == SyntaxKind.PreprocessingMessageTrivia);

                    // if it contains elastic trivia, always format
                    if (trivia.IsElastic())
                    {
                        return true;
                    }

                    if (trivia.Kind() == SyntaxKind.WhitespaceTrivia)
                    {
                        Debug.Assert(trivia.ToString() == trivia.ToFullString());
                        var text = trivia.ToString();
                        if (text.IndexOf('\t') >= 0)
                        {
                            return true;
                        }
                    }

                    // we don't touch space between two tokens on a single line that contains
                    // multiline comments between them
                    if (trivia.IsRegularOrDocComment())
                    {
                        return false;
                    }

                    if (trivia.Kind() == SyntaxKind.RegionDirectiveTrivia ||
                        trivia.Kind() == SyntaxKind.EndRegionDirectiveTrivia ||
                        SyntaxFacts.IsPreprocessorDirective(trivia.Kind()))
                    {
                        return false;
                    }
                }

                return true;
            }
Beispiel #12
0
            public static bool ShouldFormatSingleLine(TriviaList list)
            {
                foreach (var commonTrivia in list)
                {
                    var trivia = (SyntaxTrivia)commonTrivia;

                    Contract.ThrowIfTrue(trivia.CSharpKind() == SyntaxKind.EndOfLineTrivia);
                    Contract.ThrowIfTrue(trivia.CSharpKind() == SyntaxKind.SkippedTokensTrivia);
                    Contract.ThrowIfTrue(trivia.CSharpKind() == SyntaxKind.PreprocessingMessageTrivia);

                    // if it contains elastic trivia, always format
                    if (trivia.IsElastic())
                    {
                        return(true);
                    }

                    if (trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia)
                    {
                        Debug.Assert(trivia.ToString() == trivia.ToFullString());
                        var text = trivia.ToString();
                        if (text.IndexOf('\t') >= 0)
                        {
                            return(true);
                        }
                    }

                    // we don't touch space between two tokens on a single line that contains
                    // multiline comments between them
                    if (trivia.IsRegularOrDocComment())
                    {
                        return(false);
                    }

                    if (trivia.CSharpKind() == SyntaxKind.RegionDirectiveTrivia ||
                        trivia.CSharpKind() == SyntaxKind.EndRegionDirectiveTrivia ||
                        SyntaxFacts.IsPreprocessorDirective(trivia.CSharpKind()))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            private bool ShouldFormat(FormattingContext context)
            {
                var commonToken1 = this.Token1;
                var commonToken2 = this.Token2;

                var formatSpanEnd = commonToken2.Kind() == SyntaxKind.None ? commonToken1.Span.End : commonToken2.Span.Start;
                var span = TextSpan.FromBounds(commonToken1.Span.End, formatSpanEnd);
                if (context.IsSpacingSuppressed(span))
                {
                    return false;
                }

                var triviaList = new TriviaList(commonToken1.TrailingTrivia, commonToken2.LeadingTrivia);
                Contract.ThrowIfFalse(triviaList.Count > 0);

                // okay, now, check whether we need or are able to format noisy tokens
                if (ContainsSkippedTokensOrText(triviaList))
                {
                    return false;
                }

                if (!this.SecondTokenIsFirstTokenOnLine)
                {
                    return CodeShapeAnalyzer.ShouldFormatSingleLine(triviaList);
                }

                Debug.Assert(this.SecondTokenIsFirstTokenOnLine);

                if (this.OptionSet.GetOption(FormattingOptions.UseTabs, LanguageNames.CSharp))
                {
                    return true;
                }

                var firstTriviaInTree = this.Token1.Kind() == SyntaxKind.None;

                return CodeShapeAnalyzer.ShouldFormatMultiLine(context, firstTriviaInTree, triviaList);
            }
            public override TriviaData WithIndentation(
                int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
            {
                // if tokens are not in different line, there is nothing we can do here
                if (!this.SecondTokenIsFirstTokenOnLine)
                {
                    return this;
                }

                // well, we are already in a desired format, nothing to do. return as it is.
                if (this.Spaces == indentation)
                {
                    return this;
                }

                // do expansive check
                // we need to actually format here to find out indentation
                var list = new TriviaList(_token1.TrailingTrivia, _token2.LeadingTrivia);
                Contract.ThrowIfFalse(list.Count > 0);

                if (ContainsSkippedTokensOrText(list))
                {
                    // we can't format
                    return this;
                }

                // okay, we need to do expansive calculation to find out actual space between two tokens
                var trivia = Format(context, formattingRules, this.LineBreaks, indentation, cancellationToken);
                var triviaString = CreateString(trivia, cancellationToken);

                int lineBreaks;
                int spaces;
                ExtractLineAndSpace(triviaString, out lineBreaks, out spaces);

                return CreateComplexTrivia(lineBreaks, spaces, indentation);
            }
            private CodeShapeAnalyzer(FormattingContext context, bool firstTriviaInTree, TriviaList triviaList)
            {
                _context = context;
                _optionSet = context.OptionSet;
                _triviaList = triviaList;

                _indentation = 0;
                _hasTrailingSpace = false;
                _lastLineBreakIndex = firstTriviaInTree ? 0 : -1;
                _touchedNoisyCharacterOnCurrentLine = false;
            }
 public static bool ShouldFormatMultiLine(FormattingContext context, bool firstTriviaInTree, TriviaList triviaList)
 {
     var analyzer = new CodeShapeAnalyzer(context, firstTriviaInTree, triviaList);
     return analyzer.ShouldFormat();
 }
Beispiel #17
0
 declaration = declaration.ReplaceToken(previousToken, previousToken.WithTrailingTrivia(TriviaList(ElasticSpace)));
 protected override bool ContainsSkippedTokensOrText(TriviaList list)
 {
     return CodeShapeAnalyzer.ContainsSkippedTokensOrText(list);
 }
 protected override bool ContainsSkippedTokensOrText(TriviaList list)
 {
     return(CodeShapeAnalyzer.ContainsSkippedTokensOrText(list));
 }
 protected abstract bool ContainsSkippedTokensOrText(TriviaList list);