<commit_before>package com.chrisfolger.needsmoredojo.refactoring;

import com.intellij.lang.javascript.psi.*;
import com.intellij.psi.PsiElement;

public class UtilConverter implements DeclareFinder.CompletionCallback
{
    @Override
    public void run(Object[] result)
    {
        JSCallExpression expression = (JSCallExpression) result[0];
        JSReturnStatement returnStatement = (JSReturnStatement) result[1];

        // this will be used to determine what we mixin to the util
        JSArrayLiteralExpression arrayLiteral = (JSArrayLiteralExpression) expression.getArguments()[0];
        JSExpression[] expressionsToMixin = arrayLiteral.getExpressions();

        // now we need to get the object literal with all of the function names
        JSObjectLiteralExpression literal = (JSObjectLiteralExpression) expression.getArguments()[1];
        JSProperty[] methodsToConvert = literal.getProperties();

        doRefactor(returnStatement, expressionsToMixin, methodsToConvert);
    }

    public void doRefactor(JSReturnStatement originalReturnStatement, JSExpression[] mixins, JSProperty[] properties)
    {
        // insert new items before this
        PsiElement parent = originalReturnStatement.getParent();

        JSExpressionCodeFragment fragment = JSElementFactory.createExpressionCodeFragment(parent.getProject(), "var x = y", parent, true);
    }
}<commit_msg>creation of refactored nodes<commit_after>package com.chrisfolger.needsmoredojo.refactoring;

import com.intellij.lang.ASTNode;
import com.intellij.lang.javascript.psi.*;
import com.intellij.lang.javascript.psi.impl.JSChangeUtil;
import com.intellij.lang.javascript.psi.util.JSUtils;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.psi.PsiElement;

public class UtilConverter implements DeclareFinder.CompletionCallback
{
    @Override
    public void run(Object[] result)
    {
        JSCallExpression expression = (JSCallExpression) result[0];
        final JSReturnStatement returnStatement = (JSReturnStatement) result[1];

        // this will be used to determine what we mixin to the util
        JSArrayLiteralExpression arrayLiteral = (JSArrayLiteralExpression) expression.getArguments()[0];
        final JSExpression[] expressionsToMixin = arrayLiteral.getExpressions();

        // now we need to get the object literal with all of the function names
        JSObjectLiteralExpression literal = (JSObjectLiteralExpression) expression.getArguments()[1];
        final JSProperty[] methodsToConvert = literal.getProperties();

        CommandProcessor.getInstance().executeCommand(expression.getProject(), new Runnable() {
            @Override
            public void run() {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {
                    @Override
                    public void run() {
                        doRefactor(returnStatement, expressionsToMixin, methodsToConvert);
                    }
                });
            }
        },
        "Convert module to use Util Pattern",
        "Convert module to use Util Pattern");
    }

    public void doRefactor(JSReturnStatement originalReturnStatement, JSExpression[] mixins, JSProperty[] properties) {
        // insert new items before this
        PsiElement parent = originalReturnStatement.getParent();

        StringBuilder mixinArray = new StringBuilder();
        for (JSExpression mixin : mixins) {
            if (mixinArray.toString().equals("")) {
                mixinArray.append(mixin.getText());
            } else {
                mixinArray.append(", " + mixin.getText());
            }
        }
        String declareStatement = String.format("var util = declare([%s], {});", mixinArray.toString());
        ASTNode node = JSChangeUtil.createStatementFromText(parent.getProject(), declareStatement, JSUtils.getDialect(parent.getContainingFile()));
        parent.addBefore(node.getPsi(), originalReturnStatement);

        for (JSProperty property : properties) {
            String propertyStatement = String.format("\t\tutil.%s = %s;", property.getName(), property.getValue().getText());
            node = JSChangeUtil.createExpressionFromText(parent.getProject(), propertyStatement, JSUtils.getDialect(parent.getContainingFile()));
            parent.addBefore(node.getPsi(), originalReturnStatement);
        }

        originalReturnStatement.delete();
    }
}
{'fraction_non_alphanumeric': 0.0946639874972092, 'fraction_numerical': 0.0017861129716454567, 'mean_word_length': 3.357976653696498, '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': '3227444', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1105, 'n_words': 259}