1010import com .intellij .psi .PsiElement ;
1111import com .intellij .psi .util .PsiTreeUtil ;
1212import com .intellij .util .ProcessingContext ;
13- import com .jetbrains .php .PhpClassHierarchyUtils ;
1413import com .jetbrains .php .PhpIcons ;
1514import com .jetbrains .php .PhpIndex ;
1615import com .jetbrains .php .completion .PhpLookupElement ;
2221
2322import java .util .Arrays ;
2423import java .util .Collection ;
25- import java .util .Iterator ;
2624
2725public class ExtbasePersistenceCompletionContributor extends CompletionContributor {
2826 public ExtbasePersistenceCompletionContributor () {
@@ -34,10 +32,6 @@ public static class ExtbaseRepositoryMagicMethodsCompletionProvider extends Comp
3432
3533 public static final String TYPO3_CMS_EXTBASE_PERSISTENCE_REPOSITORY = "TYPO3\\ CMS\\ Extbase\\ Persistence\\ Repository" ;
3634 private static final String QUERY_RESULT_INTERFACE = "TYPO3\\ CMS\\ Extbase\\ Persistence\\ QueryResultInterface" ;
37- private static final String OBJECT_STORAGE = "TYPO3\\ CMS\\ Extbase\\ Persistence\\ ObjectStorage" ;
38-
39- private ExtbaseRepositoryMagicMethodsCompletionProvider () {
40- }
4135
4236 protected void addCompletions (@ NotNull CompletionParameters parameters , ProcessingContext context , @ NotNull CompletionResultSet result ) {
4337 PsiElement position = parameters .getPosition ().getOriginalElement ();
@@ -46,124 +40,98 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
4640 return ;
4741 }
4842
49- Iterator <PhpClass > iterator = PhpIndex .getInstance (position .getProject ()).getClassesByFQN (TYPO3_CMS_EXTBASE_PERSISTENCE_REPOSITORY ).iterator ();
50- if (!iterator .hasNext ()) {
51- return ;
52- }
53-
54- PhpClass repositoryClass = iterator .next ();
55- if (repositoryClass == null ) {
56- return ;
57- }
58-
5943 if (!(position .getParent ().getFirstChild () instanceof Variable ) && !(position .getParent ().getFirstChild () instanceof FieldReference )) {
6044 return ;
6145 }
6246
63- PhpTypedElement variable ;
64- if (position .getParent ().getFirstChild () instanceof Variable ) {
65- variable = (PhpTypedElement ) position .getParent ().getFirstChild ();
66- } else {
67- variable = (PhpTypedElement ) position .getParent ().getFirstChild ();
68- }
47+ PhpTypedElement variable = (PhpTypedElement ) position .getParent ().getFirstChild ();
6948
7049 PhpType type = variable .getType ();
7150 type .getTypes ().forEach (t -> {
7251 Collection <PhpClass > classesByFQN = PhpIndex .getInstance (position .getProject ()).getClassesByFQN (t );
73- classesByFQN .forEach (c -> {
74- if (PhpClassHierarchyUtils .isSuperClass (repositoryClass , c , true )) {
75- createLookupElementsForRepository (variable , c , result );
76- }
77- });
52+ classesByFQN
53+ .stream ()
54+ .filter (ExtbaseUtils ::isRepositoryClass )
55+ .forEach (c -> createLookupElementsForRepository (variable , c , result ));
7856 });
7957 }
8058
8159 private void createLookupElementsForRepository (@ NotNull PhpTypedElement position , @ NotNull PhpClass repositoryClass , @ NotNull CompletionResultSet result ) {
82- String repositoryClassFqn = repositoryClass .getFQN ();
83-
84- String potentialModelClass = ExtbaseUtility .convertRepositoryFQNToEntityFQN (repositoryClassFqn );
60+ String potentialModelClass = ExtbaseUtility .convertRepositoryFQNToEntityFQN (repositoryClass .getFQN ());
8561
8662 Collection <PhpClass > classesByFQN = PhpIndex .getInstance (position .getProject ()).getClassesByFQN (potentialModelClass );
8763 classesByFQN .forEach (c -> {
8864 c .getFields ().forEach (f -> {
89- if (f . isConstant () || f . isInternal () || f . getName (). equals ( "_cleanProperties" ) || f . getName (). equals ( "_isClone" )) {
65+ if (! ExtbaseUtils . fieldHasMagicFinders ( f )) {
9066 return ;
9167 }
9268
9369 // findBy* matches on single fields only and not on collections
94- if (!f .getDeclaredType ().toString ().contains (OBJECT_STORAGE )) {
95- result .addElement (new LookupElement () {
96- @ NotNull
97- @ Override
98- public String getLookupString () {
99- return "findBy" + StringUtils .capitalize (f .getName ()) + "($" + f .getName () + ")" ;
100- }
101-
102- @ Override
103- public void renderElement (LookupElementPresentation presentation ) {
104- super .renderElement (presentation );
105-
106- presentation .setItemText ("findBy" + StringUtils .capitalize (f .getName ()));
107- presentation .setTypeText ("findBy" + StringUtils .capitalize (f .getName ()));
108- presentation .setIcon (PhpIcons .METHOD_ICON );
109- presentation .setTailText ("(" + f .getName () + " : " + f .getDeclaredType () + ")" , true );
110- presentation .setTypeText (c .getName () + "[]|" + QUERY_RESULT_INTERFACE );
111- }
112- });
113- }
70+ result .addElement (new LookupElement () {
71+ @ NotNull
72+ @ Override
73+ public String getLookupString () {
74+ return "findBy" + StringUtils .capitalize (f .getName ()) + "($" + f .getName () + ")" ;
75+ }
76+
77+ @ Override
78+ public void renderElement (LookupElementPresentation presentation ) {
79+ super .renderElement (presentation );
80+
81+ presentation .setItemText ("findBy" + StringUtils .capitalize (f .getName ()));
82+ presentation .setTypeText ("findBy" + StringUtils .capitalize (f .getName ()));
83+ presentation .setIcon (PhpIcons .METHOD_ICON );
84+ presentation .setTailText ("(" + f .getName () + " : " + f .getDeclaredType () + ")" , true );
85+ presentation .setTypeText (c .getName () + "[]|" + QUERY_RESULT_INTERFACE );
86+ }
87+ });
11488
11589 // countBy* matches on single fields only and not on collections
116- if (!f .getDeclaredType ().toString ().contains (OBJECT_STORAGE )) {
117- result .addElement (new LookupElement () {
118- @ NotNull
119- @ Override
120- public String getLookupString () {
121- return "countBy" + StringUtils .capitalize (f .getName ()) + "($" + f .getName () + ")" ;
122- }
123-
124- @ Override
125- public void renderElement (LookupElementPresentation presentation ) {
126- super .renderElement (presentation );
127-
128- presentation .setItemText ("countBy" + StringUtils .capitalize (f .getName ()));
129- presentation .setTypeText ("countBy" + StringUtils .capitalize (f .getName ()));
130- presentation .setIcon (PhpIcons .METHOD_ICON );
131- presentation .setTailText ("(" + f .getName () + " : " + f .getDeclaredType () + ")" , true );
132- presentation .setTypeText ("int" );
133- }
134- });
135- }
90+ result .addElement (new LookupElement () {
91+ @ NotNull
92+ @ Override
93+ public String getLookupString () {
94+ return "countBy" + StringUtils .capitalize (f .getName ()) + "($" + f .getName () + ")" ;
95+ }
96+
97+ @ Override
98+ public void renderElement (LookupElementPresentation presentation ) {
99+ super .renderElement (presentation );
100+
101+ presentation .setItemText ("countBy" + StringUtils .capitalize (f .getName ()));
102+ presentation .setTypeText ("countBy" + StringUtils .capitalize (f .getName ()));
103+ presentation .setIcon (PhpIcons .METHOD_ICON );
104+ presentation .setTailText ("(" + f .getName () + " : " + f .getDeclaredType () + ")" , true );
105+ presentation .setTypeText ("int" );
106+ }
107+ });
136108
137109 // findOneBy* matches on single fields only and not on collections
138- if (!f .getDeclaredType ().toString ().contains (OBJECT_STORAGE )) {
139- result .addElement (new LookupElement () {
140- @ NotNull
141- @ Override
142- public String getLookupString () {
143- return "findOneBy" + StringUtils .capitalize (f .getName ()) + "($" + f .getName () + ")" ;
144- }
145-
146- @ Override
147- public void renderElement (LookupElementPresentation presentation ) {
148- super .renderElement (presentation );
149-
150- presentation .setItemText ("findOneBy" + StringUtils .capitalize (f .getName ()));
151- presentation .setTypeText ("findOneBy" + StringUtils .capitalize (f .getName ()));
152- presentation .setIcon (PhpIcons .METHOD_ICON );
153- presentation .setTailText ("(" + f .getName () + " : " + f .getDeclaredType () + ")" , true );
154- presentation .setTypeText ("null|" + c .getName ());
155- }
156- });
157- }
110+ result .addElement (new LookupElement () {
111+ @ NotNull
112+ @ Override
113+ public String getLookupString () {
114+ return "findOneBy" + StringUtils .capitalize (f .getName ()) + "($" + f .getName () + ")" ;
115+ }
116+
117+ @ Override
118+ public void renderElement (LookupElementPresentation presentation ) {
119+ super .renderElement (presentation );
120+
121+ presentation .setItemText ("findOneBy" + StringUtils .capitalize (f .getName ()));
122+ presentation .setTypeText ("findOneBy" + StringUtils .capitalize (f .getName ()));
123+ presentation .setIcon (PhpIcons .METHOD_ICON );
124+ presentation .setTailText ("(" + f .getName () + " : " + f .getDeclaredType () + ")" , true );
125+ presentation .setTypeText ("null|" + c .getName ());
126+ }
127+ });
158128 });
159129 });
160130 }
161131 }
162132
163133 public static class ExtbaseQueryBuilderCompletionProvider extends CompletionProvider <CompletionParameters > {
164134
165- public static String QUERY_BUILDER = "TYPO3\\ CMS\\ Extbase\\ Persistence\\ QueryInterface" ;
166-
167135 public static String [] QUERY_BUILDER_METHODS = {
168136 "equals" ,
169137 "like" ,
@@ -201,8 +169,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
201169 return ;
202170 }
203171
204- PhpClass containingClass = method .getContainingClass ();
205- if (!containingClass .getPresentableFQN ().equals (QUERY_BUILDER )) {
172+ if (!ExtbaseUtils .methodInstanceOf (ExtbaseUtils .EXTBASE_QUERY_INTERFACE_FQN , method )) {
206173 return ;
207174 }
208175
0 commit comments