Example #1
0
        private void AppendTextBeforeToken()
        {
            int start = _tokens.Position > 0 ? _tokens.PreviousToken.End : 0;
            int end   = _tokens.IsEndOfStream() ? _textProvider.Length : _tokens.CurrentToken.Start;

            string text = _textProvider.GetText(TextRange.FromBounds(start, end));

            if (string.IsNullOrWhiteSpace(text))
            {
                // Append any user-entered whitespace. We preserve
                // line breaks but trim unnecessary spaces such as
                // on empty lines. We must, however, preserve
                // user indentation in long argument lists and
                // in expresions split into multiple lines.

                // We preserve user indentation of last token was
                // open brace, square bracket, comma or an operator
                bool preserveUserIndent = false;
                if (_tokens.Position > 0)
                {
                    switch (_tokens.PreviousToken.TokenType)
                    {
                    case RTokenType.OpenBrace:
                    case RTokenType.OpenSquareBracket:
                    case RTokenType.OpenDoubleSquareBracket:
                    case RTokenType.Comma:
                    case RTokenType.Operator:
                        preserveUserIndent = true;
                        break;
                    }
                }

                _tb.CopyPrecedingLineBreaks(_textProvider, end);

                if (preserveUserIndent)
                {
                    int lastLineBreakIndex = text.LastIndexOfAny(CharExtensions.LineBreakChars);
                    if (lastLineBreakIndex >= 0)
                    {
                        text = text.Substring(lastLineBreakIndex + 1);
                        int textIndentInSpaces = IndentBuilder.TextIndentInSpaces(text, _options.TabSize);
                        text = IndentBuilder.GetIndentString(textIndentInSpaces, _indentBuilder.IndentType, _indentBuilder.TabSize);
                        _tb.AppendPreformattedText(text);
                    }
                }
                else
                {
                    _tb.SoftIndent();
                }
            }
            else
            {
                // If there is unrecognized text between tokens, append it verbatim
                _tb.AppendPreformattedText(text);
            }
        }
Example #2
0
        private void AppendTextBeforeToken(bool preserveUserIndent = false)
        {
            int start = _tokens.Position > 0 ? _tokens.PreviousToken.End : 0;
            int end   = _tokens.IsEndOfStream() ? _textProvider.Length : _tokens.CurrentToken.Start;

            string text           = _textProvider.GetText(TextRange.FromBounds(start, end));
            bool   whitespaceOnly = string.IsNullOrWhiteSpace(text);

            if (!preserveUserIndent && whitespaceOnly)
            {
                // Append any user-entered whitespace. We preserve line breaks but trim
                // unnecessary spaces such as on empty lines. We must, however, preserve
                // user indentation in long argument lists and in expresions split
                // into multiple lines.

                // We preserve user indentation of last token was
                // open brace, square bracket, comma or an operator
                if (_tokens.Position > 0)
                {
                    switch (_tokens.PreviousToken.TokenType)
                    {
                    case RTokenType.OpenBrace:
                    case RTokenType.OpenSquareBracket:
                    case RTokenType.OpenDoubleSquareBracket:
                    case RTokenType.Comma:
                    case RTokenType.Operator:
                        preserveUserIndent = true;
                        break;

                    case RTokenType.Comment:
                        // Preserve user indent in argument lists
                        preserveUserIndent = _braceHandler.IsInArguments();
                        break;

                    default:
                        // If expression between scope start and the current point is incomplete,
                        // (i.e. we are in a middle of multiline expression) we want to preserve
                        // user-supplied indentation.
                        preserveUserIndent = !_expressionHelper.IsCompleteExpression(_tokens.Position);
                        break;
                    }

                    // Also preserve indent before the tokens below. This matter in long lists
                    // of arguments in functions or indexers and also before comments.
                    if (!preserveUserIndent)
                    {
                        switch (_tokens.CurrentToken.TokenType)
                        {
                        case RTokenType.CloseBrace:
                        case RTokenType.CloseSquareBracket:
                        case RTokenType.CloseDoubleSquareBracket:
                        case RTokenType.Comma:
                        case RTokenType.Operator:
                        case RTokenType.Comment:
                            preserveUserIndent = true;
                            break;
                        }
                    }
                }

                // Preserve line breaks user has entered.
                _tb.CopyPrecedingLineBreaks(_textProvider, end);

                if (preserveUserIndent)
                {
                    // Construct indentation line based on the size of the user indent
                    // but using tabs or spaces per formatting options.
                    int lastLineBreakIndex = text.LastIndexOfAny(CharExtensions.LineBreakChars);
                    if (lastLineBreakIndex >= 0)
                    {
                        text = text.Substring(lastLineBreakIndex + 1);
                        int textIndentInSpaces = IndentBuilder.TextIndentInSpaces(text, _options.TabSize);
                        text = IndentBuilder.GetIndentString(textIndentInSpaces, _indentBuilder.IndentType, _indentBuilder.TabSize);
                        _tb.AppendPreformattedText(text);
                    }
                }
                else
                {
                    _tb.SoftIndent();
                }
            }
            else
            {
                // If there is unrecognized text between tokens, append it verbatim.
                _tb.AppendPreformattedText(text);
            }
        }