1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-07 16:26:11 +02:00

Bug 223660: Parameter help for constructor-style initializers.

This commit is contained in:
Markus Schorn 2010-10-11 10:44:33 +00:00
parent ea310fa845
commit d03bee7bc7
4 changed files with 86 additions and 33 deletions

View file

@ -286,4 +286,14 @@ public class BasicCompletionTest extends CompletionTestBase {
String[] expected= {"A"}; String[] expected= {"A"};
checkNonPrefixCompletion(code, true, expected); checkNonPrefixCompletion(code, true, expected);
} }
// struct S {};
// void foo() {
// S b
public void testCompletionInCtorOfVariable_223660() throws Exception {
String code = getAboveComment();
String[] expected= {"b"};
checkNonPrefixCompletion(code, true, expected);
}
} }

View file

@ -85,13 +85,19 @@ public class CPPASTName extends CPPASTNameBase implements ICPPASTCompletionConte
} }
IBinding[] bindings = CPPSemantics.findBindingsForContentAssist(n, isPrefix, namespaces); IBinding[] bindings = CPPSemantics.findBindingsForContentAssist(n, isPrefix, namespaces);
return filterByElaboratedTypeSpecifier(kind, bindings); return filterByElaboratedTypeSpecifier(kind, bindings);
} } else if (parent instanceof IASTDeclarator) {
else if (parent instanceof IASTDeclarator) {
IBinding[] bindings = CPPSemantics.findBindingsForContentAssist(n, isPrefix, namespaces); IBinding[] bindings = CPPSemantics.findBindingsForContentAssist(n, isPrefix, namespaces);
for (int i = 0; i < bindings.length; i++) { if (!isPrefix) {
if (bindings[i] instanceof ICPPNamespace || bindings[i] instanceof ICPPClassType) { // The lookup does not find the binding, that is defined by this name
} else { if (bindings.length == 0) {
bindings[i] = null; bindings= new IBinding[] {n.resolveBinding()};
}
} else {
for (int i = 0; i < bindings.length; i++) {
if (bindings[i] instanceof ICPPNamespace || bindings[i] instanceof ICPPClassType) {
} else {
bindings[i] = null;
}
} }
} }
return (IBinding[])ArrayUtil.removeNulls(IBinding.class, bindings); return (IBinding[])ArrayUtil.removeNulls(IBinding.class, bindings);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2008 QNX Software Systems and others. * Copyright (c) 2007, 2010 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* Bryan Wilkinson (QNX) - Initial API and implementation * Bryan Wilkinson (QNX) - Initial API and implementation
* Anton Leherbauer (Wind River Systems) * Anton Leherbauer (Wind River Systems)
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist2; package org.eclipse.cdt.ui.tests.text.contentassist2;
@ -23,25 +24,25 @@ public class ParameterHintTests extends AbstractContentAssistTest {
private static final String HEADER_FILE_NAME = "PHTest.h"; private static final String HEADER_FILE_NAME = "PHTest.h";
private static final String SOURCE_FILE_NAME = "PHTest.cpp"; private static final String SOURCE_FILE_NAME = "PHTest.cpp";
//{PHTest.h} // {PHTest.h}
//class aClass { // class aClass {
//public: // public:
// int aField; // int aField;
// void aMethod(char c); // void aMethod(char c);
// void aMethod(char c, int x); // void aMethod(char c, int x);
//}; // };
//class bClass { // class bClass {
//public: // public:
// bClass(int x); // bClass(int x);
// bClass(int x, int y); // bClass(int x, int y);
//}; // };
//void aFunc(int i); // void aFunc(int i);
//int anotherFunc(int i, int j); // int anotherFunc(int i, int j);
//int pi(aClass a); // int pi(aClass a);
//int pie(aClass a); // int pie(aClass a);
//int pies(aClass a); // int pies(aClass a);
//template<class T>class tClass {public:tClass(T t);}; // template<class T>class tClass {public:tClass(T t);};
//template<class T>void tFunc(T x, T y); // template<class T>void tFunc(T x, T y);
public ParameterHintTests(String name) { public ParameterHintTests(String name) {
super(name, true); super(name, true);
@ -129,7 +130,7 @@ public class ParameterHintTests extends AbstractContentAssistTest {
} }
//void foo(){bClass b( //void foo(){bClass b(
public void _testConstructor2_Bug223660() throws Exception { public void testConstructor2_Bug223660() throws Exception {
// http://bugs.eclipse.org/223660 // http://bugs.eclipse.org/223660
assertParameterHints(new String[] { assertParameterHints(new String[] {
"bClass(int x)", "bClass(int x)",
@ -138,6 +139,18 @@ public class ParameterHintTests extends AbstractContentAssistTest {
}); });
} }
// struct D {
// bClass b;
// D() : b(
public void testConstructor3_Bug327064() throws Exception {
// http://bugs.eclipse.org/327064
assertParameterHints(new String[] {
"bClass(int x)",
"bClass(int x,int y)",
"bClass(const bClass &)"
});
}
//void foo(){tClass<int> t=new tClass<int>( //void foo(){tClass<int> t=new tClass<int>(
public void testTemplateConstructor() throws Exception { public void testTemplateConstructor() throws Exception {
assertParameterHints(new String[] { assertParameterHints(new String[] {
@ -147,11 +160,11 @@ public class ParameterHintTests extends AbstractContentAssistTest {
} }
//void foo(){tClass<int> t( //void foo(){tClass<int> t(
public void _testTemplateConstructor2_Bug223660() throws Exception { public void testTemplateConstructor2_Bug223660() throws Exception {
// http://bugs.eclipse.org/223660 // http://bugs.eclipse.org/223660
assertParameterHints(new String[] { assertParameterHints(new String[] {
"tClass(T t)", "tClass(int t)",
"tClass(const tClass &)" "tClass(const tClass<int> &)"
}); });
} }

View file

@ -302,10 +302,10 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
handleClass((ICPPClassType) binding, astContext, cContext, baseRelevance, proposals); handleClass((ICPPClassType) binding, astContext, cContext, baseRelevance, proposals);
} else if (binding instanceof IFunction) { } else if (binding instanceof IFunction) {
handleFunction((IFunction)binding, cContext, baseRelevance, proposals); handleFunction((IFunction)binding, cContext, baseRelevance, proposals);
} else if (binding instanceof IVariable) {
handleVariable((IVariable) binding, cContext, baseRelevance, proposals);
} else if (!cContext.isContextInformationStyle()) { } else if (!cContext.isContextInformationStyle()) {
if (binding instanceof IVariable) { if (binding instanceof ITypedef) {
handleVariable((IVariable) binding, cContext, baseRelevance, proposals);
} else if (binding instanceof ITypedef) {
proposals.add(createProposal(name, name, getImage(binding), proposals.add(createProposal(name, name, getImage(binding),
baseRelevance + RelevanceConstants.TYPEDEF_TYPE_RELEVANCE, cContext)); baseRelevance + RelevanceConstants.TYPEDEF_TYPE_RELEVANCE, cContext));
} else if (binding instanceof ICPPNamespace) { } else if (binding instanceof ICPPNamespace) {
@ -456,6 +456,23 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
private void handleVariable(IVariable variable, CContentAssistInvocationContext context, private void handleVariable(IVariable variable, CContentAssistInvocationContext context,
int baseRelevance, List<ICompletionProposal> proposals) { int baseRelevance, List<ICompletionProposal> proposals) {
if (context.isContextInformationStyle()) {
// Handle the case where a variable is initialized with a constructor
try {
IType t = variable.getType();
t= unwindTypedefs(t);
if (t instanceof ICPPClassType) {
ICPPClassType classType= (ICPPClassType) t;
ICPPConstructor[] constructors = classType.getConstructors();
for (ICPPConstructor constructor : constructors) {
handleFunction(constructor, context, baseRelevance, proposals);
}
}
} catch (DOMException e) {
}
return;
}
StringBuilder repStringBuff = new StringBuilder(); StringBuilder repStringBuff = new StringBuilder();
repStringBuff.append(variable.getName()); repStringBuff.append(variable.getName());
@ -490,6 +507,13 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
proposals.add(proposal); proposals.add(proposal);
} }
private IType unwindTypedefs(final IType t) {
IType r= t;
while (r instanceof ITypedef)
r= ((ITypedef) r).getType();
return r != null ? r : t;
}
private static boolean isField(IVariable variable) { private static boolean isField(IVariable variable) {
return variable instanceof IField; return variable instanceof IField;
} }