### Eclipse Workspace Patch 1.0 #P org.eclipse.cdt.core.tests Index: parser/org/eclipse/cdt/core/parser/tests/prefix/BasicCompletionTest.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/prefix/BasicCompletionTest.java,v retrieving revision 1.7 diff -u -r1.7 BasicCompletionTest.java --- parser/org/eclipse/cdt/core/parser/tests/prefix/BasicCompletionTest.java 29 Jan 2007 10:27:40 -0000 1.7 +++ parser/org/eclipse/cdt/core/parser/tests/prefix/BasicCompletionTest.java 7 Feb 2007 20:20:50 -0000 @@ -23,7 +23,8 @@ private void testVar(ASTCompletionNode node) throws Exception { IASTName[] names = node.getNames(); assertEquals(1, names.length); - IBinding[] bindings = names[0].resolvePrefix(); + IBinding[] bindings = names[0].getCompletionContext().findBindings( + names[0], true); assertEquals(1, bindings.length); IVariable var = (IVariable)bindings[0]; assertEquals("blah", var.getName()); @@ -48,7 +49,8 @@ // There are three names, one as an expression, one that isn't connected, one as a declaration assertEquals(3, names.length); // The expression points to our functions - IBinding[] bindings = names[0].resolvePrefix(); + IBinding[] bindings = names[0].getCompletionContext().findBindings( + names[0], true); // There should be two since they both start with fu assertEquals(2, bindings.length); assertEquals("func", ((IFunction)bindings[0]).getName()); @@ -64,7 +66,8 @@ // There are two names, one as an expression, one as a declaration assertEquals(2, names.length); // The expression points to our functions - bindings = sortBindings(names[0].resolvePrefix()); + bindings = sortBindings(names[0].getCompletionContext().findBindings( + names[0], true)); // There should be two since they both start with fu assertEquals(2, bindings.length); assertEquals("func", ((IFunction)bindings[0]).getName()); @@ -83,7 +86,8 @@ IASTName[] names = node.getNames(); assertEquals(2, names.length); assertNull(names[0].getTranslationUnit()); - IBinding[] bindings = names[1].resolvePrefix(); + IBinding[] bindings = names[1].getCompletionContext().findBindings( + names[1], true); assertEquals(1, bindings.length); assertEquals("blah", ((ITypedef)bindings[0]).getName()); @@ -91,7 +95,7 @@ node = getGCCCompletionNode(code.toString()); names = node.getNames(); assertEquals(1, names.length); - bindings = names[0].resolvePrefix(); + bindings = names[0].getCompletionContext().findBindings(names[0], true); assertEquals(1, bindings.length); assertEquals("blah", ((ITypedef)bindings[0]).getName()); } Index: parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java,v retrieving revision 1.173 diff -u -r1.173 AST2CPPTests.java --- parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java 29 Sep 2006 07:19:50 -0000 1.173 +++ parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java 7 Feb 2007 20:20:50 -0000 @@ -3811,7 +3811,7 @@ tu.accept(col); IASTName f = col.getName(5); - f.resolvePrefix(); + f.getCompletionContext().findBindings(f, true); } public void testBug90654_1() throws Exception { @@ -3971,7 +3971,8 @@ IVariable a1 = (IVariable) col.getName(1).resolveBinding(); IVariable a2 = (IVariable) col.getName(2).resolveBinding(); - IBinding[] bs = col.getName(3).resolvePrefix(); + IBinding[] bs = col.getName(3).getCompletionContext().findBindings( + col.getName(3), true); assertEquals(bs.length, 2); assertSame(bs[0], a1); assertSame(bs[1], a2); #P org.eclipse.cdt.core Index: parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTAdapter.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTAdapter.java,v retrieving revision 1.1 diff -u -r1.1 PDOMASTAdapter.java --- parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTAdapter.java 7 Feb 2007 12:40:31 -0000 1.1 +++ parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTAdapter.java 7 Feb 2007 20:20:51 -0000 @@ -15,6 +15,7 @@ import org.eclipse.cdt.core.dom.ast.ASTNodeProperty; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; @@ -129,8 +130,8 @@ return fDelegate.resolveBinding(); } - public IBinding[] resolvePrefix() { - return fDelegate.resolvePrefix(); + public IASTCompletionContext getCompletionContext() { + return fDelegate.getCompletionContext(); } public void setBinding(IBinding binding) { Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTUsingDirective.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTUsingDirective.java,v retrieving revision 1.7 diff -u -r1.7 CPPASTUsingDirective.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTUsingDirective.java 1 Feb 2007 14:20:40 -0000 1.7 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTUsingDirective.java 7 Feb 2007 20:20:51 -0000 @@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; @@ -25,8 +26,8 @@ import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IndexFilter; import org.eclipse.cdt.core.parser.util.CharArrayUtils; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; /** * @author jcamelon @@ -80,7 +81,7 @@ return r_unclear; } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { List filtered = new ArrayList(); IndexFilter filter = new IndexFilter() { public boolean acceptBinding(IBinding binding) { @@ -97,7 +98,7 @@ if (decls[i] instanceof ICPPASTNamespaceDefinition) { ICPPASTNamespaceDefinition defn = (ICPPASTNamespaceDefinition) decls[i]; IASTName name = defn.getName(); - if (CharArrayUtils.equals(name.toCharArray(), 0, nChars.length, nChars, false)) { + if (nameMatches(name.toCharArray(), nChars, isPrefix)) { IBinding binding = name.resolveBinding(); if (filter.acceptBinding(binding)) { filtered.add(binding); @@ -110,7 +111,9 @@ if (index != null) { try { - IBinding[] bindings = index.findBindingsForPrefix(n.toCharArray(), filter); + IBinding[] bindings = isPrefix ? + index.findBindingsForPrefix(n.toCharArray(), filter) : + index.findBindings(n.toCharArray(), filter, new NullProgressMonitor()); for (int i = 0; i < bindings.length; i++) { filtered.add(bindings[i]); } @@ -120,4 +123,12 @@ return (IBinding[]) filtered.toArray(new IBinding[filtered.size()]); } + + private boolean nameMatches(char[] potential, char[] name, boolean isPrefix) { + if (isPrefix) { + return CharArrayUtils.equals(potential, 0, name.length, name, false); + } else { + return CharArrayUtils.equals(potential, name); + } + } } Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTFieldReference.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTFieldReference.java,v retrieving revision 1.10 diff -u -r1.10 CPPASTFieldReference.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTFieldReference.java 7 Feb 2007 11:14:22 -0000 1.10 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTFieldReference.java 7 Feb 2007 20:20:51 -0000 @@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; @@ -29,7 +30,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; /** * @author jcamelon @@ -115,7 +115,7 @@ return CPPVisitor.getExpressionType(this); } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { IASTExpression expression = getFieldOwner(); IType type = expression.getExpressionType(); type = CPPSemantics.getUltimateType(type, true); //stop at pointer to member? @@ -130,7 +130,7 @@ if (fields != null) { for (int i = 0; i < fields.length; i++) { char[] potential = fields[i].getNameCharArray(); - if (CharArrayUtils.equals(potential, 0, name.length, name, false)) { + if (nameMatches(potential, name, isPrefix)) { bindings.add(fields[i]); } } @@ -144,7 +144,7 @@ for (int i = 0; i < methods.length; i++) { if (!(methods[i] instanceof ICPPConstructor) && !methods[i].isImplicit()) { char[] potential = methods[i].getNameCharArray(); - if (CharArrayUtils.equals(potential, 0, name.length, name, false)) { + if (nameMatches(potential, name, isPrefix)) { bindings.add(methods[i]); } } @@ -153,16 +153,15 @@ } catch (DOMException e) { } - collectBases(classType, bindings, n.toCharArray()); + collectBases(classType, bindings, n.toCharArray(), isPrefix); return (IBinding[]) bindings.toArray(new IBinding[bindings.size()]); } return null; } - private void collectBases(ICPPClassType classType, List bindings, char[] prefix) { - if (CharArrayUtils.equals(classType.getNameCharArray(), - 0, prefix.length, prefix, false)) { + private void collectBases(ICPPClassType classType, List bindings, char[] name, boolean isPrefix) { + if (nameMatches(classType.getNameCharArray(), name, isPrefix)) { bindings.add(classType); } @@ -172,10 +171,18 @@ IBinding base = bases[i].getBaseClass(); if (base instanceof ICPPClassType) { ICPPClassType baseClass = (ICPPClassType) base; - collectBases(baseClass, bindings, prefix); + collectBases(baseClass, bindings, name, isPrefix); } } } catch (DOMException e) { } } + + private boolean nameMatches(char[] potential, char[] name, boolean isPrefix) { + if (isPrefix) { + return CharArrayUtils.equals(potential, 0, name.length, name, false); + } else { + return CharArrayUtils.equals(potential, name); + } + } } Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTUsingDeclaration.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTUsingDeclaration.java,v retrieving revision 1.7 diff -u -r1.7 CPPASTUsingDeclaration.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTUsingDeclaration.java 1 Feb 2007 14:20:40 -0000 1.7 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTUsingDeclaration.java 7 Feb 2007 20:20:51 -0000 @@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; @@ -25,8 +26,8 @@ import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IndexFilter; import org.eclipse.cdt.core.parser.util.CharArrayUtils; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; /** * @author jcamelon @@ -95,7 +96,7 @@ return r_unclear; } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { List filtered = new ArrayList(); IndexFilter filter = new IndexFilter() { public boolean acceptBinding(IBinding binding) { @@ -112,7 +113,7 @@ if (decls[i] instanceof ICPPASTNamespaceDefinition) { ICPPASTNamespaceDefinition defn = (ICPPASTNamespaceDefinition) decls[i]; IASTName name = defn.getName(); - if (CharArrayUtils.equals(name.toCharArray(), 0, nChars.length, nChars, false)) { + if (nameMatches(name.toCharArray(), nChars, isPrefix)) { IBinding binding = name.resolveBinding(); if (filter.acceptBinding(binding)) { filtered.add(binding); @@ -125,7 +126,9 @@ if (index != null) { try { - IBinding[] bindings = index.findBindingsForPrefix(n.toCharArray(), filter); + IBinding[] bindings = isPrefix ? + index.findBindingsForPrefix(n.toCharArray(), filter) : + index.findBindings(n.toCharArray(), filter, new NullProgressMonitor()); for (int i = 0; i < bindings.length; i++) { filtered.add(bindings[i]); } @@ -135,4 +138,12 @@ return (IBinding[]) filtered.toArray(new IBinding[filtered.size()]); } + + private boolean nameMatches(char[] potential, char[] name, boolean isPrefix) { + if (isPrefix) { + return CharArrayUtils.equals(potential, 0, name.length, name, false); + } else { + return CharArrayUtils.equals(potential, name); + } + } } Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTNamedTypeSpecifier.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTNamedTypeSpecifier.java,v retrieving revision 1.8 diff -u -r1.8 CPPASTNamedTypeSpecifier.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTNamedTypeSpecifier.java 1 Feb 2007 14:20:40 -0000 1.8 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTNamedTypeSpecifier.java 7 Feb 2007 20:20:51 -0000 @@ -17,6 +17,7 @@ import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IEnumeration; @@ -28,8 +29,8 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter; import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IndexFilter; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; /** * @author jcamelon @@ -97,14 +98,14 @@ return r_unclear; } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { List filtered = new ArrayList(); IScope scope = CPPVisitor.getContainingScope(n); if (scope != null) { try { - IBinding[] bindings = scope.find(n.toString(), true); + IBinding[] bindings = scope.find(n.toString(), isPrefix); for (int i = 0; i < bindings.length; i++) { if (bindings[i] instanceof ICPPTemplateParameter) { filtered.add(bindings[i]); @@ -127,7 +128,7 @@ }; try { - IBinding[] bindings = getTranslationUnit().getScope().find(n.toString(), true); + IBinding[] bindings = getTranslationUnit().getScope().find(n.toString(), isPrefix); for (int i = 0 ; i < bindings.length; i++) { if (filter.acceptBinding(bindings[i])) { filtered.add(bindings[i]); @@ -140,7 +141,9 @@ if (index != null) { try { - IBinding[] bindings = index.findBindingsForPrefix(n.toCharArray(), filter); + IBinding[] bindings = isPrefix ? + index.findBindingsForPrefix(n.toCharArray(), filter) : + index.findBindings(n.toCharArray(), filter, new NullProgressMonitor()); for (int i = 0; i < bindings.length; i++) { filtered.add(bindings[i]); } Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTemplateId.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTemplateId.java,v retrieving revision 1.18 diff -u -r1.18 CPPASTTemplateId.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTemplateId.java 22 Jan 2007 14:44:44 -0000 1.18 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTemplateId.java 7 Feb 2007 20:20:51 -0000 @@ -11,6 +11,7 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp; import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNameOwner; @@ -81,8 +82,7 @@ return binding; } - public IBinding[] resolvePrefix() { - // TODO Auto-generated method stub + public IASTCompletionContext getCompletionContext() { return null; } Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTName.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTName.java,v retrieving revision 1.16 diff -u -r1.16 CPPASTName.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTName.java 29 Jan 2007 10:27:46 -0000 1.16 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTName.java 7 Feb 2007 20:20:51 -0000 @@ -13,13 +13,13 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp; import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNameOwner; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.parser.util.CharArrayUtils; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding; /** @@ -76,17 +76,16 @@ return binding; } - public IBinding[] resolvePrefix() { - IASTNode node = getParent(); - while (!(node instanceof IASTCompletionContext)) { - if (node == null) { - return null; + public IASTCompletionContext getCompletionContext() { + IASTNode node = getParent(); + while (node != null) { + if (node instanceof IASTCompletionContext) { + return (IASTCompletionContext) node; } node = node.getParent(); } - IASTCompletionContext context = (IASTCompletionContext) node; - return context.resolvePrefix(this); + return null; } public void setBinding(IBinding binding) { Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTIdExpression.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTIdExpression.java,v retrieving revision 1.9 diff -u -r1.9 CPPASTIdExpression.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTIdExpression.java 1 Feb 2007 14:20:41 -0000 1.9 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTIdExpression.java 7 Feb 2007 20:20:51 -0000 @@ -14,6 +14,7 @@ import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; @@ -22,8 +23,8 @@ import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IndexFilter; import org.eclipse.cdt.core.parser.util.ArrayUtil; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; /** * @author jcamelon @@ -69,13 +70,13 @@ return CPPVisitor.getExpressionType(this); } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { IScope scope = CPPVisitor.getContainingScope(n); IBinding[] b1 = null; if (scope != null) { try { - b1 = scope.find(n.toString(), true); + b1 = scope.find(n.toString(), isPrefix); } catch (DOMException e) { } } @@ -85,9 +86,10 @@ IBinding[] b2 = null; if (index != null) { try { - b2 = index.findBindingsForPrefix( - n.toCharArray(), - IndexFilter.getFilter(ILinkage.CPP_LINKAGE_ID)); + IndexFilter filter = IndexFilter.getFilter(ILinkage.CPP_LINKAGE_ID); + b2 = isPrefix ? + index.findBindingsForPrefix(n.toCharArray(), filter) : + index.findBindings(n.toCharArray(), filter, new NullProgressMonitor()); } catch (CoreException e) { } } Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTBaseSpecifier.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTBaseSpecifier.java,v retrieving revision 1.8 diff -u -r1.8 CPPASTBaseSpecifier.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTBaseSpecifier.java 1 Feb 2007 14:20:40 -0000 1.8 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTBaseSpecifier.java 7 Feb 2007 20:20:51 -0000 @@ -17,6 +17,7 @@ import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IScope; @@ -25,8 +26,8 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier; import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IndexFilter; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; /** * @author jcamelon @@ -111,7 +112,7 @@ return r_unclear; } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { List filtered = new ArrayList(); IndexFilter filter = new IndexFilter(){ public boolean acceptBinding(IBinding binding) { @@ -136,7 +137,7 @@ if (scope != null) { try { - IBinding[] bindings = scope.find(n.toString(), true); + IBinding[] bindings = scope.find(n.toString(), isPrefix); for (int i = 0; i < bindings.length; i++) { if (filter.acceptBinding(bindings[i])) { filtered.add(bindings[i]); @@ -150,7 +151,9 @@ if (index != null) { try { - IBinding[] bindings = index.findBindingsForPrefix(n.toCharArray(), filter); + IBinding[] bindings = isPrefix ? + index.findBindingsForPrefix(n.toCharArray(), filter) : + index.findBindings(n.toCharArray(), filter, new NullProgressMonitor()); for (int i = 0; i < bindings.length; i++) { filtered.add(bindings[i]); } Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTQualifiedName.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTQualifiedName.java,v retrieving revision 1.23 diff -u -r1.23 CPPASTQualifiedName.java --- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTQualifiedName.java 1 Feb 2007 12:43:50 -0000 1.23 +++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTQualifiedName.java 7 Feb 2007 20:20:51 -0000 @@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNameOwner; import org.eclipse.cdt.core.dom.ast.IASTNode; @@ -30,7 +31,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.CharArrayUtils; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; /** * @author jcamelon @@ -56,10 +56,10 @@ return lastName != null ? lastName.resolveBinding() : null; } - public IBinding[] resolvePrefix() { + public IASTCompletionContext getCompletionContext() { removeNullNames(); IASTName lastName = getLastName(); - return lastName != null ? lastName.resolvePrefix() : new IBinding[0]; + return lastName != null ? lastName.getCompletionContext() : null; } /* @@ -299,20 +299,21 @@ return false; } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { IBinding binding = names[names.length - 2].resolveBinding(); if (binding instanceof ICPPClassType) { - return resolveClassScopePrefix((ICPPClassType) binding, - n.toCharArray()); + return findClassScopeBindings((ICPPClassType) binding, + n.toCharArray(), isPrefix); } else if (binding instanceof ICPPNamespace) { - return resolveNamespaceScopePrefix((ICPPNamespace) binding, - n.toCharArray()); + return findNamespaceScopeBindings((ICPPNamespace) binding, + n.toCharArray(), isPrefix); } return null; } - private IBinding[] resolveClassScopePrefix(ICPPClassType classType, char[] name) { + private IBinding[] findClassScopeBindings(ICPPClassType classType, + char[] name, boolean isPrefix) { List bindings = new ArrayList(); try { @@ -320,7 +321,7 @@ for (int i = 0; i < fields.length; i++) { if (fields[i].isStatic()) { char[] potential = fields[i].getNameCharArray(); - if (CharArrayUtils.equals(potential, 0, name.length, name, false)) { + if (nameMatches(potential, name, isPrefix)) { bindings.add(fields[i]); } } @@ -332,7 +333,7 @@ ICPPMethod[] methods = classType.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { char[] potential = methods[i].getNameCharArray(); - if (CharArrayUtils.equals(potential, 0, name.length, name, false)) { + if (nameMatches(potential, name, isPrefix)) { bindings.add(methods[i]); } } @@ -343,7 +344,7 @@ ICPPClassType[] nested = classType.getNestedClasses(); for (int i = 0; i < nested.length; i++) { char[] potential = nested[i].getNameCharArray(); - if (CharArrayUtils.equals(potential, 0, name.length, name, false)) { + if (nameMatches(potential, name, isPrefix)) { bindings.add(nested[i]); } } @@ -353,14 +354,15 @@ return (IBinding[]) bindings.toArray(new IBinding[bindings.size()]); } - private IBinding[] resolveNamespaceScopePrefix(ICPPNamespace namespace, char[] name) { + private IBinding[] findNamespaceScopeBindings(ICPPNamespace namespace, + char[] name, boolean isPrefix) { List bindings = new ArrayList(); try { IBinding[] members = namespace.getMemberBindings(); for (int i = 0 ; i < members.length; i++) { char[] potential = members[i].getNameCharArray(); - if (CharArrayUtils.equals(potential, 0, name.length, name, false)) { + if (nameMatches(potential, name, isPrefix)) { bindings.add(members[i]); } } @@ -369,4 +371,12 @@ return (IBinding[]) bindings.toArray(new IBinding[bindings.size()]); } + + private boolean nameMatches(char[] potential, char[] name, boolean isPrefix) { + if (isPrefix) { + return CharArrayUtils.equals(potential, 0, name.length, name, false); + } else { + return CharArrayUtils.equals(potential, name); + } + } } Index: parser/org/eclipse/cdt/core/dom/ast/IASTName.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTName.java,v retrieving revision 1.13 diff -u -r1.13 IASTName.java --- parser/org/eclipse/cdt/core/dom/ast/IASTName.java 12 Oct 2006 13:23:40 -0000 1.13 +++ parser/org/eclipse/cdt/core/dom/ast/IASTName.java 7 Feb 2007 20:20:51 -0000 @@ -51,12 +51,11 @@ public IBinding resolveBinding(); /** - * Return a list of bindings in the scope of the name that have the name as - * a prefix. + * Return the completion context for this name. * - * @return IBinding [] bindings that start with this name + * @return IASTCompletionContext the context for completion */ - public IBinding[] resolvePrefix(); + public IASTCompletionContext getCompletionContext(); /** * Determines the current linkage in which the name has to be resolved. Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTIdExpression.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTIdExpression.java,v retrieving revision 1.10 diff -u -r1.10 CASTIdExpression.java --- parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTIdExpression.java 1 Feb 2007 14:20:42 -0000 1.10 +++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTIdExpression.java 7 Feb 2007 20:20:51 -0000 @@ -15,6 +15,7 @@ import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; @@ -23,8 +24,8 @@ import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IndexFilter; import org.eclipse.cdt.core.parser.util.ArrayUtil; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; /** * @author jcamelon @@ -71,13 +72,13 @@ return CVisitor.getExpressionType(this); } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { IScope scope = CVisitor.getContainingScope(n); IBinding[] b1 = null; if (scope != null) { try { - b1 = scope.find(n.toString(), true); + b1 = scope.find(n.toString(), isPrefix); } catch (DOMException e) { } } @@ -87,9 +88,10 @@ IBinding[] b2 = null; if (index != null) { try { - b2 = index.findBindingsForPrefix( - n.toCharArray(), - IndexFilter.getFilter(ILinkage.C_LINKAGE_ID)); + IndexFilter filter = IndexFilter.getFilter(ILinkage.C_LINKAGE_ID); + b2 = isPrefix ? + index.findBindingsForPrefix(n.toCharArray(), filter) : + index.findBindings(n.toCharArray(), filter, new NullProgressMonitor()); } catch (CoreException e) { } } Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTName.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTName.java,v retrieving revision 1.17 diff -u -r1.17 CASTName.java --- parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTName.java 29 Jan 2007 10:27:47 -0000 1.17 +++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTName.java 7 Feb 2007 20:20:51 -0000 @@ -15,12 +15,12 @@ import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNameOwner; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.internal.core.dom.Linkage; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; /** * @author jcamelon @@ -57,17 +57,16 @@ return binding; } - public IBinding[] resolvePrefix() { + public IASTCompletionContext getCompletionContext() { IASTNode node = getParent(); - while (!(node instanceof IASTCompletionContext)) { - if (node == null) { - return null; + while (node != null) { + if (node instanceof IASTCompletionContext) { + return (IASTCompletionContext) node; } node = node.getParent(); } - IASTCompletionContext context = (IASTCompletionContext) node; - return context.resolvePrefix(this); + return null; } public void setBinding(IBinding binding) { Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTFieldReference.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTFieldReference.java,v retrieving revision 1.11 diff -u -r1.11 CASTFieldReference.java --- parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTFieldReference.java 29 Jan 2007 10:27:47 -0000 1.11 +++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTFieldReference.java 7 Feb 2007 20:20:51 -0000 @@ -17,6 +17,7 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTFieldReference; import org.eclipse.cdt.core.dom.ast.IASTName; @@ -27,7 +28,6 @@ import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics; /** @@ -104,7 +104,7 @@ return CVisitor.getExpressionType(this); } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { IASTExpression expression = getFieldOwner(); IType type = expression.getExpressionType(); type = CPPSemantics.getUltimateType(type, true); //stop at pointer to member? @@ -118,7 +118,7 @@ IField[] fields = compType.getFields(); for (int i = 0; i < fields.length; i++) { char[] potential = fields[i].getNameCharArray(); - if (CharArrayUtils.equals(potential, 0, name.length, name, false)) { + if (nameMatches(potential, name, isPrefix)) { bindings.add(fields[i]); } } @@ -130,4 +130,12 @@ return null; } + + private boolean nameMatches(char[] potential, char[] name, boolean isPrefix) { + if (isPrefix) { + return CharArrayUtils.equals(potential, 0, name.length, name, false); + } else { + return CharArrayUtils.equals(potential, name); + } + } } Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTTypedefNameSpecifier.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTTypedefNameSpecifier.java,v retrieving revision 1.8 diff -u -r1.8 CASTTypedefNameSpecifier.java --- parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTTypedefNameSpecifier.java 1 Feb 2007 14:20:42 -0000 1.8 +++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTTypedefNameSpecifier.java 7 Feb 2007 20:20:51 -0000 @@ -18,6 +18,7 @@ import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.ICompositeType; @@ -27,8 +28,8 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier; import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IndexFilter; -import org.eclipse.cdt.internal.core.dom.parser.IASTCompletionContext; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; /** * @author jcamelon @@ -78,7 +79,7 @@ return r_unclear; } - public IBinding[] resolvePrefix(IASTName n) { + public IBinding[] findBindings(IASTName n, boolean isPrefix) { List filtered = new ArrayList(); IndexFilter filter = new IndexFilter() { public boolean acceptBinding(IBinding binding) { @@ -98,7 +99,7 @@ } try { - IBinding[] bindings = scope.find(n.toString(), true); + IBinding[] bindings = scope.find(n.toString(), isPrefix); for (int i = 0 ; i < bindings.length; i++) { if (filter.acceptBinding(bindings[i])) { filtered.add(bindings[i]); @@ -111,7 +112,9 @@ if (index != null) { try { - IBinding[] bindings = index.findBindingsForPrefix(n.toCharArray(), filter); + IBinding[] bindings = isPrefix ? + index.findBindingsForPrefix(n.toCharArray(), filter) : + index.findBindings(n.toCharArray(), filter, new NullProgressMonitor()); for (int i = 0; i < bindings.length; i++) { filtered.add(bindings[i]); } Index: parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java,v retrieving revision 1.68 diff -u -r1.68 LocationMap.java --- parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java 30 Jan 2007 10:37:42 -0000 1.68 +++ parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java 7 Feb 2007 20:20:51 -0000 @@ -15,6 +15,7 @@ import java.util.ArrayList; import org.eclipse.cdt.core.dom.ILinkage; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFunctionStyleMacroParameter; import org.eclipse.cdt.core.dom.ast.IASTMacroExpansion; @@ -665,7 +666,7 @@ } - public IBinding[] resolvePrefix() { + public IASTCompletionContext getCompletionContext() { // TODO Auto-generated method stub return null; } @@ -746,9 +747,9 @@ public IBinding resolveBinding() { return null; } - public IBinding[] resolvePrefix() { - return null; - } + public IASTCompletionContext getCompletionContext() { + return null; + } public char[] toCharArray() { return name; Index: parser/org/eclipse/cdt/internal/core/dom/parser/IASTCompletionContext.java =================================================================== RCS file: parser/org/eclipse/cdt/internal/core/dom/parser/IASTCompletionContext.java diff -N parser/org/eclipse/cdt/internal/core/dom/parser/IASTCompletionContext.java --- parser/org/eclipse/cdt/internal/core/dom/parser/IASTCompletionContext.java 29 Jan 2007 10:27:48 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,44 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007 QNX Software Systems 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * QNX - Initial API and implementation - *******************************************************************************/ - -package org.eclipse.cdt.internal.core.dom.parser; - -import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IBinding; - -/** - * Interface for a code completion's context. Used for context-sensitive - * finding of bindings with a certain prefix. - * - *

- * This interface is not intended to be implemented by clients. - *

- *

- * EXPERIMENTAL. This class or interface has been added as - * part of a work in progress. There is no guarantee that this API will work or - * that it will remain the same. Please do not use this API without consulting - * with the CDT team. - *

- * - * @author Bryan Wilkinson - * @since 4.0 - */ -public interface IASTCompletionContext { - - /** - * Returns bindings that start with the given prefix, only considering those - * that are valid for this context. - * - * @param n the name containing a prefix - * @return valid bindings in this context for the given prefix - */ - IBinding[] resolvePrefix(IASTName n); -} Index: parser/org/eclipse/cdt/core/dom/ast/IASTCompletionContext.java =================================================================== RCS file: parser/org/eclipse/cdt/core/dom/ast/IASTCompletionContext.java diff -N parser/org/eclipse/cdt/core/dom/ast/IASTCompletionContext.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ parser/org/eclipse/cdt/core/dom/ast/IASTCompletionContext.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2007 QNX Software Systems 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * QNX - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.core.dom.ast; + + +/** + * Interface for a code completion's context. Used for context-sensitive + * finding of bindings with a certain name or prefix. + * + *

+ * This interface is not intended to be implemented by clients. + *

+ *

+ * EXPERIMENTAL. This class or interface has been added as + * part of a work in progress. There is no guarantee that this API will work or + * that it will remain the same. Please do not use this API without consulting + * with the CDT team. + *

+ * + * @author Bryan Wilkinson + * @since 4.0 + */ +public interface IASTCompletionContext { + + /** + * Returns bindings that start with the given name or prefix, only + * considering those that are valid for this context. + * + * @param n the name containing a prefix + * @return valid bindings in this context for the given prefix + */ + IBinding[] findBindings(IASTName n, boolean isPrefix); +} #P org.eclipse.cdt.ui Index: src/org/eclipse/cdt/internal/ui/IndexLabelProvider.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/IndexLabelProvider.java,v retrieving revision 1.5 diff -u -r1.5 IndexLabelProvider.java --- src/org/eclipse/cdt/internal/ui/IndexLabelProvider.java 20 Oct 2006 09:59:33 -0000 1.5 +++ src/org/eclipse/cdt/internal/ui/IndexLabelProvider.java 7 Feb 2007 20:20:51 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006 QNX Software Systems and others. + * Copyright (c) 2006, 2007 QNX Software Systems 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 @@ -106,7 +106,7 @@ else if (element instanceof IEnumeration) desc = CElementImageProvider.getEnumerationImageDescriptor(); else if (element instanceof IEnumerator) - CElementImageProvider.getEnumeratorImageDescriptor(); + desc = CElementImageProvider.getEnumeratorImageDescriptor(); else if (element instanceof ITypedef) desc = CElementImageProvider.getTypedefImageDescriptor(); else if (element instanceof ICProject) Index: src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java,v retrieving revision 1.2 diff -u -r1.2 DOMCompletionProposalComputer.java --- src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java 7 Feb 2007 11:05:35 -0000 1.2 +++ src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java 7 Feb 2007 20:20:51 -0000 @@ -11,8 +11,6 @@ package org.eclipse.cdt.internal.ui.text.contentassist; import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; import java.util.List; import org.eclipse.jface.resource.ImageDescriptor; @@ -24,6 +22,7 @@ import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTCompletionContext; import org.eclipse.cdt.core.dom.ast.IASTFunctionStyleMacroParameter; import org.eclipse.cdt.core.dom.ast.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTName; @@ -82,40 +81,30 @@ } else { boolean handleMacros= false; IASTName[] names = completionNode.getNames(); - if (names == null || names.length == 0) - // No names, not much we can do here - return Collections.EMPTY_LIST; - - // Find all bindings - List allBindings = new ArrayList(); + for (int i = 0; i < names.length; ++i) { if (names[i].getTranslationUnit() == null) // The node isn't properly hooked up, must have backtracked out of this node continue; - IBinding[] bindings = names[i].resolvePrefix(); - if (names[i].getParent() instanceof IASTIdExpression) { + + IASTCompletionContext astContext = names[i].getCompletionContext(); + if (astContext == null) { + continue; + } else if (astContext instanceof IASTIdExpression) { // handle macros only if there is a prefix handleMacros = prefix.length() > 0; } + + IBinding[] bindings = astContext.findBindings( + names[i], !context.isContextInformationStyle()); + if (bindings != null) - for (int j = 0; j < bindings.length; ++j) { - IBinding binding = bindings[j]; - //if (!allBindings.contains(binding)) - // TODO I removed this check since equals in the IBinding tree is currently broken - // It is returning true at times when I don't think it should (Bug 91577) - allBindings.add(binding); - } - } - - Iterator iBinding = allBindings.iterator(); - while (iBinding.hasNext()) { - IBinding binding = (IBinding)iBinding.next(); - handleBinding(binding, context, proposals); + for (int j = 0; j < bindings.length; ++j) + handleBinding(bindings[j], context, astContext, proposals); } - if (handleMacros) { + if (handleMacros) addMacroProposals(context, prefix, proposals); - } } return proposals; @@ -204,15 +193,17 @@ proposals.add(createProposal(macroName, macroName, image, context)); } - protected void handleBinding(IBinding binding, CContentAssistInvocationContext context, List proposals) { + protected void handleBinding(IBinding binding, + CContentAssistInvocationContext cContext, + IASTCompletionContext astContext, List proposals) { if (binding instanceof ICPPClassType) { - handleClass((ICPPClassType) binding, context, proposals); + handleClass((ICPPClassType) binding, cContext, proposals); } else if (binding instanceof IFunction) { - handleFunction((IFunction)binding, context, proposals); + handleFunction((IFunction)binding, cContext, proposals); } else if (binding instanceof IVariable) { - handleVariable((IVariable) binding, context, proposals); - } else { - proposals.add(createProposal(binding.getName(), binding.getName(), getImage(binding), context)); + handleVariable((IVariable) binding, cContext, proposals); + } else if (!cContext.isContextInformationStyle()) { + proposals.add(createProposal(binding.getName(), binding.getName(), getImage(binding), cContext)); } } @@ -286,7 +277,7 @@ StringBuffer dispStringBuff = new StringBuffer(repStringBuff.toString()); dispStringBuff.append(dispargString); dispStringBuff.append(')'); - if (returnTypeStr != null) { + if (returnTypeStr != null && returnTypeStr.length() > 0) { dispStringBuff.append(' '); dispStringBuff.append(returnTypeStr); } @@ -315,6 +306,8 @@ } private void handleVariable(IVariable variable, CContentAssistInvocationContext context, List proposals) { + if (context.isContextInformationStyle()) return; + StringBuffer repStringBuff = new StringBuffer(); repStringBuff.append(variable.getName()); #P org.eclipse.cdt.ui.tests Index: ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTests.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTests.java,v retrieving revision 1.4 diff -u -r1.4 CompletionTests.java --- ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTests.java 1 Feb 2007 08:59:01 -0000 1.4 +++ ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTests.java 7 Feb 2007 20:20:51 -0000 @@ -23,7 +23,7 @@ * * @since 4.0 */ -public class CompletionTests extends AbstractCompletionTest { +public class CompletionTests extends AbstractContentAssistTest { private static final String HEADER_FILE_NAME = "CompletionTest.h"; private static final String SOURCE_FILE_NAME = "CompletionTest.cpp"; @@ -128,6 +128,10 @@ return createFile(project, SOURCE_FILE_NAME, sourceContent.toString()); } + protected void assertCompletionResults(int offset, String[] expected, boolean compareIdString) throws Exception { + assertContentAssistResults(offset, expected, true, compareIdString); + } + //void gfunc() {C1 v; v.m/*cursor*/ public void _testLocalVariable() throws Exception { // fails because of additional m1private(void) Index: ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractCompletionTest.java =================================================================== RCS file: ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractCompletionTest.java diff -N ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractCompletionTest.java --- ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractCompletionTest.java 6 Feb 2007 15:00:24 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,207 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004, 2006 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Rational Software - Initial API and implementation - * Anton Leherbauer (Wind River Systems) - *******************************************************************************/ -package org.eclipse.cdt.ui.tests.text.contentassist2; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IProject; -import org.eclipse.jface.text.contentassist.ContentAssistant; -import org.eclipse.jface.text.contentassist.ICompletionProposal; -import org.eclipse.jface.text.source.ISourceViewer; -import org.eclipse.jface.text.templates.TemplateProposal; -import org.eclipse.ui.texteditor.AbstractTextEditor; -import org.eclipse.ui.texteditor.ITextEditor; - -import org.eclipse.cdt.core.dom.IPDOMManager; -import org.eclipse.cdt.core.model.ICProject; -import org.eclipse.cdt.core.parser.KeywordSetKey; -import org.eclipse.cdt.core.parser.ParserFactory; -import org.eclipse.cdt.core.parser.ParserLanguage; -import org.eclipse.cdt.core.testplugin.CProjectHelper; -import org.eclipse.cdt.ui.testplugin.CTestPlugin; -import org.eclipse.cdt.ui.tests.BaseUITestCase; -import org.eclipse.cdt.ui.tests.text.EditorTestHelper; -import org.eclipse.cdt.ui.text.ICCompletionProposal; - -import org.eclipse.cdt.internal.ui.text.contentassist.CContentAssistProcessor; - -/** - * - * @since 4.0 - */ -public abstract class AbstractCompletionTest extends BaseUITestCase { - - private ICProject fCProject; - protected IFile fCFile; - private ITextEditor fEditor; - - private final static Set fgAllKeywords= new HashSet(); - - static { - fgAllKeywords.addAll(ParserFactory.getKeywordSet(KeywordSetKey.KEYWORDS, ParserLanguage.C)); - fgAllKeywords.addAll(ParserFactory.getKeywordSet(KeywordSetKey.TYPES, ParserLanguage.C)); - fgAllKeywords.addAll(ParserFactory.getKeywordSet(KeywordSetKey.KEYWORDS, ParserLanguage.CPP)); - fgAllKeywords.addAll(ParserFactory.getKeywordSet(KeywordSetKey.TYPES, ParserLanguage.CPP)); - } - public AbstractCompletionTest(String name) { - super(name); - } - - protected void setUp() throws Exception { - super.setUp(); - fCProject= CProjectHelper.createCCProject(getName(), "unused", IPDOMManager.ID_FAST_INDEXER); - fCFile= setUpProjectContent(fCProject.getProject()); - assertNotNull(fCFile); - fEditor= (ITextEditor)EditorTestHelper.openInEditor(fCFile, true); - assertNotNull(fEditor); - EditorTestHelper.joinBackgroundActivities((AbstractTextEditor)fEditor); - } - - /** - * Setup the project's content. - * @param project - * @return the file to be opened in the editor - * @throws Exception - */ - protected abstract IFile setUpProjectContent(IProject project) throws Exception; - - protected void tearDown() throws Exception { - EditorTestHelper.closeEditor(fEditor); - fEditor= null; - CProjectHelper.delete(fCProject); - fCProject= null; - fCFile= null; - super.tearDown(); - } - - protected void assertCompletionResults(int offset, String[] expected, boolean compareIdString) throws Exception { - - if (CTestPlugin.getDefault().isDebugging()) { - System.out.println("\n\n\n\n\nTesting "+this.getClass().getName()); - } - - //Call the CContentAssistProcessor - ISourceViewer sourceViewer= EditorTestHelper.getSourceViewer((AbstractTextEditor)fEditor); - String contentType = sourceViewer.getDocument().getContentType(offset); - ContentAssistant assistant = new ContentAssistant(); - CContentAssistProcessor processor = new CContentAssistProcessor(fEditor, assistant, contentType); - long startTime= System.currentTimeMillis(); - ICompletionProposal[] results = processor.computeCompletionProposals(sourceViewer, offset); - long endTime= System.currentTimeMillis(); - assertTrue(results != null); - - results= filterProposals(results); - String[] resultStrings= toStringArray(results, compareIdString); - Arrays.sort(expected); - Arrays.sort(resultStrings); - - if (CTestPlugin.getDefault().isDebugging()) { - System.out.println("Time (ms): " + (endTime-startTime)); - for (int i = 0; i < resultStrings.length; i++) { - String proposal = resultStrings[i]; - System.out.println("Result: " + proposal); - } - } - - boolean allFound = true ; // for the time being, let's be optimistic - - for (int i = 0; i< expected.length; i++){ - boolean found = false; - for(int j = 0; j< resultStrings.length; j++){ - String proposal = resultStrings[j]; - if(expected[i].equals(proposal)){ - found = true; - if (CTestPlugin.getDefault().isDebugging()) { - System.out.println("Lookup success for " + expected[i]); - } - break; - } - } - if (!found) { - allFound = false ; - if (CTestPlugin.getDefault().isDebugging()) { - System.out.println( "Lookup failed for " + expected[i]); //$NON-NLS-1$ - } - } - } - - if (!allFound) { - assertEquals("Missing results!", toString(expected), toString(resultStrings)); - } else if (doCheckExtraResults()) { - assertEquals("Extra results!", toString(expected), toString(resultStrings)); - } - - } - - private String toString(String[] strings) { - StringBuffer buf= new StringBuffer(); - for(int i=0; i< strings.length; i++){ - buf.append(strings[i]).append('\n'); - } - return buf.toString(); - } - - private String[] toStringArray(ICompletionProposal[] proposals, boolean useIdString) { - String[] strings= new String[proposals.length]; - for(int i=0; i< proposals.length; i++){ - ICompletionProposal proposal = proposals[i]; - if (proposal instanceof ICCompletionProposal && useIdString) { - strings[i]= ((ICCompletionProposal)proposal).getIdString(); - } else { - strings[i]= proposal.getDisplayString(); - } - } - return strings; - } - - /** - * Override to relax checking of extra results - */ - protected boolean doCheckExtraResults() { - return true ; - } - - /** - * Filter out template and keyword proposals. - * @param results - * @return filtered proposals - */ - private ICompletionProposal[] filterProposals(ICompletionProposal[] results) { - List filtered= new ArrayList(); - for (int i = 0; i < results.length; i++) { - ICompletionProposal proposal = results[i]; - if (proposal instanceof TemplateProposal) { - continue; - } - if (proposal instanceof ICCompletionProposal) { - // check for keywords proposal - if (fgAllKeywords.contains(proposal.getDisplayString())) { - continue; - } - } - filtered.add(proposal); - } - return (ICompletionProposal[]) filtered.toArray(new ICompletionProposal[filtered.size()]); - } - - /** - * @return the content of the editor buffer - */ - protected String getBuffer() { - return EditorTestHelper.getDocument(fEditor).get(); - } -} \ No newline at end of file Index: ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java,v retrieving revision 1.4 diff -u -r1.4 ContentAssist2TestSuite.java --- ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java 6 Feb 2007 15:00:24 -0000 1.4 +++ ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java 7 Feb 2007 20:20:51 -0000 @@ -14,7 +14,7 @@ import junit.framework.TestSuite; /** - * This suite bundles all tests for the CContentAssistProcessor2 + * This suite bundles all tests for the CContentAssistProcessor */ public class ContentAssist2TestSuite extends TestSuite { @@ -25,7 +25,6 @@ public ContentAssist2TestSuite() { super("Tests in package org.eclipse.cdt.ui.tests.text.contentassist2"); - addTest(CompletionTest_MemberReference_Arrow_Prefix2.suite()); addTest(CompletionTest_ArgumentType_Prefix.suite()); addTest(CompletionTest_ArgumentType_Prefix2.suite()); addTest(CompletionTest_ClassReference_NoPrefix.suite()); @@ -41,6 +40,7 @@ addTest(CompletionTest_MacroRef_Prefix.suite()); addTest(CompletionTest_MemberReference_Arrow_NoPrefix.suite()); addTest(CompletionTest_MemberReference_Arrow_Prefix.suite()); + addTest(CompletionTest_MemberReference_Arrow_Prefix2.suite()); addTest(CompletionTest_MemberReference_Dot_NoPrefix.suite()); addTest(CompletionTest_MemberReference_Dot_Prefix.suite()); addTest(CompletionTest_NamespaceRef_NoPrefix.suite()); @@ -64,5 +64,6 @@ addTest(CompletionTest_VariableType_Prefix.suite()); addTest(CompletionTests.suite()); + addTest(ParameterHintTests.suite()); } } Index: ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionProposalsBaseTest.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionProposalsBaseTest.java,v retrieving revision 1.11 diff -u -r1.11 CompletionProposalsBaseTest.java --- ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionProposalsBaseTest.java 6 Feb 2007 15:00:24 -0000 1.11 +++ ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionProposalsBaseTest.java 7 Feb 2007 20:20:51 -0000 @@ -29,7 +29,7 @@ import org.eclipse.cdt.ui.testplugin.CTestPlugin; -public abstract class CompletionProposalsBaseTest extends AbstractCompletionTest { +public abstract class CompletionProposalsBaseTest extends AbstractContentAssistTest { private boolean fFailingTest; @@ -89,6 +89,10 @@ return bodyFile; } + protected void assertCompletionResults(int offset, String[] expected, boolean compareIdString) throws Exception { + assertContentAssistResults(offset, expected, true, compareIdString); + } + public void testCompletionProposals() throws Exception { String[] expected = getExpectedResultsValues(); assertCompletionResults(getCompletionPosition(), expected, false); Index: ui/org/eclipse/cdt/ui/tests/text/contentassist2/ParameterHintTests.java =================================================================== RCS file: ui/org/eclipse/cdt/ui/tests/text/contentassist2/ParameterHintTests.java diff -N ui/org/eclipse/cdt/ui/tests/text/contentassist2/ParameterHintTests.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ui/org/eclipse/cdt/ui/tests/text/contentassist2/ParameterHintTests.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,167 @@ +/******************************************************************************* + * Copyright (c) 2007 QNX Software Systems 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Bryan Wilkinson (QNX) - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text.contentassist2; + +import junit.framework.Test; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; + +import org.eclipse.cdt.core.testplugin.util.BaseTestCase; + +public class ParameterHintTests extends AbstractContentAssistTest { + + private static final String HEADER_FILE_NAME = "PHTest.h"; + private static final String SOURCE_FILE_NAME = "PHTest.cpp"; + +//{PHTest.h} +//class aClass { +//public: +// int aField; +// void aMethod(char c); +// void aMethod(char c, int x); +//}; +//class bClass { +//public: +// bClass(int x); +// bClass(int x, int y); +//}; +//void aFunc(int i); +//int anotherFunc(int i, int j); +//int pi(aClass a); +//int pie(aClass a); +//int pies(aClass a); + + public ParameterHintTests(String name) { + super(name); + } + + public static Test suite() { + return BaseTestCase.suite(ParameterHintTests.class, "_"); + } + + protected IFile setUpProjectContent(IProject project) throws Exception { + String headerContent= readTaggedComment(HEADER_FILE_NAME); + StringBuffer sourceContent= getContentsForTest(1)[0]; + sourceContent.insert(0, "#include \""+HEADER_FILE_NAME+"\"\n"); + assertNotNull(createFile(project, HEADER_FILE_NAME, headerContent)); + return createFile(project, SOURCE_FILE_NAME, sourceContent.toString()); + } + + protected void assertParameterHints(String[] expected) throws Exception { + assertContentAssistResults(getBuffer().length() - 1, expected, false, false); + } + + //void foo(){aFunc( + public void testFunction() throws Exception { + assertParameterHints(new String[] { + "aFunc(int i) void" + }); + } + + ////TODO move function into header once indexer supports templates + //templatevoid tFunc(T x, T y); + //void foo(){tFunc( + public void testTemplateFunction() throws Exception { + assertParameterHints(new String[] { + "tFunc(T x,T y) void" + }); + } + + ////TODO move function into header once indexer supports templates + //templatevoid tFunc(T x, T y); + //void foo(){tFunc( + public void _testTemplateFunction2() throws Exception { + assertParameterHints(new String[] { + "tFunc(T x,T y) void" + }); + } + + //void foo(){int a=5;aFunc ( anotherFunc ( a , (in + public void testOffsetCalculation() throws Exception { + assertParameterHints(new String[] { + "anotherFunc(int i,int j) int" + }); + } + + //void foo(){int a=pie( + public void testAccurateName() throws Exception { + assertParameterHints(new String[] { + "pie(aClass a) int" + }); + } + + //void foo(){int a=pi + public void testInvalidInvocation() throws Exception { + assertParameterHints(new String[] {}); + } + + //void aClass::aMethod( + public void testMethodDefinition() throws Exception { + assertParameterHints(new String[] { + "aMethod(char c) void", + "aMethod(char c,int x) void" + }); + } + + //void aClass::aMethod(char c){aMethod(c,aFi + public void testMethodScope() throws Exception { + assertParameterHints(new String[] { + "aMethod(char c) void", + "aMethod(char c,int x) void" + }); + } + + //void foo(){aClass a=new aClass( + public void testConstructor() throws Exception { + assertParameterHints(new String[] { + "aClass(void)", + "aClass(const aClass &)" + }); + } + + //void foo(){bClass b( + public void _testConstructor2() throws Exception { + assertParameterHints(new String[] { + "bClass(int x)", + "bClass(int x,int y)", + "bClass(const bClass &)" + }); + } + + ////TODO move class into header once indexer supports templates + //templateclass tClass {public:tClass(T t);}; + //void foo(){tClass t=new tClass( + public void _testTemplateConstructor() throws Exception { + assertParameterHints(new String[] { + "tClass(void)", + "tClass(T t)", + "tClass(const tClass &)" + }); + } + + ////TODO move class into header once indexer supports templates + //templateclass tClass {public:tClass(T t);}; + //void foo(){tClass t( + public void _testTemplateConstructor2() throws Exception { + assertParameterHints(new String[] { + "tClass(T t)", + "tClass(const tClass &)" + }); + } + + //void foo(){int pi = 3;pi( + public void testFunctionsOnly() throws Exception { + assertParameterHints(new String[] { + "pi(aClass a) int" + }); + } +} Index: ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java =================================================================== RCS file: ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java diff -N ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,209 @@ +/******************************************************************************* + * Copyright (c) 2004, 20067 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Rational Software - Initial API and implementation + * Anton Leherbauer (Wind River Systems) + * Bryan Wilkinson (QNX) + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text.contentassist2; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.jface.text.contentassist.ContentAssistant; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.templates.TemplateProposal; +import org.eclipse.ui.texteditor.AbstractTextEditor; +import org.eclipse.ui.texteditor.ITextEditor; + +import org.eclipse.cdt.core.dom.IPDOMManager; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.parser.KeywordSetKey; +import org.eclipse.cdt.core.parser.ParserFactory; +import org.eclipse.cdt.core.parser.ParserLanguage; +import org.eclipse.cdt.core.testplugin.CProjectHelper; +import org.eclipse.cdt.ui.testplugin.CTestPlugin; +import org.eclipse.cdt.ui.tests.BaseUITestCase; +import org.eclipse.cdt.ui.tests.text.EditorTestHelper; +import org.eclipse.cdt.ui.text.ICCompletionProposal; + +import org.eclipse.cdt.internal.ui.text.contentassist.CContentAssistProcessor; + +public abstract class AbstractContentAssistTest extends BaseUITestCase { + + private ICProject fCProject; + protected IFile fCFile; + private ITextEditor fEditor; + + private final static Set fgAllKeywords= new HashSet(); + + static { + fgAllKeywords.addAll(ParserFactory.getKeywordSet(KeywordSetKey.KEYWORDS, ParserLanguage.C)); + fgAllKeywords.addAll(ParserFactory.getKeywordSet(KeywordSetKey.TYPES, ParserLanguage.C)); + fgAllKeywords.addAll(ParserFactory.getKeywordSet(KeywordSetKey.KEYWORDS, ParserLanguage.CPP)); + fgAllKeywords.addAll(ParserFactory.getKeywordSet(KeywordSetKey.TYPES, ParserLanguage.CPP)); + } + public AbstractContentAssistTest(String name) { + super(name); + } + + protected void setUp() throws Exception { + super.setUp(); + fCProject= CProjectHelper.createCCProject(getName(), "unused", IPDOMManager.ID_FAST_INDEXER); + fCFile= setUpProjectContent(fCProject.getProject()); + assertNotNull(fCFile); + fEditor= (ITextEditor)EditorTestHelper.openInEditor(fCFile, true); + assertNotNull(fEditor); + EditorTestHelper.joinBackgroundActivities((AbstractTextEditor)fEditor); + } + + /** + * Setup the project's content. + * @param project + * @return the file to be opened in the editor + * @throws Exception + */ + protected abstract IFile setUpProjectContent(IProject project) throws Exception; + + protected void tearDown() throws Exception { + EditorTestHelper.closeEditor(fEditor); + fEditor= null; + CProjectHelper.delete(fCProject); + fCProject= null; + fCFile= null; + super.tearDown(); + } + + protected void assertContentAssistResults(int offset, String[] expected, boolean isCompletion, boolean compareIdString) throws Exception { + + if (CTestPlugin.getDefault().isDebugging()) { + System.out.println("\n\n\n\n\nTesting "+this.getClass().getName()); + } + + //Call the CContentAssistProcessor + ISourceViewer sourceViewer= EditorTestHelper.getSourceViewer((AbstractTextEditor)fEditor); + String contentType = sourceViewer.getDocument().getContentType(offset); + ContentAssistant assistant = new ContentAssistant(); + CContentAssistProcessor processor = new CContentAssistProcessor(fEditor, assistant, contentType); + long startTime= System.currentTimeMillis(); + Object[] results = isCompletion + ? (Object[]) processor.computeCompletionProposals(sourceViewer, offset) + : (Object[]) processor.computeContextInformation(sourceViewer, offset); + long endTime= System.currentTimeMillis(); + assertTrue(results != null); + + results= filterResults(results); + String[] resultStrings= toStringArray(results, compareIdString); + Arrays.sort(expected); + Arrays.sort(resultStrings); + + if (CTestPlugin.getDefault().isDebugging()) { + System.out.println("Time (ms): " + (endTime-startTime)); + for (int i = 0; i < resultStrings.length; i++) { + String proposal = resultStrings[i]; + System.out.println("Result: " + proposal); + } + } + + boolean allFound = true ; // for the time being, let's be optimistic + + for (int i = 0; i< expected.length; i++){ + boolean found = false; + for(int j = 0; j< resultStrings.length; j++){ + String proposal = resultStrings[j]; + if(expected[i].equals(proposal)){ + found = true; + if (CTestPlugin.getDefault().isDebugging()) { + System.out.println("Lookup success for " + expected[i]); + } + break; + } + } + if (!found) { + allFound = false ; + if (CTestPlugin.getDefault().isDebugging()) { + System.out.println( "Lookup failed for " + expected[i]); //$NON-NLS-1$ + } + } + } + + if (!allFound) { + assertEquals("Missing results!", toString(expected), toString(resultStrings)); + } else if (doCheckExtraResults()) { + assertEquals("Extra results!", toString(expected), toString(resultStrings)); + } + + } + + /** + * Filter out template and keyword proposals. + * @param results + * @return filtered proposals + */ + private Object[] filterResults(Object[] results) { + List filtered= new ArrayList(); + for (int i = 0; i < results.length; i++) { + Object result = results[i]; + if (result instanceof TemplateProposal) { + continue; + } + if (result instanceof ICCompletionProposal) { + // check for keywords proposal + if (fgAllKeywords.contains(((ICCompletionProposal)result).getDisplayString())) { + continue; + } + } + filtered.add(result); + } + return filtered.toArray(); + } + + private String[] toStringArray(Object[] results, boolean useIdString) { + String[] strings= new String[results.length]; + for(int i=0; i< results.length; i++){ + Object result = results[i]; + if (result instanceof ICCompletionProposal && useIdString) { + strings[i]= ((ICCompletionProposal)result).getIdString(); + } else if (result instanceof ICompletionProposal) { + strings[i]= ((ICompletionProposal)result).getDisplayString(); + } else { + strings[i]= ((IContextInformation)result).getContextDisplayString(); + } + } + return strings; + } + + private String toString(String[] strings) { + StringBuffer buf= new StringBuffer(); + for(int i=0; i< strings.length; i++){ + buf.append(strings[i]).append('\n'); + } + return buf.toString(); + } + + /** + * Override to relax checking of extra results + */ + protected boolean doCheckExtraResults() { + return true ; + } + + /** + * @return the content of the editor buffer + */ + protected String getBuffer() { + return EditorTestHelper.getDocument(fEditor).get(); + } +}