private void Align(AstNode lPar, AstNode alignNode, bool space) { int extraSpaces = 0; var useExtraSpaces = lPar.StartLocation.Line == alignNode.StartLocation.Line; if (useExtraSpaces) { extraSpaces = Math.Max(0, lPar.StartLocation.Column + (space ? 1 : 0) - curIndent.IndentString.Length); curIndent.ExtraSpaces += extraSpaces; ForceSpacesAfter(lPar, space); } else { curIndent.Push(IndentType.Continuation); FixIndentation(alignNode); } alignNode.AcceptVisitor(this); if (useExtraSpaces) { curIndent.ExtraSpaces -= extraSpaces; } else { curIndent.Pop(); } }
public void Apply(AstNode rootNode) { if (rootNode == null) { throw new ArgumentNullException("rootNode"); } rootNode.AcceptVisitor(new GenerateCodeVisitior(this)); }
public string ConvertVariable(IVariable v) { TypeSystemAstBuilder astBuilder = CreateAstBuilder(); AstNode astNode = astBuilder.ConvertVariable(v); CSharpFormattingOptions formatting = new CSharpFormattingOptions(); StringWriter writer = new StringWriter(); astNode.AcceptVisitor(new CSharpOutputVisitor(writer, formatting), null); return(writer.ToString().TrimEnd(';', '\r', '\n')); }
void AssertOutput(AstNode node) { RemoveTokens(node); StringWriter w = new StringWriter(); w.NewLine = "\n"; node.AcceptVisitor(new CSharpOutputVisitor(TokenWriter.CreateWriterThatSetsLocationsInAST(w), FormattingOptionsFactory.CreateSharpDevelop())); var doc = new ReadOnlyDocument(w.ToString()); ConsistencyChecker.CheckMissingTokens(node, "test.cs", doc); ConsistencyChecker.CheckPositionConsistency(node, "test.cs", doc); }
/// <summary> /// Expands all occurances of query patterns in the specified node. Returns a clone of the node with all query patterns expanded, or null if there was no query pattern to expand. /// </summary> /// <param name="node"></param> /// <returns></returns> public QueryExpressionExpansionResult ExpandQueryExpressions(AstNode node) { var visitor = new Visitor(); var astNode = node.AcceptVisitor(visitor); if (astNode != null) { astNode.Freeze(); return(new QueryExpressionExpansionResult(astNode, visitor.rangeVariables, visitor.expressions)); } else { return(null); } }
void AssertOutput(string expected, AstNode node, CSharpFormattingOptions policy = null) { if (policy == null) { policy = FormattingOptionsFactory.CreateMono(); } StringWriter w = new StringWriter(); w.NewLine = "\n"; node.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "$" }, policy)); Assert.AreEqual(expected.Replace("\r", ""), w.ToString()); }
public void NullNodesCallVisitNullNode() { foreach (Type type in typeof(AstNode).Assembly.GetExportedTypes()) { if (type.IsSubclassOf(typeof(AstNode))) { var nullField = type.GetField("Null"); if (nullField != null) { AstNode nullNode = (AstNode)nullField.GetValue(null); Assert.IsTrue(nullNode.IsNull, nullNode.GetType().Name + " should be a null node"); var v1 = new VisitNullNodeTest(); var v2 = new VisitNullNodeTest <string>(); var v3 = new VisitNullNodeTest <string, string>(); nullNode.AcceptVisitor(v1); nullNode.AcceptVisitor(v2); nullNode.AcceptVisitor(v3, null); Assert.IsTrue(v1.called, nullNode.GetType().Name + " should call 'void VisitNullNode();'"); Assert.IsTrue(v2.called, nullNode.GetType().Name + " should call 'T VisitNullNode();'"); Assert.IsTrue(v3.called, nullNode.GetType().Name + " should call 'S VisitNullNode(T data);'"); } } } }
/// <summary> /// Applies the <paramref name="visitor"/> to all nodes in this collection. /// </summary> public void AcceptVisitor(IAstVisitor visitor) { AstNode next; for (AstNode cur = node.FirstChild; cur != null; cur = next) { Debug.Assert(cur.Parent == node); // Remember next before yielding cur. // This allows removing/replacing nodes while iterating through the list. next = cur.NextSibling; if (cur.Role == role) { cur.AcceptVisitor(visitor); } } }
/// <summary> /// Expands all occurances of query patterns in the specified node. Returns a clone of the node with all query patterns expanded, or null if there was no query pattern to expand. /// </summary> /// <param name="node"></param> /// <param name="transparentIdentifierNamePicker">A sequence of names to use for transparent identifiers. Once the sequence is over, a fallback name generator is used</param> /// <returns></returns> public QueryExpressionExpansionResult ExpandQueryExpressions(AstNode node, IEnumerable <string> transparentIdentifierNamePicker) { var visitor = new Visitor(); visitor.TransparentIdentifierNamePicker = transparentIdentifierNamePicker.GetEnumerator(); var astNode = node.AcceptVisitor(visitor); if (astNode != null) { astNode.Freeze(); return(new QueryExpressionExpansionResult(astNode, visitor.rangeVariables, visitor.expressions)); } else { return(null); } }
IConstantValue ConvertConstantValue(ITypeReference targetType, AstNode expression) { ConstantValueBuilder b = new ConstantValueBuilder(); b.convertVisitor = this; ConstantExpression c = expression.AcceptVisitor(b, null); if (c == null) { return(null); } PrimitiveConstantExpression pc = c as PrimitiveConstantExpression; if (pc != null && pc.Type == targetType) { // Save memory by directly using a SimpleConstantValue. return(new SimpleConstantValue(targetType, pc.Value)); } // cast to the desired type return(new CSharpConstantValue(new ConstantCast(targetType, c), usingScope, currentTypeDefinition)); }
private void FixEmbeddedStatment(BraceStyle braceStyle, CSharpTokenNode token, bool allowInLine, AstNode node, bool statementAlreadyIndented = false) { if (node == null) { return; } bool isBlock = node is BlockStatement; FormattingChanges.TextReplaceAction beginBraceAction = null; FormattingChanges.TextReplaceAction endBraceAction = null; BlockStatement closeBlockToBeFixed = null; if (isBlock) { BlockStatement block = node as BlockStatement; if (allowInLine && block.StartLocation.Line == block.EndLocation.Line && block.Statements.Count() <= 1) { if (block.Statements.Count() == 1) { nextStatementIndent = " "; } } else { if (!statementAlreadyIndented) { FixOpenBrace(braceStyle, block.LBraceToken); } closeBlockToBeFixed = block; } if (braceStyle == BraceStyle.NextLineShifted2) { curIndent.Push(IndentType.Block); } } else { if (allowInLine && token.StartLocation.Line == node.EndLocation.Line) { nextStatementIndent = " "; } } bool pushed = false; if (policy.IndentBlocks && !( policy.AlignEmbeddedStatements && node is IfElseStatement && node.Parent is IfElseStatement || policy.AlignEmbeddedStatements && node is UsingStatement && node.Parent is UsingStatement || policy.AlignEmbeddedStatements && node is LockStatement && node.Parent is LockStatement)) { curIndent.Push(IndentType.Block); pushed = true; } if (isBlock) { VisitBlockWithoutFixingBraces((BlockStatement)node, false); } else { if (!statementAlreadyIndented) { PlaceOnNewLine(policy.EmbeddedStatementPlacement, node); nextStatementIndent = null; } node.AcceptVisitor(this); } nextStatementIndent = null; if (pushed) { curIndent.Pop(); } if (beginBraceAction != null && endBraceAction != null) { beginBraceAction.DependsOn = endBraceAction; endBraceAction.DependsOn = beginBraceAction; } if (isBlock && braceStyle == BraceStyle.NextLineShifted2) { curIndent.Pop(); } if (closeBlockToBeFixed != null) { FixClosingBrace(braceStyle, closeBlockToBeFixed.RBraceToken); } }