Skip to content

Instantly share code, notes, and snippets.

@halirutan
Created March 14, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halirutan/4359fa336b3bd318a16ea76fc2d5503f to your computer and use it in GitHub Desktop.
Save halirutan/4359fa336b3bd318a16ea76fc2d5503f to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2018 Patrick Scheibe
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package de.halirutan.mathematica.index.packageexport;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.util.indexing.*;
import com.intellij.util.indexing.FileBasedIndex.InputFilter;
import com.intellij.util.io.KeyDescriptor;
import de.halirutan.mathematica.lang.WolframLanguage;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.stream.Collectors;
/**
* Simple file index for functions that are exported from a package by giving them a usage message.
* @author patrick (01.11.16).
*/
public class WLPackageExportIndex extends ScalarIndexExtension<PackageExportSymbol> implements PsiDependentIndex{
public static final ID<PackageExportSymbol, Void> INDEX_ID = ID.create("Mathematica.fileExports");
private static final int BASE_VERSION = 14;
private static final LanguageFileType MATHEMATICA_FILE_TYPE = WolframLanguage.INSTANCE.getAssociatedFileType();
private static final Set<String> IGNORED_FILES = new HashSet<>();
static {
IGNORED_FILES.add("PacletInfo.m");
IGNORED_FILES.add("init.m");
}
@NotNull
@Override
public InputFilter getInputFilter() {
return file -> {
if (file.getFileType() == MATHEMATICA_FILE_TYPE) {
final String fileName = file.getName();
// Don't index notebooks and ignored files like PacletInfo.m
return !"nb".equals(file.getExtension()) && !IGNORED_FILES.contains(fileName);
}
return false;
};
}
public static List<String> getSymbolNames(Project project) {
return FileBasedIndex.getInstance().getAllKeys(INDEX_ID, project).stream().map(PackageExportSymbol::getSymbol)
.collect(Collectors.toList());
}
@Override
public boolean indexDirectories() {
return false;
}
@Override
public boolean dependsOnFileContent() {
return true;
}
@NotNull
@Override
public ID<PackageExportSymbol, Void> getName() {
return INDEX_ID;
}
@NotNull
@Override
public DataIndexer<PackageExportSymbol, Void, FileContent> getIndexer() {
return inputData -> {
final PsiFile psiFile = inputData.getPsiFile();
DefaultPackageClassifier visitor = new DefaultPackageClassifier(psiFile);
Collection<PackageExportSymbol> listOfExportSymbols = Collections.emptyList();
if (visitor.isApplicable()) {
listOfExportSymbols = visitor.indexFile();
}
final Map<PackageExportSymbol, Void> map = new HashMap<>();
for (PackageExportSymbol symbol : listOfExportSymbols) {
map.putIfAbsent(symbol, null);
}
return map;
};
}
@NotNull
@Override
public KeyDescriptor<PackageExportSymbol> getKeyDescriptor() {
return PackageExportSymbol.INSTANCE;
}
@Override
public int getVersion() {
return BASE_VERSION;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment