aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThierry BLIND2018-05-15 11:59:02 -0400
committerThierry BLIND2018-05-16 09:26:10 -0400
commit42d066404c185a5acf182070346f34c87e274a17 (patch)
treecfb9c480c319a20c10521a6ff2685428e25979ed
parenteb2655b2e45f2133e4dd39b3e6af13a2e7c2b397 (diff)
downloadorg.eclipse.pdt-42d066404c185a5acf182070346f34c87e274a17.zip
org.eclipse.pdt-42d066404c185a5acf182070346f34c87e274a17.tar.gz
org.eclipse.pdt-42d066404c185a5acf182070346f34c87e274a17.tar.xz
Bug 533276 - const vs define and tooltips
Change-Id: Iccbb2b60ad56d0386fbd8ee70b9dae5fed2697fa Signed-off-by: Thierry BLIND <thierryblind@msn.com>
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/ASTNode.java3
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/locator/PHPElementConciliator.java36
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/search/ConstantsOccurrencesFinder.java25
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/PHPModelUtils.java23
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant5.pdtt18
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant6.pdtt26
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant7.pdtt26
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant8.pdtt25
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant9.pdtt18
9 files changed, 175 insertions, 25 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/ASTNode.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/ASTNode.java
index c559ac2..0fc15c3 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/ASTNode.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/ASTNode.java
@@ -107,6 +107,9 @@ public abstract class ASTNode implements Visitable {
public static final int GOTO_STATEMENT = 69;
public static final int LAMBDA_FUNCTION_DECLARATION = 70;
public static final int TRAIT_USE_STATEMENT = 71;
+ /**
+ * @deprecated
+ */
public static final int TRAIT_DECLARATION = 72;
public static final int FULLY_QUALIFIED_TRAIT_METHOD_REFERENCE = 73;
public static final int TRAIT_ALIAS = 74;
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/locator/PHPElementConciliator.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/locator/PHPElementConciliator.java
index a6aeb5c..6ae6b4a 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/locator/PHPElementConciliator.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/locator/PHPElementConciliator.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2010 IBM Corporation and others.
+ * Copyright (c) 2009, 2010, 2018 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -56,13 +56,15 @@ public class PHPElementConciliator {
return CONCILIATOR_LOCAL_VARIABLE;
} else if (isDispatch(locateNode)) {
return CONCILIATOR_CLASS_MEMBER;
- } else if (isGlobalConstant(locateNode)) {
+ } else if (isGlobalNamespacedConstant(locateNode)) {
+ return CONCILIATOR_CONSTANT;
+ } else if (isGlobalConstDeclaration(locateNode)) {
return CONCILIATOR_CONSTANT;
}
return CONCILIATOR_UNKNOWN;
}
- public static boolean isGlobalConstant(ASTNode locateNode) {
+ public static boolean isGlobalNamespacedConstant(ASTNode locateNode) {
assert locateNode != null;
// check if it is an identifier
@@ -85,7 +87,32 @@ public class PHPElementConciliator {
}
return true;
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=533276
+ // look for "const FOO = ..." declarations OUTSIDE class or interface
+ // declaration
+ public static boolean isGlobalConstDeclaration(ASTNode locateNode) {
+ assert locateNode != null;
+
+ // check if it is an identifier
+ if (locateNode.getType() != ASTNode.IDENTIFIER) {
+ return false;
+ }
+
+ ASTNode parent = locateNode.getParent();
+ if (parent.getType() == ASTNode.CONSTANT_DECLARATION) {
+ while (parent.getParent() != null) {
+ parent = parent.getParent();
+ if (parent.getType() == ASTNode.CLASS_DECLARATION || parent.getType() == ASTNode.INTERFACE_DECLARATION
+ || parent.getType() == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
}
/**
@@ -153,7 +180,8 @@ public class PHPElementConciliator {
}
}
- if (parent.getType() == ASTNode.CONSTANT_DECLARATION) {
+ // look for "const FOO = ..." declarations INSIDE class or interface declaration
+ if (parent.getType() == ASTNode.CONSTANT_DECLARATION && !isGlobalConstDeclaration(locateNode)) {
return true;
}
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/search/ConstantsOccurrencesFinder.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/search/ConstantsOccurrencesFinder.java
index fb55f3f..86e226f 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/search/ConstantsOccurrencesFinder.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/search/ConstantsOccurrencesFinder.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009 IBM Corporation and others.
+ * Copyright (c) 2009, 2018 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -35,9 +35,10 @@ public class ConstantsOccurrencesFinder extends AbstractOccurrencesFinder {
/**
* @param root
- * the AST root
+ * the AST root
* @param node
- * the selected node (must be an {@link Scalar} instance)
+ * the selected node (must be an {@link Scalar} or an
+ * {@link Identifier} instance)
* @return returns a message if there is a problem
*/
@Override
@@ -52,7 +53,7 @@ public class ConstantsOccurrencesFinder extends AbstractOccurrencesFinder {
constantName = constantName.substring(1, constantName.length() - 1);
}
return null;
- } else if (node.getType() == ASTNode.IDENTIFIER && node.getParent().getType() == ASTNode.NAMESPACE_NAME) {
+ } else if (node.getType() == ASTNode.IDENTIFIER) {
nameNode = node;
constantName = ((Identifier) node).getName();
return null;
@@ -84,11 +85,17 @@ public class ConstantsOccurrencesFinder extends AbstractOccurrencesFinder {
@Override
public boolean visit(Identifier identifier) {
- if (checkEquality(identifier.getName()) && PHPElementConciliator.isGlobalConstant(identifier)) {
- nodeToFullName.put(identifier,
- getFullName((Identifier) identifier.getParent(), fLastUseParts, fCurrentNamespace));
- nodeToOccurrence.put(identifier, new OccurrenceLocation(identifier.getStart(), identifier.getLength(),
- getOccurrenceType(identifier), fDescription));
+ if (checkEquality(identifier.getName())) {
+ if (PHPElementConciliator.isGlobalNamespacedConstant(identifier)) {
+ nodeToFullName.put(identifier,
+ getFullName((Identifier) identifier.getParent(), fLastUseParts, fCurrentNamespace));
+ nodeToOccurrence.put(identifier, new OccurrenceLocation(identifier.getStart(), identifier.getLength(),
+ getOccurrenceType(identifier), fDescription));
+ } else if (PHPElementConciliator.isGlobalConstDeclaration(identifier)) {
+ nodeToFullName.put(identifier, getFullName(identifier, fLastUseParts, fCurrentNamespace));
+ nodeToOccurrence.put(identifier, new OccurrenceLocation(identifier.getStart(), identifier.getLength(),
+ getOccurrenceType(identifier), fDescription));
+ }
}
return true;
}
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/PHPModelUtils.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/PHPModelUtils.java
index cb61e01..6ffa877 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/PHPModelUtils.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/PHPModelUtils.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2016, 2017 IBM Corporation and others.
+ * Copyright (c) 2009, 2016, 2017, 2018 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -795,19 +795,18 @@ public class PHPModelUtils {
if (fields.length > 0) {
return fields;
}
-
- // For functions and constants, PHP will fall back to global
- // functions or constants if a namespaced function or
- // constant does not exist:
- IDLTKSearchScope scope = SearchEngine.createSearchScope(sourceModule.getScriptProject());
- fields = PHPModelAccess.getDefault().findFields(fieldName, MatchRule.EXACT,
- Modifiers.AccConstant | Modifiers.AccGlobal, 0, scope, null);
-
- Collection<IField> filteredElements = filterElements(sourceModule,
- filterTrueGlobal(Arrays.asList(fields)), cache, monitor);
- return filteredElements.toArray(new IField[filteredElements.size()]);
}
}
+
+ // look for globals, class constants or namespaced/non-namespaced "const"
+ // fields (since PHP 5.3)
+ IDLTKSearchScope scope = SearchEngine.createSearchScope(sourceModule.getScriptProject());
+ IField[] fields = PHPModelAccess.getDefault().findFields(fieldName, MatchRule.EXACT,
+ Modifiers.AccConstant | Modifiers.AccGlobal, 0, scope, null);
+
+ Collection<IField> filteredElements = filterElements(sourceModule, filterTrueGlobal(Arrays.asList(fields)),
+ cache, monitor);
+ return filteredElements.toArray(new IField[filteredElements.size()]);
}
IDLTKSearchScope scope = SearchEngine.createSearchScope(sourceModule.getScriptProject());
IField[] fields = PHPModelAccess.getDefault().findFields(fieldName, MatchRule.EXACT, Modifiers.AccGlobal, 0,
diff --git a/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant5.pdtt b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant5.pdtt
new file mode 100644
index 0000000..a5a00b1
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant5.pdtt
@@ -0,0 +1,18 @@
+--TEST--
+Bug 533276 - const vs define and tooltips
+--FILE--
+<?php
+class MYCLASS1
+{
+ const FOO = 'bar';
+}
+
+define('BAZ', 'QUX');
+
+const FOO = 'bar';
+echo MYCLASS1::FOO;
+echo BAZ;
+echo |FOO|;
+?>
+--EXPECT--
+field(FOO)
diff --git a/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant6.pdtt b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant6.pdtt
new file mode 100644
index 0000000..d0a93b2
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant6.pdtt
@@ -0,0 +1,26 @@
+--TEST--
+Bug 533276 - const vs define and tooltips
+--FILE--
+<?php
+namespace MYNS1
+{
+ class MYCLASS1
+ {
+ const FOO = 'bar';
+ }
+
+ define('BAZ', 'QUX');
+
+ // NB: "const" are still "namespaced"
+ const FOO = 'bar';
+ echo MYCLASS1::FOO;
+ echo BAZ;
+ echo |FOO|;
+}
+namespace
+{
+ echo MYNS1\FOO;
+}
+?>
+--EXPECT--
+field(FOO)
diff --git a/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant7.pdtt b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant7.pdtt
new file mode 100644
index 0000000..0b02a1d
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant7.pdtt
@@ -0,0 +1,26 @@
+--TEST--
+Bug 533276 - const vs define and tooltips
+--FILE--
+<?php
+namespace MYNS1
+{
+ class MYCLASS1
+ {
+ const FOO = 'bar';
+ }
+
+ define('BAZ', 'QUX');
+
+ // NB: "const" are still "namespaced"
+ const FOO = 'bar';
+ echo MYCLASS1::FOO;
+ echo BAZ;
+ echo FOO;
+}
+namespace
+{
+ echo MYNS1\|FOO|;
+}
+?>
+--EXPECT--
+field(FOO)
diff --git a/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant8.pdtt b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant8.pdtt
new file mode 100644
index 0000000..03ff881
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant8.pdtt
@@ -0,0 +1,25 @@
+--TEST--
+Bug 533276 - const vs define and tooltips
+--FILE--
+<?php
+namespace MYNS1
+{
+ class MYCLASS1
+ {
+ const FOO = 'bar';
+ }
+
+ define('BAZ', 'QUX');
+
+ // NB: "const" are still "namespaced"
+ const FOO = 'bar';
+ echo MYCLASS1::FOO;
+ echo BAZ;
+ echo FOO;
+}
+namespace
+{
+ echo |FOO|;
+}
+?>
+--EXPECT--
diff --git a/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant9.pdtt b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant9.pdtt
new file mode 100644
index 0000000..1029987
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/selection/php53/testGlobalConstant9.pdtt
@@ -0,0 +1,18 @@
+--TEST--
+Bug 533276 - const vs define and tooltips
+--FILE--
+<?php
+class MYCLASS1
+{
+ const FOO = 'bar';
+}
+
+define('BAZ', 'QUX');
+
+const FOO = 'bar';
+echo MYCLASS1::FOO;
+echo BAZ;
+echo |\FOO|;
+?>
+--EXPECT--
+field(FOO)