Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Xu2017-06-05 22:22:36 -0400
committerAlex Xu2017-06-05 22:49:41 -0400
commit13bcaf50e39bcf6aa15e6f4e01290b07aa5a1026 (patch)
tree0fd27ba770ffd08441ae136bf397390db1837788
parent3c2b3d77c651834564edb7e7f398def5aa22ba4a (diff)
downloadorg.eclipse.pdt-13bcaf50e39bcf6aa15e6f4e01290b07aa5a1026.tar.gz
org.eclipse.pdt-13bcaf50e39bcf6aa15e6f4e01290b07aa5a1026.tar.xz
org.eclipse.pdt-13bcaf50e39bcf6aa15e6f4e01290b07aa5a1026.zip
Bug 517835 - Organize use statements doesn't work well for imported
namespace Change-Id: I6d9baf08da9d5067e9c391ec3e9448231a70d741 Signed-off-by: Alex Xu <ibazzi@qq.com>
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/corext/codemanipulation/OrganizeUseStatementsOperation.java24
-rw-r--r--tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement06.pdtt26
-rw-r--r--tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement07.pdtt42
3 files changed, 87 insertions, 5 deletions
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/corext/codemanipulation/OrganizeUseStatementsOperation.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/corext/codemanipulation/OrganizeUseStatementsOperation.java
index 0e359f2df..a8fc74b70 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/corext/codemanipulation/OrganizeUseStatementsOperation.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/corext/codemanipulation/OrganizeUseStatementsOperation.java
@@ -231,13 +231,15 @@ public class OrganizeUseStatementsOperation implements IWorkspaceRunnable {
public void add(Identifier ref) {
NamespaceDeclaration namespace = fRoot.getNamespaceDeclaration(ref.getStart());
String typeName = ref.getName();
+ String importName = typeName;
int index = typeName.indexOf(NamespaceReference.NAMESPACE_DELIMITER);
if (index > 0) {
- typeName = typeName.substring(0, index);
+ importName = typeName.substring(0, index);
+ typeName = typeName.substring(index + 1);
}
- if (fImportsAdded.get(namespace) == null || fImportsAdded.get(namespace).contains(typeName)) {
+ if (fImportsAdded.get(namespace) == null || fImportsAdded.get(namespace).contains(importName)) {
return;
}
@@ -254,12 +256,24 @@ public class OrganizeUseStatementsOperation implements IWorkspaceRunnable {
if (typeBindingName.startsWith(NamespaceReference.NAMESPACE_DELIMITER)) {
typeBindingName = typeBindingName.substring(1);
}
- if (!typeBindingName.endsWith(typeName)) {
- alias = typeName;
+ int indexOfNs = typeBindingName.lastIndexOf(typeName);
+ if (indexOfNs > 0 && !importName.equalsIgnoreCase(typeName)) {
+ typeBindingName = typeBindingName.substring(0, indexOfNs);
+ if (typeBindingName.endsWith(NamespaceReference.NAMESPACE_DELIMITER)) {
+ typeBindingName = typeBindingName.substring(0, typeBindingName.length() - 1);
+ }
+ }
+ String lastSeg = typeBindingName;
+ String[] segs = typeBindingName.split("\\\\"); //$NON-NLS-1$
+ if (segs.length > 0) {
+ lastSeg = segs[segs.length - 1];
+ }
+ if (!lastSeg.equalsIgnoreCase(importName)) {
+ alias = importName;
}
}
fImpStructure.addImport(namespace, typeBindingName, alias);
- fImportsAdded.get(namespace).add(typeName);
+ fImportsAdded.get(namespace).add(importName);
return;
}
}
diff --git a/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement06.pdtt b/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement06.pdtt
new file mode 100644
index 000000000..d721cea07
--- /dev/null
+++ b/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement06.pdtt
@@ -0,0 +1,26 @@
+--TEST--
+Test for import namespace
+--FILE--
+<?php
+namespace name;
+
+use name\ns1\ns1;
+
+new ns1\Bar();
+
+namespace name\ns1\ns1;
+
+class Bar {
+}
+--EXPECT--
+<?php
+namespace name;
+
+use name\ns1\ns1;
+
+new ns1\Bar();
+
+namespace name\ns1\ns1;
+
+class Bar {
+} \ No newline at end of file
diff --git a/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement07.pdtt b/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement07.pdtt
new file mode 100644
index 000000000..0c35c82ff
--- /dev/null
+++ b/tests/org.eclipse.php.ui.tests/workspace/organize-imports/php53/organizeUseStatement07.pdtt
@@ -0,0 +1,42 @@
+--TEST--
+Test for import namespace as alias
+--FILE--
+<?php
+namespace name;
+
+use name\ns1 as Alias;
+use name\ns1\ns1 as Alias2;
+
+new alias\Foo();
+new alias\ns1\Bar();
+new alias2\Bar();
+
+namespace name\ns1;
+
+class Foo {
+}
+
+namespace name\ns1\ns1;
+
+class Bar {
+}
+--EXPECT--
+<?php
+namespace name;
+
+use name\ns1 as alias;
+use name\ns1\ns1 as alias2;
+
+new alias\Foo();
+new alias\ns1\Bar();
+new alias2\Bar();
+
+namespace name\ns1;
+
+class Foo {
+}
+
+namespace name\ns1\ns1;
+
+class Bar {
+} \ No newline at end of file

Back to the top