<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.project.Project;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryType;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.indexing.IndexableSetContributor;
import gnu.trove.THashSet;
import org.jetbrains.annotations.Nullable;

import java.util.Set;

/**
 * @author <NAME>
 */
public abstract class ScriptingIndexableSetContributor extends IndexableSetContributor {

  @Override
  public Set<VirtualFile> getAdditionalRootsToIndex(@Nullable Project project) {
    final Set<VirtualFile> predefinedFiles = getPredefinedFilesToIndex();
    final THashSet<VirtualFile> filesToIndex = new THashSet<VirtualFile>();
    filesToIndex.addAll(predefinedFiles);
    filesToIndex.addAll(getLibraryFiles(project));
    return filesToIndex;
  }

  public Set<VirtualFile> getLibraryFiles(Project project) {
    final THashSet<VirtualFile> libFiles = new THashSet<VirtualFile>();
    LibraryType libType = getLibraryType();
    if (project != null) {
      LibraryTable libTable = ScriptingLibraryManager.getLibraryTable(project, ScriptingLibraryManager.LibraryLevel.GLOBAL);
      if (libTable != null) {
        for (Library lib : libTable.getLibraries()) {
          if (lib instanceof LibraryEx && ((LibraryEx)lib).getType().equals(libType)) {
            for (VirtualFile libFile : lib.getFiles(OrderRootType.SOURCES)) {
              libFile.putUserData(getIndexKey(), "");
              libFiles.add(libFile);
            }
          }
        }
      }
    }
    return libFiles;
  }

  @Override
  public Set<VirtualFile> getAdditionalRootsToIndex() {
    return getAdditionalRootsToIndex(null);
  }

  public abstract Set<VirtualFile> getPredefinedFilesToIndex();

  public abstract Key<String> getIndexKey();

  public abstract LibraryType getLibraryType();
}<commit_msg>fix NPE, most libraries do not have type<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.project.Project;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryType;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.indexing.IndexableSetContributor;
import gnu.trove.THashSet;
import org.jetbrains.annotations.Nullable;

import java.util.Set;

/**
 * @author <NAME>
 */
public abstract class ScriptingIndexableSetContributor extends IndexableSetContributor {

  @Override
  public Set<VirtualFile> getAdditionalRootsToIndex(@Nullable Project project) {
    final Set<VirtualFile> predefinedFiles = getPredefinedFilesToIndex();
    final THashSet<VirtualFile> filesToIndex = new THashSet<VirtualFile>();
    filesToIndex.addAll(predefinedFiles);
    filesToIndex.addAll(getLibraryFiles(project));
    return filesToIndex;
  }

  public Set<VirtualFile> getLibraryFiles(Project project) {
    final THashSet<VirtualFile> libFiles = new THashSet<VirtualFile>();
    LibraryType libType = getLibraryType();
    if (project != null) {
      LibraryTable libTable = ScriptingLibraryManager.getLibraryTable(project, ScriptingLibraryManager.LibraryLevel.GLOBAL);
      if (libTable != null) {
        for (Library lib : libTable.getLibraries()) {
          if (lib instanceof LibraryEx && libType.equals(((LibraryEx)lib).getType())) {
            for (VirtualFile libFile : lib.getFiles(OrderRootType.SOURCES)) {
              libFile.putUserData(getIndexKey(), "");
              libFiles.add(libFile);
            }
          }
        }
      }
    }
    return libFiles;
  }

  @Override
  public Set<VirtualFile> getAdditionalRootsToIndex() {
    return getAdditionalRootsToIndex(null);
  }

  public abstract Set<VirtualFile> getPredefinedFilesToIndex();

  public abstract Key<String> getIndexKey();

  public abstract LibraryType getLibraryType();
}
{'fraction_non_alphanumeric': 0.09650552236103567, 'fraction_numerical': 0.0043454644215100485, 'mean_word_length': 4.270992366412214, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '5665663', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1569, 'n_words': 393}