Skip to content

Commit 4ae594c

Browse files
committed
Move ResourcePath indexing away from DirectoryScanner and towards indexes
Fixes: #82
1 parent 018c5dc commit 4ae594c

File tree

6 files changed

+94
-117
lines changed

6 files changed

+94
-117
lines changed

src/main/java/com/cedricziel/idea/typo3/annotation/PathResourceAnnotator.java

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package com.cedricziel.idea.typo3.annotation;
22

3+
import com.cedricziel.idea.typo3.index.ResourcePathIndex;
34
import com.intellij.lang.annotation.AnnotationHolder;
45
import com.intellij.lang.annotation.Annotator;
5-
import com.intellij.openapi.vfs.VirtualFile;
66
import com.intellij.psi.PsiElement;
7-
import com.intellij.psi.PsiFile;
8-
import com.intellij.psi.search.FilenameIndex;
9-
import com.intellij.psi.search.GlobalSearchScope;
107
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
11-
import org.codehaus.plexus.util.DirectoryScanner;
128
import org.jetbrains.annotations.NotNull;
139

14-
import java.util.Arrays;
15-
import java.util.List;
16-
1710
/**
1811
* Matches {@link StringLiteralExpression} elements and annotates them if a resource does not exist.
1912
* <p>
@@ -38,42 +31,15 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
3831
return;
3932
}
4033

41-
String[] split = resourceName.split("/");
42-
String fileName = split[split.length - 1];
43-
DirectoryScanner scanner = new DirectoryScanner();
44-
45-
scanner.setBasedir(element.getProject().getBasePath());
46-
scanner.addDefaultExcludes();
47-
scanner.setIncludes(new String[]{"**/" + resourceName + "/"});
48-
scanner.scan();
49-
50-
String[] paths = scanner.getIncludedFiles();
51-
if (paths.length != 0) {
52-
// the resource is a directory
34+
if (ResourcePathIndex.projectContainsResourceFile(element.getProject(), content)) {
35+
// exact match found
5336
return;
5437
}
5538

56-
List<PsiFile> filesByName = Arrays.asList(FilenameIndex.getFilesByName(element.getProject(), fileName, GlobalSearchScope.allScope(element.getProject())));
57-
if (filesByName.size() == 0) {
58-
createErrorMessage(element, holder, resourceName);
59-
39+
if (ResourcePathIndex.projectContainsResourceDirectory(element.getProject(), content)) {
6040
return;
6141
}
6242

63-
for (PsiFile file : filesByName) {
64-
VirtualFile virtualFile = file.getVirtualFile();
65-
66-
67-
if (virtualFile.getPath().contains("typo3conf/ext/" + resourceName) || virtualFile.getPath().contains("sysext/" + resourceName)) {
68-
// all good
69-
return;
70-
}
71-
72-
if (virtualFile.isDirectory() && virtualFile.getPath().endsWith(resourceName)) {
73-
return;
74-
}
75-
}
76-
7743
createErrorMessage(element, holder, resourceName);
7844
}
7945

src/main/java/com/cedricziel/idea/typo3/codeInsight/PathResourceCompletionContributor.java

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.cedricziel.idea.typo3.codeInsight;
22

3+
import com.cedricziel.idea.typo3.index.ResourcePathIndex;
34
import com.intellij.codeInsight.completion.*;
4-
import com.intellij.codeInsight.lookup.LookupElement;
5-
import com.intellij.codeInsight.lookup.LookupElementPresentation;
6-
import com.intellij.icons.AllIcons;
5+
import com.intellij.codeInsight.lookup.LookupElementBuilder;
76
import com.intellij.openapi.project.Project;
87
import com.intellij.patterns.PlatformPatterns;
98
import com.intellij.psi.PsiElement;
109
import com.intellij.util.ProcessingContext;
1110
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
12-
import org.codehaus.plexus.util.DirectoryScanner;
1311
import org.jetbrains.annotations.NotNull;
1412

15-
import java.util.Arrays;
16-
1713
public class PathResourceCompletionContributor extends CompletionContributor {
1814
public PathResourceCompletionContributor() {
1915
extend(
@@ -31,79 +27,8 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
3127
return;
3228
}
3329

34-
String[] includes;
35-
if (currentText.contains("/")) {
36-
String current = currentText
37-
.split("/", 1)[0]
38-
.replace("EXT:", "")
39-
.replace("IntellijIdeaRulezzz", "")
40-
.replace("'", "")
41-
.trim();
42-
43-
includes = new String[]{
44-
"**/sysext/" + current + "**",
45-
"**/typo3conf/ext/" + current + "**",
46-
};
47-
} else {
48-
includes = new String[]{
49-
"**/sysext/*/",
50-
"**/typo3conf/ext/*/",
51-
};
52-
}
53-
54-
DirectoryScanner scanner = new DirectoryScanner();
55-
56-
scanner.setBasedir(project.getBasePath());
57-
scanner.addDefaultExcludes();
58-
scanner.setIncludes(includes);
59-
scanner.scan();
60-
61-
Arrays.stream(scanner.getIncludedFiles()).forEach(f -> {
62-
result.addElement(new LookupElement() {
63-
@NotNull
64-
@Override
65-
public String getLookupString() {
66-
String[] splitted = f.split("sysext/");
67-
if (splitted.length > 1) {
68-
return "EXT:" + splitted[1];
69-
}
70-
71-
splitted = f.split("typo3conf/ext/");
72-
if (splitted.length > 1) {
73-
return "EXT:" + splitted[1];
74-
}
75-
76-
return "EXT:" + f;
77-
}
78-
});
79-
});
80-
81-
Arrays.stream(scanner.getIncludedDirectories()).forEach(f -> {
82-
result.addElement(new LookupElement() {
83-
84-
@Override
85-
public void renderElement(LookupElementPresentation presentation) {
86-
presentation.setIcon(AllIcons.Modules.SourceFolder);
87-
88-
super.renderElement(presentation);
89-
}
90-
91-
@NotNull
92-
@Override
93-
public String getLookupString() {
94-
String[] splitted = f.split("sysext/");
95-
if (splitted.length > 1) {
96-
return "EXT:" + splitted[1];
97-
}
98-
99-
splitted = f.split("typo3conf/ext/");
100-
if (splitted.length > 1) {
101-
return "EXT:" + splitted[1];
102-
}
103-
104-
return "EXT:" + f;
105-
}
106-
});
30+
ResourcePathIndex.getAvailableExtensionResourceFiles(project).forEach(identifier -> {
31+
result.addElement(LookupElementBuilder.create(identifier));
10732
});
10833
}
10934
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.cedricziel.idea.typo3.index;
2+
3+
import com.intellij.openapi.project.Project;
4+
import com.intellij.util.indexing.*;
5+
import com.intellij.util.io.EnumeratorStringDescriptor;
6+
import com.intellij.util.io.KeyDescriptor;
7+
import gnu.trove.THashMap;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import java.util.Collection;
11+
import java.util.Map;
12+
13+
public class ResourcePathIndex extends ScalarIndexExtension<String> {
14+
15+
public static final ID<String, Void> KEY = ID.create("com.cedricziel.idea.typo3.index.resource_path");
16+
17+
@NotNull
18+
@Override
19+
public ID<String, Void> getName() {
20+
return KEY;
21+
}
22+
23+
@NotNull
24+
@Override
25+
public DataIndexer<String, Void, FileContent> getIndexer() {
26+
return inputData -> {
27+
Map<String, Void> map = new THashMap<>();
28+
29+
map.put(compileId(inputData), null);
30+
31+
return map;
32+
};
33+
}
34+
35+
private String compileId(FileContent inputData) {
36+
String path = inputData.getFile().getPath();
37+
String filePosition = "";
38+
if (path.contains("typo3conf/ext/")) {
39+
filePosition = path.split("typo3conf/ext/")[1];
40+
}
41+
if (path.contains("sysext/")) {
42+
filePosition = path.split("sysext/")[1];
43+
}
44+
45+
return "EXT:" + filePosition;
46+
}
47+
48+
@NotNull
49+
@Override
50+
public KeyDescriptor<String> getKeyDescriptor() {
51+
return EnumeratorStringDescriptor.INSTANCE;
52+
}
53+
54+
@Override
55+
public int getVersion() {
56+
return 0;
57+
}
58+
59+
@NotNull
60+
@Override
61+
public FileBasedIndex.InputFilter getInputFilter() {
62+
return file -> file.isInLocalFileSystem() && (file.getPath().contains("sysext") ||file.getPath().contains("typo3conf/ext"));
63+
}
64+
65+
@Override
66+
public boolean dependsOnFileContent() {
67+
return false;
68+
}
69+
70+
public static Collection<String> getAvailableExtensionResourceFiles(@NotNull Project project) {
71+
return FileBasedIndex.getInstance().getAllKeys(ResourcePathIndex.KEY, project);
72+
}
73+
74+
public static boolean projectContainsResourceFile(@NotNull Project project, @NotNull String resourceId) {
75+
return FileBasedIndex.getInstance().getAllKeys(ResourcePathIndex.KEY, project).contains(resourceId);
76+
}
77+
78+
public static boolean projectContainsResourceDirectory(@NotNull Project project, @NotNull String resourceId) {
79+
return false;
80+
}
81+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ It is a great inspiration for possible solutions and parts of the code.</p>
268268
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.CoreServiceMapStubIndex"/>
269269
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.ExtensionNameStubIndex"/>
270270
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.IconIndex"/>
271+
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.ResourcePathIndex"/>
271272
<fileBasedIndex implementation="com.cedricziel.idea.typo3.index.TablenameFileIndex"/>
272273

273274
<!-- completion -->
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.cedricziel.idea.typo3.util;
2+
3+
public class ResourceUtilTest {
4+
}

tests/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)