<commit_before>
package com.intellij.refactoring.migration;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Key;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.IncorrectOperationException;

import java.util.ArrayList;

/**
 *
 */
public class MigrationUtil {
  private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.migration.MigrationUtil");

  private static final Key ROOT = Key.create("ROOT");

  public static UsageInfo[] findPackageUsages(PsiManager manager,
                                              PsiMigration migration,
                                              String qName) {
    PsiPackage aPackage = findOrCreatePackage(manager, migration, qName);

    final ArrayList<UsageInfo> results = new ArrayList<UsageInfo>();
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject());
    PsiReference[] usages = manager.getSearchHelper().findReferences(aPackage, projectScope, false);
    for(int i = 0; i < usages.length; i++){
      results.add(new UsageInfo(usages[i].getElement()));
    }

    return results.toArray(new UsageInfo[results.size()]);
  }

  public static void doPackageMigration(PsiManager manager,
                                        PsiMigration migration, String newQName,
                                        UsageInfo[] usages) {
    try{
      PsiPackage aPackage = findOrCreatePackage(manager, migration, newQName);

      // rename all references
      for(int i = 0; i < usages.length; i++){
        UsageInfo usage = usages[i];
        if (!usage.getElement().isValid()) continue;
        if (usage.getElement() instanceof PsiJavaCodeReferenceElement){
          ((PsiJavaCodeReferenceElement)usage.getElement()).bindToElement(aPackage);
        }
      }
    }
    catch(IncorrectOperationException e){
      // should not happen!
      LOG.error(e);
    }
  }

  public static UsageInfo[] findClassUsages(PsiManager manager,
                                            PsiMigration migration,
                                            String qName) {
    PsiClass aClass = findOrCreateClass(manager, migration, qName);

    final ArrayList<UsageInfo> results = new ArrayList<UsageInfo>();
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject());
    PsiReference[] usages = manager.getSearchHelper().findReferences(aClass, projectScope, false);
    for(int i = 0; i < usages.length; i++){
      results.add(new UsageInfo(usages[i].getElement()));
    }

    return results.toArray(new UsageInfo[results.size()]);
  }

  public static void doClassMigration(PsiManager manager,
                                      PsiMigration migration,
                                      String newQName,
                                      UsageInfo[] usages) {
    try{
      PsiClass aClass = findOrCreateClass(manager, migration, newQName);

      // rename all references
      for(int i = 0; i < usages.length; i++){
        UsageInfo usage = usages[i];
        if (!usage.getElement().isValid()) continue;
        if (usage.getElement() instanceof PsiJavaCodeReferenceElement){
          final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)usage.getElement();
          referenceElement.bindToElement(aClass);
        }
      }
    }
    catch(IncorrectOperationException e){
      // should not happen!
      LOG.error(e);
    }
  }
<commit_msg>IDEADEV-18051<commit_after>
package com.intellij.refactoring.migration;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.IncorrectOperationException;

import java.util.ArrayList;

/**
 *
 */
public class MigrationUtil {
  private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.migration.MigrationUtil");

  public static UsageInfo[] findPackageUsages(PsiManager manager,
                                              PsiMigration migration,
                                              String qName) {
    PsiPackage aPackage = findOrCreatePackage(manager, migration, qName);

    return findRefs(manager, aPackage);
  }

  public static void doPackageMigration(PsiManager manager,
                                        PsiMigration migration, String newQName,
                                        UsageInfo[] usages) {
    try{
      PsiPackage aPackage = findOrCreatePackage(manager, migration, newQName);

      // rename all references
      for (UsageInfo usage : usages) {
        PsiElement element = usage.getElement();
        if (element == null || !element.isValid()) continue;
        if (element instanceof PsiJavaCodeReferenceElement) {
          ((PsiJavaCodeReferenceElement)element).bindToElement(aPackage);
        }
      }
    }
    catch(IncorrectOperationException e){
      // should not happen!
      LOG.error(e);
    }
  }

  public static UsageInfo[] findClassUsages(PsiManager manager,
                                            PsiMigration migration,
                                            String qName) {
    PsiClass aClass = findOrCreateClass(manager, migration, qName);

    return findRefs(manager, aClass);
  }

  private static UsageInfo[] findRefs(final PsiManager manager, final PsiElement aClass) {
    final ArrayList<UsageInfo> results = new ArrayList<UsageInfo>();
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject());
    PsiReference[] usages = manager.getSearchHelper().findReferences(aClass, projectScope, false);
    for (PsiReference usage : usages) {
      results.add(new UsageInfo(usage.getElement()));
    }

    return results.toArray(new UsageInfo[results.size()]);
  }

  public static void doClassMigration(PsiManager manager,
                                      PsiMigration migration,
                                      String newQName,
                                      UsageInfo[] usages) {
    try{
      PsiClass aClass = findOrCreateClass(manager, migration, newQName);

      // rename all references
      for (UsageInfo usage : usages) {
        PsiElement element = usage.getElement();
        if (element == null || !element.isValid()) continue;
        if (element instanceof PsiJavaCodeReferenceElement) {
          final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)element;
          referenceElement.bindToElement(aClass);
        }
      }
    }
    catch(IncorrectOperationException e){
      // should not happen!
      LOG.error(e);
    }
  }

{'fraction_non_alphanumeric': 0.0935176925364258, 'fraction_numerical': 0.0013380909901873326, 'mean_word_length': 2.4746900826446283, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4442671', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1678, 'n_words': 377}