<commit_before>/*
 * Copyright 2003-2006 <NAME>, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ipp.enumswitch;

import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NonNls;

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

public class CreateEnumSwitchBranchesIntention extends Intention {

    @NotNull
    protected PsiElementPredicate getElementPredicate() {
        return new EnumSwitchPredicate();
    }

    public void processIntention(PsiElement element)
            throws IncorrectOperationException {
        final PsiSwitchStatement switchStatement =
                (PsiSwitchStatement)element;
        final PsiCodeBlock body = switchStatement.getBody();
        if (body == null) {
            return;
        }
        final PsiExpression switchExpression = switchStatement.getExpression();
        if (switchExpression == null) {
            return;
        }
        final PsiClassType switchType =
                (PsiClassType)switchExpression.getType();
        if (switchType == null) {
            return;
        }
        final PsiClass enumClass = switchType.resolve();
        if (enumClass == null) {
            return;
        }
        final PsiField[] fields = enumClass.getFields();
        final Set<String> missingEnumElements =
                new HashSet<String>(fields.length);
        for (final PsiField field : fields) {
            if (field instanceof PsiEnumConstant) {
                missingEnumElements.add(field.getName());
            }
        }
        final PsiStatement[] statements = body.getStatements();
        for (final PsiStatement statement : statements) {
            if (statement instanceof PsiSwitchLabelStatement) {
                final PsiSwitchLabelStatement labelStatement =
                        (PsiSwitchLabelStatement)statement;
                final PsiExpression value = labelStatement.getCaseValue();
                if (value instanceof PsiReferenceExpression) {
                    final PsiReferenceExpression reference =
                            (PsiReferenceExpression)value;
                    final PsiElement resolved = reference.resolve();
                    if (resolved instanceof PsiEnumConstant) {
                        final PsiEnumConstant enumConstant =
                                (PsiEnumConstant)resolved;
                        missingEnumElements.remove(enumConstant.getName());
                    }
                }
            }
        }
        @NonNls final StringBuilder buffer = new StringBuilder(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 =
                missingEnumElements.toArray(
                        new String[missingEnumElements.size()]);
        Arrays.sort(missingElementsArray);
        for (String aMissingElementsArray : missingElementsArray) {
            buffer.append("case ");
            buffer.append(aMissingElementsArray);
            buffer.append(": break;");
        }
        buffer.append('}');
        final String newStatement = buffer.toString();
        replaceStatement(newStatement, switchStatement);
    }
}<commit_msg>IDEADEV-38460 ("Create 'switch' branches" for a switch over enum creates branches in the alphabetical order)<commit_after>/*
 * Copyright 2003-2009 <NAME>, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ipp.enumswitch;

import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class CreateEnumSwitchBranchesIntention extends Intention {

    @NotNull
    protected PsiElementPredicate getElementPredicate() {
        return new EnumSwitchPredicate();
    }

    public void processIntention(@NotNull PsiElement element)
            throws IncorrectOperationException {
        final PsiSwitchStatement switchStatement =
                (PsiSwitchStatement)element;
        final PsiCodeBlock body = switchStatement.getBody();
        if (body == null) {
            return;
        }
        final PsiExpression switchExpression = switchStatement.getExpression();
        if (switchExpression == null) {
            return;
        }
        final PsiClassType switchType =
                (PsiClassType)switchExpression.getType();
        if (switchType == null) {
            return;
        }
        final PsiClass enumClass = switchType.resolve();
        if (enumClass == null) {
            return;
        }
        final PsiField[] fields = enumClass.getFields();
        final List<String> missingEnumElements =
                new ArrayList<String>(fields.length);
        for (final PsiField field : fields) {
            if (field instanceof PsiEnumConstant) {
                missingEnumElements.add(field.getName());
            }
        }
        final PsiStatement[] statements = body.getStatements();
        for (final PsiStatement statement : statements) {
            if (!(statement instanceof PsiSwitchLabelStatement)) {
                continue;
            }
            final PsiSwitchLabelStatement labelStatement =
                    (PsiSwitchLabelStatement)statement;
            final PsiExpression value = labelStatement.getCaseValue();
            if (!(value instanceof PsiReferenceExpression)) {
                continue;
            }
            final PsiReferenceExpression reference =
                    (PsiReferenceExpression)value;
            final PsiElement resolved = reference.resolve();
            if (!(resolved instanceof PsiEnumConstant)) {
                continue;
            }
            final PsiEnumConstant enumConstant = (PsiEnumConstant) resolved;
            missingEnumElements.remove(enumConstant.getName());
        }
        @NonNls final StringBuilder buffer = new StringBuilder();
        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());
        }
        for (String missingEnumElement : missingEnumElements) {
            buffer.append("case ");
            buffer.append(missingEnumElement);
            buffer.append(": break;");
        }
        buffer.append('}');
        final String newStatement = buffer.toString();
        replaceStatement(newStatement, switchStatement);
    }
}
{'fraction_non_alphanumeric': 0.08734865332344947, 'fraction_numerical': 0.004447739065974796, 'mean_word_length': 2.36450540315877, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4049655', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1911, 'n_words': 534}