3
3
import com .intellij .codeInsight .lookup .LookupElement ;
4
4
import com .intellij .lang .xml .XMLLanguage ;
5
5
import com .intellij .openapi .project .Project ;
6
- import com .intellij .openapi .util .Condition ;
7
6
import com .intellij .openapi .util .Key ;
8
7
import com .intellij .openapi .util .Pair ;
9
8
import com .intellij .openapi .vfs .VirtualFile ;
13
12
import com .intellij .psi .search .GlobalSearchScope ;
14
13
import com .intellij .psi .util .*;
15
14
import com .intellij .psi .xml .XmlTag ;
16
- import com .intellij .util .Processor ;
17
15
import com .intellij .util .indexing .FileBasedIndex ;
18
16
import com .jetbrains .php .PhpIndex ;
19
17
import com .jetbrains .php .lang .psi .elements .PhpClass ;
25
23
import fr .adrienbrault .idea .symfony2plugin .stubs .cache .FileIndexCaches ;
26
24
import fr .adrienbrault .idea .symfony2plugin .stubs .indexes .DoctrineMetadataFileStubIndex ;
27
25
import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
28
- import fr .adrienbrault .idea .symfony2plugin .util .PsiElementUtils ;
29
26
import org .apache .commons .lang .StringUtils ;
30
- import org .apache .xmlbeans .XmlLanguage ;
31
27
import org .jetbrains .annotations .NotNull ;
32
28
import org .jetbrains .annotations .Nullable ;
33
29
import org .jetbrains .yaml .psi .*;
41
37
*/
42
38
public class DoctrineMetadataUtil {
43
39
44
- private static final Key <CachedValue <Set <String >>> CLASS_KEYS = new Key <CachedValue < Set < String >> >("CLASS_KEYS" );
40
+ private static final Key <CachedValue <Set <String >>> CLASS_KEYS = new Key <>("CLASS_KEYS" );
45
41
46
42
private static DoctrineMappingDriverInterface [] MAPPING_DRIVERS = new DoctrineMappingDriverInterface [] {
47
43
new DoctrineXmlMappingDriver (),
@@ -51,7 +47,7 @@ public class DoctrineMetadataUtil {
51
47
52
48
@ NotNull
53
49
public static Collection <LookupElement > getObjectRepositoryLookupElements (@ NotNull Project project ) {
54
- return new ArrayList <LookupElement >(DoctrineRepositoryLookupElement .create (PhpIndex .getInstance (project ).getAllSubclasses ("\\ Doctrine\\ Common\\ Persistence\\ ObjectRepository" )));
50
+ return new ArrayList <>(DoctrineRepositoryLookupElement .create (PhpIndex .getInstance (project ).getAllSubclasses ("\\ Doctrine\\ Common\\ Persistence\\ ObjectRepository" )));
55
51
}
56
52
57
53
/**
@@ -60,90 +56,81 @@ public static Collection<LookupElement> getObjectRepositoryLookupElements(@NotNu
60
56
@ Nullable
61
57
public static PhpClass getClassRepository (final @ NotNull Project project , final @ NotNull String className ) {
62
58
63
- final PhpClass [] phpClass = {null };
64
59
for (VirtualFile virtualFile : FileBasedIndex .getInstance ().getContainingFiles (DoctrineMetadataFileStubIndex .KEY , className , GlobalSearchScope .allScope (project ))) {
65
60
66
- FileBasedIndex .getInstance ().processValues (DoctrineMetadataFileStubIndex .KEY , className , virtualFile , new FileBasedIndex .ValueProcessor <DoctrineModelInterface >() {
67
- @ Override
68
- public boolean process (VirtualFile virtualFile , DoctrineModelInterface model ) {
69
- if (model == null || model .getRepositoryClass () == null ) {
70
- return true ;
71
- }
61
+ final String [] phpClass = {null };
72
62
73
- phpClass [0 ] = PhpElementsUtil .getClassInsideNamespaceScope (project , className , model .getRepositoryClass ());
63
+ FileBasedIndex .getInstance ().processValues (DoctrineMetadataFileStubIndex .KEY , className , virtualFile , (virtualFile1 , model ) -> {
64
+ if (phpClass [0 ] != null || model == null || model .getRepositoryClass () == null ) {
74
65
return true ;
75
66
}
67
+
68
+ // piping value out of this index thread
69
+ phpClass [0 ] = model .getRepositoryClass ();
70
+
71
+ return true ;
76
72
}, GlobalSearchScope .allScope (project ));
77
73
78
74
if (phpClass [0 ] != null ) {
79
- return phpClass [0 ];
75
+ return PhpElementsUtil . getClassInsideNamespaceScope ( project , className , phpClass [0 ]) ;
80
76
}
81
77
}
82
78
83
- return phpClass [ 0 ] ;
79
+ return null ;
84
80
}
85
81
86
82
@ NotNull
87
83
public static Collection <VirtualFile > findMetadataFiles (@ NotNull Project project , @ NotNull String className ) {
88
84
89
- final Collection <VirtualFile > virtualFiles = new ArrayList <VirtualFile >();
85
+ final Collection <VirtualFile > virtualFiles = new ArrayList <>();
90
86
91
- FileBasedIndex .getInstance ().getFilesWithKey (DoctrineMetadataFileStubIndex .KEY , new HashSet <String >(Collections .singletonList (className )), new Processor <VirtualFile >() {
92
- @ Override
93
- public boolean process (VirtualFile virtualFile ) {
94
- virtualFiles .add (virtualFile );
95
- return true ;
96
- }
87
+ FileBasedIndex .getInstance ().getFilesWithKey (DoctrineMetadataFileStubIndex .KEY , new HashSet <>(Collections .singletonList (className )), virtualFile -> {
88
+ virtualFiles .add (virtualFile );
89
+ return true ;
97
90
}, GlobalSearchScope .allScope (project ));
98
91
99
92
return virtualFiles ;
100
93
}
101
94
@ NotNull
102
95
public static Collection <VirtualFile > findMetadataForRepositoryClass (@ NotNull PhpClass phpClass ) {
103
- String presentableFQN = phpClass .getPresentableFQN ();
104
- if (presentableFQN == null ) {
105
- return Collections .emptyList ();
106
- }
96
+ return findMetadataForRepositoryClass (
97
+ phpClass .getProject (),
98
+ StringUtils .stripStart (phpClass .getPresentableFQN (), "\\ " )
99
+ );
100
+ }
107
101
108
- if (presentableFQN .startsWith ("\\ " )) {
109
- presentableFQN = presentableFQN .substring (1 );
110
- }
102
+ private static final Key <CachedValue <Map <String , Collection <String >>>> DOCTRINE_REPOSITORY_CACHE ;
111
103
112
- return findMetadataForRepositoryClass (phpClass .getProject (), presentableFQN );
104
+ static {
105
+ DOCTRINE_REPOSITORY_CACHE = new Key <>("DOCTRINE_REPOSITORY_CACHE" );
113
106
}
114
107
115
- private static final Key <CachedValue <Map <String , Collection <String >>>> DOCTRINE_REPOSITORY_CACHE = new Key <CachedValue <Map <String , Collection <String >>>>("DOCTRINE_REPOSITORY_CACHE" );
116
-
117
108
@ NotNull
118
109
public static Collection <VirtualFile > findMetadataForRepositoryClass (final @ NotNull Project project , @ NotNull String repositoryClass ) {
119
110
120
111
CachedValue <Map <String , Collection <String >>> cache = project .getUserData (DOCTRINE_REPOSITORY_CACHE );
121
112
if (cache == null ) {
122
- cache = CachedValuesManager .getManager (project ).createCachedValue (new CachedValueProvider <Map <String , Collection <String >>>() {
123
- @ Nullable
124
- @ Override
125
- public Result <Map <String , Collection <String >>> compute () {
126
- Map <String , Collection <String >> repositoryMap = new HashMap <String , Collection <String >>();
127
- for (String key : FileIndexCaches .getIndexKeysCache (project , CLASS_KEYS , DoctrineMetadataFileStubIndex .KEY )) {
128
- for (DoctrineModelInterface repositoryDefinition : FileBasedIndex .getInstance ().getValues (DoctrineMetadataFileStubIndex .KEY , key , GlobalSearchScope .allScope (project ))) {
129
- if (StringUtils .isBlank (repositoryDefinition .getRepositoryClass ())) {
130
- continue ;
131
- }
132
-
133
- PhpClass phpClass = PhpElementsUtil .getClassInsideNamespaceScope (project , key , repositoryDefinition .getRepositoryClass ());
134
- if (phpClass != null && phpClass .getPresentableFQN () != null ) {
135
- String presentableFQN = phpClass .getPresentableFQN ();
136
- if (!repositoryMap .containsKey (presentableFQN )) {
137
- repositoryMap .put (presentableFQN , new HashSet <String >());
138
- }
113
+ cache = CachedValuesManager .getManager (project ).createCachedValue (() -> {
114
+ Map <String , Collection <String >> repositoryMap = new HashMap <>();
115
+ for (String key : FileIndexCaches .getIndexKeysCache (project , CLASS_KEYS , DoctrineMetadataFileStubIndex .KEY )) {
116
+ for (DoctrineModelInterface repositoryDefinition : FileBasedIndex .getInstance ().getValues (DoctrineMetadataFileStubIndex .KEY , key , GlobalSearchScope .allScope (project ))) {
117
+ if (StringUtils .isBlank (repositoryDefinition .getRepositoryClass ())) {
118
+ continue ;
119
+ }
139
120
140
- repositoryMap .get (presentableFQN ).add (key );
121
+ PhpClass phpClass = PhpElementsUtil .getClassInsideNamespaceScope (project , key , repositoryDefinition .getRepositoryClass ());
122
+ if (phpClass != null ) {
123
+ String presentableFQN = phpClass .getPresentableFQN ();
124
+ if (!repositoryMap .containsKey (presentableFQN )) {
125
+ repositoryMap .put (presentableFQN , new HashSet <>());
141
126
}
127
+
128
+ repositoryMap .get (presentableFQN ).add (key );
142
129
}
143
130
}
144
-
145
- return Result .create (repositoryMap , PsiModificationTracker .MODIFICATION_COUNT );
146
131
}
132
+
133
+ return CachedValueProvider .Result .create (repositoryMap , PsiModificationTracker .MODIFICATION_COUNT );
147
134
}, false );
148
135
149
136
project .putUserData (DOCTRINE_REPOSITORY_CACHE , cache );
@@ -190,7 +177,7 @@ public static Collection<Pair<String, PsiElement>> getTables(@NotNull Project pr
190
177
}
191
178
192
179
// @TODO: add target
193
- pair .add (new Pair <String , PsiElement >(table , psiFile ));
180
+ pair .add (new Pair <>(table , psiFile ));
194
181
}
195
182
}
196
183
}
@@ -251,7 +238,7 @@ public static DoctrineMetadataModel getModelFields(@NotNull Project project, @No
251
238
@ NotNull
252
239
public static Collection <PhpClass > getModels (@ NotNull Project project ) {
253
240
254
- Collection <PhpClass > phpClasses = new ArrayList <PhpClass >();
241
+ Collection <PhpClass > phpClasses = new ArrayList <>();
255
242
for (String key : FileIndexCaches .getIndexKeysCache (project , CLASS_KEYS , DoctrineMetadataFileStubIndex .KEY )) {
256
243
PhpClass classInterface = PhpElementsUtil .getClassInterface (project , key );
257
244
if (classInterface != null ) {
@@ -282,16 +269,13 @@ public static DoctrineManagerEnum findManagerByScope(@NotNull PsiElement psiElem
282
269
public static String findModelNameInScope (@ NotNull PsiElement psiElement ) {
283
270
284
271
if (psiElement .getLanguage ().equals (XMLLanguage .INSTANCE )) {
285
- PsiElement firstParent = PsiTreeUtil .findFirstParent (psiElement , new Condition <PsiElement >() {
286
- @ Override
287
- public boolean value (PsiElement psiElement ) {
288
- if (!(psiElement instanceof XmlTag )) {
289
- return false ;
290
- }
291
-
292
- String name = ((XmlTag ) psiElement ).getName ();
293
- return name .equals ("entity" ) || name .equals ("document" ) || name .equals ("embedded" ) || name .equals ("embedded-document" );
272
+ PsiElement firstParent = PsiTreeUtil .findFirstParent (psiElement , psiElement1 -> {
273
+ if (!(psiElement1 instanceof XmlTag )) {
274
+ return false ;
294
275
}
276
+
277
+ String name = ((XmlTag ) psiElement1 ).getName ();
278
+ return name .equals ("entity" ) || name .equals ("document" ) || name .equals ("embedded" ) || name .equals ("embedded-document" );
295
279
});
296
280
297
281
if (firstParent instanceof XmlTag ) {
@@ -324,7 +308,7 @@ public boolean value(PsiElement psiElement) {
324
308
325
309
@ NotNull
326
310
public static Collection <PhpClass > getClassInsideScope (@ NotNull PsiElement psiElement , @ NotNull String originValue ) {
327
- Collection <PhpClass > classesInterface = new ArrayList <PhpClass >();
311
+ Collection <PhpClass > classesInterface = new ArrayList <>();
328
312
String modelNameInScope = DoctrineMetadataUtil .findModelNameInScope (psiElement );
329
313
if (modelNameInScope != null ) {
330
314
PhpClass classInsideNamespaceScope = PhpElementsUtil .getClassInsideNamespaceScope (psiElement .getProject (), modelNameInScope , originValue );
0 commit comments