Skip to content

Commit e96d4ca

Browse files
authored
Merge pull request #78 from cedricziel/extension-name-index
Implement ExtensionNameStubIndex
2 parents 3e01af3 + 3a2ac82 commit e96d4ca

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

src/main/java/com/cedricziel/idea/typo3/index/ExtensionIndex.java

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.cedricziel.idea.typo3.index;
2+
3+
import com.cedricziel.idea.typo3.index.externalizer.ObjectStreamDataExternalizer;
4+
import com.intellij.openapi.vfs.VirtualFile;
5+
import com.intellij.util.indexing.*;
6+
import com.intellij.util.io.DataExternalizer;
7+
import com.intellij.util.io.EnumeratorStringDescriptor;
8+
import com.intellij.util.io.KeyDescriptor;
9+
import gnu.trove.THashMap;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
import java.util.Map;
13+
14+
public class ExtensionNameStubIndex extends FileBasedIndexExtension<String, String> {
15+
16+
private static ObjectStreamDataExternalizer<String> EXTERNALIZER = new ObjectStreamDataExternalizer<>();
17+
18+
public static final ID<String, String> KEY = ID.create("com.cedricziel.idea.typo3.index.extension_name");
19+
20+
private final KeyDescriptor<String> myKeyDescriptor = new EnumeratorStringDescriptor();
21+
22+
@NotNull
23+
@Override
24+
public ID<String, String> getName() {
25+
return KEY;
26+
}
27+
28+
@NotNull
29+
@Override
30+
public DataIndexer<String, String, FileContent> getIndexer() {
31+
return virtualFile -> {
32+
final Map<String, String> items = new THashMap<>();
33+
34+
VirtualFile file = virtualFile.getFile();
35+
VirtualFile parentDirectory = file.getParent();
36+
if (parentDirectory != null) {
37+
VirtualFile ancestorDirectory = parentDirectory.getParent();
38+
if (ancestorDirectory != null && ancestorDirectory.isDirectory()) {
39+
String ancestorDirectoryName = ancestorDirectory.getName();
40+
if (ancestorDirectoryName.equals("sysext") || ancestorDirectoryName.equals("ext")) {
41+
String path = file.getPath();
42+
items.put(parentDirectory.getName(), path);
43+
}
44+
45+
// handle the case where the extension name can *not* be inferred from the directory name
46+
}
47+
}
48+
49+
return items;
50+
};
51+
}
52+
53+
@NotNull
54+
@Override
55+
public KeyDescriptor<String> getKeyDescriptor() {
56+
return myKeyDescriptor;
57+
}
58+
59+
@NotNull
60+
@Override
61+
public DataExternalizer<String> getValueExternalizer() {
62+
return EXTERNALIZER;
63+
}
64+
65+
@Override
66+
public int getVersion() {
67+
return 0;
68+
}
69+
70+
@NotNull
71+
@Override
72+
public FileBasedIndex.InputFilter getInputFilter() {
73+
return virtualFile -> virtualFile.getName().equals("ext_emconf.php");
74+
}
75+
76+
@Override
77+
public boolean dependsOnFileContent() {
78+
return true;
79+
}
80+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.cedricziel.idea.typo3.index.externalizer;
2+
3+
import com.intellij.util.io.DataExternalizer;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.io.*;
7+
8+
/**
9+
* @author Daniel Espendiller <daniel@espendiller.net>
10+
*/
11+
public class ObjectStreamDataExternalizer<T extends Serializable> implements DataExternalizer<T> {
12+
13+
@Override
14+
public void save(@NotNull DataOutput out, T value) throws IOException {
15+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
16+
ObjectOutput output = new ObjectOutputStream(stream);
17+
18+
output.writeObject(value);
19+
20+
out.writeInt(stream.size());
21+
out.write(stream.toByteArray());
22+
}
23+
24+
@Override
25+
public T read(@NotNull DataInput in) throws IOException {
26+
int bufferSize = in.readInt();
27+
byte[] buffer = new byte[bufferSize];
28+
in.readFully(buffer, 0, bufferSize);
29+
30+
ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
31+
ObjectInput input = new ObjectInputStream(stream);
32+
33+
T object = null;
34+
try {
35+
object = (T) input.readObject();
36+
} catch (ClassNotFoundException | ClassCastException ignored) {
37+
}
38+
39+
return object;
40+
}
41+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ It is a great inspiration for possible solutions and parts of the code.</p>
266266
<extensions defaultExtensionNs="com.intellij">
267267
<!-- indexes -->
268268
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.CoreServiceMapStubIndex"/>
269+
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.ExtensionNameStubIndex"/>
269270

270271
<!-- completion -->
271272
<completion.contributor language="PHP"

0 commit comments

Comments
 (0)