Skip to content

Commit 96d8772

Browse files
committedApr 3, 2015
finalize command references #454
1 parent e0fa4f8 commit 96d8772

File tree

4 files changed

+105
-26
lines changed

4 files changed

+105
-26
lines changed
 

‎src/fr/adrienbrault/idea/symfony2plugin/completion/command/PhpCommandGotoCompletionRegistrar.java

+65-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar;
1515
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrarParameter;
1616
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;
17+
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1718
import org.apache.commons.lang.StringUtils;
1819
import org.jetbrains.annotations.NotNull;
1920
import org.jetbrains.annotations.Nullable;
@@ -158,13 +159,7 @@ private Map<String, CommandArg> getCommandConfigurationMap(@NotNull PhpClass php
158159
return Collections.emptyMap();
159160
}
160161

161-
PsiElement[] psiElements = PsiTreeUtil.collectElements(configure, new PsiElementFilter() {
162-
@Override
163-
public boolean isAccepted(PsiElement psiElement) {
164-
return psiElement instanceof MethodReference && methodName.equals(((MethodReference) psiElement).getName());
165-
}
166-
});
167-
162+
PsiElement[] psiElements = PsiTreeUtil.collectElements(configure, new CommandDefPsiElementFilter(methodName));
168163
if(psiElements.length == 0) {
169164
return Collections.emptyMap();
170165
}
@@ -177,27 +172,79 @@ public boolean isAccepted(PsiElement psiElement) {
177172
continue;
178173
}
179174

180-
PsiElement[] parameters = ((MethodReference) element).getParameters();
181-
if(parameters.length > 0 && parameters[0] instanceof StringLiteralExpression) {
182-
String contents = ((StringLiteralExpression) parameters[0]).getContents();
183-
if(StringUtils.isNotBlank(contents)) {
184-
185-
if(methodName.equals("addOption")) {
186-
targets.put(contents, new CommandArg(parameters[0], contents, getParameterStringValue(parameters, 3), getParameterStringValue(parameters, 4)));
187-
} else if(methodName.equals("addArgument")) {
188-
targets.put(contents, new CommandArg(parameters[0], contents, getParameterStringValue(parameters, 2), getParameterStringValue(parameters, 3)));
189-
} else {
190-
targets.put(contents, new CommandArg(parameters[0], contents));
175+
/*
176+
->setDefinition(new InputArgument())
177+
->setDefinition(array(
178+
new InputArgument(),
179+
new InputOption(),
180+
));
181+
*/
182+
if("setDefinition".equals(((MethodReference) element).getName())) {
183+
184+
Collection<NewExpression> newExpressions = PsiTreeUtil.collectElementsOfType(element, NewExpression.class);
185+
for (NewExpression newExpression : newExpressions) {
186+
if(methodName.equals("addOption") && PhpElementsUtil.getNewExpressionPhpClassWithInstance(newExpression, "Symfony\\Component\\Console\\Input\\InputOption") != null) {
187+
188+
// new InputOption()
189+
PsiElement[] parameters = newExpression.getParameters();
190+
String contents = getParameterStringValue(parameters, 0);
191+
if(contents != null && StringUtils.isNotBlank(contents)) {
192+
targets.put(contents, new CommandArg(parameters[0], contents, getParameterStringValue(parameters, 2), getParameterStringValue(parameters, 3)));
193+
}
194+
195+
} else if(methodName.equals("addArgument") && PhpElementsUtil.getNewExpressionPhpClassWithInstance(newExpression, "Symfony\\Component\\Console\\Input\\InputArgument") != null) {
196+
197+
// new InputArgument()
198+
PsiElement[] parameters = newExpression.getParameters();
199+
String contents = getParameterStringValue(parameters, 0);
200+
if(contents != null && StringUtils.isNotBlank(contents)) {
201+
targets.put(contents, new CommandArg(parameters[0], contents, getParameterStringValue(parameters, 2), getParameterStringValue(parameters, 3)));
202+
}
191203
}
192204

193205
}
206+
207+
} else {
208+
209+
/*
210+
->addArgument('arg3', null, 'desc')
211+
->addOption('opt1', null, null, 'desc', 'default')
212+
*/
213+
PsiElement[] parameters = ((MethodReference) element).getParameters();
214+
if(parameters.length > 0 && parameters[0] instanceof StringLiteralExpression) {
215+
String contents = ((StringLiteralExpression) parameters[0]).getContents();
216+
if(StringUtils.isNotBlank(contents)) {
217+
if(methodName.equals("addOption")) {
218+
targets.put(contents, new CommandArg(parameters[0], contents, getParameterStringValue(parameters, 3), getParameterStringValue(parameters, 4)));
219+
} else if(methodName.equals("addArgument")) {
220+
targets.put(contents, new CommandArg(parameters[0], contents, getParameterStringValue(parameters, 2), getParameterStringValue(parameters, 3)));
221+
}
222+
}
223+
}
194224
}
195225

196226
}
197227

198228
return targets;
199229
}
200230

231+
private static class CommandDefPsiElementFilter implements PsiElementFilter {
232+
private final String methodName;
233+
234+
public CommandDefPsiElementFilter(String methodName) {
235+
this.methodName = methodName;
236+
}
237+
238+
@Override
239+
public boolean isAccepted(PsiElement psiElement) {
240+
if(!(psiElement instanceof MethodReference)) {
241+
return false;
242+
}
243+
244+
String name = ((MethodReference) psiElement).getName();
245+
return methodName.equals(name) || "setDefinition".equals(name);
246+
}
247+
}
201248
}
202249

