<commit_before>/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * 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.intellij.openapi.roots.libraries.scripting;

import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.libraries.LibraryTableBase;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
import com.intellij.openapi.roots.libraries.LibraryType;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;

/**
 * @author <NAME>
 */
public class ScriptingLibraryManager {

  public enum LibraryLevel {GLOBAL, PROJECT}

  private Project myProject;
  private LibraryLevel myLibLevel = LibraryLevel.PROJECT;
  private ScriptingLibraryTable myLibTable;
  private LibraryType myLibraryType;

  public ScriptingLibraryManager(Project project, LibraryType libraryType) {
    this(LibraryLevel.GLOBAL, project, libraryType);
  }

  public ScriptingLibraryManager(LibraryLevel libLevel, Project project, LibraryType libraryType) {
    myProject = project;
    myLibLevel = libLevel;
    myLibraryType = libraryType;
  }

  public ScriptingLibraryTable getScriptingLibraryTable() {
    ensureModel();
    return myLibTable;
  }

  public void commitChanges() {
    if (myLibTable != null) {
      LibraryTable libTable = getLibraryTable();
      if (libTable != null) {
        LibraryTable.ModifiableModel libTableModel = libTable.getModifiableModel();
        updateLibraries(libTableModel, OrderRootType.SOURCES);
        updateLibraries(libTableModel, OrderRootType.CLASSES);
        libTableModel.commit();
      }
      myLibTable = null;
    }
    if (myLibLevel == LibraryLevel.GLOBAL) {
      ModuleManager.getInstance(myProject).getModifiableModel().commit();
    }
  }

  private void updateLibraries(LibraryTable.ModifiableModel libTableModel, OrderRootType rootType) {
    for (Library library : libTableModel.getLibraries()) {
      ScriptingLibraryTable.LibraryModel scriptingLibModel = myLibTable.getLibraryByName(library.getName());
      if (scriptingLibModel == null) {
        libTableModel.removeLibrary(library);
      }
      else {
        Library.ModifiableModel libModel = library.getModifiableModel();
        for (VirtualFile libRoot : libModel.getFiles(rootType)) {
          libModel.removeRoot(libRoot.getUrl(), rootType);
        }
        for (VirtualFile newRoot : scriptingLibModel.getFiles(rootType)) {
          libModel.addRoot(newRoot, rootType);
        }
        libModel.commit();
      }
    }
    for (ScriptingLibraryTable.LibraryModel scriptingLibModel : myLibTable.getLibraries()) {
      Library library = libTableModel.getLibraryByName(scriptingLibModel.getName());
      if (library == null && libTableModel instanceof LibraryTableBase.ModifiableModelEx) {
        library = ((LibraryTableBase.ModifiableModelEx)libTableModel).createLibrary(scriptingLibModel.getName(), myLibraryType);
        Library.ModifiableModel libModel = library.getModifiableModel();
        for (VirtualFile newRoot : scriptingLibModel.getFiles(rootType)) {
          libModel.addRoot(newRoot, rootType);<commit_msg>Minor JavaScript library management/UI fixes<commit_after>/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * 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.intellij.openapi.roots.libraries.scripting;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.roots.ex.ProjectRootManagerEx;
import com.intellij.openapi.roots.impl.libraries.LibraryTableBase;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
import com.intellij.openapi.roots.libraries.LibraryType;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;

/**
 * @author <NAME>
 */
public class ScriptingLibraryManager {

  public enum LibraryLevel {GLOBAL, PROJECT}

  private Project myProject;
  private LibraryLevel myLibLevel = LibraryLevel.PROJECT;
  private ScriptingLibraryTable myLibTable;
  private LibraryType myLibraryType;

  public ScriptingLibraryManager(Project project, LibraryType libraryType) {
    this(LibraryLevel.GLOBAL, project, libraryType);
  }

  public ScriptingLibraryManager(LibraryLevel libLevel, Project project, LibraryType libraryType) {
    myProject = project;
    myLibLevel = libLevel;
    myLibraryType = libraryType;
  }

  public ScriptingLibraryTable getScriptingLibraryTable() {
    ensureModel();
    return myLibTable;
  }

  public void commitChanges() {
    if (myLibTable != null) {
      LibraryTable libTable = getLibraryTable();
      if (libTable != null) {
        LibraryTable.ModifiableModel libTableModel = libTable.getModifiableModel();
        updateLibraries(libTableModel, OrderRootType.SOURCES);
        updateLibraries(libTableModel, OrderRootType.CLASSES);
        libTableModel.commit();
      }
      updateOpenProjects();
      myLibTable = null;
    }
  }

  private static void updateOpenProjects() {
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      public void run() {
        for (Project project : ProjectManager.getInstance().getOpenProjects()) {
          ProjectRootManagerEx.getInstanceEx(project).makeRootsChange(EmptyRunnable.getInstance(), false, true);
        }
      }
    });
  }

  private void updateLibraries(LibraryTable.ModifiableModel libTableModel, OrderRootType rootType) {
    for (Library library : libTableModel.getLibraries()) {
      ScriptingLibraryTable.LibraryModel scriptingLibModel = myLibTable.getLibraryByName(library.getName());
      if (scriptingLibModel == null) {
        libTableModel.removeLibrary(library);
      }
      else {
        Library.ModifiableModel libModel = library.getModifiableModel();
        for (VirtualFile libRoot : libModel.getFiles(rootType)) {
          libModel.removeRoot(libRoot.getUrl(), rootType);
        }
        for (VirtualFile newRoot : scriptingLibModel.getFiles(rootType)) {
          libModel.addRoot(newRoot, rootType);
        }
        libModel.commit();
      }
    }
    for (ScriptingLibraryTable.LibraryModel scriptingLibModel : myLibTable.getLibraries()) {
      Library library = libTableModel.getLibraryByName(scriptingLibModel.getName());
      if (library == null && libTableModel instanceof LibraryTableBase.ModifiableModelEx) {
        library = ((LibraryTableBase.ModifiableModelEx)libTableModel).createLibrary(scriptingLibModel.getName(), myLibraryType);
        Library.ModifiableModel libModel = library.getModifiableModel();
        for (VirtualFile newRoot : scriptingLibModel.getFiles(rootType)) {
          libModel.addRoot(newRoot, rootType);
{'fraction_non_alphanumeric': 0.0908429351121892, 'fraction_numerical': 0.0029108550636749546, 'mean_word_length': 4.252229299363058, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '539433', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2261, 'n_words': 506}