Пример #1
0
        static string GetFirstLine(string text)
        {
            int start = 0;

            while (start < text.Length)
            {
                char ch = text [start];
                if (ch != ' ' && ch != '\t')
                {
                    break;
                }
                start++;
            }
            int end = start;

            while (end < text.Length)
            {
                char ch = text [end];
                if (NewLine.IsNewLine(ch))
                {
                    break;
                }
                end++;
            }
            if (end <= start)
            {
                return("");
            }
            return(text.Substring(start, end - start));
        }
Пример #2
0
 private bool IsLineIsEmptyUpToEol(int startOffset)
 {
     for (int offset = startOffset - 1; offset >= 0; offset--)
     {
         char ch = document.GetCharAt(offset);
         if (ch != ' ' && ch != '\t')
         {
             return(NewLine.IsNewLine(ch));
         }
     }
     return(true);
 }
Пример #3
0
            static DocumentRegion GetRegion(SyntaxTrivia trivia)
            {
                var fullSpan = trivia.FullSpan;
                var text     = trivia.ToString();

                if (text.Length > 2)
                {
                    if (text [text.Length - 2] == '\r' && text [text.Length - 1] == '\n')
                    {
                        fullSpan = new Microsoft.CodeAnalysis.Text.TextSpan(fullSpan.Start, fullSpan.Length - 2);
                    }
                    else if (NewLine.IsNewLine(text [text.Length - 1]))
                    {
                        fullSpan = new Microsoft.CodeAnalysis.Text.TextSpan(fullSpan.Start, fullSpan.Length - 1);
                    }
                }
                try {
                    var lineSpan = trivia.SyntaxTree.GetLineSpan(fullSpan);
                    return((DocumentRegion)lineSpan);
                } catch (Exception) {
                    return(DocumentRegion.Empty);
                }
            }
        static void ConvertNormalToVerbatimString(ITextDocument textEditorData, int offset)
        {
            var endOffset = offset;

            while (endOffset < textEditorData.Length)
            {
                char ch = textEditorData.GetCharAt(endOffset);
                if (ch == '\\')
                {
                    if (endOffset + 1 < textEditorData.Length && NewLine.IsNewLine(textEditorData.GetCharAt(endOffset + 1)))
                    {
                        return;
                    }

                    endOffset += 2;
                    continue;
                }
                if (ch == '"')
                {
                    break;
                }
                if (NewLine.IsNewLine(ch))
                {
                    return;
                }
                endOffset++;
            }
            if (offset > endOffset || endOffset == textEditorData.Length)
            {
                return;
            }
            var plainText = CSharpTextPasteHandler.TextPasteUtils.StringLiteralPasteStrategy.Instance.Decode(textEditorData.GetTextAt(offset, endOffset - offset));
            var newText   = CSharpTextPasteHandler.TextPasteUtils.VerbatimStringStrategy.Encode(plainText);

            textEditorData.ReplaceText(offset, endOffset - offset, newText);
        }
Пример #5
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());
        }
