<commit_before>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.lang.java;

import com.intellij.ProjectTopics;
import com.intellij.codeInsight.daemon.impl.analysis.JavaModuleGraphUtil;
import com.intellij.openapi.application.AppUIExecutor;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.module.JavaModuleType;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.project.ModuleListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiJavaModule;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.psi.impl.light.LightJavaModule;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.rename.AutomaticRenamingDialog;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.refactoring.rename.naming.AutomaticRenamer;
import com.intellij.util.Function;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static com.intellij.openapi.util.Pair.pair;

public class JavaModuleRenameListener implements StartupActivity, ModuleListener {
  @Override
  public void runActivity(@NotNull Project project) {
    if (!project.isDefault()) {
      project.getMessageBus().connect().subscribe(ProjectTopics.MODULES, this);
    }
  }

  @Override
  @SuppressWarnings("BoundedWildcard")
  public void modulesRenamed(@NotNull Project project, @NotNull List<Module> modules, @NotNull Function<Module, String> oldNameProvider) {
    List<Pair<SmartPsiElementPointer<PsiJavaModule>, String>> suggestions = new ArrayList<>();

    for (Module module : modules) {
      if (ModuleType.get(module) == JavaModuleType.getModuleType()) {
        PsiJavaModule javaModule = Stream.of(ModuleRootManager.getInstance(module).getSourceRoots(false))
            .map(root -> JavaModuleGraphUtil.findDescriptorByFile(root, project))
            .filter(Objects::nonNull)
            .findFirst().orElse(null);
        if (javaModule != null && javaModule.getName().equals(LightJavaModule.moduleName(oldNameProvider.fun(module)))) {
          suggestions.add(pair(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(javaModule),
                           LightJavaModule.moduleName(module.getName())));
        }
      }
    }

    if (!suggestions.isEmpty()) {
      AppUIExecutor.onUiThread(ModalityState.NON_MODAL)
          .later()
          .inSmartMode(project)
          .execute(() -> renameModules(project, suggestions));
    }
  }

  private static void renameModules(Project project, List<? extends Pair<SmartPsiElementPointer<PsiJavaModule>, String>> suggestions) {
    MyAutomaticRenamer renamer = new MyAutomaticRenamer();
    for (Pair<SmartPsiElementPointer<PsiJavaModule>, String> rename : suggestions) {
      PsiJavaModule javaModule = rename.first.getElement();
      if (javaModule != null) {
        renamer.addElement(javaModule, rename.second);
      }
    }

    if (!renamer.getElements().isEmpty()) {
      AutomaticRenamingDialog dialog = new AutomaticRenamingDialog(project, renamer);<commit_msg>Cleanup (formatting)<commit_after>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.lang.java;

import com.intellij.ProjectTopics;
import com.intellij.codeInsight.daemon.impl.analysis.JavaModuleGraphUtil;
import com.intellij.openapi.application.AppUIExecutor;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.module.JavaModuleType;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.project.ModuleListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiJavaModule;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.psi.impl.light.LightJavaModule;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.rename.AutomaticRenamingDialog;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.refactoring.rename.naming.AutomaticRenamer;
import com.intellij.util.Function;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static com.intellij.openapi.util.Pair.pair;

public class JavaModuleRenameListener implements StartupActivity, ModuleListener {
  @Override
  public void runActivity(@NotNull Project project) {
    if (!project.isDefault()) {
      project.getMessageBus().connect().subscribe(ProjectTopics.MODULES, this);
    }
  }

  @Override
  @SuppressWarnings("BoundedWildcard")
  public void modulesRenamed(@NotNull Project project, @NotNull List<Module> modules, @NotNull Function<Module, String> oldNameProvider) {
    List<Pair<SmartPsiElementPointer<PsiJavaModule>, String>> suggestions = new ArrayList<>();

    for (Module module : modules) {
      if (ModuleType.get(module) == JavaModuleType.getModuleType()) {
        PsiJavaModule javaModule = Stream.of(ModuleRootManager.getInstance(module).getSourceRoots(false))
            .map(root -> JavaModuleGraphUtil.findDescriptorByFile(root, project))
            .filter(Objects::nonNull)
            .findFirst().orElse(null);
        if (javaModule != null && javaModule.getName().equals(LightJavaModule.moduleName(oldNameProvider.fun(module)))) {
          suggestions.add(pair(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(javaModule),
                               LightJavaModule.moduleName(module.getName())));
        }
      }
    }

    if (!suggestions.isEmpty()) {
      AppUIExecutor.onUiThread(ModalityState.NON_MODAL).later().inSmartMode(project).execute(() -> renameModules(project, suggestions));
    }
  }

  private static void renameModules(Project project, List<? extends Pair<SmartPsiElementPointer<PsiJavaModule>, String>> suggestions) {
    MyAutomaticRenamer renamer = new MyAutomaticRenamer();
    for (Pair<SmartPsiElementPointer<PsiJavaModule>, String> rename : suggestions) {
      PsiJavaModule javaModule = rename.first.getElement();
      if (javaModule != null) {
        renamer.addElement(javaModule, rename.second);
      }
    }

    if (!renamer.getElements().isEmpty()) {
      AutomaticRenamingDialog dialog = new AutomaticRenamingDialog(project, renamer);
{'fraction_non_alphanumeric': 0.1076880377463225, 'fraction_numerical': 0.002775464890369137, 'mean_word_length': 4.883265306122449, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, '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': '4265554', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2085, 'n_words': 369}