Example #1
0
        /// <inheritdoc />
        string ITextPasteHandler.FormatPlainText(int offset, string text, byte[] copyData)
        {
            if (copyData != null && copyData.Length == 1)
            {
                var strategy = TextPasteUtils.Strategies[(PasteStrategy)copyData[0]];
                text = strategy.Decode(text);
            }
            engine.Update(offset);
            if (engine.IsInsideStringLiteral)
            {
                int idx = text.IndexOf('"');
                if (idx > 0)
                {
                    var o = offset;
                    while (o < engine.Document.TextLength)
                    {
                        char ch = engine.Document.GetCharAt(o);
                        engine.Push(ch);
                        if (NewLine.IsNewLine(ch))
                        {
                            break;
                        }
                        o++;
                        if (!engine.IsInsideStringLiteral)
                        {
                            return(TextPasteUtils.StringLiteralStrategy.Encode(text));
                        }
                    }
                    return(TextPasteUtils.StringLiteralStrategy.Encode(text.Substring(0, idx)) + text.Substring(idx));
                }
                return(TextPasteUtils.StringLiteralStrategy.Encode(text));
            }
            else if (engine.IsInsideVerbatimString)
            {
                int idx = text.IndexOf('"');
                if (idx > 0)
                {
                    var o = offset;
                    while (o < engine.Document.TextLength)
                    {
                        char ch = engine.Document.GetCharAt(o);
                        engine.Push(ch);
                        o++;
                        if (!engine.IsInsideVerbatimString)
                        {
                            return(TextPasteUtils.VerbatimStringStrategy.Encode(text));
                        }
                    }
                    return(TextPasteUtils.VerbatimStringStrategy.Encode(text.Substring(0, idx)) + text.Substring(idx));
                }

                return(TextPasteUtils.VerbatimStringStrategy.Encode(text));
            }
            var  line = engine.Document.GetLineByOffset(offset);
            var  pasteAtLineStart = line.Offset == offset;
            var  indentedText = new StringBuilder();
            var  curLine = new StringBuilder();
            var  clonedEngine = engine.Clone();
            bool isNewLine = false, gotNewLine = false;

            for (int i = 0; i < text.Length; i++)
            {
                var ch = text[i];
                if (clonedEngine.IsInsideVerbatimString || clonedEngine.IsInsideMultiLineComment)
                {
                    clonedEngine.Push(ch);
                    curLine.Append(ch);
                    continue;
                }

                var delimiterLength = NewLine.GetDelimiterLength(ch, i + 1 < text.Length ? text[i + 1] : ' ');
                if (delimiterLength > 0)
                {
                    isNewLine = true;
                    if (gotNewLine || pasteAtLineStart)
                    {
                        if (curLine.Length > 0 || formattingOptions.EmptyLineFormatting == EmptyLineFormatting.Indent)
                        {
                            indentedText.Append(clonedEngine.ThisLineIndent);
                        }
                    }
                    indentedText.Append(curLine);
                    indentedText.Append(textEditorOptions.EolMarker);
                    curLine.Length = 0;
                    gotNewLine     = true;
                    i += delimiterLength - 1;
                    // textEditorOptions.EolMarker[0] is the newLineChar used by the indentation engine.
                    clonedEngine.Push(textEditorOptions.EolMarker[0]);
                }
                else
                {
                    if (isNewLine)
                    {
                        if (ch == '\t' || ch == ' ')
                        {
                            clonedEngine.Push(ch);
                            continue;
                        }
                        isNewLine = false;
                    }
                    curLine.Append(ch);
                    clonedEngine.Push(ch);
                }
                if (clonedEngine.IsInsideVerbatimString || clonedEngine.IsInsideMultiLineComment &&
                    !(clonedEngine.LineBeganInsideVerbatimString || clonedEngine.LineBeganInsideMultiLineComment))
                {
                    if (gotNewLine)
                    {
                        if (curLine.Length > 0 || formattingOptions.EmptyLineFormatting == EmptyLineFormatting.Indent)
                        {
                            indentedText.Append(clonedEngine.ThisLineIndent);
                        }
                    }
                    pasteAtLineStart = false;
                    indentedText.Append(curLine);
                    curLine.Length = 0;
                    gotNewLine     = false;
                    continue;
                }
            }
            if (gotNewLine && (!pasteAtLineStart || curLine.Length > 0))
            {
                indentedText.Append(clonedEngine.ThisLineIndent);
            }
            if (curLine.Length > 0)
            {
                indentedText.Append(curLine);
            }
            return(indentedText.ToString());
        }
