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));
            }
            public override TriviaData WithSpace(int space, FormattingContext context, ChainedFormattingRules formattingRules)
            {
                if (this.LineBreaks == 0 && this.Spaces == space)
                {
                    return this;
                }

                return new ModifiedWhitespace(this.OptionSet, this, /*lineBreak*/0, space, elastic: false, language: this.Language);
            }
 public override void Format(
     FormattingContext context,
     ChainedFormattingRules formattingRules,
     Action<int, TriviaData> formattingResultApplier,
     CancellationToken cancellationToken,
     int tokenPairIndex = TokenPairIndexNotNeeded)
 {
     formattingResultApplier(tokenPairIndex, new FormattedWhitespace(this.OptionSet, this.LineBreaks, this.Spaces, this.Language));
 }
            public override TriviaData WithIndentation(
                int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
            {
                if (this.Spaces == indentation)
                {
                    return this;
                }

                return new ModifiedWhitespace(this.OptionSet, this, this.LineBreaks, indentation, elastic: false, language: this.Language);
            }
            public Partitioner(FormattingContext context, TokenStream tokenStream, TokenPairWithOperations[] operationPairs)
            {
                Contract.ThrowIfNull(context);
                Contract.ThrowIfNull(tokenStream);
                Contract.ThrowIfNull(operationPairs);

                _context = context;
                _tokenStream = tokenStream;
                _operationPairs = operationPairs;
            }
            public override TriviaData WithLine(int line, int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
            {
                Contract.ThrowIfFalse(line > 0);

                if (this.LineBreaks == line && this.Spaces == indentation)
                {
                    return this;
                }

                return new ModifiedWhitespace(this.OptionSet, this, line, indentation, elastic: false, language: this.Language);
            }
 public CSharpTriviaFormatter(
     FormattingContext context,
     ChainedFormattingRules formattingRules,
     SyntaxToken token1,
     SyntaxToken token2,
     string originalString,
     int lineBreaks,
     int spaces) :
     base(context, formattingRules, token1, token2, originalString, lineBreaks, spaces)
 {
 }
            public override TriviaData WithSpace(int space, FormattingContext context, ChainedFormattingRules formattingRules)
            {
                if (_original == null)
                {
                    return base.WithSpace(space, context, formattingRules);
                }

                if (this.LineBreaks == _original.LineBreaks && _original.Spaces == space)
                {
                    return _original;
                }

                return base.WithSpace(space, context, formattingRules);
            }
            public override TriviaData WithLine(int line, int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
            {
                if (_original == null)
                {
                    return base.WithLine(line, indentation, context, formattingRules, cancellationToken);
                }

                if (_original.LineBreaks == line && _original.Spaces == indentation)
                {
                    return _original;
                }

                return base.WithLine(line, indentation, context, formattingRules, cancellationToken);
            }
            public override TriviaData WithIndentation(
                int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
            {
                if (this.original == null)
                {
                    return base.WithIndentation(indentation, context, formattingRules, cancellationToken);
                }

                if (this.LineBreaks == this.original.LineBreaks && this.original.Spaces == indentation)
                {
                    return this.original;
                }

                return base.WithIndentation(indentation, context, formattingRules, cancellationToken);
            }
            public override TriviaData WithSpace(int space, FormattingContext context, ChainedFormattingRules formattingRules)
            {
                // two tokens are on a single line, we don't allow changing spaces between two
                // tokens that contain noisy characters between them.
                if (!this.SecondTokenIsFirstTokenOnLine)
                {
                    return this;
                }

                // okay, two tokens are on different lines, we are basically asked to remove line breaks between them
                // and make them to be on a single line. well, that is not allowed when there are noisy chars between them
                if (this.SecondTokenIsFirstTokenOnLine)
                {
                    return this;
                }

                return Contract.FailWithReturn<TriviaData>("Can not reach here");
            }
            public override TriviaData WithLine(
                int line, int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
            {
                Contract.ThrowIfFalse(line > 0);

                // if we have elastic trivia, always let it be modified
                if (this.TreatAsElastic)
                {
                    return CreateComplexTrivia(line, indentation);
                }

                // two tokens are on a single line, it is always allowed to put those two tokens on a different lines
                if (!this.SecondTokenIsFirstTokenOnLine)
                {
                    return CreateComplexTrivia(line, indentation);
                }

                // okay, two tokens are on different lines, now we need to see whether we can add more lines or not
                if (this.SecondTokenIsFirstTokenOnLine)
                {
                    // we are asked to add more lines. sure, no problem
                    if (this.LineBreaks < line)
                    {
                        return CreateComplexTrivia(line, indentation);
                    }

                    // we already has same number of lines, but it is asking changing indentation
                    if (this.LineBreaks == line)
                    {
                        return WithIndentation(indentation, context, formattingRules, cancellationToken);
                    }

                    // sorry, we can't reduce lines if it contains noisy chars
                    if (this.LineBreaks > line)
                    {
                        return this;
                    }
                }

                return Contract.FailWithReturn<TriviaData>("Can not reach here");
            }
            public FormattedComplexTrivia(
                FormattingContext context,
                ChainedFormattingRules formattingRules,
                SyntaxToken token1,
                SyntaxToken token2,
                int lineBreaks,
                int spaces,
                string originalString,
                CancellationToken cancellationToken) :
                base(context.OptionSet, LanguageNames.CSharp)
            {
                Contract.ThrowIfNull(context);
                Contract.ThrowIfNull(formattingRules);
                Contract.ThrowIfNull(originalString);

                this.LineBreaks = Math.Max(0, lineBreaks);
                this.Spaces = Math.Max(0, spaces);

                _formatter = new CSharpTriviaFormatter(context, formattingRules, token1, token2, originalString, this.LineBreaks, this.Spaces);
                _textChanges = _formatter.FormatToTextChanges(cancellationToken);
            }
            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 WithSpace(int space, FormattingContext context, ChainedFormattingRules formattingRules)
 {
     return _original.WithSpace(space, context, formattingRules);
 }
 public OperationApplier(FormattingContext context, TokenStream tokenStream, ChainedFormattingRules formattingRules)
 {
     this.context = context;
     this.tokenStream = tokenStream;
     this.formattingRules = formattingRules;
 }
 public override void Format(
     FormattingContext context, ChainedFormattingRules formattingRules, Action<int, TriviaData> formattingResultApplier, CancellationToken cancellationToken, int tokenPairIndex = TokenPairIndexNotNeeded)
 {
     throw new NotImplementedException();
 }
            public override void Format(
                FormattingContext context,
                ChainedFormattingRules formattingRules,
                Action<int, TriviaData> formattingResultApplier,
                CancellationToken cancellationToken,
                int tokenPairIndex = TokenPairIndexNotNeeded)
            {
                if (!ShouldFormat(context))
                {
                    return;
                }

                formattingResultApplier(tokenPairIndex, Format(context, formattingRules, this.LineBreaks, this.Spaces, cancellationToken));
            }
 public override void Format(
     FormattingContext context,
     ChainedFormattingRules formattingRules,
     Action<int, TriviaData> formattingResultApplier,
     CancellationToken cancellationToken,
     int tokenPairIndex = TokenPairIndexNotNeeded)
 {
     // nothing changed, nothing to format
 }
 protected abstract TriviaDataWithList Format(FormattingContext context, ChainedFormattingRules formattingRules, int lines, int spaces, CancellationToken cancellationToken);
Exemple #21
0
 public abstract void Format(
     FormattingContext context,
     ChainedFormattingRules formattingRules,
     Action<int, TriviaData> formattingResultApplier,
     CancellationToken cancellationToken,
     int tokenPairIndex = TokenPairIndexNotNeeded);
Exemple #22
0
 public abstract TriviaData WithSpace(int space, FormattingContext context, ChainedFormattingRules formattingRules);
Exemple #23
0
 public abstract TriviaData WithIndentation(int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken);
 public OperationApplier(FormattingContext context, TokenStream tokenStream, ChainedFormattingRules formattingRules)
 {
     _context         = context;
     _tokenStream     = tokenStream;
     _formattingRules = formattingRules;
 }
 public override TriviaData WithIndentation(
     int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
 {
     return _original.WithIndentation(indentation, context, formattingRules, cancellationToken);
 }
Exemple #26
0
 public OperationApplier(FormattingContext context, ChainedFormattingRules formattingRules)
 {
     _context         = context;
     _formattingRules = formattingRules;
 }
Exemple #27
0
 public abstract TriviaData WithSpace(int space, FormattingContext context, ChainedFormattingRules formattingRules);
Exemple #28
0
 protected abstract TriviaDataWithList Format(FormattingContext context, ChainedFormattingRules formattingRules, int lines, int spaces, CancellationToken cancellationToken);
Exemple #29
0
 public abstract TriviaData WithIndentation(int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken);
 public override TriviaData WithIndentation(int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
            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 override TriviaData WithSpace(int space, FormattingContext context, ChainedFormattingRules formattingRules)
 {
     throw new NotImplementedException();
 }
 public override TriviaData WithSpace(int space, FormattingContext context, ChainedFormattingRules formattingRules)
 {
     throw new NotImplementedException();
 }
 public static bool ShouldFormatMultiLine(FormattingContext context, bool firstTriviaInTree, TriviaList triviaList)
 {
     var analyzer = new CodeShapeAnalyzer(context, firstTriviaInTree, triviaList);
     return analyzer.ShouldFormat();
 }
 public override TriviaData WithIndentation(int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
 public override void Format(
     FormattingContext context, ChainedFormattingRules formattingRules, Action <int, TriviaData> formattingResultApplier, CancellationToken cancellationToken, int tokenPairIndex = TokenPairIndexNotNeeded)
 {
     throw new NotImplementedException();
 }
Exemple #38
0
 public OperationApplier(FormattingContext context, TokenStream tokenStream, ChainedFormattingRules formattingRules)
 {
     this.context         = context;
     this.tokenStream     = tokenStream;
     this.formattingRules = formattingRules;
 }
Exemple #39
0
 public abstract void Format(
     FormattingContext context,
     ChainedFormattingRules formattingRules,
     Action <int, TokenStream, TriviaData> formattingResultApplier,
     CancellationToken cancellationToken,
     int tokenPairIndex = TokenPairIndexNotNeeded);
 public OperationApplier(FormattingContext context, TokenStream tokenStream, ChainedFormattingRules formattingRules)
 {
     _context = context;
     _tokenStream = tokenStream;
     _formattingRules = formattingRules;
 }