aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJacek Pospychala2012-10-29 10:08:33 (EDT)
committerJacek Pospychala2012-10-29 10:08:33 (EDT)
commitf99df1e9da170370ae08f097e3abe91bdc7b533f (patch)
treeacfbfdf42cacca96b73282ea4a2dfaf30a282ea5
parent0458f555ff43d88cebb779f4f93368342bf88080 (diff)
downloadorg.eclipse.pdt-f99df1e9da170370ae08f097e3abe91bdc7b533f.zip
org.eclipse.pdt-f99df1e9da170370ae08f097e3abe91bdc7b533f.tar.gz
org.eclipse.pdt-f99df1e9da170370ae08f097e3abe91bdc7b533f.tar.bz2
Bug 393039 - Improved typed array codeassist
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/FormalParameterEvaluator.java11
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/IteratorTypeGoalEvaluator.java88
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/phpdoc/PHPDocClassVariableEvaluator.java17
3 files changed, 109 insertions, 7 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/FormalParameterEvaluator.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/FormalParameterEvaluator.java
index 8e9f1b5..8937d3c 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/FormalParameterEvaluator.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/FormalParameterEvaluator.java
@@ -26,6 +26,7 @@ import org.eclipse.php.internal.core.typeinference.PHPClassType;
import org.eclipse.php.internal.core.typeinference.PHPModelUtils;
import org.eclipse.php.internal.core.typeinference.PHPSimpleTypes;
import org.eclipse.php.internal.core.typeinference.context.MethodContext;
+import org.eclipse.php.internal.core.typeinference.evaluators.phpdoc.PHPDocClassVariableEvaluator;
public class FormalParameterEvaluator extends GoalEvaluator {
@@ -40,7 +41,7 @@ public class FormalParameterEvaluator extends GoalEvaluator {
FormalParameter parameter = (FormalParameter) typedGoal.getExpression();
SimpleReference type = parameter.getParameterType();
- if (type != null) {
+ if (type != null && "array".equals(type.getName()) == false) {
result = PHPClassType.fromSimpleReference(type);
} else {
IContext context = typedGoal.getContext();
@@ -87,9 +88,15 @@ public class FormalParameterEvaluator extends GoalEvaluator {
// methodContext.getSourceModule(),
// references[1].sourceStart()));
// fix unit test testDoctag7.pdtt
+ String typeName = references[1]
+ .getName();
+ if (typeName
+ .endsWith(PHPDocClassVariableEvaluator.BRACKETS)) {
+ typeName = typeName.substring(0, typeName.length() -2);
+ }
result = PHPClassType
.fromTypeName(
- references[1].getName(),
+ typeName,
methodContext
.getSourceModule(),
references[1]
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/IteratorTypeGoalEvaluator.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/IteratorTypeGoalEvaluator.java
index 9819367..1eacc90 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/IteratorTypeGoalEvaluator.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/IteratorTypeGoalEvaluator.java
@@ -3,10 +3,9 @@ package org.eclipse.php.internal.core.typeinference.evaluators;
import java.util.LinkedList;
import java.util.List;
-import org.eclipse.dltk.core.DLTKCore;
-import org.eclipse.dltk.core.ISourceModule;
-import org.eclipse.dltk.core.IType;
-import org.eclipse.dltk.core.ModelException;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.dltk.ast.references.SimpleReference;
+import org.eclipse.dltk.core.*;
import org.eclipse.dltk.evaluation.types.MultiTypeType;
import org.eclipse.dltk.ti.GoalState;
import org.eclipse.dltk.ti.ISourceModuleContext;
@@ -14,8 +13,13 @@ import org.eclipse.dltk.ti.goals.ExpressionTypeGoal;
import org.eclipse.dltk.ti.goals.GoalEvaluator;
import org.eclipse.dltk.ti.goals.IGoal;
import org.eclipse.dltk.ti.types.IEvaluatedType;
+import org.eclipse.php.internal.core.compiler.ast.nodes.PHPDocBlock;
+import org.eclipse.php.internal.core.compiler.ast.nodes.PHPDocTag;
+import org.eclipse.php.internal.core.compiler.ast.nodes.PHPMethodDeclaration;
import org.eclipse.php.internal.core.typeinference.PHPClassType;
import org.eclipse.php.internal.core.typeinference.PHPModelUtils;
+import org.eclipse.php.internal.core.typeinference.context.MethodContext;
+import org.eclipse.php.internal.core.typeinference.evaluators.phpdoc.PHPDocClassVariableEvaluator;
import org.eclipse.php.internal.core.typeinference.goals.IteratorTypeGoal;
import org.eclipse.php.internal.core.typeinference.goals.MethodElementReturnTypeGoal;
import org.eclipse.php.internal.core.typeinference.goals.phpdoc.PHPDocMethodReturnTypeGoal;
@@ -39,6 +43,7 @@ public class IteratorTypeGoalEvaluator extends GoalEvaluator {
}
public IGoal[] subGoalDone(IGoal subgoal, Object result, GoalState state) {
+
if (state != GoalState.RECURSIVE) {
if (result instanceof PHPClassType) {
if (subgoal instanceof ExpressionTypeGoal) {
@@ -52,6 +57,20 @@ public class IteratorTypeGoalEvaluator extends GoalEvaluator {
for (IType type : types) {
IType[] superTypes = PHPModelUtils.getSuperClasses(
type, null);
+
+ if (subgoal.getContext() instanceof MethodContext) {
+
+ MethodContext methodContext = (MethodContext) subgoal
+ .getContext();
+
+ if (isArrayType(methodContext, type)) {
+ MultiTypeType mType = new MultiTypeType();
+ mType.addType((IEvaluatedType) result);
+ this.result = mType;
+ return IGoal.NO_GOALS;
+ }
+ }
+
if (isImplementedIterator(superTypes)) {
subGoals.add(new MethodElementReturnTypeGoal(
subgoal.getContext(),
@@ -78,6 +97,67 @@ public class IteratorTypeGoalEvaluator extends GoalEvaluator {
return IGoal.NO_GOALS;
}
+ /**
+ * Check if IType is a typed array in the methodContext:
+ *
+ * <pre>
+ * /*
+ * * @param SomeClass[] $elements
+ * *\/
+ * public function foo(array $elements);
+ * </pre>
+ *
+ *
+ * @param methodContext
+ * @param type
+ * @return boolean
+ */
+ private boolean isArrayType(MethodContext methodContext, IType type) {
+
+ PHPMethodDeclaration methodDeclaration = (PHPMethodDeclaration) methodContext
+ .getMethodNode();
+
+ PHPDocBlock[] docBlocks = new PHPDocBlock[0];
+
+ try {
+ IModelElement element = methodContext.getSourceModule()
+ .getElementAt(methodDeclaration.getNameStart());
+ if (element instanceof IMethod) {
+ IMethod method = (IMethod) element;
+ if (method.getDeclaringType() != null) {
+ docBlocks = PHPModelUtils.getTypeHierarchyMethodDoc(
+ method.getDeclaringType(), method.getElementName(),
+ true, null);
+ } else {
+ docBlocks = new PHPDocBlock[] { methodDeclaration
+ .getPHPDoc() };
+ }
+ } else {
+ docBlocks = new PHPDocBlock[] { methodDeclaration.getPHPDoc() };
+ }
+
+ } catch (CoreException e) {
+ }
+
+ if (docBlocks.length > 0) {
+ for (int i = 0; i < docBlocks.length; i++) {
+ PHPDocTag[] tags = docBlocks[i].getTags();
+ for (int j = 0; j < tags.length; j++) {
+ PHPDocTag tag = tags[i];
+ if (tag.getTagKind() == PHPDocTag.PARAM) {
+ SimpleReference[] refs = tag.getReferences();
+ if (refs[1].getName().equals(
+ type.getElementName() + PHPDocClassVariableEvaluator.BRACKETS)) {
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
private boolean isImplementedIterator(IType[] superClasses) {
if (superClasses == null)
return false;
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/phpdoc/PHPDocClassVariableEvaluator.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/phpdoc/PHPDocClassVariableEvaluator.java
index 349cc1e..964662c 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/phpdoc/PHPDocClassVariableEvaluator.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/phpdoc/PHPDocClassVariableEvaluator.java
@@ -42,6 +42,8 @@ public class PHPDocClassVariableEvaluator extends AbstractPHPGoalEvaluator {
private static final String SPLASH = "\\";
+ public static final String BRACKETS = "[]";
+
private List<IEvaluatedType> evaluated = new LinkedList<IEvaluatedType>();
private final static Pattern ARRAY_TYPE_PATTERN = Pattern
@@ -106,6 +108,17 @@ public class PHPDocClassVariableEvaluator extends AbstractPHPGoalEvaluator {
if (m.find()) {
evaluated.add(getArrayType(m.group(),
currentNamespace, doc.sourceStart()));
+ } else if (typeName.endsWith(BRACKETS)
+ && typeName.length() > 2) {
+ int offset = 0;
+ try {
+ offset = typeField.getSourceRange().getOffset();
+ } catch (ModelException e) {
+ }
+ evaluated.add(getArrayType(typeName.substring(0,
+ typeName.length() - 2), currentNamespace,
+ offset));
+
} else {
if (currentNamespace != null) {
ModuleDeclaration moduleDeclaration = SourceParserUtil
@@ -177,7 +190,9 @@ public class PHPDocClassVariableEvaluator extends AbstractPHPGoalEvaluator {
int offset) {
int beginIndex = type.indexOf("[") + 1;
int endIndex = type.lastIndexOf("]");
- type = type.substring(beginIndex, endIndex);
+ if (endIndex != -1) {
+ type = type.substring(beginIndex, endIndex);
+ }
MultiTypeType arrayType = new MultiTypeType();
Matcher m = ARRAY_TYPE_PATTERN.matcher(type);
if (m.find()) {