private ICElementHandle getCElementForName(ICProject project, IIndex index, IName declName) throws CoreException { if (declName instanceof IIndexName) { return IndexUI.getCElementForName(project, index, (IIndexName) declName); } if (declName instanceof IASTName) { IASTName astName = (IASTName) declName; IBinding binding = astName.resolveBinding(); if (binding != null) { ITranslationUnit tu = IndexUI.getTranslationUnit(project, astName); if (tu != null) { IASTFileLocation loc = astName.getFileLocation(); IRegion region = new Region(loc.getNodeOffset(), loc.getNodeLength()); return CElementHandleFactory.create(tu, binding, astName.isDefinition(), region, 0); } } return null; } return null; }
private boolean navigationFallBack(IASTTranslationUnit ast, IASTName sourceName, NameKind kind) { // Bug 102643, as a fall-back we look up the selected word in the index. if (fSelectedText != null && fSelectedText.length() > 0) { try { final ICProject project = fTranslationUnit.getCProject(); final char[] name = fSelectedText.toCharArray(); List<ICElement> elems = new ArrayList<ICElement>(); // Bug 252549, search for names in the AST first. Set<IBinding> primaryBindings = new HashSet<IBinding>(); ASTNameCollector nc = new ASTNameCollector(fSelectedText); ast.accept(nc); IASTName[] candidates = nc.getNames(); for (IASTName astName : candidates) { try { IBinding b = astName.resolveBinding(); if (b != null && !(b instanceof IProblemBinding)) { primaryBindings.add(b); } } catch (RuntimeException e) { CUIPlugin.log(e); } } // Search the index, also. final IndexFilter filter = IndexFilter.getDeclaredBindingFilter(ast.getLinkage().getLinkageID(), false); final IIndexBinding[] idxBindings = fIndex.findBindings(name, false, filter, fMonitor); for (IIndexBinding idxBinding : idxBindings) { primaryBindings.add(idxBinding); } // Search for a macro in the index. IIndexMacro[] macros = fIndex.findMacros(name, filter, fMonitor); for (IIndexMacro macro : macros) { ICElement elem = IndexUI.getCElementForMacro(project, fIndex, macro); if (elem != null) { elems.add(elem); } } Collection<IBinding> secondaryBindings; if (ast instanceof ICPPASTTranslationUnit) { secondaryBindings = cppRemoveSecondaryBindings(primaryBindings, sourceName); } else { secondaryBindings = defaultRemoveSecondaryBindings(primaryBindings, sourceName); } // Convert bindings to CElements. Collection<IBinding> bs = primaryBindings; for (int k = 0; k < 2; k++) { for (IBinding binding : bs) { IName[] names = findNames(fIndex, ast, kind, binding); // Exclude names of the same kind. for (int i = 0; i < names.length; i++) { if (getNameKind(names[i]) == kind) { names[i] = null; } } names = (IName[]) ArrayUtil.removeNulls(IName.class, names); convertToCElements(project, fIndex, names, elems); } // In case we did not find anything, consider the secondary bindings. if (!elems.isEmpty()) break; bs = secondaryBindings; } if (navigateCElements(elems)) { return true; } if (sourceName != null && sourceName.isDeclaration()) { // Select the name at the current location as the last resort. return navigateToName(sourceName); } } catch (CoreException e) { CUIPlugin.log(e); } } return false; }