<commit_before>package com.guidewire.build.ijdevkitmvn.ijplugin;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.roots.LibraryOrderEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.OrderEntry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.devkit.module.PluginModuleType;
import org.jetbrains.idea.maven.importing.MavenImporter;
import org.jetbrains.idea.maven.importing.MavenModifiableModelsProvider;
import org.jetbrains.idea.maven.importing.MavenRootModelAdapter;
import org.jetbrains.idea.maven.model.MavenConstants;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectChanges;
import org.jetbrains.idea.maven.project.MavenProjectsProcessorTask;
import org.jetbrains.idea.maven.project.MavenProjectsTree;
import org.jetbrains.idea.maven.project.SupportedRequestType;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 */
public class PluginModuleImporter extends MavenImporter {

  public static final String IJ_PLUGIN_PACKAGING = "ij-plugin";
  public static final String IDEA_SDK_PREFIX = "com.jetbrains.intellij.";
  public static final String IJPLUGIN_GROUP_ID = "com.guidewire.build";
  public static final String IJPLUGIN_ARTIFACT_ID = "ijplugin-maven-plugin";


  public PluginModuleImporter() {
    super(IJPLUGIN_GROUP_ID, IJPLUGIN_ARTIFACT_ID);
  }

  @Override
  public void preProcess(Module module, MavenProject mavenProject, MavenProjectChanges mavenProjectChanges, MavenModifiableModelsProvider mavenModifiableModelsProvider) {
  }

  @Override
  public void process(MavenModifiableModelsProvider modelsProvider,
                      Module module,
                      MavenRootModelAdapter mavenRootModelAdapter,
                      MavenProjectsTree mavenProjectsTree,
                      MavenProject mavenProject,
                      MavenProjectChanges mavenProjectChanges,
                      Map<MavenProject, String> mavenProjectStringMap,
                      List<MavenProjectsProcessorTask> mavenProjectsProcessorTasks) {

    // Remove all entries with groupIds starting from com.jetbrains.intellij.
    // They should be provided through IntelliJ SDK
    ModifiableRootModel rootModel = mavenRootModelAdapter.getRootModel();

    for (OrderEntry entry : rootModel.getOrderEntries()) {
      if (entry instanceof LibraryOrderEntry) {
        LibraryOrderEntry loe = (LibraryOrderEntry) entry;
        if (loe.getLibraryName().startsWith("Maven: com.jetbrains.intellij.")) {
          rootModel.removeOrderEntry(entry);
        }
      }<commit_msg>Another attempt at fixing stray libraries<commit_after>package com.guidewire.build.ijdevkitmvn.ijplugin;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.roots.LibraryOrderEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.OrderEntry;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
import com.intellij.openapi.roots.libraries.Library;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.devkit.module.PluginModuleType;
import org.jetbrains.idea.maven.importing.MavenImporter;
import org.jetbrains.idea.maven.importing.MavenModifiableModelsProvider;
import org.jetbrains.idea.maven.importing.MavenRootModelAdapter;
import org.jetbrains.idea.maven.model.MavenConstants;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectChanges;
import org.jetbrains.idea.maven.project.MavenProjectsProcessorTask;
import org.jetbrains.idea.maven.project.MavenProjectsTree;
import org.jetbrains.idea.maven.project.SupportedRequestType;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 */
public class PluginModuleImporter extends MavenImporter {

  public static final String IJ_PLUGIN_PACKAGING = "ij-plugin";
  public static final String IDEA_SDK_PREFIX = "com.jetbrains.intellij.";
  public static final String IJPLUGIN_GROUP_ID = "com.guidewire.build";
  public static final String IJPLUGIN_ARTIFACT_ID = "ijplugin-maven-plugin";


  public PluginModuleImporter() {
    super(IJPLUGIN_GROUP_ID, IJPLUGIN_ARTIFACT_ID);
  }

  @Override
  public void preProcess(Module module, MavenProject mavenProject, MavenProjectChanges mavenProjectChanges, MavenModifiableModelsProvider mavenModifiableModelsProvider) {
  }

  @Override
  public void process(MavenModifiableModelsProvider modelsProvider,
                      Module module,
                      MavenRootModelAdapter mavenRootModelAdapter,
                      MavenProjectsTree mavenProjectsTree,
                      MavenProject mavenProject,
                      MavenProjectChanges mavenProjectChanges,
                      Map<MavenProject, String> mavenProjectStringMap,
                      List<MavenProjectsProcessorTask> mavenProjectsProcessorTasks) {

    // Remove all entries with groupIds starting from com.jetbrains.intellij.
    // They should be provided through IntelliJ SDK
    ModifiableRootModel rootModel = mavenRootModelAdapter.getRootModel();

    for (OrderEntry entry : rootModel.getOrderEntries()) {
      if (entry instanceof LibraryOrderEntry) {
        LibraryOrderEntry loe = (LibraryOrderEntry) entry;
        if (loe.getLibraryName().startsWith("Maven: com.jetbrains.intellij.")) {
          rootModel.removeOrderEntry(entry);

          // XXX: If library is not used anymore, it will not get properly commited. As a result,
          // roots of type CLASSES will be empty. MavenProjectImporter.removeUnusedProjectLibraries
          // will treat such a library as having "user changes" and will not remove it.
          // Therefore, we forcefully "commit" root URLs.
          // We cannot use "commit" method since there is a chance library will be committed again
          // later, which will generate an exception.
          // So, we just copy roots to make MavenProjectImporter.removeUnusedProjectLibraries happy.
          Library.ModifiableModel modifiableModel = modelsProvider.getLibraryModel(loe.getLibrary());
          if (!((LibraryEx) modifiableModel).isDisposed()) {
            try {
              Method m = LibraryImpl.class.getDeclaredMethod("copyRootsFrom", LibraryImpl.class);
              m.setAccessible(true);
              m.invoke(loe.getLibrary(), modifiableModel);
            } catch (Exception e) {
              // Just log and continue
              e.printStackTrace();
            }
          }
        }
      }
{'fraction_non_alphanumeric': 0.08018310691080921, 'fraction_numerical': 0.0, 'mean_word_length': 3.742997198879552, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1697901', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1866, 'n_words': 386}