Exemple #1
0
        private static void ProvideWriteMethod1(CodeTypeMemberCollection members, CodeMemberField currentIndent, CodeMemberField endsWithNewline, CodeMemberProperty GenerationEnvironment, CodeVariableReferenceExpression textToAppend)
        {
            CodeExpression   codeExpression   = typeof(Environment).Expr().Prop("NewLine");
            CodeMemberMethod codeMemberMethod = CodeDomHelpers.CreateMethod(null, "Write", "Write text directly into the generated output", (MemberAttributes)24578, new CodeConditionStatement(CodeDomHelpers.Call(typeof(string), "IsNullOrEmpty", textToAppend), new CodeMethodReturnStatement()), new CodeCommentStatement("If we're starting off, or if the previous text ended with a newline,"), new CodeCommentStatement("we have to append the current indent first."), new CodeConditionStatement(new CodeBinaryOperatorExpression(GenerationEnvironment.Ref().Prop("Length").VEquals(0.Prim()), CodeBinaryOperatorType.BooleanOr, endsWithNewline.Ref()), GenerationEnvironment.Ref().CallS("Append", currentIndent.Ref()), endsWithNewline.Ref().Assign(false.Prim())), new CodeCommentStatement("Check if the current text ends with a newline"), new CodeConditionStatement(textToAppend.Call("EndsWith", codeExpression, typeof(StringComparison).Expr().Prop("CurrentCulture")), new CodeAssignStatement(endsWithNewline.Ref(), true.Prim())), new CodeCommentStatement("This is an optimization. If the current indent is \"\", then we don't have to do any"), new CodeCommentStatement("of the more complex stuff further down."), new CodeConditionStatement(currentIndent.Ref().Prop("Length").VEquals(0.Prim()), GenerationEnvironment.Ref().CallS("Append", textToAppend), new CodeMethodReturnStatement()), new CodeCommentStatement("Everywhere there is a newline in the text, add an indent after it"), textToAppend.Assign(textToAppend.Call("Replace", codeExpression, codeExpression.Add(currentIndent.Ref()))), new CodeCommentStatement("If the text ends with a newline, then we should strip off the indent added at the very end"), new CodeCommentStatement("because the appropriate indent will be added when the next time Write() is called"), new CodeConditionStatement(endsWithNewline.Ref(), new CodeStatement[1]
            {
                GenerationEnvironment.Ref().CallS("Append", textToAppend, 0.Prim(), textToAppend.Prop("Length").Subtract(currentIndent.Ref().Prop("Length")))
            }, new CodeStatement[1]
            {
                GenerationEnvironment.Ref().CallS("Append", textToAppend)
            }));

            codeMemberMethod.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Transform-time helpers"));
            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), textToAppend.VariableName));
            members.Add(codeMemberMethod);
        }
Exemple #2
0
        private static void ProvideWriteMethod2(CodeTypeMemberCollection members, CodeParameterDeclarationExpression argsParam)
        {
            CodeMemberMethod codeMemberMethod = CodeDomHelpers.CreateMethod(null, "Write", "Write formatted text directly into the generated output", (MemberAttributes)24578, CodeDomHelpers.Call("Write", CodeDomHelpers.Call(typeof(string), "Format", typeof(CultureInfo).Expr().Prop("CurrentCulture"), new CodeVariableReferenceExpression("format"), new CodeVariableReferenceExpression("args"))));

            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "format"));
            argsParam.CustomAttributes.Add(new CodeAttributeDeclaration("System.ParamArrayAttribute"));
            codeMemberMethod.Parameters.Add(argsParam);
            members.Add(codeMemberMethod);
        }
Exemple #3
0
        private static void ProvideWriteLineMethod1(CodeTypeMemberCollection members, CodeMemberField endsWithNewline, CodeMemberProperty GenerationEnvironment, CodeVariableReferenceExpression textToAppend)
        {
            CodeMemberMethod codeMemberMethod = CodeDomHelpers.CreateMethod(null, "WriteLine", "Write text directly into the generated output", (MemberAttributes)24578, CodeDomHelpers.Call("Write", textToAppend), GenerationEnvironment.Ref().CallS("AppendLine"), endsWithNewline.Ref().Assign(true.Prim()));

            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), textToAppend.VariableName));
            members.Add(codeMemberMethod);
        }
        private static void ProvideWriteLineMethod2(CodeTypeMemberCollection members, CodeParameterDeclarationExpression argsParam)
        {
            CodeMemberMethod method = CodeDomHelpers.CreateMethod(null, "WriteLine", "Write formatted text directly into the generated output", MemberAttributes.Public | MemberAttributes.Final, new CodeObject[] { CodeDomHelpers.Call("WriteLine", new CodeExpression[] { CodeDomHelpers.Call(typeof(string), "Format", new CodeExpression[] { typeof(CultureInfo).Expr().Prop("CurrentCulture"), new CodeVariableReferenceExpression("format"), new CodeVariableReferenceExpression("args") }) }) });

            method.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "format"));
            method.Parameters.Add(argsParam);
            members.Add(method);
        }