Ejemplo n.º 1
0
 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;
   }
   declNames = (IName[]) ArrayUtil.removeNulls(IName.class, declNames);
   if (declNames.length == 0) {
     declNames =
         index.findNames(
             binding, IIndex.FIND_DECLARATIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES);
   }
   return declNames;
 }
Ejemplo n.º 2
0
  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;
  }