Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThierry BLIND2019-06-19 10:26:45 -0400
committerThierry BLIND2019-06-19 10:26:45 -0400
commit71429455a0422a57f7265e9e35c9067c38d8fb7f (patch)
treec17b87842f58b4fe52e5e5c82692368fe91971ff
parent97167349f48218195da5ce8ef611fd2971ed8cac (diff)
downloadorg.eclipse.pdt-71429455a0422a57f7265e9e35c9067c38d8fb7f.tar.gz
org.eclipse.pdt-71429455a0422a57f7265e9e35c9067c38d8fb7f.tar.xz
org.eclipse.pdt-71429455a0422a57f7265e9e35c9067c38d8fb7f.zip
Bug 535805 - "Remove unused use statement" doesn't work with grouped
import Change-Id: I5b1819256dd8a744941bb11b0c6dad200839c3a2 Signed-off-by: Thierry BLIND <thierryblind@msn.com>
-rw-r--r--plugins/org.eclipse.php.core/Resources/parserTools/parser/php53/php_ast_parser.cup6
-rw-r--r--plugins/org.eclipse.php.core/Resources/parserTools/parser/php54/php_ast_parser.cup6
-rw-r--r--plugins/org.eclipse.php.core/Resources/parserTools/parser/php55/php_ast_parser.cup6
-rw-r--r--plugins/org.eclipse.php.core/Resources/parserTools/parser/php56/php_ast_parser.cup6
-rw-r--r--plugins/org.eclipse.php.core/Resources/parserTools/parser/php7/php_ast_parser.cup7
-rw-r--r--plugins/org.eclipse.php.core/Resources/parserTools/parser/php71/php_ast_parser.cup7
-rw-r--r--plugins/org.eclipse.php.core/Resources/parserTools/parser/php72/php_ast_parser.cup7
-rw-r--r--plugins/org.eclipse.php.core/Resources/parserTools/parser/php73/php_ast_parser.cup7
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/UseStatementPart.java16
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/core/compiler/ast/nodes/UsePart.java16
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ASTRewriteAnalyzer.java2
-rw-r--r--plugins/org.eclipse.php.formatter.core/src/org/eclipse/php/formatter/core/CodeFormatterVisitor.java1
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/quickfix/UnusedUseStatementProcessor.java3
-rw-r--r--plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/text/correction/proposals/RemoveUnusedUseStatementProposal.java66
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobal1.pdtt2
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobalSubNamespace1.pdtt2
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03.pdtt4
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03B.pdtt4
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01.pdtt4
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01B.pdtt4
20 files changed, 90 insertions, 86 deletions
diff --git a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php53/php_ast_parser.cup b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php53/php_ast_parser.cup
index 34f85a80c..8963c25d3 100644
--- a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php53/php_ast_parser.cup
+++ b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php53/php_ast_parser.cup
@@ -488,15 +488,13 @@ namespace_name:list
| T_NS_SEPARATOR:s namespace_name:list
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, listright, parser.ast,
+ RESULT = new UseStatementPart(sleft, listright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false), null);
:}
| T_NS_SEPARATOR:s namespace_name:list T_AS T_STRING:aliasName
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, aliasNameright, parser.ast,
+ RESULT = new UseStatementPart(sleft, aliasNameright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false),
new Identifier(aliasNameleft, aliasNameright, parser.ast, aliasName));
:}
diff --git a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php54/php_ast_parser.cup b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php54/php_ast_parser.cup
index 487e231f3..aecf1bc29 100644
--- a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php54/php_ast_parser.cup
+++ b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php54/php_ast_parser.cup
@@ -513,15 +513,13 @@ namespace_name:list
| T_NS_SEPARATOR:s namespace_name:list
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, listright, parser.ast,
+ RESULT = new UseStatementPart(sleft, listright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false), null);
:}
| T_NS_SEPARATOR:s namespace_name:list T_AS T_STRING:aliasName
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, aliasNameright, parser.ast,
+ RESULT = new UseStatementPart(sleft, aliasNameright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false),
new Identifier(aliasNameleft, aliasNameright, parser.ast, aliasName));
:}
diff --git a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php55/php_ast_parser.cup b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php55/php_ast_parser.cup
index ae660e074..9fbcb8ab9 100644
--- a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php55/php_ast_parser.cup
+++ b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php55/php_ast_parser.cup
@@ -534,15 +534,13 @@ namespace_name:list
| T_NS_SEPARATOR:s namespace_name:list
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, listright, parser.ast,
+ RESULT = new UseStatementPart(sleft, listright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false), null);
:}
| T_NS_SEPARATOR:s namespace_name:list T_AS T_STRING:aliasName
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, aliasNameright, parser.ast,
+ RESULT = new UseStatementPart(sleft, aliasNameright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false),
new Identifier(aliasNameleft, aliasNameright, parser.ast, aliasName));
:}
diff --git a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php56/php_ast_parser.cup b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php56/php_ast_parser.cup
index 78c369787..6a7dd3bfc 100644
--- a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php56/php_ast_parser.cup
+++ b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php56/php_ast_parser.cup
@@ -567,15 +567,13 @@ namespace_name:list
| T_NS_SEPARATOR:s namespace_name:list
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, listright, parser.ast,
+ RESULT = new UseStatementPart(sleft, listright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false), null);
:}
| T_NS_SEPARATOR:s namespace_name:list T_AS T_STRING:aliasName
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, aliasNameright, parser.ast,
+ RESULT = new UseStatementPart(sleft, aliasNameright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false),
new Identifier(aliasNameleft, aliasNameright, parser.ast, aliasName));
:}
diff --git a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php7/php_ast_parser.cup b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php7/php_ast_parser.cup
index 44ef7da2f..908af1d9b 100644
--- a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php7/php_ast_parser.cup
+++ b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php7/php_ast_parser.cup
@@ -852,6 +852,7 @@ use_declaration:useDeclaration
| use_type:useType use_declaration:useDeclaration
{:
+ useDeclaration.setSourceRange(useTypeleft, useDeclaration.getEnd() - useTypeleft);
useDeclaration.setStatementType(useType);
RESULT = useDeclaration;
:}
@@ -888,15 +889,13 @@ namespace_name:list
| T_NS_SEPARATOR:s namespace_name:list
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, listright, parser.ast,
+ RESULT = new UseStatementPart(sleft, listright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false), null);
:}
| T_NS_SEPARATOR:s namespace_name:list T_AS T_STRING:aliasName
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, aliasNameright, parser.ast,
+ RESULT = new UseStatementPart(sleft, aliasNameright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false),
new Identifier(aliasNameleft, aliasNameright, parser.ast, aliasName));
:}
diff --git a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php71/php_ast_parser.cup b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php71/php_ast_parser.cup
index 80e2829be..56854de28 100644
--- a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php71/php_ast_parser.cup
+++ b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php71/php_ast_parser.cup
@@ -851,6 +851,7 @@ use_declaration:useDeclaration
| use_type:useType use_declaration:useDeclaration
{:
+ useDeclaration.setSourceRange(useTypeleft, useDeclaration.getEnd() - useTypeleft);
useDeclaration.setStatementType(useType);
RESULT = useDeclaration;
:}
@@ -887,15 +888,13 @@ namespace_name:list
| T_NS_SEPARATOR:s namespace_name:list
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, listright, parser.ast,
+ RESULT = new UseStatementPart(sleft, listright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false), null);
:}
| T_NS_SEPARATOR:s namespace_name:list T_AS T_STRING:aliasName
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, aliasNameright, parser.ast,
+ RESULT = new UseStatementPart(sleft, aliasNameright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false),
new Identifier(aliasNameleft, aliasNameright, parser.ast, aliasName));
:}
diff --git a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php72/php_ast_parser.cup b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php72/php_ast_parser.cup
index 9bf513920..c7542145c 100644
--- a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php72/php_ast_parser.cup
+++ b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php72/php_ast_parser.cup
@@ -860,6 +860,7 @@ use_declaration:useDeclaration
| use_type:useType use_declaration:useDeclaration
{:
+ useDeclaration.setSourceRange(useTypeleft, useDeclaration.getEnd() - useTypeleft);
useDeclaration.setStatementType(useType);
RESULT = useDeclaration;
:}
@@ -896,15 +897,13 @@ namespace_name:list
| T_NS_SEPARATOR:s namespace_name:list
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, listright, parser.ast,
+ RESULT = new UseStatementPart(sleft, listright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false), null);
:}
| T_NS_SEPARATOR:s namespace_name:list T_AS T_STRING:aliasName
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, aliasNameright, parser.ast,
+ RESULT = new UseStatementPart(sleft, aliasNameright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false),
new Identifier(aliasNameleft, aliasNameright, parser.ast, aliasName));
:}
diff --git a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php73/php_ast_parser.cup b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php73/php_ast_parser.cup
index 01a32c499..2537e374d 100644
--- a/plugins/org.eclipse.php.core/Resources/parserTools/parser/php73/php_ast_parser.cup
+++ b/plugins/org.eclipse.php.core/Resources/parserTools/parser/php73/php_ast_parser.cup
@@ -860,6 +860,7 @@ use_declaration:useDeclaration
| use_type:useType use_declaration:useDeclaration
{:
+ useDeclaration.setSourceRange(useTypeleft, useDeclaration.getEnd() - useTypeleft);
useDeclaration.setStatementType(useType);
RESULT = useDeclaration;
:}
@@ -896,15 +897,13 @@ namespace_name:list
| T_NS_SEPARATOR:s namespace_name:list
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, listright, parser.ast,
+ RESULT = new UseStatementPart(sleft, listright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false), null);
:}
| T_NS_SEPARATOR:s namespace_name:list T_AS T_STRING:aliasName
{:
- // NB: leading backslash is optional and has no special meaning here, so totally ignore it
- RESULT = new UseStatementPart(sright, aliasNameright, parser.ast,
+ RESULT = new UseStatementPart(sleft, aliasNameright, parser.ast,
new NamespaceName(sright, listright, parser.ast, list, false, false),
new Identifier(aliasNameleft, aliasNameright, parser.ast, aliasName));
:}
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/UseStatementPart.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/UseStatementPart.java
index 1fe2cb464..b5185129e 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/UseStatementPart.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/core/ast/nodes/UseStatementPart.java
@@ -224,6 +224,14 @@ public class UseStatementPart extends ASTNode {
postValueChange(STATEMENT_TYPE_PROPERTY);
}
+ /**
+ * <b>WARNING:</b> returns "fully computated" use statement type, based on
+ * parent's UseStatement#getStatementType() value and current element type
+ * value. Also note that (raw) current element type is not stored for now,
+ * but can be easily recalculated.
+ *
+ * @see UseStatement#getStatementType()
+ */
public int getStatementType() {
return statementType;
}
@@ -289,10 +297,10 @@ public class UseStatementPart extends ASTNode {
/**
* Returns the fully qualified name (without leading '\') that is the
- * concatenation of the group use statement name (if any) and this use statement
- * part name. Supports normal use statements and grouped use statements.
- * <b>Returned name will not be null, will not be empty and will have no leading
- * '\'.</b>
+ * concatenation of the group use statement name (if any) and this use
+ * statement part name. Supports normal use statements and grouped use
+ * statements. <b>Returned name will not be null, will not be empty and will
+ * have no leading '\'.</b>
*
* @return full use statement name
*/
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/core/compiler/ast/nodes/UsePart.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/core/compiler/ast/nodes/UsePart.java
index a84575f78..669cfa8db 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/core/compiler/ast/nodes/UsePart.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/core/compiler/ast/nodes/UsePart.java
@@ -111,16 +111,24 @@ public class UsePart extends ASTNode {
this.statementType = statementType;
}
+ /**
+ * <b>WARNING:</b> returns "fully computated" use statement type, based on
+ * parent's UseStatement#getStatementType() value and current element type
+ * value. Also note that (raw) current element type is not stored for now,
+ * but can be easily recalculated.
+ *
+ * @see UseStatement#getStatementType()
+ */
public int getStatementType() {
return statementType;
}
/**
* Returns the fully qualified name (without leading '\') that is the
- * concatenation of the group use statement name (if any) and this use statement
- * part name. Supports normal use statements and grouped use statements.
- * <b>Returned name will not be null, will not be empty and will have no leading
- * '\'.</b>
+ * concatenation of the group use statement name (if any) and this use
+ * statement part name. Supports normal use statements and grouped use
+ * statements. <b>Returned name will not be null, will not be empty and will
+ * have no leading '\'.</b>
*
* @return full use statement name
*/
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ASTRewriteAnalyzer.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ASTRewriteAnalyzer.java
index aa639d62c..251f88dcd 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ASTRewriteAnalyzer.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/ast/rewrite/ASTRewriteAnalyzer.java
@@ -3785,7 +3785,7 @@ public final class ASTRewriteAnalyzer extends AbstractVisitor {
} else if (useStatementPart.getStatementType() == UseStatement.T_CONST) {
insertString += "const "; //$NON-NLS-1$
}
- int length = useStatementPart.getStart() - start;
+ int length = useStatementPart.getName().getStart() - start;
doTextReplace(start, length, insertString, getEditGroup(event));
break;
}
diff --git a/plugins/org.eclipse.php.formatter.core/src/org/eclipse/php/formatter/core/CodeFormatterVisitor.java b/plugins/org.eclipse.php.formatter.core/src/org/eclipse/php/formatter/core/CodeFormatterVisitor.java
index 8a4c4d580..cc4b83e47 100644
--- a/plugins/org.eclipse.php.formatter.core/src/org/eclipse/php/formatter/core/CodeFormatterVisitor.java
+++ b/plugins/org.eclipse.php.formatter.core/src/org/eclipse/php/formatter/core/CodeFormatterVisitor.java
@@ -5416,6 +5416,7 @@ public class CodeFormatterVisitor extends AbstractVisitor implements ICodeFormat
@Override
public boolean visit(UseStatementPart useStatementPart) {
appendStatementType(useStatementPart.getStatementType());
+ handleChars(useStatementPart.getStart(), useStatementPart.getName().getStart());
useStatementPart.getName().accept(this);
Identifier alias = useStatementPart.getAlias();
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/quickfix/UnusedUseStatementProcessor.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/quickfix/UnusedUseStatementProcessor.java
index 284947326..6d9aadbe4 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/quickfix/UnusedUseStatementProcessor.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/quickfix/UnusedUseStatementProcessor.java
@@ -82,7 +82,8 @@ public class UnusedUseStatementProcessor implements IQuickFixProcessor {
}
if (coveringNode instanceof UseStatement) {
rewrite.remove(coveringNode, editGroup);
- } else if (coveringNode instanceof UseStatementPart) {
+ } else if (coveringNode instanceof UseStatementPart
+ && coveringNode.getParent() instanceof UseStatement) {
UseStatement useStatement = (UseStatement) coveringNode.getParent();
if (useStatement.parts().size() == 1) {
rewrite.remove(useStatement, editGroup);
diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/text/correction/proposals/RemoveUnusedUseStatementProposal.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/text/correction/proposals/RemoveUnusedUseStatementProposal.java
index be8b697af..6b41ee1ec 100644
--- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/text/correction/proposals/RemoveUnusedUseStatementProposal.java
+++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/text/correction/proposals/RemoveUnusedUseStatementProposal.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2017 Alex Xu and others.
+ * Copyright (c) 2017, 2019 Alex Xu and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
@@ -14,12 +14,15 @@ package org.eclipse.php.internal.ui.text.correction.proposals;
import java.util.List;
+import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.dltk.ui.DLTKPluginImages;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
-import org.eclipse.php.core.ast.nodes.*;
-import org.eclipse.php.core.compiler.ast.nodes.NamespaceReference;
+import org.eclipse.php.core.ast.nodes.ASTNode;
+import org.eclipse.php.core.ast.nodes.Program;
+import org.eclipse.php.core.ast.nodes.UseStatement;
+import org.eclipse.php.core.ast.nodes.UseStatementPart;
import org.eclipse.php.internal.core.compiler.ast.parser.PHPProblemIdentifier;
import org.eclipse.php.internal.ui.PHPUiPlugin;
import org.eclipse.php.internal.ui.text.correction.CorrectionMessages;
@@ -74,32 +77,42 @@ public class RemoveUnusedUseStatementProposal extends CUCorrectionProposal {
}
ASTNode coveredNode = fLocation.getCoveredNode(astRoot);
- ASTNode current = coveredNode;
+ UseStatementPart currentUseStatementPart = null;
while (coveredNode != null && !(coveredNode instanceof UseStatement)) {
+ if (coveredNode instanceof UseStatementPart) {
+ currentUseStatementPart = (UseStatementPart) coveredNode;
+ }
coveredNode = coveredNode.getParent();
}
- if (coveredNode == null) {
+ if (coveredNode == null || currentUseStatementPart == null) {
return;
}
- UseStatement use = (UseStatement) coveredNode;
- List<UseStatementPart> parts = use.parts();
+ UseStatement currentUseStatement = (UseStatement) coveredNode;
+ List<UseStatementPart> parts = currentUseStatement.parts();
+ // Remove group use statements with only a use part or remove single
+ // use statements:
if (parts.size() == 1) {
- int line = doc.getLineOfOffset(coveredNode.getStart());
- int lineStart = doc.getLineOffset(line);
- int length = doc.getLineLength(line);
- root.addChild(new DeleteEdit(lineStart, length));
+ int currentStartOffset = coveredNode.getStart();
+ int currentEndOffset = coveredNode.getEnd();
+ int lineStart = doc.getLineOfOffset(currentStartOffset);
+ int lineEnd = doc.getLineOfOffset(currentEndOffset);
+ int lineStartOffset = doc.getLineOffset(lineStart);
+ // End offset *after* the line's delimiter:
+ int lineEndOffset = doc.getLineOffset(lineEnd) + doc.getLineLength(lineEnd);
+ if (currentStartOffset > lineStartOffset
+ && StringUtils.isBlank(doc.get(lineStartOffset, currentStartOffset - lineStartOffset))) {
+ currentStartOffset = lineStartOffset;
+ }
+ if (currentEndOffset < lineEndOffset
+ && StringUtils.isBlank(doc.get(currentEndOffset, lineEndOffset - currentEndOffset))) {
+ currentEndOffset = lineEndOffset;
+ }
+ root.addChild(new DeleteEdit(currentStartOffset, currentEndOffset - currentStartOffset));
return;
}
- String currentNamespace = null;
- if (current instanceof NamespaceName) {
- currentNamespace = getNamespaceName((NamespaceName) current);
- } else {
- currentNamespace = ((Identifier) current).getName();
- }
int index = 0;
for (UseStatementPart part : parts) {
- String namespace = part.getFullUseStatementName();
- if (currentNamespace.equals(namespace)) {
+ if (part == currentUseStatementPart) {
int start = part.getStart();
int length = 0;
if (index == 0) {
@@ -121,19 +134,4 @@ public class RemoveUnusedUseStatementProposal extends CUCorrectionProposal {
}
}
- private static String getNamespaceName(NamespaceName namespace) {
- StringBuilder namespaces = new StringBuilder(""); //$NON-NLS-1$
- List<Identifier> segments = namespace.segments();
- int idx = 0;
- for (Identifier segment : segments) {
- if (idx == 0) {
- namespaces.append(segment.getName());
- } else {
- namespaces.append(NamespaceReference.NAMESPACE_DELIMITER).append(segment.getName());
- }
- idx++;
- }
- return namespaces.toString();
- }
-
} \ No newline at end of file
diff --git a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobal1.pdtt b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobal1.pdtt
index ddd2a939a..e0b87b3a3 100644
--- a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobal1.pdtt
+++ b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobal1.pdtt
@@ -6,7 +6,7 @@ Test Use Global
<Program start='0' length='16'>
<Statements>
<UseStatement start='6' length='7'>
- <UseStatementPart start='11' length='1'>
+ <UseStatementPart start='10' length='2'>
<Name>
<NamespaceName start='11' length='1' global='false' current='false'>
<Identifier start='11' length='1' name='A'/>
diff --git a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobalSubNamespace1.pdtt b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobalSubNamespace1.pdtt
index 41d7b8d71..ff23ad7ae 100644
--- a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobalSubNamespace1.pdtt
+++ b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php53/testUseGlobalSubNamespace1.pdtt
@@ -6,7 +6,7 @@ Test Use Global Sub Namespace
<Program start='0' length='20'>
<Statements>
<UseStatement start='6' length='11'>
- <UseStatementPart start='11' length='5'>
+ <UseStatementPart start='10' length='6'>
<Name>
<NamespaceName start='11' length='5' global='false' current='false'>
<Identifier start='11' length='1' name='A'/>
diff --git a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03.pdtt b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03.pdtt
index 1b7572924..ca4d3d32d 100644
--- a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03.pdtt
+++ b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03.pdtt
@@ -17,14 +17,14 @@ Test group use statement
</NamespaceName>
</Name>
</UseStatementPart>
- <UseStatementPart start='34' length='2' statementType='2'>
+ <UseStatementPart start='28' length='8' statementType='2'>
<Name>
<NamespaceName start='34' length='2' global='false' current='false'>
<Identifier start='34' length='2' name='PI'/>
</NamespaceName>
</Name>
</UseStatementPart>
- <UseStatementPart start='47' length='14' statementType='1'>
+ <UseStatementPart start='38' length='23' statementType='1'>
<Name>
<NamespaceName start='47' length='3' global='false' current='false'>
<Identifier start='47' length='3' name='sin'/>
diff --git a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03B.pdtt b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03B.pdtt
index 2527d42af..b40212a4d 100644
--- a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03B.pdtt
+++ b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php7/groupUseStatement03B.pdtt
@@ -17,14 +17,14 @@ Test group use statement
</NamespaceName>
</Name>
</UseStatementPart>
- <UseStatementPart start='35' length='2' statementType='2'>
+ <UseStatementPart start='29' length='8' statementType='2'>
<Name>
<NamespaceName start='35' length='2' global='false' current='false'>
<Identifier start='35' length='2' name='PI'/>
</NamespaceName>
</Name>
</UseStatementPart>
- <UseStatementPart start='48' length='14' statementType='1'>
+ <UseStatementPart start='39' length='23' statementType='1'>
<Name>
<NamespaceName start='48' length='3' global='false' current='false'>
<Identifier start='48' length='3' name='sin'/>
diff --git a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01.pdtt b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01.pdtt
index 88868f044..e57680d2f 100644
--- a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01.pdtt
+++ b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01.pdtt
@@ -52,14 +52,14 @@ isset($a, $b, $c, );
<Identifier start='92' length='3' name='Foo'/>
<Identifier start='96' length='3' name='Bar'/>
</NamespaceName>
- <UseStatementPart start='111' length='3' statementType='1'>
+ <UseStatementPart start='102' length='12' statementType='1'>
<Name>
<NamespaceName start='111' length='3' global='false' current='false'>
<Identifier start='111' length='3' name='Foo'/>
</NamespaceName>
</Name>
</UseStatementPart>
- <UseStatementPart start='122' length='3' statementType='2'>
+ <UseStatementPart start='116' length='9' statementType='2'>
<Name>
<NamespaceName start='122' length='3' global='false' current='false'>
<Identifier start='122' length='3' name='Bar'/>
diff --git a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01B.pdtt b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01B.pdtt
index 717f29021..394a732ed 100644
--- a/tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01B.pdtt
+++ b/tests/org.eclipse.php.core.tests/workspace/dom_parser/php72/trailingComma01B.pdtt
@@ -52,14 +52,14 @@ isset($a, $b, $c, );
<Identifier start='94' length='3' name='Foo'/>
<Identifier start='98' length='3' name='Bar'/>
</NamespaceName>
- <UseStatementPart start='113' length='3' statementType='1'>
+ <UseStatementPart start='104' length='12' statementType='1'>
<Name>
<NamespaceName start='113' length='3' global='false' current='false'>
<Identifier start='113' length='3' name='Foo'/>
</NamespaceName>
</Name>
</UseStatementPart>
- <UseStatementPart start='124' length='3' statementType='2'>
+ <UseStatementPart start='118' length='9' statementType='2'>
<Name>
<NamespaceName start='124' length='3' global='false' current='false'>
<Identifier start='124' length='3' name='Bar'/>

Back to the top