From cd8ca24457864e67317ef237255942a0451e3bae Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Mon, 21 May 2012 21:06:26 -0700 Subject: [PATCH] Bug 380197 - F3 prefers using declaration to a real one --- .../CPPSelectionTestsAnyIndexer.java | 28 +++++++++++++++++++ .../search/actions/OpenDeclarationsJob.java | 26 ++++++++++++----- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsAnyIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsAnyIndexer.java index d4aeea46804..9e307564bd0 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsAnyIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsAnyIndexer.java @@ -1247,4 +1247,32 @@ public abstract class CPPSelectionTestsAnyIndexer extends BaseSelectionTestsInde assertEquals("ambiguous input: 2", e.getMessage()); } } + + // namespace ns { + // void func(); + // } + + // #include "test.h" + // using ns::func; + // + // void test() { + // func(); + // } + public void testBug380197() throws Exception { + StringBuilder[] buffers= getContents(2); + String hcode= buffers[0].toString(); + String scode= buffers[1].toString(); + IFile hfile = importFile("test.h", hcode); + IFile file = importFile("test.cpp", scode); + waitUntilFileIsIndexed(index, file, MAX_WAIT_TIME); + + int hoffset= hcode.indexOf("func"); + int offset = scode.indexOf("func()"); + IASTNode def = testF3(file, offset + 1); + assertTrue(def instanceof IASTName); + assertEquals("func", def.toString()); + IASTFileLocation location = def.getFileLocation(); + assertEquals(hfile.getLocation().toOSString(), location.getFileName()); + assertEquals(hoffset, location.getNodeOffset()); + } } 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 4980170cc42..f7faf7fa1c7 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 @@ -57,6 +57,7 @@ import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration; import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization; @@ -321,15 +322,26 @@ class OpenDeclarationsJob extends Job implements ASTRunnable { } private IName[] findDeclarations(IIndex index, IASTTranslationUnit ast, IBinding binding) throws CoreException { - IName[] declNames= ast.getDeclarationsInAST(binding); - for (int i = 0; i < declNames.length; i++) { - IName name = declNames[i]; - if (name.isDefinition()) - declNames[i]= null; + IASTName[] astNames= ast.getDeclarationsInAST(binding); + ArrayList usingDeclarations = null; + for (int i = 0; i < astNames.length; i++) { + IASTName name = astNames[i]; + if (name.isDefinition()) { + astNames[i]= null; + } else if (CPPVisitor.findAncestorWithType(name, ICPPASTUsingDeclaration.class) != null) { + if (usingDeclarations == null) + usingDeclarations = new ArrayList(1); + usingDeclarations.add(name); + astNames[i]= null; + } } - declNames= ArrayUtil.removeNulls(IName.class, declNames); + IName[] declNames= ArrayUtil.removeNulls(astNames); if (declNames.length == 0) { - declNames= index.findNames(binding, IIndex.FIND_DECLARATIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES); + declNames = index.findNames(binding, IIndex.FIND_DECLARATIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES); + } + // 'using' declarations are considered only when there are no other declarations. + if (declNames.length == 0 && usingDeclarations != null) { + declNames = usingDeclarations.toArray(new IName[usingDeclarations.size()]); } return declNames; }