<commit_before>package org.jetbrains.android.importDependencies;

import com.intellij.CommonBundle;
import com.intellij.ide.highlighter.ModuleFileType;
import com.intellij.ide.util.newProjectWizard.SourcePathsStep;
import com.intellij.ide.util.projectWizard.importSources.JavaModuleSourceRoot;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.HashMap;
import com.intellij.util.containers.OrderedSet;
import org.jetbrains.android.util.AndroidBundle;
import org.jetbrains.android.util.AndroidUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author Eugene.Kudelevsky
 */
public class ImportDependenciesUtil {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.android.importDependencies.ImportDependenciesUtil");
  private static final Key<Boolean> WAIT_FOR_IMPORTING_DEPENDENCIES_KEY = new Key<Boolean>("WAIT_FOR_IMPORTING_DEPENDENCIES_KEY");
  private static final Object LOCK = new Object();

  private ImportDependenciesUtil() {
  }

  public static void importDependencies(@NotNull Module module,
                                        final boolean updateBackwardDependencies) {
    synchronized (LOCK) {
      final Project project = module.getProject();

      module.putUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY, Boolean.TRUE);

      if (project.getUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY) != Boolean.TRUE) {
        project.putUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY, Boolean.TRUE);
        StartupManager.getInstance(project).runWhenProjectIsInitialized(new Runnable() {
          @Override
          public void run() {
            ApplicationManager.getApplication().invokeLater(new Runnable() {
              @Override
              public void run() {
                importDependenciesForMarkedModules(project, updateBackwardDependencies);
              }
            });
          }
        });
      }
    }
  }

  private static void importDependenciesForMarkedModules(Project project, boolean updateBackwardDependencies) {
    if (project.getUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY) != Boolean.TRUE) {
      return;
    }

    project.putUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY, null);

    final List<Module> modulesToProcess = new ArrayList<Module>();

    for (Module module : ModuleManager.getInstance(project).getModules()) {
      if (module.getUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY) == Boolean.TRUE) {
        module.putUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY, null);
        modulesToProcess.add(module);
      }
    }<commit_msg>perform importing dependencies from property files after module added<commit_after>package org.jetbrains.android.importDependencies;

import com.intellij.CommonBundle;
import com.intellij.ProjectTopics;
import com.intellij.ide.highlighter.ModuleFileType;
import com.intellij.ide.util.newProjectWizard.SourcePathsStep;
import com.intellij.ide.util.projectWizard.importSources.JavaModuleSourceRoot;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.ModuleAdapter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.HashMap;
import com.intellij.util.containers.OrderedSet;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.android.util.AndroidBundle;
import org.jetbrains.android.util.AndroidUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author Eugene.Kudelevsky
 */
public class ImportDependenciesUtil {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.android.importDependencies.ImportDependenciesUtil");
  private static final Key<Boolean> WAIT_FOR_IMPORTING_DEPENDENCIES_KEY = new Key<Boolean>("WAIT_FOR_IMPORTING_DEPENDENCIES_KEY");
  private static final Object LOCK = new Object();

  private ImportDependenciesUtil() {
  }

  public static void importDependencies(@NotNull final Module module,
                                        final boolean updateBackwardDependencies) {
    synchronized (LOCK) {
      final Project project = module.getProject();

      module.putUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY, Boolean.TRUE);

      if (project.getUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY) != Boolean.TRUE) {
        project.putUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY, Boolean.TRUE);

        StartupManager.getInstance(project).runWhenProjectIsInitialized(new Runnable() {
          @Override
          public void run() {
            // todo: this doesn't work in module configurator after 'Apply' button pressed
            if (module.isLoaded()) {
              importDependenciesForMarkedModules(project, updateBackwardDependencies);
            }
            else {
              final MessageBusConnection connection = module.getMessageBus().connect();
              connection.subscribe(ProjectTopics.MODULES, new ModuleAdapter() {
                @Override
                public void moduleAdded(final Project project, final Module addedModule) {
                  if (module.equals(addedModule)) {
                    connection.disconnect();
                    importDependenciesForMarkedModules(project, updateBackwardDependencies);
                  }
                }
              });
            }
          }
        });
      }
    }
  }

  private static void importDependenciesForMarkedModules(final Project project, final boolean updateBackwardDependencies) {
    ApplicationManager.getApplication().invokeLater(new Runnable() {
      @Override
      public void run() {
        doImportDependenciesForMarkedModules(project, updateBackwardDependencies);
      }
    });
  }

  private static void doImportDependenciesForMarkedModules(Project project, boolean updateBackwardDependencies) {
    if (project.getUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY) != Boolean.TRUE) {
      return;
    }

    project.putUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY, null);

    final List<Module> modulesToProcess = new ArrayList<Module>();

    for (Module module : ModuleManager.getInstance(project).getModules()) {
      if (module.getUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY) == Boolean.TRUE) {
        module.putUserData(WAIT_FOR_IMPORTING_DEPENDENCIES_KEY, null);
        modulesToProcess.add(module);
      }
    }
{'fraction_non_alphanumeric': 0.09096511771231357, 'fraction_numerical': 0.0, 'mean_word_length': 4.151746031746032, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6004254', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2340, 'n_words': 379}