<commit_before>package com.siyeh.ipp.enumswitch;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;

import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;

public class CreateEnumSwitchBranchesIntention extends Intention{
    protected PsiElementPredicate getElementPredicate(){
        return new EnumSwitchPredicate();
    }

    public String getText(){
        return "Create 'switch' branches";
    }

    public String getFamilyName(){
        return "Create Enum Switch Branches";
    }

    public void invoke(Project project, Editor editor, PsiFile file)
            throws IncorrectOperationException{
        if(isFileReadOnly(project, file)){
            return;
        }

        final PsiSwitchStatement switchStatement =
                (PsiSwitchStatement) findMatchingElement(file, editor);
        final PsiCodeBlock body = switchStatement.getBody();
        final PsiExpression switchExpression = switchStatement.getExpression();
        final PsiClassType switchType = (PsiClassType) switchExpression.getType();
        final PsiClass enumClass = switchType.resolve();
        final PsiField[] fields = enumClass.getFields();
        final Set missingEnumElements = new HashSet(fields.length);
        for(int i = 0; i < fields.length; i++){
            final PsiField field = fields[i];
            if(field.getType().equals(switchType)){
                final String fieldName = field.getName();
                missingEnumElements.add(fieldName);
            }
        }
        final PsiStatement[] statements = body.getStatements();
        for(int i = 0; i < statements.length; i++){
            final PsiStatement statement = statements[i];
            if(statement instanceof PsiSwitchLabelStatement){
                final PsiSwitchLabelStatement labelStatement =
                        (PsiSwitchLabelStatement) statement;
                final PsiExpression value = labelStatement.getCaseValue();
                if(value != null){
                    final String label = value.getText();
                    missingEnumElements.remove(label);
                }
            }
        }
        final StringBuffer buffer = new StringBuffer(512);

        buffer.append("switch(");
        buffer.append(switchExpression.getText());
        buffer.append("){");
        final PsiElement[] children = body.getChildren();
        for(int i = 1; i < children.length -1; i++){
            buffer.append(children[i].getText());
        }
        final String[] missingElementsArray = (String[]) missingEnumElements.toArray(new String[missingEnumElements.size()]);
        Arrays.sort(missingElementsArray);

        for(int i = 0; i < missingElementsArray.length; i++){
            buffer.append("case ");
            buffer.append(missingElementsArray[i]);
            buffer.append(": break;");
        }
        buffer.append('}');
        final String newStatement = buffer.toString();
        replaceStatement(project, newStatement, switchStatement);
    }
}
<commit_msg>Some PSI usage cleanup<commit_after>package com.siyeh.ipp.enumswitch;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;

import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;

public class CreateEnumSwitchBranchesIntention extends Intention{
    protected PsiElementPredicate getElementPredicate(){
        return new EnumSwitchPredicate();
    }

    public String getText(){
        return "Create 'switch' branches";
    }

    public String getFamilyName(){
        return "Create Enum Switch Branches";
    }

    public void invoke(Project project, Editor editor, PsiFile file)
            throws IncorrectOperationException{
        if(isFileReadOnly(project, file)){
            return;
        }

        final PsiSwitchStatement switchStatement =
                (PsiSwitchStatement) findMatchingElement(file, editor);
        final PsiCodeBlock body = switchStatement.getBody();
        final PsiExpression switchExpression = switchStatement.getExpression();
        final PsiClassType switchType = (PsiClassType) switchExpression.getType();
        final PsiClass enumClass = switchType.resolve();
        final PsiField[] fields = enumClass.getFields();
        final Set missingEnumElements = new HashSet(fields.length);
        for(int i = 0; i < fields.length; i++){
            final PsiField field = fields[i];
            if(field instanceof PsiEnumConstant){
                missingEnumElements.add(field.getName());
            }
        }
        final PsiStatement[] statements = body.getStatements();
        for(int i = 0; i < statements.length; i++){
            final PsiStatement statement = statements[i];
            if(statement instanceof PsiSwitchLabelStatement){
                final PsiSwitchLabelStatement labelStatement =
                        (PsiSwitchLabelStatement) statement;
                final PsiExpression value = labelStatement.getCaseValue();
                if(value instanceof PsiReferenceExpression){
                  final PsiElement resolved = ((PsiReferenceExpression)value).resolve();
                  if (resolved instanceof PsiEnumConstant) {
                    missingEnumElements.remove(((PsiEnumConstant)resolved).getName());
                  }
                }
            }
        }
        final StringBuffer buffer = new StringBuffer(512);

        buffer.append("switch(");
        buffer.append(switchExpression.getText());
        buffer.append("){");
        final PsiElement[] children = body.getChildren();
        for(int i = 1; i < children.length -1; i++){
            buffer.append(children[i].getText());
        }
        final String[] missingElementsArray = (String[]) missingEnumElements.toArray(new String[missingEnumElements.size()]);
        Arrays.sort(missingElementsArray);

        for(int i = 0; i < missingElementsArray.length; i++){
            buffer.append("case ");
            buffer.append(missingElementsArray[i]);
            buffer.append(": break;");
        }
        buffer.append('}');
        final String newStatement = buffer.toString();
        replaceStatement(project, newStatement, switchStatement);
    }
}

{'fraction_non_alphanumeric': 0.10144486692015209, 'fraction_numerical': 0.0024334600760456274, 'mean_word_length': 2.6052631578947367, 'pattern_counts': {'":': 2, '<': 11, '<?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': '5940853', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1628, 'n_words': 376}