|
1 | 1 | package fr.adrienbrault.idea.symfony2plugin.translation.provider; |
2 | 2 |
|
3 | 3 | import com.intellij.openapi.project.Project; |
| 4 | +import com.intellij.openapi.util.Key; |
4 | 5 | import com.intellij.openapi.vfs.VirtualFile; |
5 | 6 | import com.intellij.psi.PsiElement; |
6 | 7 | import com.intellij.psi.PsiFile; |
7 | 8 | import com.intellij.psi.search.GlobalSearchScope; |
| 9 | +import com.intellij.psi.util.CachedValue; |
| 10 | +import com.intellij.psi.util.CachedValueProvider; |
| 11 | +import com.intellij.psi.util.CachedValuesManager; |
8 | 12 | import com.intellij.util.indexing.FileBasedIndex; |
9 | 13 | import com.jetbrains.php.PhpIndex; |
10 | 14 | import fr.adrienbrault.idea.symfony2plugin.extension.TranslatorProvider; |
11 | 15 | import fr.adrienbrault.idea.symfony2plugin.extension.TranslatorProviderDict; |
12 | 16 | import fr.adrienbrault.idea.symfony2plugin.stubs.SymfonyProcessors; |
| 17 | +import fr.adrienbrault.idea.symfony2plugin.stubs.cache.FileIndexCaches; |
13 | 18 | import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TranslationStubIndex; |
14 | 19 | import fr.adrienbrault.idea.symfony2plugin.translation.dict.TranslationUtil; |
15 | 20 | import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; |
16 | 21 | import org.jetbrains.annotations.NotNull; |
17 | 22 |
|
18 | 23 | import java.util.*; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
19 | 25 | import java.util.stream.Collectors; |
20 | 26 |
|
21 | 27 | /** |
|
24 | 30 | * @author Daniel Espendiller <daniel@espendiller.net> |
25 | 31 | */ |
26 | 32 | public class IndexTranslatorProvider implements TranslatorProvider { |
| 33 | + private static final Key<CachedValue<Set<String>>> TRANSLATION_DOMAINS = |
| 34 | + Key.create("SYMFONY_TRANSLATION_DOMAINS"); |
| 35 | + |
| 36 | + private static final Key<CachedValue<Map<String, Set<String>>>> TRANSLATION_KEYS_BY_DOMAIN = |
| 37 | + Key.create("SYMFONY_TRANSLATION_KEYS_BY_DOMAIN"); |
| 38 | + |
27 | 39 | @Override |
28 | 40 | public boolean hasTranslationKey(@NotNull Project project, @NotNull String keyName, @NotNull String domainName) { |
29 | | - for(Set<String> keys: FileBasedIndex.getInstance().getValues(TranslationStubIndex.KEY, domainName, GlobalSearchScope.allScope(project))){ |
30 | | - if(keys.contains(keyName)) { |
31 | | - return true; |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - return false; |
| 41 | + return hasIndexedTranslationKey(project, keyName, domainName); |
36 | 42 | } |
37 | 43 |
|
38 | 44 | @Override |
39 | 45 | public boolean hasDomain(@NotNull Project project, @NotNull String domainName) { |
40 | | - return !FileBasedIndex.getInstance().getValues( |
41 | | - TranslationStubIndex.KEY, |
42 | | - domainName, |
43 | | - GlobalSearchScope.allScope(project) |
44 | | - ).isEmpty(); |
| 46 | + return hasIndexedDomain(project, domainName); |
45 | 47 | } |
46 | 48 |
|
47 | 49 | @NotNull |
48 | 50 | @Override |
49 | 51 | public Collection<TranslatorProviderDict.TranslationDomain> getTranslationDomains(@NotNull Project project) { |
50 | | - return SymfonyProcessors |
51 | | - .createResult(project, TranslationStubIndex.KEY) |
| 52 | + return getIndexedDomains(project) |
52 | 53 | .stream() |
53 | 54 | .map(s -> new TranslatorProviderDict.TranslationDomain(s, true)) |
54 | 55 | .collect(Collectors.toList()); |
@@ -88,11 +89,52 @@ public Collection<VirtualFile> getDomainPsiFiles(Project project, @NotNull Strin |
88 | 89 | @NotNull |
89 | 90 | @Override |
90 | 91 | public Collection<TranslatorProviderDict.TranslationKey> getTranslationsForDomain(@NotNull Project project, @NotNull String domainName) { |
91 | | - return FileBasedIndex.getInstance() |
92 | | - .getValues(TranslationStubIndex.KEY, domainName, GlobalSearchScope.allScope(project)) |
| 92 | + return getIndexedTranslationKeys(project, domainName) |
93 | 93 | .stream() |
94 | | - .flatMap(Collection::stream) |
95 | 94 | .map(key -> new TranslatorProviderDict.TranslationKey(key, true)) |
96 | 95 | .collect(Collectors.toSet()); |
97 | 96 | } |
| 97 | + |
| 98 | + @NotNull |
| 99 | + private static Map<String, Set<String>> getTranslationKeysByDomain(@NotNull Project project) { |
| 100 | + return CachedValuesManager.getManager(project).getCachedValue( |
| 101 | + project, |
| 102 | + TRANSLATION_KEYS_BY_DOMAIN, |
| 103 | + () -> CachedValueProvider.Result.create( |
| 104 | + new ConcurrentHashMap<>(), |
| 105 | + FileIndexCaches.getModificationTrackerForIndexId(project, TranslationStubIndex.KEY) |
| 106 | + ), |
| 107 | + false |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + public static boolean hasIndexedTranslationKey(@NotNull Project project, @NotNull String keyName, @NotNull String domainName) { |
| 112 | + return getIndexedTranslationKeys(project, domainName).contains(keyName); |
| 113 | + } |
| 114 | + |
| 115 | + public static boolean hasIndexedDomain(@NotNull Project project, @NotNull String domainName) { |
| 116 | + return getIndexedDomains(project).contains(domainName); |
| 117 | + } |
| 118 | + |
| 119 | + @NotNull |
| 120 | + private static Set<String> getIndexedDomains(@NotNull Project project) { |
| 121 | + return FileIndexCaches.getIndexKeysCache(project, TRANSLATION_DOMAINS, TranslationStubIndex.KEY); |
| 122 | + } |
| 123 | + |
| 124 | + @NotNull |
| 125 | + private static Set<String> getIndexedTranslationKeys(@NotNull Project project, @NotNull String domainName) { |
| 126 | + if (!hasIndexedDomain(project, domainName)) { |
| 127 | + return Collections.emptySet(); |
| 128 | + } |
| 129 | + |
| 130 | + return getTranslationKeysByDomain(project).computeIfAbsent(domainName, domain -> { |
| 131 | + FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance(); |
| 132 | + GlobalSearchScope scope = GlobalSearchScope.allScope(project); |
| 133 | + |
| 134 | + return fileBasedIndex.getValues(TranslationStubIndex.KEY, domain, scope) |
| 135 | + .stream() |
| 136 | + .flatMap(Collection::stream) |
| 137 | + .collect(Collectors.toSet()); |
| 138 | + }); |
| 139 | + } |
98 | 140 | } |
0 commit comments