Example #2
0
        /// <inheritdoc />
        string ITextPasteHandler.FormatPlainText(SourceText sourceText, int offset, string text, byte [] copyData)
        {
            if (copyData != null && copyData.Length == 1)
            {
                var strategy = TextPasteUtils.Strategies [(PasteStrategy)copyData [0]];
                text = strategy.Decode(text);
            }
            engine.Update(sourceText, offset);

            if (engine.IsInsideStringLiteral)
            {
                int idx = text.IndexOf('"');
                if (idx > 0)
                {
                    var o = offset;
                    while (o < sourceText.Length)
                    {
                        char ch = sourceText [o];
                        engine.Push(ch);
                        if (NewLine.IsNewLine(ch))
                        {
                            break;
                        }
                        o++;
                        if (!engine.IsInsideStringLiteral)
                        {
                            return(TextPasteUtils.StringLiteralStrategy.Encode(text));
                        }
                    }
                    return(TextPasteUtils.StringLiteralStrategy.Encode(text.Substring(0, idx)) + text.Substring(idx));
                }
                return(TextPasteUtils.StringLiteralStrategy.Encode(text));
            }
            else if (engine.IsInsideVerbatimString)
            {
                int idx = text.IndexOf('"');
                if (idx > 0)
                {
                    var o = offset;
                    while (o < sourceText.Length)
                    {
                        char ch = sourceText [o];
                        engine.Push(ch);
                        o++;
                        if (!engine.IsInsideVerbatimString)
                        {
                            return(TextPasteUtils.VerbatimStringStrategy.Encode(text));
                        }
                    }
                    return(TextPasteUtils.VerbatimStringStrategy.Encode(text.Substring(0, idx)) + text.Substring(idx));
                }

                return(TextPasteUtils.VerbatimStringStrategy.Encode(text));
            }


            // on the fly formatting is done in post formatting, if turned off just correct indenting.
            if (!InUnitTestMode && DefaultSourceEditorOptions.Instance.OnTheFlyFormatting)
            {
                return(text);
            }

            var  line = sourceText.Lines.GetLineFromPosition(offset);
            var  pasteAtLineStart = line.Start == offset;
            var  indentedText = StringBuilderCache.Allocate();
            var  curLine = StringBuilderCache.Allocate();
            var  clonedEngine = engine.Clone();
            bool isNewLine = false, gotNewLine = false;

            for (int i = 0; i < text.Length; i++)
            {
                var ch = text [i];
                if (clonedEngine.IsInsideVerbatimString || clonedEngine.IsInsideMultiLineComment || clonedEngine.IsInsidePreprocessorComment)
                {
                    clonedEngine.Push(ch);
                    curLine.Append(ch);
                    continue;
                }

                var delimiterLength = NewLine.GetDelimiterLength(ch, i + 1 < text.Length ? text [i + 1] : ' ');
                if (delimiterLength > 0)
                {
                    isNewLine = true;
                    if (gotNewLine || pasteAtLineStart)
                    {
                        if (curLine.Length > 0 /*|| formattingOptions.EmptyLineFormatting == EmptyLineFormatting.Indent*/)
                        {
                            indentedText.Append(clonedEngine.ThisLineIndent);
                        }
                    }
                    indentedText.Append(curLine);
                    var newLine = options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
                    indentedText.Append(newLine);
                    curLine.Length = 0;
                    gotNewLine     = true;
                    i += delimiterLength - 1;
                    // textEditorOptions.EolMarker[0] is the newLineChar used by the indentation engine.
                    clonedEngine.Push(newLine [0]);
                }
                else
                {
                    if (isNewLine)
                    {
                        if (ch == '\t' || ch == ' ')
                        {
                            clonedEngine.Push(ch);
                            continue;
                        }
                        isNewLine = false;
                    }
                    curLine.Append(ch);
                    clonedEngine.Push(ch);
                }
                if (clonedEngine.IsInsideVerbatimString || clonedEngine.IsInsideMultiLineComment &&
                    !(clonedEngine.LineBeganInsideVerbatimString || clonedEngine.LineBeganInsideMultiLineComment))
                {
                    if (gotNewLine)
                    {
                        if (curLine.Length > 0 /*|| formattingOptions.EmptyLineFormatting == EmptyLineFormatting.Indent*/)
                        {
                            indentedText.Append(clonedEngine.ThisLineIndent);
                        }
                    }
                    pasteAtLineStart = false;
                    indentedText.Append(curLine);
                    curLine.Length = 0;
                    gotNewLine     = false;
                    continue;
                }
            }
            if (gotNewLine && (!pasteAtLineStart || curLine.Length > 0))
            {
                indentedText.Append(clonedEngine.ThisLineIndent);
            }
            if (curLine.Length > 0)
            {
                indentedText.Append(curLine);
            }
            StringBuilderCache.Free(curLine);
            return(StringBuilderCache.ReturnAndFree(indentedText));
        }
Example #3
0
 /// <inheritdoc />
 public void Push(char ch)
 {
     currentEngine.Push(ch);
 }
 /// <inheritdoc />
 public void Push(char ch)
 {
     engine.Push(ch);
 }