/// <inheritdoc/> protected override void HandleWhile(CodeIterationStatement obj, Context ctx) { ctx.Writer.Write("while ("); ctx.HandlerProvider.ExpressionHandler.Handle(obj.TestExpression, ctx); ctx.Writer.Write(")"); CSharpUtils.HandleStatementCollection(obj.Statements, ctx, true, false); }
/// <inheritdoc/> protected override void HandleUsing(CodeUsingStatement obj, Context ctx) { ctx.Writer.Write("using ("); WriteTypeOrVar(obj.Type, ctx); ctx.Writer.Write($" {obj.VariableName.AsCsId()} = "); ctx.HandlerProvider.ExpressionHandler.Handle(obj.InitializerExpression, ctx); ctx.Writer.Write(")"); CSharpUtils.HandleStatementCollection(obj.Statements, ctx, true, false); }
/// <inheritdoc/> protected override void HandleForEach(CodeForEachStatement obj, Context ctx) { ctx.Writer.Write("foreach ("); WriteTypeOrVar(obj.ItemType, ctx); ctx.Writer.Write($" {obj.ItemName.AsCsId()} in "); ctx.HandlerProvider.ExpressionHandler.Handle(obj.ObjectToIterate, ctx); ctx.Writer.Write(")"); CSharpUtils.HandleStatementCollection(obj.Statements, ctx, true, false); }
/// <inheritdoc/> public bool Handle(CodeNamespaceImport obj, Context ctx) { ctx.Writer.Write("using "); if (obj is CodeNamespaceImportExt objExt && objExt.IsStatic) { ctx.Writer.Write("static "); } ctx.Writer.Write(CSharpUtils.GetValidNamespaceIdentifier(obj.Namespace)); ctx.ImportedNamespaces.Add(obj.Namespace); return(true); }
/// <inheritdoc/> protected override void HandleDoWhile(CodePostTestIterationStatement obj, Context ctx) { ctx.Writer.WriteLine("do"); ctx.Writer.IndentAndWriteLine("{", ctx); ctx.Indent(); CSharpUtils.HandleStatementCollection(obj.Statements, ctx, false); ctx.Unindent(); ctx.Writer.IndentAndWrite("} while (", ctx); ctx.HandlerProvider.ExpressionHandler.Handle(obj.TestExpression, ctx); ctx.Writer.Write(");"); }
public bool Handle(CodeCatchClause obj, Context ctx) { ctx.Writer.Write("catch ("); ctx.HandlerProvider.TypeReferenceHandler.Handle(obj.CatchExceptionType, ctx); if (!string.IsNullOrEmpty(obj.LocalName)) { ctx.Writer.Write($" {obj.LocalName.AsCsId()}"); } ctx.Writer.Write(")"); CSharpUtils.HandleStatementCollection(obj.Statements, ctx, true, false); return(true); }
/// <inheritdoc/> protected override void HandleTryCatchFinally(CodeTryCatchFinallyStatement obj, Context ctx) { ctx.Writer.Write("try"); CSharpUtils.HandleStatementCollection(obj.TryStatements, ctx); GeneralUtils.HandleCollection(obj.CatchClauses.Cast <CodeCatchClause>(), _catchClauseHandler, ctx, preAction: (c) => c.Writer.Indent(c), postAction: (c) => c.Writer.NewLine(), doPostActionOnLast: false); if (obj.FinallyStatements.Count > 0) { ctx.Writer.Write("finally"); CSharpUtils.HandleStatementCollection(obj.FinallyStatements, ctx, true, false); } }
/// <summary> /// Returns the provided string as a valid c# identifier. /// </summary> /// <param name="self"></param> /// <returns></returns> public static string AsCsId(this string self) { if (self.Length == 0) { return(null); } if (self.Contains(".")) { return(CSharpUtils.GetValidNamespaceIdentifier(self)); } if (self.IsCsKeyword()) { return("@" + self); } return(self); }
/// <inheritdoc/> protected override void HandleFor(CodeIterationStatement obj, Context ctx) { ctx.StatementShouldTerminate = false; ctx.Writer.Write("for ("); if (obj.InitStatement != null) { ctx.HandlerProvider.StatementHandler.Handle(obj.InitStatement, ctx); } ctx.Writer.Write("; "); ctx.HandlerProvider.ExpressionHandler.Handle(obj.TestExpression, ctx); ctx.Writer.Write("; "); if (obj.IncrementStatement != null) { ctx.HandlerProvider.StatementHandler.Handle(obj.IncrementStatement, ctx); } ctx.Writer.Write(")"); ctx.StatementShouldTerminate = true; CSharpUtils.HandleStatementCollection(obj.Statements, ctx, true, false); }
/// <inheritdoc/> protected override bool HandleDynamic(CodeLambdaDeclarationExpression obj, Context ctx) { ctx.Writer.Write("("); GeneralUtils.HandleCollectionCommaSeparated(obj.Parameters, ctx.HandlerProvider.ExpressionHandler, ctx); ctx.Writer.Write(") =>"); if (obj.Statements.Count == 1 && obj.Statements[0] is CodeExpressionStatement statement) { ctx.Writer.Write(" "); ctx.HandlerProvider.ExpressionHandler.Handle(statement.Expression, ctx); } else { ctx.Writer.NewLine(); ctx.Writer.IndentAndWriteLine("{", ctx); ctx.Indent(); CSharpUtils.HandleStatementCollection(obj.Statements, ctx, false); ctx.Unindent(); ctx.Writer.IndentAndWrite("}", ctx); } return(true); }
/// <inheritdoc/> protected override void HandleCondition(CodeConditionStatement obj, Context ctx) { ctx.Writer.Write("if ("); ctx.HandlerProvider.ExpressionHandler.Handle(obj.Condition, ctx); ctx.Writer.Write(")"); CSharpUtils.HandleStatementCollection(obj.TrueStatements, ctx, true, false); if (obj.FalseStatements.Count > 0) { ctx.Writer.NewLine(); ctx.Writer.IndentAndWrite("else", ctx); if (obj.FalseStatements.Count == 1 && obj.FalseStatements[0] is CodeConditionStatement elseIf) { ctx.Writer.Write(" "); HandleCondition(elseIf, ctx); } else { CSharpUtils.HandleStatementCollection(obj.FalseStatements, ctx, true, false); } } }
/// <inheritdoc/> protected override void DoHandle(CodeNamespace obj, Context ctx) { ctx.CurrentNamespace = obj.Name; ctx.Writer.WriteLine($"namespace {CSharpUtils.GetValidNamespaceIdentifier(ctx.CurrentNamespace)}"); ctx.Writer.WriteLine("{"); ctx.Indent(); ctx.ImportedNamespaces.Clear(); GeneralUtils.HandleCollection(obj.Imports.Cast <CodeNamespaceImport>(), ctx.HandlerProvider.NamespaceImportHandler, ctx, preAction: (c) => c.Writer.Indent(c), postAction: (c) => c.Writer.WriteLine(";"), doPostActionOnLast: true); if (obj.Imports.Count > 0 && obj.Types.Count > 0) { ctx.Writer.NewLine(); } GeneralUtils.HandleCollection(obj.Types.Cast <CodeTypeDeclaration>(), ctx.HandlerProvider.TypeDeclarationHandler, ctx, preAction: (c) => c.Writer.Indent(c), postAction: (c) => c.Writer.NewLine(), doPostActionOnLast: false); ctx.Unindent(); ctx.Writer.WriteLine("}"); }
/// <inheritdoc/> protected override void HandleProperty(CodeMemberProperty obj, Context ctx, bool isExt, CodeMemberPropertyExt objExt, bool doDefaultImplementation) { if (obj.PrivateImplementationType == null) { ctx.HandlerProvider.MemberAttributesHandler.Handle(obj.Attributes, ctx); } ctx.HandlerProvider.TypeReferenceHandler.Handle(obj.Type, ctx); ctx.Writer.Write(" "); if (obj.PrivateImplementationType == null) { ctx.Writer.Write(obj.Name.AsCsId()); } else { HandlePrivateImplementationTypeMemberName(obj.Name, obj.PrivateImplementationType, ctx); } ctx.Writer.NewLine(); ctx.Writer.IndentAndWriteLine("{", ctx); ctx.Indent(); if (obj.HasGet) { ctx.Writer.Indent(ctx); if (isExt && objExt.GetAccessibilityLevel != obj.Attributes.GetAccessibilityLevel() && obj.PrivateImplementationType == null) { ctx.Writer.Write($"{CSharpKeywordsUtils.AccessibilityLevelKeyword(objExt.GetAccessibilityLevel)} "); } ctx.Writer.Write("get"); if (doDefaultImplementation) { ctx.Writer.WriteLine(";"); } else { CSharpUtils.HandleStatementCollection(obj.GetStatements, ctx); } } if (obj.HasSet) { ctx.Writer.Indent(ctx); if (isExt && objExt.SetAccessibilityLevel != obj.Attributes.GetAccessibilityLevel() && obj.PrivateImplementationType == null) { ctx.Writer.Write($"{CSharpKeywordsUtils.AccessibilityLevelKeyword(objExt.SetAccessibilityLevel)} "); } ctx.Writer.Write("set"); if (doDefaultImplementation) { ctx.Writer.WriteLine(";"); } else { CSharpUtils.HandleStatementCollection(obj.SetStatements, ctx); } } ctx.Unindent(); ctx.Writer.IndentAndWrite("}", ctx); if (isExt && objExt.PropertyInitializer != null) { ctx.Writer.Write(" = "); ctx.HandlerProvider.ExpressionHandler.Handle(objExt.PropertyInitializer, ctx); ctx.Writer.Write(";"); } ctx.Writer.NewLine(); }
private void HandleMethodStatements(CodeMemberMethod obj, Context ctx) { CSharpUtils.HandleStatementCollection(obj.Statements, ctx); }
/// <inheritdoc/> protected override string AsValidNamespace(string s) { return(CSharpUtils.GetValidNamespaceIdentifier(s)); }