Example #1
0
        public static bool GetKeywordArgumentBounds(TokenStream <RdToken> tokens, out int startTokenIndex, out int endTokenIndex)
        {
            startTokenIndex = -1;
            endTokenIndex   = -1;

            TokenBraceCounter <RdToken> braceCounter = new TokenBraceCounter <RdToken>(
                new RdToken(RdTokenType.OpenCurlyBrace),
                new RdToken(RdTokenType.CloseCurlyBrace),
                new RdToken(RdTokenType.OpenSquareBracket),
                new RdToken(RdTokenType.CloseSquareBracket),
                new RdTokenTypeComparer());

            for (int pos = tokens.Position; pos < tokens.Length; pos++)
            {
                RdToken token = tokens[pos];

                if (braceCounter.CountBrace(token))
                {
                    if (startTokenIndex < 0)
                    {
                        startTokenIndex = pos;
                    }

                    if (braceCounter.Count == 0)
                    {
                        endTokenIndex = pos;
                        break;
                    }
                }
            }

            return(startTokenIndex >= 0 && endTokenIndex >= 0 && startTokenIndex < endTokenIndex);
        }
Example #2
0
        private int FindMatchingCloseBrace(TokenStream <RToken> tokens, int openBraceTokenIndex)
        {
            var position = tokens.Position;

            tokens.Position = openBraceTokenIndex;

            var result = TokenBraceCounter <RToken> .GetMatchingBrace(tokens,
                                                                      new RToken(RTokenType.OpenCurlyBrace), new RToken(RTokenType.CloseCurlyBrace),
                                                                      new RTokenTypeComparer());

            tokens.Position = position;
            return(result);
        }
Example #3
0
        public FormattingScope(TextBuilder tb, TokenStream <RToken> tokens, RFormatOptions options)
        {
            Debug.Assert(tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace);

            _options = options;
            _tb      = tb;

            CloseBracePosition = TokenBraceCounter <RToken> .GetMatchingBrace(tokens,
                                                                              new RToken(RTokenType.OpenCurlyBrace), new RToken(RTokenType.CloseCurlyBrace),
                                                                              new RTokenTypeComparer());

            _previousIndentLevel = tb.IndentBuilder.IndentLevel;
            tb.IndentBuilder.SetIndentLevelForSize(CurrentLineIndent(tb));
        }
Example #4
0
        public bool Open(ITextProvider textProvider, TokenStream <RToken> tokens, RFormatOptions options)
        {
            Debug.Assert(tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace);

            // When formatting scope in function arguments, use user indent
            // where appropriate. User indent can be determined by
            //  a. current indentation of { if 'braces on new line' is on
            //     and the open brace indent is deeper than the default.
            //  b. if the above does not apply, it is equal to the indent
            //     of the previous line.

            // System.Action x = () => {
            // }; <<- equal to the statement indent.

            // System.Action x = () =>
            // { <<- equal to the statement indent.
            // };

            // System.Action x = () =>
            //      {
            //      }; <<- based on the *opening* brace position.

            CloseBracePosition = TokenBraceCounter <RToken> .GetMatchingBrace(tokens,
                                                                              new RToken(RTokenType.OpenCurlyBrace), new RToken(RTokenType.CloseCurlyBrace), new RTokenTypeComparer());

            //if (CloseBracePosition > 0)
            //{
            //    if (!IsLineBreakInRange(textProvider, tokens.CurrentToken.End, tokens[CloseBracePosition].Start))
            //    {
            //        SuppressLineBreakCount++;
            //        return true;
            //    }
            //}

            if (options.BracesOnNewLine)
            {
                // If open curly is on its own line (there is only whitespace
                // between line break and the curly, find out current indent
                // and if it is deeper than the default one, use it,
                // otherwise continue with default.
                CompareAndSetIndent(textProvider, tokens, tokens.CurrentToken.Start, options);
                return(true);
            }

            return(false);
        }
Example #5
0
        /// <summary>
        /// Formats conditional statement after 'if' or 'while'
        /// </summary>
        private void AppendCondition()
        {
            // Expected open brace
            if (_tokens.CurrentToken.TokenType != RTokenType.OpenBrace)
            {
                return;
            }

            var braceCounter = new TokenBraceCounter <RToken>(new RToken(RTokenType.OpenBrace), new RToken(RTokenType.CloseBrace), new RTokenTypeComparer());

            braceCounter.CountBrace(_tokens.CurrentToken);

            AppendToken(leadingSpace: LeadingSpaceNeeded(), trailingSpace: false);

            while (braceCounter.Count > 0 && !_tokens.IsEndOfStream())
            {
                braceCounter.CountBrace(_tokens.CurrentToken);

                if (ShouldAppendTextBeforeToken())
                {
                    AppendTextBeforeToken();
                }

                switch (_tokens.CurrentToken.TokenType)
                {
                case RTokenType.Keyword:
                    AppendKeyword();
                    break;

                case RTokenType.Comma:
                    AppendToken(leadingSpace: false, trailingSpace: _options.SpaceAfterComma);
                    break;

                case RTokenType.Operator:
                    AppendOperator();
                    break;

                default:
                    AppendToken(leadingSpace: LeadingSpaceNeeded(), trailingSpace: false);
                    break;
                }
            }
        }
Example #6
0
        /// <summary>
        /// Calculates position of a end of a single-line scope.
        /// The simple scope ends at a line break or at opening
        /// or closing curly brace.
        /// </summary>
        /// <returns></returns>
        private int GetSingleLineScopeEnd()
        {
            int closeBraceIndex = TokenBraceCounter <RToken> .GetMatchingBrace(
                _tokens,
                new RToken(RTokenType.OpenCurlyBrace),
                new RToken(RTokenType.CloseCurlyBrace),
                new RTokenTypeComparer());

            int end = closeBraceIndex < 0 ? _textProvider.Length : _tokens[closeBraceIndex].End;

            for (int i = _tokens.CurrentToken.End; i < end; i++)
            {
                if (CharExtensions.IsLineBreak(_textProvider[i]))
                {
                    return(-1);
                }
            }

            return(end);
        }
Example #7
0
        /// <summary>
        /// Formats conditional statement after 'if' or 'while'
        /// </summary>
        private void AppendCondition() {
            // Expected open brace
            if (_tokens.CurrentToken.TokenType != RTokenType.OpenBrace) {
                return;
            }

            var braceCounter = new TokenBraceCounter<RToken>(new RToken(RTokenType.OpenBrace), new RToken(RTokenType.CloseBrace), new RTokenTypeComparer());
            braceCounter.CountBrace(_tokens.CurrentToken);

            AppendToken(leadingSpace: LeadingSpaceNeeded(), trailingSpace: false);

            while (braceCounter.Count > 0 && !_tokens.IsEndOfStream()) {
                if (braceCounter.CountBrace(_tokens.CurrentToken)) {
                    AppendToken(leadingSpace: LeadingSpaceNeeded(), trailingSpace: false);
                    continue;
                }

                switch (_tokens.CurrentToken.TokenType) {
                    case RTokenType.Keyword:
                        AppendKeyword();
                        break;

                    case RTokenType.Comma:
                        AppendToken(leadingSpace: false, trailingSpace: _options.SpaceAfterComma);
                        break;

                    case RTokenType.Operator:
                        AppendOperator();
                        break;

                    default:
                        AppendToken(leadingSpace: LeadingSpaceNeeded(), trailingSpace: false);
                        break;
                }
            }
        }