|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Patrick Scheibe |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | + * of this software and associated documentation files (the "Software"), to deal |
| 5 | + * in the Software without restriction, including without limitation the rights |
| 6 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | + * copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in |
| 11 | + * all copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | + * THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package de.halirutan.mathematica.index; |
| 23 | + |
| 24 | +import com.intellij.openapi.vfs.VirtualFile; |
| 25 | +import com.intellij.psi.PsiFile; |
| 26 | +import com.intellij.util.containers.HashMap; |
| 27 | +import com.intellij.util.indexing.*; |
| 28 | +import com.intellij.util.indexing.FileBasedIndex.InputFilter; |
| 29 | +import com.intellij.util.io.DataExternalizer; |
| 30 | +import com.intellij.util.io.IOUtil; |
| 31 | +import com.intellij.util.io.KeyDescriptor; |
| 32 | +import de.halirutan.mathematica.index.MathematicaPackageExportIndex.Key; |
| 33 | +import org.jetbrains.annotations.NotNull; |
| 34 | + |
| 35 | +import java.io.DataInput; |
| 36 | +import java.io.DataOutput; |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author patrick (01.11.16). |
| 44 | + */ |
| 45 | +public class MathematicaPackageExportIndex extends FileBasedIndexExtension<Key, List<PackageExportInfo>> { |
| 46 | + |
| 47 | + public static final ID<Key,List<PackageExportInfo>> INDEX_ID = ID.create("Mathematica.fileExports"); |
| 48 | + private static final int BASE_VERSION = 1; |
| 49 | + |
| 50 | + @NotNull |
| 51 | + @Override |
| 52 | + public InputFilter getInputFilter() { |
| 53 | + return new InputFilter() { |
| 54 | + @Override |
| 55 | + public boolean acceptInput(@NotNull VirtualFile file) { |
| 56 | + return "m".equals(file.getExtension()); |
| 57 | + } |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean dependsOnFileContent() { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + @NotNull |
| 67 | + @Override |
| 68 | + public ID<Key, List<PackageExportInfo>> getName() { |
| 69 | + return INDEX_ID; |
| 70 | + } |
| 71 | + |
| 72 | + @NotNull |
| 73 | + @Override |
| 74 | + public DataIndexer<Key, List<PackageExportInfo>, FileContent> getIndexer() { |
| 75 | + return new DataIndexer<Key, List<PackageExportInfo>, FileContent>() { |
| 76 | + @NotNull |
| 77 | + @Override |
| 78 | + public Map<Key, List<PackageExportInfo>> map(@NotNull FileContent inputData) { |
| 79 | + final Map<Key, List<PackageExportInfo>> map = new HashMap<Key, List<PackageExportInfo>>(); |
| 80 | + if (!"m".equals(inputData.getFile().getExtension())) return map; |
| 81 | + final PsiFile psiFile = inputData.getPsiFile(); |
| 82 | + ExportSymbolVisitor visitor = new ExportSymbolVisitor(); |
| 83 | + psiFile.accept(visitor); |
| 84 | + map.put(new FileKey(inputData.getFile()), visitor.getInfos()); |
| 85 | + return map; |
| 86 | + } |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + @NotNull |
| 91 | + @Override |
| 92 | + public KeyDescriptor<Key> getKeyDescriptor() { |
| 93 | + //noinspection AnonymousInnerClassWithTooManyMethods,OverlyComplexAnonymousInnerClass |
| 94 | + return new KeyDescriptor<Key>() { |
| 95 | + @Override |
| 96 | + public int getHashCode(Key value) { |
| 97 | + return value.hashCode(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean isEqual(Key val1, Key val2) { |
| 102 | + return val1.equals(val2); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void save(@NotNull DataOutput out, Key value) throws IOException { |
| 107 | + value.writeValue(out); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Key read(@NotNull DataInput in) throws IOException { |
| 112 | + return new FileKey(in.readInt()); |
| 113 | + } |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + @NotNull |
| 118 | + @Override |
| 119 | + public DataExternalizer<List<PackageExportInfo>> getValueExternalizer() { |
| 120 | + return new DataExternalizer<List<PackageExportInfo>>() { |
| 121 | + @Override |
| 122 | + public void save(@NotNull DataOutput out, List<PackageExportInfo> value) throws IOException { |
| 123 | + out.writeInt(value.size()); |
| 124 | + for (PackageExportInfo info : value) { |
| 125 | + IOUtil.writeUTF(out, info.nameSpace); |
| 126 | + IOUtil.writeUTF(out, info.symbol); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public List<PackageExportInfo> read(@NotNull DataInput in) throws IOException { |
| 132 | + int size = in.readInt(); |
| 133 | + ArrayList<PackageExportInfo> infos = new ArrayList<PackageExportInfo>(size); |
| 134 | + for (int i = 0; i < size; i++) { |
| 135 | + infos.add(new PackageExportInfo(IOUtil.readUTF(in), IOUtil.readUTF(in))); |
| 136 | + } |
| 137 | + return infos; |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public int getVersion() { |
| 144 | + return BASE_VERSION; |
| 145 | + } |
| 146 | + |
| 147 | + interface Key { |
| 148 | + void writeValue(DataOutput out) throws IOException; |
| 149 | + } |
| 150 | + |
| 151 | + private static class FileKey implements Key { |
| 152 | + private final int myFileId; |
| 153 | + |
| 154 | + private FileKey(int fileId) { |
| 155 | + myFileId = fileId; |
| 156 | + } |
| 157 | + |
| 158 | + private FileKey(VirtualFile file) { |
| 159 | + myFileId = FileBasedIndex.getFileId(file); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void writeValue(DataOutput out) throws IOException { |
| 164 | + out.writeInt(myFileId); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public int hashCode() { |
| 169 | + return myFileId; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public boolean equals(Object obj) { |
| 174 | + return obj instanceof FileKey && ((FileKey)obj).myFileId == myFileId; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments