aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzzhongwei2013-01-14 05:15:30 (EST)
committerzzhongwei2013-01-14 05:19:30 (EST)
commit579518e5f1c4a7462093ed5c28f1862f60fac3ab (patch)
tree6d6713c689a790e985fa307004709cba6dc6e035
parent3b8f060d132f6e79a4972787991e453da2b1f53a (diff)
downloadorg.eclipse.pdt-579518e5f1c4a7462093ed5c28f1862f60fac3ab.zip
org.eclipse.pdt-579518e5f1c4a7462093ed5c28f1862f60fac3ab.tar.gz
org.eclipse.pdt-579518e5f1c4a7462093ed5c28f1862f60fac3ab.tar.bz2
381524: Incorrect resolution of namespaces in doc comments (BUG).
https://bugs.eclipse.org/bugs/show_bug.cgi?id=381524
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/contexts/NamespaceAbstractPHPDocTagStartContext.java109
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/NamespaceDocTypesStrategy.java27
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag10.pdtt42
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag11.pdtt42
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag12.pdtt42
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag13.pdtt42
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag14.pdtt42
-rw-r--r--tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag9.pdtt42
8 files changed, 374 insertions, 14 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/contexts/NamespaceAbstractPHPDocTagStartContext.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/contexts/NamespaceAbstractPHPDocTagStartContext.java
index 495e022..9f11ef5 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/contexts/NamespaceAbstractPHPDocTagStartContext.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/contexts/NamespaceAbstractPHPDocTagStartContext.java
@@ -14,11 +14,17 @@ import java.util.List;
import java.util.Stack;
import java.util.StringTokenizer;
+import org.eclipse.dltk.ast.Modifiers;
import org.eclipse.dltk.core.*;
+import org.eclipse.dltk.core.index2.search.ISearchEngine.MatchRule;
+import org.eclipse.dltk.core.search.IDLTKSearchScope;
+import org.eclipse.dltk.core.search.SearchEngine;
import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.php.core.compiler.PHPFlags;
import org.eclipse.php.internal.core.compiler.ast.nodes.NamespaceReference;
import org.eclipse.php.internal.core.documentModel.parser.regions.IPhpScriptRegion;
import org.eclipse.php.internal.core.documentModel.parser.regions.PHPRegionTypes;
+import org.eclipse.php.internal.core.model.PhpModelAccess;
import org.eclipse.php.internal.core.typeinference.PHPModelUtils;
import org.eclipse.php.internal.core.util.text.PHPTextSequenceUtilities;
import org.eclipse.php.internal.core.util.text.TextSequence;
@@ -36,6 +42,9 @@ public abstract class NamespaceAbstractPHPDocTagStartContext extends
private IType[] namespaces;
private boolean isGlobal;
+ private IType[] possibleNamespaces;
+ private IType currentNS;
+ private String nsPrefix;
public boolean isValid(ISourceModule sourceModule, int offset,
CompletionRequestor requestor) {
@@ -57,34 +66,94 @@ public abstract class NamespaceAbstractPHPDocTagStartContext extends
String lastWord = stack.pop();
if (lastWord.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) >= 0) {
if (!stack.empty() && isPrefix(lastWord)) {
+ if (lastWord.startsWith("\\")) {
+ // isGlobal = true;
+ }
if (lastWord.startsWith("\\")
&& lastWord
.lastIndexOf(NamespaceReference.NAMESPACE_SEPARATOR) == 0) {
isGlobal = true;
} else {
- String nsName = lastWord;
- nsName = nsName
- .substring(
- 0,
- nsName.lastIndexOf(NamespaceReference.NAMESPACE_SEPARATOR) + 1);
-
- try {
- namespaces = PHPModelUtils.getNamespaceOf(nsName,
- sourceModule, offset, null, null);
- } catch (ModelException e) {
- if (DLTKCore.DEBUG) {
- e.printStackTrace();
- }
- }
+ initRelativeNamespace(sourceModule, offset, lastWord);
}
lastWord = stack.pop();
return lastWord.endsWith(tagName);
}
+ } else {
+ initRelativeNamespace(sourceModule, offset, lastWord);
+ lastWord = stack.pop();
+ return lastWord.endsWith(tagName);
}
}
return false;
}
+ private void initRelativeNamespace(ISourceModule sourceModule, int offset,
+ String lastWord) {
+ String nsName = lastWord;
+ String fullName = lastWord;
+ nsPrefix = null;
+ if (lastWord.lastIndexOf(NamespaceReference.NAMESPACE_SEPARATOR) > 0) {
+ nsPrefix = lastWord.substring(0, lastWord
+ .lastIndexOf(NamespaceReference.NAMESPACE_SEPARATOR));
+ nsName = nsName
+ .substring(
+ 0,
+ nsName.lastIndexOf(NamespaceReference.NAMESPACE_SEPARATOR) + 1);
+
+ try {
+ namespaces = PHPModelUtils.getNamespaceOf(nsName, sourceModule,
+ offset, null, null);
+ } catch (ModelException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ }
+ } else {
+ namespaces = PhpModelAccess.NULL_TYPES;
+ }
+ if (lastWord.startsWith("\\")) {
+ nsPrefix = null;
+ } else {
+
+ currentNS = null;
+ try {
+ IType type = (IType) sourceModule.getElementAt(offset)
+ .getAncestor(IModelElement.TYPE);
+ if (type.getParent() instanceof IType) {
+ type = (IType) type.getParent();
+ }
+ if (type != null && (PHPFlags.isNamespace(type.getFlags()))) {
+ currentNS = type;
+ fullName = NamespaceReference.NAMESPACE_SEPARATOR
+ + currentNS.getElementName()
+ + NamespaceReference.NAMESPACE_SEPARATOR + lastWord;
+ } else {
+
+ }
+ } catch (ModelException e1) {
+ e1.printStackTrace();
+ }
+ if (currentNS != null) {
+ if (nsPrefix == null) {
+ nsPrefix = currentNS.getElementName();
+ } else {
+ nsPrefix = currentNS.getElementName()
+ + NamespaceReference.NAMESPACE_SEPARATOR + nsPrefix;
+ }
+ }
+ }
+
+ IDLTKSearchScope scope = SearchEngine.createSearchScope(sourceModule
+ .getScriptProject());
+ if (fullName.startsWith("\\")) {
+ fullName = fullName.substring(1);
+ }
+ possibleNamespaces = PhpModelAccess.getDefault().findTypes(null,
+ fullName, MatchRule.PREFIX, Modifiers.AccNameSpace, 0, scope,
+ null);
+ }
+
/**
* Returns the left hand side possible namespace elements.
*/
@@ -92,6 +161,18 @@ public abstract class NamespaceAbstractPHPDocTagStartContext extends
return namespaces;
}
+ public IType[] getPossibleNamespaces() {
+ return possibleNamespaces;
+ }
+
+ public IType getCurrentNS() {
+ return currentNS;
+ }
+
+ public String getNsPrefix() {
+ return nsPrefix;
+ }
+
/**
* Returns whether the namespace is global (only the '\' presents in the
* left side)
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/NamespaceDocTypesStrategy.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/NamespaceDocTypesStrategy.java
index eb46633..c8058f0 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/NamespaceDocTypesStrategy.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/NamespaceDocTypesStrategy.java
@@ -16,15 +16,18 @@ import java.util.List;
import org.eclipse.dltk.core.IType;
import org.eclipse.dltk.core.ModelException;
+import org.eclipse.dltk.internal.core.ModelElement;
import org.eclipse.dltk.internal.core.SourceRange;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.php.core.codeassist.ICompletionContext;
import org.eclipse.php.core.codeassist.IElementFilter;
import org.eclipse.php.internal.core.PHPCorePlugin;
+import org.eclipse.php.internal.core.codeassist.AliasType;
import org.eclipse.php.internal.core.codeassist.CodeAssistUtils;
import org.eclipse.php.internal.core.codeassist.ICompletionReporter;
import org.eclipse.php.internal.core.codeassist.ProposalExtraInfo;
import org.eclipse.php.internal.core.codeassist.contexts.NamespacePHPDocVarStartContext;
+import org.eclipse.php.internal.core.compiler.ast.nodes.NamespaceReference;
/**
* This strategy completes namespace classes and interfaces
@@ -75,9 +78,33 @@ public class NamespaceDocTypesStrategy extends AbstractCompletionStrategy {
PHPCorePlugin.log(e);
}
}
+ for (IType ns : context.getPossibleNamespaces()) {
+ if (context.getNsPrefix() == null) {
+ result.add(ns);
+ } else {
+ String fullName = ns.getElementName();
+ String alias = getAlias(ns, context.getNsPrefix());
+ if (alias == null) {
+ result.add(ns);
+ } else {
+ result.add(new AliasType((ModelElement) ns, fullName, alias));
+ }
+ }
+ }
return (IType[]) result.toArray(new IType[result.size()]);
}
+ private String getAlias(IType ns, String currentNSName) {
+ String result = ns.getElementName();
+ currentNSName = currentNSName + NamespaceReference.NAMESPACE_SEPARATOR;
+ if (result.startsWith(currentNSName)) {
+ result = result.substring(currentNSName.length());
+ } else {
+ result = null;
+ }
+ return result;
+ }
+
public SourceRange getReplacementRange(ICompletionContext context)
throws BadLocationException {
SourceRange replacementRange = super.getReplacementRange(context);
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag10.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag10.pdtt
new file mode 100644
index 0000000..9ed5dd5
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag10.pdtt
@@ -0,0 +1,42 @@
+--TEST--
+Test var
+--FILE--
+<?
+namespace foo\bar\baz1 {
+
+ class Target {
+ function foo_bar_baz();
+ }
+}
+
+namespace bar\baz {
+
+ class Target {
+ function bar_baz();
+ }
+}
+
+namespace foo {
+
+ class TestAbsolute {
+ /**
+ *
+ * @return \bar\baz\Target
+ *
+ */
+ function test() {
+ }
+ }
+ class TestRelative {
+ /**
+ *
+ * @return bar\b|
+ *
+ */
+ function test() {
+ }
+ }
+}
+?>
+--EXPECT--
+type(baz1)
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag11.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag11.pdtt
new file mode 100644
index 0000000..b56a75d
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag11.pdtt
@@ -0,0 +1,42 @@
+--TEST--
+Test var
+--FILE--
+<?
+namespace foo\bar\baz1 {
+
+ class Target {
+ function foo_bar_baz();
+ }
+}
+
+namespace bar\baz {
+
+ class Target {
+ function bar_baz();
+ }
+}
+
+namespace foo {
+
+ class TestAbsolute {
+ /**
+ *
+ * @return \bar\baz\Target
+ *
+ */
+ function test() {
+ }
+ }
+ class TestRelative {
+ /**
+ *
+ * @return bar\baz1\|
+ *
+ */
+ function test() {
+ }
+ }
+}
+?>
+--EXPECT--
+type(Target)
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag12.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag12.pdtt
new file mode 100644
index 0000000..84969f7
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag12.pdtt
@@ -0,0 +1,42 @@
+--TEST--
+Test var
+--FILE--
+<?
+namespace foo\bar\baz1 {
+
+ class Target {
+ function foo_bar_baz();
+ }
+}
+
+namespace bar\baz {
+
+ class Target {
+ function bar_baz();
+ }
+}
+
+namespace foo {
+
+ class TestAbsolute {
+ /**
+ *
+ * @return \bar|
+ *
+ */
+ function test() {
+ }
+ }
+ class TestRelative {
+ /**
+ *
+ * @return bar\baz1\
+ *
+ */
+ function test() {
+ }
+ }
+}
+?>
+--EXPECT--
+type(bar\baz)
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag13.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag13.pdtt
new file mode 100644
index 0000000..245c668
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag13.pdtt
@@ -0,0 +1,42 @@
+--TEST--
+Test var
+--FILE--
+<?
+namespace foo\bar\baz1 {
+
+ class Target {
+ function foo_bar_baz();
+ }
+}
+
+namespace bar\baz {
+
+ class Target {
+ function bar_baz();
+ }
+}
+
+namespace foo {
+
+ class TestAbsolute {
+ /**
+ *
+ * @return \bar\ba|
+ *
+ */
+ function test() {
+ }
+ }
+ class TestRelative {
+ /**
+ *
+ * @return bar\baz1\
+ *
+ */
+ function test() {
+ }
+ }
+}
+?>
+--EXPECT--
+type(bar\baz)
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag14.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag14.pdtt
new file mode 100644
index 0000000..d3bd49a
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag14.pdtt
@@ -0,0 +1,42 @@
+--TEST--
+Test var
+--FILE--
+<?
+namespace foo\bar\baz1 {
+
+ class Target {
+ function foo_bar_baz();
+ }
+}
+
+namespace bar\baz {
+
+ class Target {
+ function bar_baz();
+ }
+}
+
+namespace foo {
+
+ class TestAbsolute {
+ /**
+ *
+ * @return \bar\baz\|
+ *
+ */
+ function test() {
+ }
+ }
+ class TestRelative {
+ /**
+ *
+ * @return bar\baz1\
+ *
+ */
+ function test() {
+ }
+ }
+}
+?>
+--EXPECT--
+type(Target)
diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag9.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag9.pdtt
new file mode 100644
index 0000000..3c98a47
--- /dev/null
+++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/testDoctag9.pdtt
@@ -0,0 +1,42 @@
+--TEST--
+Test var
+--FILE--
+<?
+namespace foo\bar\baz1 {
+
+ class Target {
+ function foo_bar_baz();
+ }
+}
+
+namespace bar\baz {
+
+ class Target {
+ function bar_baz();
+ }
+}
+
+namespace foo {
+
+ class TestAbsolute {
+ /**
+ *
+ * @return \bar\baz\Target
+ *
+ */
+ function test() {
+ }
+ }
+ class TestRelative {
+ /**
+ *
+ * @return bar|
+ *
+ */
+ function test() {
+ }
+ }
+}
+?>
+--EXPECT--
+type(bar\baz1)