Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThierry BLIND2019-06-20 08:24:45 -0400
committerThierry BLIND2019-06-20 08:24:45 -0400
commit7af02399ed17fbe5f8e17422370bf0a9e317fa62 (patch)
tree03209d9a78424b4777ce447f36d3566a561026d1
parent71429455a0422a57f7265e9e35c9067c38d8fb7f (diff)
downloadorg.eclipse.pdt-7af02399ed17fbe5f8e17422370bf0a9e317fa62.tar.gz
org.eclipse.pdt-7af02399ed17fbe5f8e17422370bf0a9e317fa62.tar.xz
org.eclipse.pdt-7af02399ed17fbe5f8e17422370bf0a9e317fa62.zip
Bug 548280 - do better case-insensitive comparisons in class
UseStatementInjector Change-Id: I0880cd33fdf3bdf3dbe60930242b5b45561dde8b Signed-off-by: Thierry BLIND <thierryblind@msn.com>
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/parser/ASTUtils.java12
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/contentassist/UseStatementInjector.java48
-rw-r--r--tests/org.eclipse.php.ui.tests/workspace/codeassist/php53/namespace14.pdtt60
3 files changed, 97 insertions, 23 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/parser/ASTUtils.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/parser/ASTUtils.java
index 8aa7b0935..f6cff1f2e 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/parser/ASTUtils.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/parser/ASTUtils.java
@@ -582,10 +582,12 @@ public class ASTUtils {
* @param namespace
* Namespace name
* @param offset
- * Current position in the file (this is needed since we don't want
- * to take USE statements placed below current position into account)
- * @return USE statement part node, or <code>null</code> in case relevant
- * statement couldn't be found
+ * Current position in the file (this is needed since we don't
+ * want to take USE statements placed below current position into
+ * account)
+ * @return USE statement part node (whose getFullUseStatementName() value is
+ * case-insensitive-equal to the value of parameter "namespace"), or
+ * <code>null</code> when relevant statement couldn't be found
*/
public static UsePart findUseStatementByNamespace(ModuleDeclaration moduleDeclaration, final String namespace,
final int offset) {
@@ -607,7 +609,7 @@ public class ASTUtils {
* example, this method will return an object for type <code>"A\B\C\D\E"</code>
* from statement <code>"use A\B\ { \C\D\E };"</code>. <b>Note that this type
* will have its source start and end range limited to the source start and end
- * range of <code>"\C\D\E"</code>. Also note that UsePart aliases are not
+ * range of <code>"C\D\E"</code>. Also note that UsePart aliases are not
* handled by this method.</b>
*
* @param usePart
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/contentassist/UseStatementInjector.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/contentassist/UseStatementInjector.java
index f2ec32b6d..2d24340f8 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/contentassist/UseStatementInjector.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/editor/contentassist/UseStatementInjector.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009 IBM Corporation and others.
+ * Copyright (c) 2009-2019 IBM Corporation and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
@@ -72,8 +72,8 @@ public class UseStatementInjector {
}
/**
- * Inserts USE statement into beginning of the document, or after the last USE
- * statement.
+ * Inserts USE statement into beginning of the document, or after the last
+ * USE statement.
*
* @param document
* @param textViewer
@@ -121,7 +121,8 @@ public class UseStatementInjector {
if (offset > 0) {
String prefix = document.get(proposal.getReplacementOffset(), proposal.getReplacementLength());
String fullName = ((IType) modelElement).getElementName();
- if (fullName.startsWith(prefix) && prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) < 0) {
+ if (fullName.toLowerCase().startsWith(prefix.toLowerCase())
+ && prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) < 0) {
// int
ITextEditor textEditor = ((PHPStructuredTextViewer) textViewer).getTextEditor();
if (textEditor instanceof PHPStructuredEditor) {
@@ -163,7 +164,7 @@ public class UseStatementInjector {
// if the class/namespace has not been imported
// add use statement
- if (program != null && !importedTypeName.contains(typeName)) {
+ if (program != null && !importedTypeName.contains(typeName.toLowerCase())) {
program.recordModifications();
AST ast = program.getAST();
@@ -231,17 +232,17 @@ public class UseStatementInjector {
offset += edits.getLength();
proposal.setReplacementOffset(replacementOffset);
} else if (!useAlias && (usePart == null
- || !usePartName.equals(usePart.getFullUseStatementName()))) {
+ // Redondant check:
+ // || !usePartName.equalsIgnoreCase(usePart
+ // .getFullUseStatementName())
+ )) {
// if the type name already exists, use
- // fully
- // qualified name to replace
+ // fully qualified name to replace
proposal.setReplacementString(NamespaceReference.NAMESPACE_SEPARATOR + fullName);
}
}
}
-
}
- return offset;
}
return offset;
} else
@@ -308,14 +309,14 @@ public class UseStatementInjector {
List<String> importedTypeName = getImportedTypeName(moduleDeclaration, offset);
String typeName = ""; //$NON-NLS-1$
if (!useAlias) {
- typeName = modelElement.getElementName().toLowerCase();
+ typeName = modelElement.getElementName();
} else {
if (usePart != null && usePart.getAlias() != null && usePart.getAlias().getName() != null) {
typeName = usePart.getAlias().getName();
} else {
String elementName = PHPModelUtils.extractElementName(namespaceName);
if (elementName != null) {
- typeName = elementName.toLowerCase();
+ typeName = elementName;
}
}
}
@@ -323,7 +324,7 @@ public class UseStatementInjector {
PHPVersion phpVersion = ProjectOptions.getPHPVersion(modelElement);
// if the class/namespace has not been imported
// add use statement
- if (program != null && !importedTypeName.contains(typeName)
+ if (program != null && !importedTypeName.contains(typeName.toLowerCase())
&& canInsertUseStatement(getUseStatementType(modelElement), phpVersion)) {
program.recordModifications();
AST ast = program.getAST();
@@ -399,16 +400,26 @@ public class UseStatementInjector {
int replacementOffset = proposal.getReplacementOffset() + edits.getLength();
offset += edits.getLength();
proposal.setReplacementOffset(replacementOffset);
- } else if (!useAlias && (usePart == null || !usePartName.equals(usePart.getFullUseStatementName()))) {
+ } else if (!useAlias && (usePart == null
+ // Redondant check:
+ // || !usePartName.equalsIgnoreCase(usePart
+ // .getFullUseStatementName())
+ )) {
String namespacePrefix = NamespaceReference.NAMESPACE_SEPARATOR + namespaceName
+ NamespaceReference.NAMESPACE_SEPARATOR;
String replacementString = proposal.getReplacementString();
+ // looks for the namespace prefix, even in use statements
String existingNamespacePrefix = readNamespacePrefix(sourceModule, document, offset, phpVersion);
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=312880
+ // if the type name already exists in "importedTypeName" but
+ // is defined in different namespaces, use fully
+ // qualified name
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=459306
- // if the type name already exists, use fully
- // qualified name to replace:
+ // if the type name already exists in "importedTypeName", use
+ // fully qualified name when there is no match between current
+ // namespace prefix and modelElement's namespace
if (existingNamespacePrefix == null || !namespaceName.equalsIgnoreCase(existingNamespacePrefix)) {
replacementString = namespacePrefix + replacementString;
}
@@ -559,7 +570,8 @@ public class UseStatementInjector {
}
/**
- * Get all the class / namespace names which have been imported by use statement
+ * Get all the class / namespace names which have been imported by use
+ * statement
*
* @param moduleDeclaration
* @param offset
@@ -622,7 +634,7 @@ public class UseStatementInjector {
if (currentNamespace == null) {
return false;
}
- if (namespaceName.equals(getNamespaceName(currentNamespace))) {
+ if (namespaceName.equalsIgnoreCase(getNamespaceName(currentNamespace))) {
return true;
}
return false;
diff --git a/tests/org.eclipse.php.ui.tests/workspace/codeassist/php53/namespace14.pdtt b/tests/org.eclipse.php.ui.tests/workspace/codeassist/php53/namespace14.pdtt
new file mode 100644
index 000000000..3e5a6026c
--- /dev/null
+++ b/tests/org.eclipse.php.ui.tests/workspace/codeassist/php53/namespace14.pdtt
@@ -0,0 +1,60 @@
+--TEST--
+Bug 548280 - do better case-insensitive comparisons in class UseStatementInjector
+--FILE--
+<?php
+
+namespace Bar\baz
+{
+ class Target { function bar_baz(); }
+}
+
+namespace foo
+{
+ class TestAbsolute
+ {
+ /**
+ * @return \bar\baz\Target
+ */
+ function test() {}
+ }
+
+ class TestRelative
+ {
+ /**
+ * @return bar\baz\Target
+ */
+ function test() {}
+ }
+ bar| //call CA here
+}
+?>
+--EXPECT--
+<?php
+
+namespace Bar\baz
+{
+ class Target { function bar_baz(); }
+}
+
+namespace foo
+{
+ use Bar;
+
+ class TestAbsolute
+ {
+ /**
+ * @return \bar\baz\Target
+ */
+ function test() {}
+ }
+
+ class TestRelative
+ {
+ /**
+ * @return bar\baz\Target
+ */
+ function test() {}
+ }
+ Bar\baz\ //call CA here
+}
+?> \ No newline at end of file

Back to the top