Skip to content

Commit 4dfa283

Browse files
committed
replace CachedValuesManager#createCachedValue usage with direct fetch and build result API #getCachedValue
1 parent 209549a commit 4dfa283

File tree

19 files changed

+203
-345
lines changed

19 files changed

+203
-345
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/action/SymfonySymbolSearchAction.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ public void processNames(@NotNull Processor<String> processor, @NotNull GlobalSe
166166
}
167167

168168
// Twig Extensions
169-
TwigExtensionParser twigExtensionParser = new TwigExtensionParser(project);
170-
for (Map<String, TwigExtension> extensionMap : Arrays.asList(twigExtensionParser.getFilters(), twigExtensionParser.getFunctions())) {
169+
for (Map<String, TwigExtension> extensionMap : Arrays.asList(TwigExtensionParser.getFilters(project), TwigExtensionParser.getFunctions(project))) {
171170
for(String twigFilter: extensionMap.keySet()) {
172171
processor.process(twigFilter);
173172
}
@@ -248,8 +247,7 @@ public void processElementsWithName(@NotNull String name, @NotNull Processor<Nav
248247
}
249248

250249
// Twig Extensions
251-
TwigExtensionParser twigExtensionParser = new TwigExtensionParser(project);
252-
for (Map<String, TwigExtension> extensionMap : Arrays.asList(twigExtensionParser.getFilters(), twigExtensionParser.getFunctions())) {
250+
for (Map<String, TwigExtension> extensionMap : Arrays.asList(TwigExtensionParser.getFilters(project), TwigExtensionParser.getFunctions(project))) {
253251
for(Map.Entry<String, TwigExtension> twigFunc: extensionMap.entrySet()) {
254252
if(twigFunc.getKey().equals(name)) {
255253
TwigExtension twigExtension = twigFunc.getValue();
@@ -275,7 +273,7 @@ public NavigationItem[] getItemsByName(String name, String pattern, Project proj
275273
}
276274
}
277275

278-
class MyGotoCallback extends GotoActionBase.GotoActionCallback<FileType> {
276+
static class MyGotoCallback extends GotoActionBase.GotoActionCallback<FileType> {
279277
@Override
280278
public void elementChosen(ChooseByNamePopup popup, Object element) {
281279
if(element instanceof NavigationItem) {

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/EventDispatcherSubscriberUtil.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,12 @@ public class EventDispatcherSubscriberUtil {
5050

5151
@NotNull
5252
public static Collection<EventDispatcherSubscribedEvent> getSubscribedEvents(final @NotNull Project project) {
53-
54-
CachedValue<Collection<EventDispatcherSubscribedEvent>> cache = project.getUserData(EVENT_SUBSCRIBERS);
55-
if (cache == null) {
56-
cache = CachedValuesManager.getManager(project).createCachedValue(() ->
57-
CachedValueProvider.Result.create(getSubscribedEventsProxy(project), PsiModificationTracker.MODIFICATION_COUNT), false
58-
);
59-
project.putUserData(EVENT_SUBSCRIBERS, cache);
60-
}
61-
62-
return cache.getValue();
53+
return CachedValuesManager.getManager(project).getCachedValue(
54+
project,
55+
EVENT_SUBSCRIBERS,
56+
() -> CachedValueProvider.Result.create(getSubscribedEventsProxy(project), PsiModificationTracker.MODIFICATION_COUNT),
57+
false
58+
);
6359
}
6460

6561
@NotNull

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/utils/ConfigUtil.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,12 @@ public class ConfigUtil {
3030
*/
3131
@NotNull
3232
public static Map<String, Collection<String>> getTreeSignatures(@NotNull Project project) {
33-
CachedValue<Map<String, Collection<String>>> cache = project.getUserData(TREE_SIGNATURE_CACHE);
34-
35-
if (cache == null) {
36-
cache = CachedValuesManager.getManager(project).createCachedValue(() ->
37-
CachedValueProvider.Result.create(visitTreeSignatures(project), PsiModificationTracker.MODIFICATION_COUNT)
38-
, false);
39-
40-
project.putUserData(TREE_SIGNATURE_CACHE, cache);
41-
}
42-
43-
return cache.getValue();
33+
return CachedValuesManager.getManager(project).getCachedValue(
34+
project,
35+
TREE_SIGNATURE_CACHE,
36+
() -> CachedValueProvider.Result.create(visitTreeSignatures(project), PsiModificationTracker.MODIFICATION_COUNT),
37+
false
38+
);
4439
}
4540

4641
@NotNull

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/container/util/DotEnvUtil.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,20 @@ public static Collection<PsiElement> getEnvironmentVariableTargetsForParameter(@
6262

6363
@NotNull
6464
public static Collection<String> getEnvironmentVariables(@NotNull Project project) {
65-
CachedValue<Set<String>> cache = project.getUserData(DOT_ENV_VARIABLE_CACHE);
66-
if (cache == null) {
67-
cache = CachedValuesManager.getManager(project).createCachedValue(() -> {
65+
return CachedValuesManager.getManager(project).getCachedValue(
66+
project,
67+
DOT_ENV_VARIABLE_CACHE,
68+
() -> {
6869
Set<String> items = new HashSet<>();
6970

7071
DotEnvUtil.visitEnvironment(project, pair ->
7172
items.add(pair.getFirst())
7273
);
7374

7475
return CachedValueProvider.Result.create(items, PsiModificationTracker.MODIFICATION_COUNT);
75-
}, false
76-
);
77-
project.putUserData(DOT_ENV_VARIABLE_CACHE, cache);
78-
}
79-
80-
return cache.getValue();
76+
},
77+
false
78+
);
8179
}
8280

8381
@NotNull

src/main/java/fr/adrienbrault/idea/symfony2plugin/doctrine/metadata/util/DoctrineMetadataUtil.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public static Collection<VirtualFile> findMetadataForRepositoryClass(@NotNull Ph
111111

112112
@NotNull
113113
public static Collection<VirtualFile> findMetadataForRepositoryClass(final @NotNull Project project, @NotNull String repositoryClass) {
114-
115-
CachedValue<Map<String, Collection<String>>> cache = project.getUserData(DOCTRINE_REPOSITORY_CACHE);
116-
if(cache == null) {
117-
cache = CachedValuesManager.getManager(project).createCachedValue(() -> {
114+
Map<String, Collection<String>> cache = CachedValuesManager.getManager(project).getCachedValue(
115+
project,
116+
DOCTRINE_REPOSITORY_CACHE,
117+
() -> {
118118
Map<String, Collection<String>> repositoryMap = new HashMap<>();
119119
for (String key : FileIndexCaches.getIndexKeysCache(project, CLASS_KEYS, DoctrineMetadataFileStubIndex.KEY)) {
120120
for (DoctrineModelInterface repositoryDefinition : FileBasedIndex.getInstance().getValues(DoctrineMetadataFileStubIndex.KEY, key, GlobalSearchScope.allScope(project))) {
@@ -135,19 +135,18 @@ public static Collection<VirtualFile> findMetadataForRepositoryClass(final @NotN
135135
}
136136

137137
return CachedValueProvider.Result.create(repositoryMap, PsiModificationTracker.MODIFICATION_COUNT);
138-
}, false);
139-
140-
project.putUserData(DOCTRINE_REPOSITORY_CACHE, cache);
141-
}
138+
},
139+
false
140+
);
142141

143142
repositoryClass = StringUtils.stripStart(repositoryClass,"\\");
144-
if(!cache.getValue().containsKey(repositoryClass)) {
143+
if(!cache.containsKey(repositoryClass)) {
145144
return Collections.emptyList();
146145
}
147146

148147
Set<VirtualFile> virtualFiles = new HashSet<>();
149148

150-
for (String s : cache.getValue().get(repositoryClass)) {
149+
for (String s : cache.get(repositoryClass)) {
151150
virtualFiles.addAll(
152151
FileBasedIndex.getInstance().getContainingFiles(DoctrineMetadataFileStubIndex.KEY, s, GlobalSearchScope.allScope(project))
153152
);

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,17 +1045,12 @@ public static List<PsiElement> getRouteDefinitionTargets(Project project, String
10451045

10461046
@NotNull
10471047
synchronized public static Map<String, Route> getAllRoutes(final @NotNull Project project) {
1048-
CachedValue<Map<String, Route>> cache = project.getUserData(ROUTE_CACHE);
1049-
1050-
if (cache == null) {
1051-
cache = CachedValuesManager.getManager(project).createCachedValue(() ->
1052-
CachedValueProvider.Result.create(getAllRoutesProxy(project), PsiModificationTracker.MODIFICATION_COUNT),
1053-
false
1054-
);
1055-
project.putUserData(ROUTE_CACHE, cache);
1056-
}
1057-
1058-
return cache.getValue();
1048+
return CachedValuesManager.getManager(project).getCachedValue(
1049+
project,
1050+
ROUTE_CACHE,
1051+
() -> CachedValueProvider.Result.create(getAllRoutesProxy(project), PsiModificationTracker.MODIFICATION_COUNT),
1052+
false
1053+
);
10591054
}
10601055

10611056
@NotNull

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,12 @@ public static GlobalSearchScope getRestrictedFileTypesScope(@NotNull Project pro
167167

168168
@NotNull
169169
public static Map<String, Collection<ContainerService>> getDecoratedServices(@NotNull Project project) {
170-
CachedValue<Map<String, Collection<ContainerService>>> cache = project.getUserData(SERVICE_DECORATION_CACHE);
171-
172-
if (cache == null) {
173-
cache = CachedValuesManager.getManager(project).createCachedValue(() ->
174-
CachedValueProvider.Result.create(getDecoratedServicesInner(project), PsiModificationTracker.MODIFICATION_COUNT)
175-
, false);
176-
177-
project.putUserData(SERVICE_DECORATION_CACHE, cache);
178-
}
179-
180-
return cache.getValue();
170+
return CachedValuesManager.getManager(project).getCachedValue(
171+
project,
172+
SERVICE_DECORATION_CACHE,
173+
() -> CachedValueProvider.Result.create(getDecoratedServicesInner(project), PsiModificationTracker.MODIFICATION_COUNT),
174+
false
175+
);
181176
}
182177

183178
@NotNull
@@ -209,17 +204,12 @@ private static Map<String, Collection<ContainerService>> getDecoratedServicesInn
209204
*/
210205
@NotNull
211206
public static Map<String, Collection<ContainerService>> getParentServices(@NotNull Project project) {
212-
CachedValue<Map<String, Collection<ContainerService>>> cache = project.getUserData(SERVICE_PARENT);
213-
214-
if (cache == null) {
215-
cache = CachedValuesManager.getManager(project).createCachedValue(() ->
216-
CachedValueProvider.Result.create(getParentServicesInner(project), PsiModificationTracker.MODIFICATION_COUNT)
217-
, false);
218-
219-
project.putUserData(SERVICE_PARENT, cache);
220-
}
221-
222-
return cache.getValue();
207+
return CachedValuesManager.getManager(project).getCachedValue(
208+
project,
209+
SERVICE_PARENT,
210+
() -> CachedValueProvider.Result.create(getParentServicesInner(project), PsiModificationTracker.MODIFICATION_COUNT),
211+
false
212+
);
223213
}
224214

225215
/**

src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/cache/FileIndexCaches.java

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,37 @@ public class FileIndexCaches {
3030
* @param dataHolderNames Cache extracted name Set
3131
*/
3232
static public synchronized <T> Map<String, List<T>> getSetDataCache(@NotNull final Project project, @NotNull Key<CachedValue<Map<String, List<T>>>> dataHolderKey, final @NotNull Key<CachedValue<Set<String>>> dataHolderNames, @NotNull final ID<String, T> ID, @NotNull final GlobalSearchScope scope) {
33-
34-
CachedValue<Map<String, List<T>>> cache = project.getUserData(dataHolderKey);
35-
36-
if(cache == null) {
37-
cache = CachedValuesManager.getManager(project).createCachedValue(() -> {
33+
return CachedValuesManager.getManager(project).getCachedValue(
34+
project,
35+
dataHolderKey,
36+
() -> {
3837
Map<String, List<T>> items = new HashMap<>();
3938

4039
final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
4140

42-
getIndexKeysCache(project, dataHolderNames, ID).stream().forEach(service ->
41+
getIndexKeysCache(project, dataHolderNames, ID).forEach(service ->
4342
items.put(service, fileBasedIndex.getValues(ID, service, scope))
4443
);
4544

4645
return CachedValueProvider.Result.create(items, PsiModificationTracker.MODIFICATION_COUNT);
47-
}, false);
48-
49-
project.putUserData(dataHolderKey, cache);
50-
}
51-
52-
return cache.getValue();
46+
},
47+
false
48+
);
5349
}
5450

5551
/**
5652
* @param dataHolderKey Main data to cache
5753
* @param dataHolderNames Cache extracted name Set
5854
*/
5955
static public synchronized Map<String, List<String>> getStringDataCache(@NotNull final Project project, @NotNull Key<CachedValue<Map<String, List<String>>>> dataHolderKey, final @NotNull Key<CachedValue<Set<String>>> dataHolderNames, @NotNull final ID<String, String> ID, @NotNull final GlobalSearchScope scope) {
60-
61-
CachedValue<Map<String, List<String>>> cache = project.getUserData(dataHolderKey);
62-
if(cache == null) {
63-
cache = CachedValuesManager.getManager(project).createCachedValue(() -> {
64-
56+
return CachedValuesManager.getManager(project).getCachedValue(
57+
project,
58+
dataHolderKey,
59+
() -> {
6560
Map<String, List<String>> strings = new HashMap<>();
6661

6762
final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
68-
getIndexKeysCache(project, dataHolderNames, ID).stream().forEach(parameterName -> {
63+
getIndexKeysCache(project, dataHolderNames, ID).forEach(parameterName -> {
6964
// just for secure
7065
if(parameterName == null) {
7166
return;
@@ -75,30 +70,21 @@ static public synchronized Map<String, List<String>> getStringDataCache(@NotNull
7570
});
7671

7772
return CachedValueProvider.Result.create(strings, PsiModificationTracker.MODIFICATION_COUNT);
78-
}, false);
79-
80-
project.putUserData(dataHolderKey, cache);
81-
}
82-
83-
return cache.getValue();
73+
},
74+
false
75+
);
8476
}
8577

8678
/**
8779
* There several methods that just need to check for names, as they also needed for value extraction, so cache them also
8880
*/
8981
static public synchronized Set<String> getIndexKeysCache(@NotNull final Project project, @NotNull Key<CachedValue<Set<String>>> dataHolderKey, @NotNull final ID<String, ?> ID) {
90-
91-
CachedValue<Set<String>> cache = project.getUserData(dataHolderKey);
92-
93-
if(cache == null) {
94-
cache = CachedValuesManager.getManager(project).createCachedValue(() ->
95-
CachedValueProvider.Result.create(SymfonyProcessors.createResult(project, ID), PsiModificationTracker.MODIFICATION_COUNT), false
96-
);
97-
98-
project.putUserData(dataHolderKey, cache);
99-
}
100-
101-
return cache.getValue();
82+
return CachedValuesManager.getManager(project).getCachedValue(
83+
project,
84+
dataHolderKey,
85+
() -> CachedValueProvider.Result.create(SymfonyProcessors.createResult(project, ID), PsiModificationTracker.MODIFICATION_COUNT),
86+
false
87+
);
10288
}
10389

10490
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/FilterGotoCompletionRegistrar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private static class MyFilterTagGotoCompletionProvider extends GotoCompletionPro
4646
public Collection<LookupElement> getLookupElements() {
4747
Collection<LookupElement> lookupElements = new ArrayList<>();
4848

49-
for (Map.Entry<String, TwigExtension> extension : new TwigExtensionParser(getProject()).getFilters().entrySet()) {
49+
for (Map.Entry<String, TwigExtension> extension : TwigExtensionParser.getFilters(getProject()).entrySet()) {
5050
lookupElements.add(new TwigExtensionLookupElement(getProject(), extension.getKey(), extension.getValue()));
5151
}
5252

@@ -63,7 +63,7 @@ public Collection<PsiElement> getPsiTargets(PsiElement element) {
6363

6464
Collection<PsiElement> targets = new ArrayList<>();
6565

66-
for (Map.Entry<String, TwigExtension> extension : new TwigExtensionParser(getProject()).getFilters().entrySet()) {
66+
for (Map.Entry<String, TwigExtension> extension : TwigExtensionParser.getFilters(getProject()).entrySet()) {
6767
if(!text.equals(extension.getKey())) {
6868
continue;
6969
}

0 commit comments

Comments
 (0)