aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzzhongwei2012-08-08 03:36:23 (EDT)
committerzzhongwei2012-08-08 03:36:23 (EDT)
commit121d0b8a8326d10ae8111df487567d0dbadd5bb6 (patch)
treefb19a49d5e20e63e3b9e249784f1106c0d5a240d
parentf59d3c03f99519286cb8b03dae704e791688db70 (diff)
downloadorg.eclipse.pdt-121d0b8a8326d10ae8111df487567d0dbadd5bb6.zip
org.eclipse.pdt-121d0b8a8326d10ae8111df487567d0dbadd5bb6.tar.gz
org.eclipse.pdt-121d0b8a8326d10ae8111df487567d0dbadd5bb6.tar.bz2
386797: Anonymous functions now support using $this and class scope
https://bugs.eclipse.org/bugs/show_bug.cgi?id=386797
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/LocalMethodVariablesStrategy.java27
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java44
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure1.pdtt17
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure2.pdtt16
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure3.pdtt17
5 files changed, 112 insertions, 9 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/LocalMethodVariablesStrategy.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/LocalMethodVariablesStrategy.java
index 753ef6e..4755e42 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/LocalMethodVariablesStrategy.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/LocalMethodVariablesStrategy.java
@@ -34,6 +34,8 @@ import org.eclipse.php.internal.core.typeinference.PHPModelUtils;
*/
public class LocalMethodVariablesStrategy extends GlobalElementStrategy {
+ private static final String THIS = "$this";
+
public LocalMethodVariablesStrategy(ICompletionContext context,
IElementFilter elementFilter) {
super(context, elementFilter);
@@ -70,10 +72,27 @@ public class LocalMethodVariablesStrategy extends GlobalElementStrategy {
if (!PHPFlags.isStatic(enclosingMethod.getFlags())) {
IType declaringType = enclosingMethod.getDeclaringType();
if (declaringType != null) {
- if ("$this".startsWith(prefix)) { //$NON-NLS-1$
- reporter.reportField(
- new FakeField((ModelElement) declaringType,
- "$this", 0, 0), suffix, replaceRange, false, ICompletionReporter.RELEVANCE_ADJUST); //NON-NLS-1 //$NON-NLS-2$
+ if (THIS.startsWith(prefix)) { //$NON-NLS-1$
+ reporter.reportField(new FakeField(
+ (ModelElement) declaringType, THIS, 0, 0), suffix,
+ replaceRange, false,
+ ICompletionReporter.RELEVANCE_ADJUST); //NON-NLS-1 //$NON-NLS-2$
+ }
+ } else {
+ if (enclosingMethod.getParent() instanceof IField
+ && concreteContext.getPhpVersion().isGreaterThan(
+ PHPVersion.PHP5_3)) {
+ IMethod method = (IMethod) enclosingMethod.getParent()
+ .getAncestor(IModelElement.METHOD);
+ if (method != null) {
+ declaringType = method.getDeclaringType();
+ if (THIS.startsWith(prefix)) { //$NON-NLS-1$
+ reporter.reportField(new FakeField(
+ (ModelElement) declaringType, THIS, 0, 0),
+ suffix, replaceRange, false,
+ ICompletionReporter.RELEVANCE_ADJUST); //NON-NLS-1 //$NON-NLS-2$
+ }
+ }
}
}
}
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java
index 6d7b477..0467a78 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java
@@ -28,10 +28,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.PHPVersion;
import org.eclipse.php.internal.core.compiler.ast.nodes.*;
+import org.eclipse.php.internal.core.project.ProjectOptions;
import org.eclipse.php.internal.core.typeinference.ArrayDeclaration;
import org.eclipse.php.internal.core.typeinference.Declaration;
import org.eclipse.php.internal.core.typeinference.PHPTypeInferenceUtils;
+import org.eclipse.php.internal.core.typeinference.context.ContextFinder;
import org.eclipse.php.internal.core.typeinference.context.FileContext;
import org.eclipse.php.internal.core.typeinference.context.MethodContext;
import org.eclipse.php.internal.core.typeinference.goals.ArrayDeclarationGoal;
@@ -52,7 +55,7 @@ public class VariableReferenceEvaluator extends GoalEvaluator {
}
public IGoal[] init() {
- VariableReference variableReference = (VariableReference) ((ExpressionTypeGoal) goal)
+ final VariableReference variableReference = (VariableReference) ((ExpressionTypeGoal) goal)
.getExpression();
IContext context = goal.getContext();
@@ -60,11 +63,42 @@ public class VariableReferenceEvaluator extends GoalEvaluator {
if (variableReference.getName().equals("$this")) {
if (context instanceof MethodContext) {
MethodContext methodContext = (MethodContext) context;
- IEvaluatedType instanceType = methodContext.getInstanceType();
- if (instanceType != null) {
- this.results.add(instanceType);
- } else {
+ final LambdaFunctionDeclaration[] lambdas = new LambdaFunctionDeclaration[1];
+ ContextFinder contextFinder = new ContextFinder(
+ methodContext.getSourceModule()) {
+ @Override
+ public boolean visit(Expression s) throws Exception {
+ if (s instanceof LambdaFunctionDeclaration) {
+ LambdaFunctionDeclaration lambda = (LambdaFunctionDeclaration) s;
+ if (variableReference.sourceStart() > lambda
+ .sourceStart()
+ && variableReference.sourceEnd() < lambda
+ .sourceEnd()) {
+ lambdas[0] = lambda;
+ }
+ }
+ return super.visit(s);
+ }
+ };
+ try {
+ methodContext.getRootNode().traverse(contextFinder);
+ } catch (Exception e) {
+ }
+ PHPVersion phpVersion = ProjectOptions
+ .getPhpVersion(methodContext.getSourceModule()
+ .getScriptProject().getProject());
+ if (lambdas[0] != null
+ && (lambdas[0].isStatic() || phpVersion
+ .isLessThan(PHPVersion.PHP5_4))) {
this.results.add(new SimpleType(SimpleType.TYPE_NULL));
+ } else {
+ IEvaluatedType instanceType = methodContext
+ .getInstanceType();
+ if (instanceType != null) {
+ this.results.add(instanceType);
+ } else {
+ this.results.add(new SimpleType(SimpleType.TYPE_NULL));
+ }
}
return IGoal.NO_GOALS;
}
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure1.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure1.pdtt
new file mode 100644
index 0000000..de2ad27
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure1.pdtt
@@ -0,0 +1,17 @@
+--TEST--
+Test array dereferencing 1
+--PREFERENCES--
+org.eclipse.php.core/contentAssistShowStrictOptions=false
+--FILE--
+<?php
+
+class class_name {
+ function fun1() {
+ $clousure = function() {
+ $th|
+ };
+ }
+}
+?>
+--EXPECT--
+field($this) \ No newline at end of file
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure2.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure2.pdtt
new file mode 100644
index 0000000..22e75df
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure2.pdtt
@@ -0,0 +1,16 @@
+--TEST--
+Test array dereferencing 1
+--PREFERENCES--
+org.eclipse.php.core/contentAssistShowStrictOptions=false
+--FILE--
+<?php
+
+class class_name {
+ function fun1() {
+ $clousure = static function() {
+ $th|
+ };
+ }
+}
+?>
+--EXPECT-- \ No newline at end of file
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure3.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure3.pdtt
new file mode 100644
index 0000000..934c573
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testClosure3.pdtt
@@ -0,0 +1,17 @@
+--TEST--
+Test array dereferencing 1
+--PREFERENCES--
+org.eclipse.php.core/contentAssistShowStrictOptions=false
+--FILE--
+<?php
+
+class class_name {
+ function fun1() {
+ $clousure = function() {
+ $this->|
+ };
+ }
+}
+?>
+--EXPECT--
+method(fun1) \ No newline at end of file