diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java index 9387ce50bdd..ee7d52a896c 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java @@ -1191,4 +1191,31 @@ public class CPPSelectionTestsNoIndexer extends BaseSelectionTests { int offset = code.indexOf("obj.waldo") + 4; assertTrue(testF3(file, offset) instanceof IASTName); } + + // struct A { + // A(); + // }; + // + // template + // struct B { + // B() {} + // }; + // + // struct C { + // C(); + // B* b; + // }; + // + // C::C() : b(new B()) {} + public void testAmbiguityWithImplicitName_463234() throws Exception { + String code = getAboveComment(); + IFile file = importFile("testBug463234.cpp", code); + + int offset = code.indexOf("new B") + 6; + // There should be two ambiguous targets, the class A and the constructor B::B, + // with the class A being the first one (index 0). + IASTNode target = testF3WithAmbiguity(file, offset, 0); + assertTrue(target instanceof IASTName); + assertEquals("A", ((IASTName) target).toString()); + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsJob.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsJob.java index 5a9b4c72949..3cefaa6fa07 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsJob.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsJob.java @@ -528,9 +528,10 @@ class OpenDeclarationsJob extends Job implements ASTRunnable { if (uniqueElements.size() == 2) { final ICElement e0 = uniqueElements.get(0); final ICElement e1 = uniqueElements.get(1); - if (e0 instanceof IStructureDeclaration && e1 instanceof IMethodDeclaration) { + // Prefer a method of a class, to the class itself. + if (isMethodOfClass(e1, e0)) { target= (ISourceReference) e1; - } else if (e1 instanceof IStructureDeclaration && e0 instanceof IMethodDeclaration) { + } else if (isMethodOfClass(e0, e1)) { target= (ISourceReference) e0; } } @@ -555,6 +556,13 @@ class OpenDeclarationsJob extends Job implements ASTRunnable { } } } + + private boolean isMethodOfClass(ICElement method, ICElement clazz) { + return method instanceof IMethodDeclaration + && clazz instanceof IStructureDeclaration + && method.getParent() != null + && method.getParent().equals(clazz); + } }); return true; }