Skip to content

Commit e73cff8

Browse files
authored
Merge pull request #2228 from Haehnchen/feature/index-key-project-filter
"FileBasedIndex.getInstance().getAllKeys" is a non project filtered list; provide post filter
2 parents 865ed0b + 7f6497a commit e73cff8

File tree

7 files changed

+50
-17
lines changed

7 files changed

+50
-17
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ public static String getRouteUrl(Route route) {
11861186

11871187
}
11881188

1189-
return url.length() == 0 ? null : url;
1189+
return url.isEmpty() ? null : url;
11901190
}
11911191

11921192
public static List<LookupElement> getRoutesLookupElements(final @NotNull Project project) {
@@ -1201,12 +1201,12 @@ public static List<LookupElement> getRoutesLookupElements(final @NotNull Project
12011201
uniqueSet.add(route.getName());
12021202
}
12031203

1204-
for(String routeName: SymfonyProcessors.createResult(project, RoutesStubIndex.KEY, uniqueSet)) {
1205-
if(uniqueSet.contains(routeName)) {
1204+
for(String routeName: SymfonyProcessors.createResult(project, RoutesStubIndex.KEY)) {
1205+
if (uniqueSet.contains(routeName)) {
12061206
continue;
12071207
}
12081208

1209-
for(StubIndexedRoute route: FileBasedIndex.getInstance().getValues(RoutesStubIndex.KEY, routeName, GlobalSearchScope.allScope(project))) {
1209+
for (StubIndexedRoute route: FileBasedIndex.getInstance().getValues(RoutesStubIndex.KEY, routeName, GlobalSearchScope.allScope(project))) {
12101210
lookupElements.add(new RouteLookupElement(new Route(route), true));
12111211
uniqueSet.add(routeName);
12121212
}
@@ -1270,7 +1270,7 @@ public static Map<String, Route> getAllRoutes(final @NotNull Project project) {
12701270
Map<String, Route> routes = new HashMap<>(RouteHelper.getCompiledRoutes(project));
12711271
Set<String> uniqueKeySet = new HashSet<>(routes.keySet());
12721272

1273-
for (String routeName: SymfonyProcessors.createResult(project, RoutesStubIndex.KEY, uniqueKeySet)) {
1273+
for (String routeName: SymfonyProcessors.createResult(project, RoutesStubIndex.KEY)) {
12741274
if (uniqueKeySet.contains(routeName)) {
12751275
continue;
12761276
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/SymfonyProcessors.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.intellij.util.Processor;
66
import com.intellij.util.indexing.FileBasedIndex;
77
import com.intellij.util.indexing.ID;
8+
import fr.adrienbrault.idea.symfony2plugin.stubs.util.IndexUtil;
89
import org.jetbrains.annotations.NotNull;
910

1011
import java.util.Collection;
@@ -16,6 +17,8 @@
1617
* @author Daniel Espendiller <daniel@espendiller.net>
1718
*/
1819
public class SymfonyProcessors {
20+
21+
@Deprecated
1922
public static class CollectProjectUniqueKeys implements Processor<String> {
2023
@NotNull
2124
final Project project;
@@ -43,12 +46,13 @@ public boolean process(String s) {
4346
public Set<String> getResult() {
4447
return stringSet.stream()
4548
.filter(
46-
s -> FileBasedIndex.getInstance().getContainingFiles(id, s, GlobalSearchScope.allScope(project)).size() > 0)
49+
s -> !FileBasedIndex.getInstance().getContainingFiles(id, s, GlobalSearchScope.allScope(project)).isEmpty())
4750
.collect(Collectors.toSet()
4851
);
4952
}
5053
}
5154

55+
@Deprecated
5256
public static class CollectProjectUniqueKeysStrong implements Processor<String> {
5357
@NotNull
5458
final Project project;
@@ -82,7 +86,7 @@ public boolean process(String s) {
8286
public Set<String> getResult() {
8387
return stringSet.stream()
8488
.filter(
85-
s -> FileBasedIndex.getInstance().getContainingFiles(id, s, GlobalSearchScope.allScope(project)).size() > 0
89+
s -> !FileBasedIndex.getInstance().getContainingFiles(id, s, GlobalSearchScope.allScope(project)).isEmpty()
8690
)
8791
.collect(Collectors.toSet()
8892
);
@@ -91,11 +95,10 @@ public Set<String> getResult() {
9195

9296
@NotNull
9397
public static Set<String> createResult(@NotNull Project project, @NotNull ID<String, ?> id) {
94-
CollectProjectUniqueKeys collector = new CollectProjectUniqueKeys(project, id);
95-
FileBasedIndex.getInstance().processAllKeys(id, collector, project);
96-
return collector.getResult();
98+
return new HashSet<>(IndexUtil.getAllKeysForProject(id, project));
9799
}
98100

101+
@Deprecated
99102
@NotNull
100103
public static Set<String> createResult(@NotNull Project project, @NotNull ID<String, ?> id, @NotNull Collection<String> strongKeys) {
101104
CollectProjectUniqueKeysStrong collector = new CollectProjectUniqueKeysStrong(project, id, strongKeys);

src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/util/IndexUtil.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package fr.adrienbrault.idea.symfony2plugin.stubs.util;
22

3+
import com.intellij.openapi.project.Project;
4+
import com.intellij.psi.search.GlobalSearchScope;
35
import com.intellij.util.indexing.FileBasedIndex;
46
import com.intellij.util.indexing.ID;
57
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.*;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import java.util.Collection;
11+
import java.util.HashSet;
12+
import java.util.Set;
613

714
/**
815
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -31,4 +38,23 @@ public static void forceReindex() {
3138
FileBasedIndex.getInstance().scheduleRebuild(id, new Throwable());
3239
}
3340
}
41+
42+
/**
43+
* "FileBasedIndex.getInstance().getAllKeys" does provide not only project keys, it needs a project filter.
44+
*
45+
* @link <a href="https://intellij-support.jetbrains.com/hc/en-us/community/posts/206766415/comments/207499699">...</a>
46+
*/
47+
public static <K> Collection<K> getAllKeysForProject(@NotNull ID<K, ?> indexId, @NotNull Project project) {
48+
Set<K> items = new HashSet<>();
49+
50+
FileBasedIndex index = FileBasedIndex.getInstance();
51+
for (K s : index.getAllKeys(indexId, project)) {
52+
boolean inScope = !index.processValues(indexId, s, null, (file, value) -> false, GlobalSearchScope.allScope(project));
53+
if (inScope) {
54+
items.add(s);
55+
}
56+
}
57+
58+
return items;
59+
}
3460
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/util/TwigUtil.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import fr.adrienbrault.idea.symfony2plugin.stubs.cache.FileIndexCaches;
4646
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.TemplateUsage;
4747
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.*;
48+
import fr.adrienbrault.idea.symfony2plugin.stubs.util.IndexUtil;
4849
import fr.adrienbrault.idea.symfony2plugin.templating.TemplateLookupElement;
4950
import fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern;
5051
import fr.adrienbrault.idea.symfony2plugin.templating.dict.*;
@@ -2927,7 +2928,7 @@ private static void visitTemplateExtendsConsumer(@NotNull PsiElement element, @N
29272928

29282929
public static List<String> getIncludeTemplateUsageAsOrderedList(@NotNull Project project) {
29292930
return CachedValuesManager.getManager(project).getCachedValue(project, SYMFONY_TEMPLATE_INCLUDE_LIST, () -> {
2930-
Set<String> allKeys = FileBasedIndex.getInstance().getAllKeys(TwigIncludeStubIndex.KEY, project)
2931+
Set<String> allKeys = IndexUtil.getAllKeysForProject(TwigIncludeStubIndex.KEY, project)
29312932
.stream()
29322933
.filter(s -> !s.toLowerCase().contains("@webprofiler") && !s.toLowerCase().contains("/profiler/") && !s.toLowerCase().contains("@twig") && !s.equalsIgnoreCase("form_div_layout.html.twig"))
29332934
.collect(Collectors.toSet());
@@ -2956,7 +2957,7 @@ public static List<String> getIncludeTemplateUsageAsOrderedList(@NotNull Project
29562957

29572958
public static List<String> getFormThemeTemplateUsageAsOrderedList(@NotNull Project project) {
29582959
return CachedValuesManager.getManager(project).getCachedValue(project, SYMFONY_TEMPLATE_FORM_THEME_LIST, () -> {
2959-
Set<String> allKeys = FileBasedIndex.getInstance().getAllKeys(TwigIncludeStubIndex.KEY, project)
2960+
Set<String> allKeys = IndexUtil.getAllKeysForProject(TwigIncludeStubIndex.KEY, project)
29602961
.stream()
29612962
.filter(s -> !s.toLowerCase().contains("@webprofiler") && !s.toLowerCase().contains("/profiler/") && !s.toLowerCase().contains("@twig"))
29622963
.collect(Collectors.toSet());
@@ -3005,7 +3006,7 @@ public static List<String> getFormThemeTemplateUsageAsOrderedList(@NotNull Proje
30053006

30063007
public static List<String> getEmbedTemplateUsageAsOrderedList(@NotNull Project project) {
30073008
return CachedValuesManager.getManager(project).getCachedValue(project, SYMFONY_TEMPLATE_EMBED_LIST, () -> {
3008-
Set<String> allKeys = FileBasedIndex.getInstance().getAllKeys(TwigIncludeStubIndex.KEY, project)
3009+
Set<String> allKeys = IndexUtil.getAllKeysForProject(TwigIncludeStubIndex.KEY, project)
30093010
.stream()
30103011
.filter(s -> !s.toLowerCase().contains("@webprofiler") && !s.toLowerCase().contains("/profiler/") && !s.toLowerCase().contains("@twig") && !s.equalsIgnoreCase("form_div_layout.html.twig"))
30113012
.collect(Collectors.toSet());
@@ -3034,7 +3035,7 @@ public static List<String> getEmbedTemplateUsageAsOrderedList(@NotNull Project p
30343035

30353036
public static List<String> getExtendsTemplateUsageAsOrderedList(@NotNull Project project) {
30363037
return CachedValuesManager.getManager(project).getCachedValue(project, SYMFONY_TEMPLATE_EXTENDS_LIST, () -> {
3037-
Set<String> allKeys = FileBasedIndex.getInstance().getAllKeys(TwigExtendsStubIndex.KEY, project)
3038+
Set<String> allKeys = IndexUtil.getAllKeysForProject(TwigExtendsStubIndex.KEY, project)
30383039
.stream()
30393040
.filter(s -> !s.toLowerCase().contains("@webprofiler") && !s.toLowerCase().contains("/profiler/") && !s.toLowerCase().contains("@twig") && !s.equalsIgnoreCase("form_div_layout.html.twig"))
30403041
.collect(Collectors.toSet());

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/SerializerUtil.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.jetbrains.php.lang.psi.PhpFile;
1010
import com.jetbrains.php.lang.psi.elements.*;
1111
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.SerializerClassUsageStubIndex;
12+
import fr.adrienbrault.idea.symfony2plugin.stubs.util.IndexUtil;
1213
import kotlin.Pair;
1314
import org.apache.commons.lang.StringUtils;
1415
import org.jetbrains.annotations.NotNull;
@@ -93,6 +94,6 @@ public static Collection<PsiElement> getClassTargetForSerializer(@NotNull Projec
9394
}
9495

9596
public static boolean hasClassTargetForSerializer(@NotNull Project project, @NotNull String className) {
96-
return FileBasedIndex.getInstance().getAllKeys(SerializerClassUsageStubIndex.KEY, project).contains(className.toLowerCase());
97+
return IndexUtil.getAllKeysForProject(SerializerClassUsageStubIndex.KEY, project).contains(className.toLowerCase());
9798
}
9899
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/UxUtil.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import fr.adrienbrault.idea.symfony2plugin.stubs.cache.FileIndexCaches;
1717
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.UxComponent;
1818
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.UxTemplateStubIndex;
19+
import fr.adrienbrault.idea.symfony2plugin.stubs.util.IndexUtil;
1920
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigTypeResolveUtil;
2021
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
2122
import kotlin.Pair;
@@ -119,7 +120,7 @@ private static Collection<UxComponent> getAllUxComponents(@NotNull Project proje
119120
() -> {
120121
Collection<UxComponent> uxComponents = new ArrayList<>();
121122

122-
for (String key : FileBasedIndex.getInstance().getAllKeys(UxTemplateStubIndex.KEY, project)) {
123+
for (String key : IndexUtil.getAllKeysForProject(UxTemplateStubIndex.KEY, project)) {
123124
uxComponents.addAll(FileBasedIndex.getInstance().getValues(UxTemplateStubIndex.KEY, key, GlobalSearchScope.allScope(project)));
124125
}
125126

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/resource/FileResourceUtil.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.FileResource;
2121
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.FileResourceContextTypeEnum;
2222
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.FileResourcesIndex;
23+
import fr.adrienbrault.idea.symfony2plugin.stubs.util.IndexUtil;
2324
import fr.adrienbrault.idea.symfony2plugin.util.FileResourceVisitorUtil;
2425
import fr.adrienbrault.idea.symfony2plugin.util.PhpIndexUtil;
2526
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
@@ -94,7 +95,7 @@ public static Collection<Pair<VirtualFile, String>> getFileResources(@NotNull Pr
9495
*/
9596
public static void visitFileResources(@NotNull Project project, @NotNull VirtualFile virtualFile, @NotNull Function<Pair<VirtualFile, String>, Boolean> consumer) {
9697
Set<VirtualFile> files = new HashSet<>();
97-
for (String resource : FileBasedIndex.getInstance().getAllKeys(FileResourcesIndex.KEY, project)) {
98+
for (String resource : IndexUtil.getAllKeysForProject(FileResourcesIndex.KEY, project)) {
9899
files.addAll(FileBasedIndex.getInstance().getContainingFiles(FileResourcesIndex.KEY, resource, GlobalSearchScope.allScope(project)));
99100
}
100101

0 commit comments

Comments
 (0)