Пример #6
0
        /// <inheritdoc />
        public void Push(char ch)
        {
            // append this char to the wordbuf if it can form a valid keyword, otherwise check
            // if the last sequence of chars form a valid keyword and reset the wordbuf.
            if ((wordToken.Length == 0 ? char.IsLetter(ch) : char.IsLetterOrDigit(ch)) || ch == '_')
            {
                wordToken.Append(ch);
            }
            else if (wordToken.Length > 0)
            {
                currentState.CheckKeyword(wordToken.ToString());
                previousKeyword            = wordToken.ToString();
                wordToken.Length           = 0;
                isLineStartBeforeWordToken = false;
            }

            var isNewLine = NewLine.IsNewLine(ch);

            if (!isNewLine)
            {
                currentState.Push(currentChar = ch);
                offset++;
                previousNewline = '\0';
                // ignore whitespace and newline chars
                var isWhitespace = currentChar == ' ' || currentChar == '\t';
                if (!isWhitespace)
                {
                    previousChar = currentChar;
                    isLineStart  = false;
                }

                if (isLineStart)
                {
                    currentIndent.Append(ch);
                }

                if (ch == '\t')
                {
                    var nextTabStop = (column - 1 + textEditorOptions.IndentSize) / textEditorOptions.IndentSize;
                    column = 1 + nextTabStop * textEditorOptions.IndentSize;
                }
                else
                {
                    column++;
                }
            }
            else
            {
                if (ch == NewLine.LF && previousNewline == NewLine.CR)
                {
                    offset++;
                    return;
                }
                currentState.Push(currentChar = newLineChar);
                offset++;

                previousNewline = ch;
                // there can be more than one chars that determine the EOL,
                // the engine uses only one of them defined with newLineChar
                if (currentChar != newLineChar)
                {
                    return;
                }
                currentIndent.Length       = 0;
                isLineStart                = true;
                isLineStartBeforeWordToken = true;
                column = 1;
                line++;

                lineBeganInsideMultiLineComment = IsInsideMultiLineComment;
                lineBeganInsideVerbatimString   = IsInsideVerbatimString;
            }
        }
        bool IsInsideType(IUnresolvedEntity currentType, TextLocation location)
        {
            if (currentType.Region.IsEmpty)
            {
                return(false);
            }
            int startOffset = document.GetOffset(currentType.Region.Begin);
            int endOffset   = document.GetOffset(location);
            //bool foundEndBracket = false;

            var bracketStack = new Stack <char> ();

            bool isInString = false, isInChar = false;
            bool isInLineComment = false, isInBlockComment = false;

            for (int i = startOffset; i < endOffset; i++)
            {
                char ch = document.GetCharAt(i);
                switch (ch)
                {
                case '(':
                case '[':
                case '{':
                    if (!isInString && !isInChar && !isInLineComment && !isInBlockComment)
                    {
                        bracketStack.Push(ch);
                    }
                    break;

                case ')':
                case ']':
                case '}':
                    if (!isInString && !isInChar && !isInLineComment && !isInBlockComment)
                    {
                        if (bracketStack.Count > 0)
                        {
                            bracketStack.Pop();
                        }
                    }
                    break;

                case '/':
                    if (isInBlockComment)
                    {
                        if (i > 0 && document.GetCharAt(i - 1) == '*')
                        {
                            isInBlockComment = false;
                        }
                    }
                    else if (!isInString && !isInChar && i + 1 < document.TextLength)
                    {
                        char nextChar = document.GetCharAt(i + 1);
                        if (nextChar == '/')
                        {
                            isInLineComment = true;
                        }
                        if (!isInLineComment && nextChar == '*')
                        {
                            isInBlockComment = true;
                        }
                    }
                    break;

                case '"':
                    if (!(isInChar || isInLineComment || isInBlockComment))
                    {
                        isInString = !isInString;
                    }
                    break;

                case '\'':
                    if (!(isInString || isInLineComment || isInBlockComment))
                    {
                        isInChar = !isInChar;
                    }
                    break;

                default:
                    if (NewLine.IsNewLine(ch))
                    {
                        isInLineComment = false;
                    }
                    break;
                }
            }
            return(bracketStack.Any(t => t == '{'));
        }
Пример #8
0
        public void Push(char ch)
        {
            var isNewLine = NewLine.IsNewLine(ch);

            if (!isNewLine)
            {
                if (ch == '"')
                {
                    isInString = !IsInsideString;
                }
                if (ch == '{' || ch == '[')
                {
                    nextLineIndent.Push(IndentType.Block);
                }
                else if (ch == '}' || ch == ']')
                {
                    if (thisLineIndent.Count > 0)
                    {
                        thisLineIndent.Pop();
                    }
                    if (nextLineIndent.Count > 0)
                    {
                        nextLineIndent.Pop();
                    }
                }
            }
            else
            {
                if (ch == NewLine.LF && previousChar == NewLine.CR)
                {
                    offset++;
                    previousChar = ch;
                    return;
                }
            }

            offset++;
            if (!isNewLine)
            {
                previousNewline = '\0';

                isLineStart &= char.IsWhiteSpace(ch);

                if (isLineStart)
                {
                    currentIndent.Append(ch);
                }

                if (ch == '\t')
                {
                    var nextTabStop = (column - 1 + data.Options.IndentationSize) / data.Options.IndentationSize;
                    column = 1 + nextTabStop * data.Options.IndentationSize;
                }
                else
                {
                    column++;
                }
            }
            else
            {
                previousNewline      = ch;
                currentIndent.Length = 0;
                isLineStart          = true;
                column = 1;
                line++;
                thisLineIndent = nextLineIndent.Clone();
            }
            previousChar = ch;
        }