203250
private static class CommandArg {

‎src/fr/adrienbrault/idea/symfony2plugin/config/SymfonyPhpReferenceContributor.java

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ private PhpClass getClassArrayCreationParameter(PsiElement psiElement, int param
222222
if (newExpression instanceof NewExpression) {
223223
ParameterBag currentIndex = PsiElementUtils.getCurrentParameterIndex(arrayCreationExpression);
224224
if (currentIndex != null && currentIndex.getIndex() == parameterIndex) {
225+
// @TODO: getNewExpressionPhpClassWithInstance
225226
ClassReference classReference = ((NewExpression) newExpression).getClassReference();
226227
if(classReference != null) {
227228
String fqn = classReference.getFQN();

‎src/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java

+30
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.jetbrains.php.lang.psi.PhpPsiUtil;
2121
import com.jetbrains.php.lang.psi.elements.*;
2222
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
23+
import fr.adrienbrault.idea.symfony2plugin.Symfony2InterfacesUtil;
2324
import fr.adrienbrault.idea.symfony2plugin.dic.MethodReferenceBag;
2425
import org.apache.commons.lang.StringUtils;
2526
import org.jetbrains.annotations.NotNull;
@@ -809,4 +810,33 @@ public static interface ClassForCompletionVisitor {
809810
public void visit(PhpClass phpClass, String presentableFQN, String prefix);
810811
}
811812

813+
/**
814+
* new FooClass()
815+
*/
816+
@Nullable
817+
private static PhpClass getNewExpressionPhpClass(@NotNull NewExpression newExpression) {
818+
ClassReference classReference = newExpression.getClassReference();
819+
if(classReference != null) {
820+
String fqn = classReference.getFQN();
821+
if(fqn != null) {
822+
return PhpElementsUtil.getClass(newExpression.getProject(), fqn);
823+
}
824+
}
825+
826+
return null;
827+
}
828+
829+
/**
830+
* Get PhpClass from "new FooClass()" only if match instance condition
831+
*/
832+
public static PhpClass getNewExpressionPhpClassWithInstance(@NotNull NewExpression newExpression, @NotNull String instance) {
833+
834+
PhpClass phpClass = getNewExpressionPhpClass(newExpression);
835+
if(phpClass != null && new Symfony2InterfacesUtil().isInstanceOf(phpClass, instance)) {
836+
return phpClass;
837+
}
838+
839+
return null;
840+
}
841+
812842
}

‎tests/fr/adrienbrault/idea/symfony2plugin/tests/completion/command/PhpCommandGotoCompletionRegistrar.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ public String getTestDataPath() {
1919
return new File(this.getClass().getResource("fixtures").getFile()).getAbsolutePath();
2020
}
2121

22-
public void testTwigExtensionFilterCompletion() {
23-
24-
String[] strings = {"arg1", "arg2", "arg3" /*,"argDef", "optDef", "optSingleDef" */};
25-
26-
assertAtTextCompletionContains("<getArgument>", strings);
27-
assertAtTextCompletionContains("<hasArgument>", strings);
28-
assertAtTextCompletionContains("<getOption>", strings);
29-
assertAtTextCompletionContains("<hasOption>", strings);
22+
public void testCommandArguments() {
23+
String[] argsLookup = {"arg1", "arg2", "arg3" ,"argDef"};
24+
assertAtTextCompletionContains("<getArgument>", argsLookup);
25+
assertAtTextCompletionContains("<hasArgument>", argsLookup);
3026
}
3127

28+
public void testCommandOptions() {
29+
String[] optsLookup = {"opt1", "opt2", "opt3" ,"optDef", "optSingleDef"};
30+
assertAtTextCompletionContains("<getOption>", optsLookup);
31+
assertAtTextCompletionContains("<hasOption>", optsLookup);
32+
}
3233

3334
}

0 commit comments

Comments
 (0)
Please sign in to comment.