Beispiel #1
0
		Indent(Indent engine)
		{
			this.indentStack = new Stack<IndentType>(engine.indentStack.Reverse());
			this.options = engine.options;
			this.curIndent = engine.curIndent;
			this.extraSpaces = engine.extraSpaces;
			this.indentString = engine.indentString;
		}
 public CSharpIndentEngine(IDocument document, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions)
 {
     this.document = document;
     this.options = formattingOptions;
     this.textEditorOptions = textEditorOptions;
     this.indent = new Indent(textEditorOptions);
     this.thisLineindent = new Indent(textEditorOptions);
 }
Beispiel #3
0
		Indent(Indent engine)
		{
			this.indentStack = engine.indentStack.Clone();
			this.options = engine.options;
			this.curIndent = engine.curIndent;
			this.extraSpaces = engine.extraSpaces;
			this.indentString = engine.indentString;
		}
 public AstFormattingVisitor(CSharpFormattingOptions policy, IDocument document, TextEditorOptions options = null)
 {
     if (policy == null) {
         throw new ArgumentNullException("policy");
     }
     if (document == null) {
         throw new ArgumentNullException("document");
     }
     this.policy = policy;
     this.document = document;
     this.options = options ?? TextEditorOptions.Default;
     curIndent = new Indent(this.options);
 }
        public FormattingVisitor(CSharpFormatter formatter, IDocument document, FormattingChanges changes, CancellationToken token)
        {
            if (formatter == null)
                throw new ArgumentNullException("formatter");
            if (document == null)
                throw new ArgumentNullException("document");
            if (changes == null)
                throw new ArgumentNullException("changes");

            this.formatter = formatter;
            this.changes = changes;
            this.document = document;
            this.token = token;

            curIndent = new Indent(formatter.TextEditorOptions);
        }
 CSharpIndentEngine(CSharpIndentEngine prototype)
 {
     this.document = prototype.document;
     this.options = prototype.options;
     this.textEditorOptions = prototype.textEditorOptions;
     this.indent = prototype.indent.Clone();
     this.thisLineindent = prototype.thisLineindent.Clone();
     this.offset = prototype.offset;
     this.inside = prototype.inside;
     this.IsLineStart = prototype.IsLineStart;
     this.pc = prototype.pc;
     this.parenStack = new Stack<TextLocation>(prototype.parenStack.Reverse ());
     this.currentBody = prototype.currentBody;
     this.nextBody = prototype.nextBody;
     this.addContinuation = prototype.addContinuation;
     this.line = prototype.line;
     this.col = prototype.col;
     this.popNextParenBlock = prototype.popNextParenBlock;
 }
        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 '\r':

                    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;
                case '\n':
                    if (pc == '\r')
                        break;
                    goto case '\r';
                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;
            }

            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' || ch == '\n' || ch == '\r';
            pc = ch;
            if (ch != '\n' && ch != '\r')
                col++;
            offset++;
        }
Beispiel #8
0
		public void Push(Indent indent)
		{
			foreach (var i in indent.indentStack)
				Push(i);
		}
Beispiel #9
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;
		}
Beispiel #10
0
		public Indent GetIndentWithoutSpace ()
		{
			var result = new Indent(options);
			foreach (var i in indentStack)
					result.Push(i);
			return result;
		}
Beispiel #11
0
 public Indent Clone()
 {
     var result = new Indent(options, new Stack<IndentType>(indentStack), curIndent);
     result.indentString = indentString;
     return result;
 }
Beispiel #12
0
		public void Reset ()
		{
			offset = 0;
			line = column = 1;
			thisLineIndent = new Indent (CreateNRefactoryTextEditorOptions (data));
			nextLineIndent = new Indent (CreateNRefactoryTextEditorOptions (data));
			currentIndent = new StringBuilder ();
			previousNewline = '\0';
			previousChar = '\0';
			isLineStart = true;
			isInString = false;
		}
Beispiel #13
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;
		}
        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 '\r':

                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;

            case '\n':
                if (pc == '\r')
                {
                    break;
                }
                goto case '\r';

            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;
            }

            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' || ch == '\n' || ch == '\r';
            pc           = ch;
            if (ch != '\n' && ch != '\r')
            {
                col++;
            }
            offset++;
        }