Esempio n. 1
0
        public static Indent ConvertFrom(string indentString, Indent correctIndent, TextEditorOptions options = null)
        {
            options = options ?? TextEditorOptions.Default;
            var result = new Indent(options);

            var indent      = string.Concat(indentString.Where(c => c == ' ' || c == '\t'));
            var indentTypes = new Stack <IndentType>(correctIndent.indentStack);

            foreach (var _ in indent.TakeWhile(c => c == '\t'))
            {
                if (indentTypes.Count > 0)
                {
                    result.Push(indentTypes.Pop());
                }
                else
                {
                    result.Push(IndentType.Continuation);
                }
            }

            result.ExtraSpaces = indent
                                 .SkipWhile(c => c == '\t')
                                 .TakeWhile(c => c == ' ')
                                 .Count();

            return(result);
        }
        void AddIndentation(BraceStyle braceStyle)
        {
            switch (braceStyle)
            {
            case BraceStyle.DoNotChange:
            case BraceStyle.EndOfLine:
            case BraceStyle.EndOfLineWithoutSpace:
            case BraceStyle.NextLine:
            case BraceStyle.NextLineShifted:
            case BraceStyle.BannerStyle:
                indentDelta.Push(IndentType.Block);
                break;

            case BraceStyle.NextLineShifted2:
                indentDelta.Push(IndentType.DoubleBlock);
                break;
            }
        }
Esempio n. 3
0
        public Indent GetIndentWithoutSpace()
        {
            var result = new Indent(options);

            foreach (var i in indentStack)
            {
                result.Push(i);
            }
            return(result);
        }
Esempio n. 4
0
        void FixOpenBrace(BraceStyle braceStyle, AstNode lbrace)
        {
            if (lbrace.IsNull)
            {
                return;
            }
            switch (braceStyle)
            {
            case BraceStyle.DoNotChange:
                return;

            case BraceStyle.BannerStyle:
            case BraceStyle.EndOfLine:
                var prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                int prevOffset = document.GetOffset(prev.EndLocation);

                if (prev is Comment || prev is PreProcessorDirective)
                {
                    int next = document.GetOffset(lbrace.GetNextNode().StartLocation);
                    EnsureText(prevOffset, next, "");
                    while (prev is Comment || prev is PreProcessorDirective)
                    {
                        prev = prev.GetPrevNode();
                    }
                    prevOffset = document.GetOffset(prev.EndLocation);
                    AddChange(prevOffset, 0, " {");
                }
                else
                {
                    int braceOffset2 = document.GetOffset(lbrace.StartLocation);
                    EnsureText(prevOffset, braceOffset2, " ");
                }
                break;

            case BraceStyle.EndOfLineWithoutSpace:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset = document.GetOffset(prev.EndLocation);
                int braceOffset = document.GetOffset(lbrace.StartLocation);
                EnsureText(prevOffset, braceOffset, "");
                break;

            case BraceStyle.NextLine:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString);
                break;

            case BraceStyle.NextLineShifted:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                curIndent.Push(IndentType.Block);
                EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString);
                curIndent.Pop();
                break;

            case BraceStyle.NextLineShifted2:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                curIndent.Push(IndentType.Block);
                EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString);
                curIndent.Pop();
                break;
            }
        }
        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++;
        }
Esempio n. 6
0
		public static Indent ConvertFrom(string indentString, Indent correctIndent, TextEditorOptions options = null)
		{
			options = options ?? TextEditorOptions.Default;
			var result = new Indent(options);

			var indent = string.Concat(indentString.Where(c => c == ' ' || c == '\t'));
			var indentTypes = new Stack<IndentType>(correctIndent.indentStack);

			foreach (var _ in indent.TakeWhile(c => c == '\t'))
			{
				if (indentTypes.Count > 0)
					result.Push(indentTypes.Pop());
				else
					result.Push(IndentType.Continuation);
			}

			result.ExtraSpaces = indent
				.SkipWhile(c => c == '\t')
				.TakeWhile(c => c == ' ')
				.Count();

			return result;
		}
Esempio n. 7
0
		public Indent GetIndentWithoutSpace ()
		{
			var result = new Indent(options);
			foreach (var i in indentStack)
					result.Push(i);
			return result;
		}