<commit_before>package uk.co.littlemike.editree.text;

import uk.co.littlemike.editree.language.statements.Assignment;
import uk.co.littlemike.editree.language.statements.Statement;
import uk.co.littlemike.editree.language.statements.StatementVisitor;
import uk.co.littlemike.editree.language.statements.VariableDeclaration;
import uk.co.littlemike.editree.language.statements.control.WhileLoop;

public class StatementRenderer {
    private final ExpressionRenderer expressionRenderer = new ExpressionRenderer();

    public String render(Statement statement) {
        return renderWithContext(statement, new StatementRenderContext());
    }

    private String renderWithContext(Statement statement, StatementRenderContext context) {
        statement.visit(new StatementVisitor() {
            @Override
            public void visit(VariableDeclaration declaration) {
                renderToString(declaration, context);
            }
            @Override
            public void visit(WhileLoop loop) {
                renderToString(loop, context);
            }

            @Override
            public void visit(Assignment assignment) {
                renderToString(assignment, context);
            }
        });
        return context.getRenderedText();
    }

    private void renderToString(Assignment assignment, StatementRenderContext context) {
        context.appendLine(
                assignment.getVariableName() +
                " = " +
                expressionRenderer.render(assignment.getValue()) +
                ";"
        );
    }

    private void renderToString(VariableDeclaration declaration, StatementRenderContext context) {
        if (declaration.hasInitialValue()) {
            context.appendLine(
                    declaration.getType().getName() +
                    " " +
                    declaration.getName() +
                    " = " +
                    expressionRenderer.render(declaration.getInitialValue().get()) +
                    ";"
            );
        }
        else {
            context.appendLine(
                    declaration.getType().getName() +
                    " " +
                    declaration.getName() +
                    ";"
            );
        }
    }

    private void renderToString(WhileLoop loop, StatementRenderContext context) {
        context.appendLine(String.format(
                "while (%s) {",<commit_msg>Use String.format<commit_after>package uk.co.littlemike.editree.text;

import uk.co.littlemike.editree.language.statements.Assignment;
import uk.co.littlemike.editree.language.statements.Statement;
import uk.co.littlemike.editree.language.statements.StatementVisitor;
import uk.co.littlemike.editree.language.statements.VariableDeclaration;
import uk.co.littlemike.editree.language.statements.control.WhileLoop;

public class StatementRenderer {
    private final ExpressionRenderer expressionRenderer = new ExpressionRenderer();

    public String render(Statement statement) {
        return renderWithContext(statement, new StatementRenderContext());
    }

    private String renderWithContext(Statement statement, StatementRenderContext context) {
        statement.visit(new StatementVisitor() {
            @Override
            public void visit(VariableDeclaration declaration) {
                renderToString(declaration, context);
            }
            @Override
            public void visit(WhileLoop loop) {
                renderToString(loop, context);
            }

            @Override
            public void visit(Assignment assignment) {
                renderToString(assignment, context);
            }
        });
        return context.getRenderedText();
    }

    private void renderToString(Assignment assignment, StatementRenderContext context) {
        context.appendLine(String.format(
                "%s = %s;",
                assignment.getVariableName(),
                expressionRenderer.render(assignment.getValue())
        ));
    }

    private void renderToString(VariableDeclaration declaration, StatementRenderContext context) {
        if (declaration.hasInitialValue()) {
            context.appendLine(String.format(
                    "%s %s = %s;",
                    declaration.getType().getName(),
                    declaration.getName(),
                    expressionRenderer.render(declaration.getInitialValue().get())
            ));
        }
        else {
            context.appendLine(String.format(
                    "%s %s;",
                    declaration.getType().getName(),
                    declaration.getName()
            ));
        }
    }

    private void renderToString(WhileLoop loop, StatementRenderContext context) {
        context.appendLine(String.format(
                "while (%s) {",
{'fraction_non_alphanumeric': 0.09036144578313253, 'fraction_numerical': 0.0, 'mean_word_length': 2.1004507405022537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4154132', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1083, 'n_words': 196}