Пример #9
0
        public void Push(char ch)
        {
            if (readPreprocessorExpression)
            {
                wordBuf.Append(ch);
            }

            if (inside.HasFlag(Inside.VerbatimString) && pc == '"' && ch != '"')
            {
                inside &= ~Inside.StringLiteral;
            }
            switch (ch)
            {
            case '#':
                if (IsLineStart)
                {
                    inside = Inside.PreProcessor;
                }
                break;

            case '/':
                if (IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (pc == '/')
                {
                    if (inside.HasFlag(Inside.Comment))
                    {
                        inside |= Inside.DocComment;
                    }
                    else
                    {
                        inside |= Inside.Comment;
                    }
                }
                break;

            case '*':
                if (IsInStringOrChar || IsInComment || IsInPreProcessorComment)
                {
                    break;
                }
                if (pc == '/')
                {
                    inside |= Inside.MultiLineComment;
                }
                break;

            case ' ':
                currentIndent.Append(' ');
                break;

            case '\t':
                var nextTabStop = (col - 1 + textEditorOptions.IndentSize) / textEditorOptions.IndentSize;
                col = 1 + nextTabStop * textEditorOptions.IndentSize;
                currentIndent.Append('\t');
                offset++;
                return;

            case '"':
                if (IsInComment || IsInPreProcessorComment)
                {
                    break;
                }
                if (inside.HasFlag(Inside.StringLiteral))
                {
                    if (pc != '\\')
                    {
                        inside &= ~Inside.StringLiteral;
                    }
                    break;
                }

                if (pc == '@')
                {
                    inside |= Inside.VerbatimString;
                }
                else
                {
                    inside |= Inside.StringLiteral;
                }
                break;

            case '<':
            case '[':
            case '(':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                parenStack.Push(new TextLocation(line, col));
                popNextParenBlock = true;
                indent.Push(IndentType.Block);
                break;

            case '>':
            case ']':
            case ')':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (popNextParenBlock && parenStack.Count > 0)
                {
                    parenStack.Pop();
                }
                if (indent.Count > 0)
                {
                    indent.Pop();
                }
                indent.ExtraSpaces = 0;
                break;

            case ',':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (parenStack.Count > 0 && parenStack.Peek().Line == line)
                {
                    if (indent.Count > 0)
                    {
                        indent.Pop();
                    }
                    popNextParenBlock  = false;
                    indent.ExtraSpaces = parenStack.Peek().Column - 1 - thisLineindent.CurIndent;
                }
                break;

            case '{':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                currentBody = nextBody;
                if (indent.Count > 0 && indent.Peek() == IndentType.Continuation)
                {
                    indent.Pop();
                }
                addContinuation = false;
                AddIndentation(currentBody);
                break;

            case '}':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (indentDelta.CurIndent > 0)
                {
                    indentDelta.Pop();
                    if (indentDelta.Count > 0 && indentDelta.Peek() == IndentType.Continuation)
                    {
                        indentDelta.Pop();
                    }
                }
                else
                {
                    if (thisLineindent.Count > 0)
                    {
                        thisLineindent.Pop();
                    }
                    if (indent.Count > 0)
                    {
                        indent.Pop();
                    }
                }
                break;

            case ';':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (indent.Count > 0 && indent.Peek() == IndentType.Continuation)
                {
                    indent.Pop();
                }
                break;

            case '\'':
                if (IsInComment || inside.HasFlag(Inside.StringLiteral) || IsInPreProcessorComment)
                {
                    break;
                }
                if (inside.HasFlag(Inside.CharLiteral))
                {
                    if (pc != '\\')
                    {
                        inside &= ~Inside.CharLiteral;
                    }
                }
                else
                {
                    inside &= Inside.CharLiteral;
                }
                break;

            default:
                var nl = NewLine.GetDelimiterLength(ch, pc);
                if (nl == 2)
                {
                    break;
                }
                if (nl == 1)
                {
                    if (readPreprocessorExpression)
                    {
                        if (!eval(wordBuf.ToString()))
                        {
                            inside |= Inside.PreProcessorComment;
                        }
                    }

                    inside &= ~(Inside.Comment | Inside.String | Inside.CharLiteral | Inside.PreProcessor);
                    CheckKeyword(wordBuf.ToString());
                    wordBuf.Length = 0;
                    indent.Push(indentDelta);
                    indentDelta = new Indent(textEditorOptions);


                    if (addContinuation)
                    {
                        indent.Push(IndentType.Continuation);
                    }
                    thisLineindent             = indent.Clone();
                    addContinuation            = false;
                    IsLineStart                = true;
                    readPreprocessorExpression = false;
                    col = 1;
                    line++;
                    currentIndent.Length = 0;
                }
                break;
            }

            if (!IsInComment && !IsInStringOrChar && !readPreprocessorExpression)
            {
                if ((wordBuf.Length == 0 ? char.IsLetter(ch) : char.IsLetterOrDigit(ch)) || ch == '_')
                {
                    wordBuf.Append(ch);
                }
                else
                {
                    if (inside.HasFlag(Inside.PreProcessor))
                    {
                        if (wordBuf.ToString() == "endif")
                        {
                            inside &= ~Inside.PreProcessorComment;
                        }
                        else if (wordBuf.ToString() == "if")
                        {
                            readPreprocessorExpression = true;
                        }
                        else if (wordBuf.ToString() == "elif")
                        {
                            inside &= ~Inside.PreProcessorComment;
                            readPreprocessorExpression = true;
                        }
                    }
                    else
                    {
                        CheckKeyword(wordBuf.ToString());
                    }
                    wordBuf.Length = 0;
                }
            }
            if (addContinuation)
            {
                indent.Push(IndentType.Continuation);
                addContinuation = false;
            }
            IsLineStart &= ch == ' ' || ch == '\t' || NewLine.IsNewLine(ch);
            pc           = ch;
            if (!NewLine.IsNewLine(ch))
            {
                col++;
            }
            offset++;
        }
        static Stack <Tuple <char, int> > GetBracketStack(string memberText)
        {
            var bracketStack = new Stack <Tuple <char, int> > ();

            bool inSingleComment = false, inString = false, inVerbatimString = false, inChar = false, inMultiLineComment = false;

            for (int i = 0; i < memberText.Length; i++)
            {
                char ch     = memberText [i];
                char nextCh = i + 1 < memberText.Length ? memberText [i + 1] : '\0';
                switch (ch)
                {
                case '(':
                case '[':
                case '{':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    bracketStack.Push(Tuple.Create(ch, i));
                    break;

                case ')':
                case ']':
                case '}':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    if (bracketStack.Count > 0)
                    {
                        bracketStack.Pop();
                    }
                    break;

                case '/':
                    if (inString || inChar || inVerbatimString)
                    {
                        break;
                    }
                    if (nextCh == '/')
                    {
                        i++;
                        inSingleComment = true;
                    }
                    if (nextCh == '*')
                    {
                        inMultiLineComment = true;
                    }
                    break;

                case '*':
                    if (inString || inChar || inVerbatimString || inSingleComment)
                    {
                        break;
                    }
                    if (nextCh == '/')
                    {
                        i++;
                        inMultiLineComment = false;
                    }
                    break;

                case '@':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    if (nextCh == '"')
                    {
                        i++;
                        inVerbatimString = true;
                    }
                    break;

                case '\\':
                    if (inString || inChar)
                    {
                        i++;
                    }
                    break;

                case '"':
                    if (inSingleComment || inMultiLineComment || inChar)
                    {
                        break;
                    }
                    if (inVerbatimString)
                    {
                        if (nextCh == '"')
                        {
                            i++;
                            break;
                        }
                        inVerbatimString = false;
                        break;
                    }
                    inString = !inString;
                    break;

                case '\'':
                    if (inSingleComment || inMultiLineComment || inString || inVerbatimString)
                    {
                        break;
                    }
                    inChar = !inChar;
                    break;

                default:
                    if (NewLine.IsNewLine(ch))
                    {
                        inSingleComment = false;
                        inString        = false;
                        inChar          = false;
                    }
                    break;
                }
            }
            return(bracketStack);
        }
        public int GetCurrentParameterIndex(int triggerOffset, int endOffset, out List <string> usedNamedParameters)
        {
            usedNamedParameters = new List <string> ();
            var  parameter = new Stack <int> ();
            var  bracketStack = new Stack <Stack <int> > ();
            bool inSingleComment = false, inString = false, inVerbatimString = false, inChar = false, inMultiLineComment = false;
            var  word                      = new StringBuilder();
            bool foundCharAfterOpenBracket = false;

            for (int i = triggerOffset; i < endOffset; i++)
            {
                char ch     = document.GetCharAt(i);
                char nextCh = i + 1 < document.TextLength ? document.GetCharAt(i + 1) : '\0';
                if (ch == ':')
                {
                    usedNamedParameters.Add(word.ToString());
                    word.Length = 0;
                }
                else if (char.IsLetterOrDigit(ch) || ch == '_')
                {
                    word.Append(ch);
                }
                else if (char.IsWhiteSpace(ch))
                {
                }
                else
                {
                    word.Length = 0;
                }
                if (!char.IsWhiteSpace(ch) && parameter.Count > 0)
                {
                    foundCharAfterOpenBracket = true;
                }

                switch (ch)
                {
                case '{':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    bracketStack.Push(parameter);
                    parameter = new Stack <int> ();
                    break;

                case '[':
                case '(':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    parameter.Push(0);
                    break;

                case '}':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    if (bracketStack.Count > 0)
                    {
                        parameter = bracketStack.Pop();
                    }
                    else
                    {
                        return(-1);
                    }
                    break;

                case ']':
                case ')':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    if (parameter.Count > 0)
                    {
                        parameter.Pop();
                    }
                    else
                    {
                        return(-1);
                    }
                    break;

                case '<':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    parameter.Push(0);
                    break;

                case '=':
                    if (nextCh == '>')
                    {
                        i++;
                        continue;
                    }
                    break;

                case '>':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    if (parameter.Count > 0)
                    {
                        parameter.Pop();
                    }
                    break;

                case ',':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    if (parameter.Count > 0)
                    {
                        parameter.Push(parameter.Pop() + 1);
                    }
                    break;

                case '/':
                    if (inString || inChar || inVerbatimString)
                    {
                        break;
                    }
                    if (nextCh == '/')
                    {
                        i++;
                        inSingleComment = true;
                    }
                    if (nextCh == '*')
                    {
                        inMultiLineComment = true;
                    }
                    break;

                case '*':
                    if (inString || inChar || inVerbatimString || inSingleComment)
                    {
                        break;
                    }
                    if (nextCh == '/')
                    {
                        i++;
                        inMultiLineComment = false;
                    }
                    break;

                case '@':
                    if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
                    {
                        break;
                    }
                    if (nextCh == '"')
                    {
                        i++;
                        inVerbatimString = true;
                    }
                    break;

                case '\\':
                    if (inString || inChar)
                    {
                        i++;
                    }
                    break;

                case '"':
                    if (inSingleComment || inMultiLineComment || inChar)
                    {
                        break;
                    }
                    if (inVerbatimString)
                    {
                        if (nextCh == '"')
                        {
                            i++;
                            break;
                        }
                        inVerbatimString = false;
                        break;
                    }
                    inString = !inString;
                    break;

                case '\'':
                    if (inSingleComment || inMultiLineComment || inString || inVerbatimString)
                    {
                        break;
                    }
                    inChar = !inChar;
                    break;

                default:
                    if (NewLine.IsNewLine(ch))
                    {
                        inSingleComment = false;
                        inString        = false;
                        inChar          = false;
                    }
                    break;
                }
            }
            if (parameter.Count != 1 || bracketStack.Count > 0)
            {
                return(-1);
            }
            if (!foundCharAfterOpenBracket)
            {
                return(0);
            }
            return(parameter.Pop() + 1);
        }
Пример #12
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));
        }
