Ejemplo n.º 1
0
        /// <summary>
        /// Opens new formatting scope. Scope is opened when either
        /// code discovers open curly brace or when line break
        /// suppression is on.
        /// </summary>
        private void OpenFormattingScope()
        {
            Debug.Assert(_tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace);

            // If scope is empty, make it { } unless there is a line break already in it
            if (_tokens.NextToken.TokenType == RTokenType.CloseCurlyBrace &&
                _textProvider.IsWhiteSpaceOnlyRange(_tokens.CurrentToken.End, _tokens.NextToken.Start))
            {
                var ls = _tokens.PreviousToken.TokenType == RTokenType.CloseBrace && _options.SpaceBeforeCurly;
                AppendToken(leadingSpace: ls, trailingSpace: true);
                AppendToken(leadingSpace: false, trailingSpace: false);
                return;
            }

            // Determine if the scope is a single line like if(TRUE) { 1 } and keep it
            // on a single line unless there are line breaks in it. Continue without
            // breaks until the scope ends. Includes multiple nested scopes such as {{{ 1 }}}.
            // We continue on the outer scope boundaries.
            if (_singleLineScopeEnd < 0)
            {
                _singleLineScopeEnd = GetSingleLineScopeEnd();
            }

            if (_options.BracesOnNewLine && !SingleLineScope)
            {
                // Add line break if brace positioning is expanded and it is not a single line scope like { }
                SoftLineBreak();
            }
            else
            {
                // Add space if curly is preceded by )
                bool precededByCloseBrace = _tokens.PreviousToken.TokenType == RTokenType.CloseBrace;
                if (precededByCloseBrace && _options.SpaceBeforeCurly)
                {
                    _tb.AppendSpace();
                }
            }

            // Append { to the formatted text
            AppendToken(leadingSpace: false, trailingSpace: false);

            // Open new scope. It is one indent level deeper than either current
            // indent level or one level deeper than the current line indent
            // such as when formatting multiline expression that also includes
            // scope statements such as
            //   x <-
            //      if(...) {
            //          z
            //      }
            //
            _formattingScopes.OpenScope(new FormattingScope(_tb, _tokens, _tokens.Position - 1, _options, _braceHandler));

            if (!SingleLineScope)
            {
                _tb.SoftLineBreak();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Opens new formatting scope. Scope is opened when either
        /// code discovers open curly brace or when line break
        /// suppression is on.
        /// </summary>
        private void OpenFormattingScope()
        {
            Debug.Assert(_tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace);

            if (IsInArguments())
            {
                // Inside argument lists indentation rules are different
                // so open new set of options and indentation
                FormattingScope formattingScope = new FormattingScope(_tb, _tokens, _options);
                _formattingScopes.Push(formattingScope);
            }

            // If scope is empty, make it { } unless there is a line break already in it
            if (_tokens.NextToken.TokenType == RTokenType.CloseCurlyBrace &&
                _textProvider.IsWhiteSpaceOnlyRange(_tokens.CurrentToken.End, _tokens.NextToken.Start))
            {
                AppendToken(leadingSpace: _tokens.PreviousToken.TokenType == RTokenType.CloseBrace, trailingSpace: true);
                AppendToken(leadingSpace: false, trailingSpace: false);
                return;
            }
            else
            {
                // Determine if the scope is a single line like if(TRUE) { 1 } and keep it
                // on a single line unless there are line breaks in it. Continue without
                // breaks until the scope ends. Includes multiple nested scopes such as {{{ 1 }}}.
                // We continue on the outer scope boundaries.
                if (_singleLineScopeEnd < 0)
                {
                    _singleLineScopeEnd = GetSingleLineScopeEnd();
                }

                if (_options.BracesOnNewLine && !SingleLineScope)
                {
                    SoftLineBreak();
                }
                else if (!IsOpenBraceToken(_tokens.PreviousToken.TokenType))
                {
                    _tb.AppendSpace();
                }
            }

            AppendToken(leadingSpace: false, trailingSpace: false);

            if (!SingleLineScope)
            {
                _tb.SoftLineBreak();
                _tb.NewIndentLevel();
            }
        }