Пример #13
0
        public void Push(char ch)
        {
            var isNewLine = NewLine.IsNewLine(ch);

            if (!isNewLine)
            {
                if (ch == '"')
                {
                    isInString = !IsInsideString;
                    if (isInString)
                    {
                        savedStringIndent = nextLineIndent;
                        nextLineIndent    = 0;
                    }
                    else
                    {
                        nextLineIndent = savedStringIndent;
                    }
                }
                if (ch == '{' || ch == '[')
                {
                    nextLineIndent++;
                }
                else if (ch == '}' || ch == ']')
                {
                    if (thisLineIndent > 0)
                    {
                        thisLineIndent--;
                    }
                    if (nextLineIndent > 0)
                    {
                        nextLineIndent--;
                    }
                }
            }
            else
            {
                if (ch == NewLine.LF && previousChar == NewLine.CR)
                {
                    offset++;
                    previousChar = ch;
                    return;
                }
            }

            offset++;
            if (!isNewLine)
            {
                // previousNewline = '\0';

                isLineStart &= char.IsWhiteSpace(ch);

                if (isLineStart)
                {
                    currentIndent.Append(ch);
                }

                if (ch == '\t')
                {
                    var nextTabStop = (column - 1 + editor.Options.IndentationSize) / editor.Options.IndentationSize;
                    column = 1 + nextTabStop * editor.Options.IndentationSize;
                }
                else
                {
                    column++;
                }
            }
            else
            {
                // previousNewline = ch;
                currentIndent.Length = 0;
                isLineStart          = true;
                column = 1;
                line++;
                thisLineIndent = nextLineIndent;
            }
            previousChar = ch;
        }