private void reindexPackage(RPackage rPackage, RPackage indexPackage) {
    // replace package

    RHelperUtil.LOG.info("detecting methods in " + rPackage.getName());

    String allFunsConcat =
        RHelperUtil.runCommand("cat(getNamespaceExports('" + rPackage.getName() + "'))").trim();
    List<String> allFuns = Splitter.on(" ").trimResults().splitToList(allFunsConcat);

    List<Function> functions =
        Lists.transform(
            allFuns,
            new com.google.common.base.Function<String, Function>() {
              @Override
              public Function apply(String s) {
                return new Function(s, "NA");
              }
            });

    rPackage.setFunctions(functions);

    if (indexPackage != null) {
      allPackages.remove(indexPackage);
    }

    allPackages.add(rPackage);
  }
  @Nullable
  public RPackage getByName(String packageName) {
    for (RPackage aPackage : getInstance().getPackages()) {
      if (aPackage.getName().equals(packageName)) {
        return aPackage;
      }
    }

    return null;
  }
  @NotNull
  public List<RPackage> getContainingPackages(String functionName) {
    List<RPackage> funPackages = Lists.newArrayList();

    for (RPackage rPackage : getPackages()) {
      if (rPackage.hasFunction(functionName)) {
        funPackages.add(rPackage);
      }
    }

    return funPackages;
  }
  public List<Function> getFunctionByName(String funName) {
    List<Function> funList = new ArrayList<Function>();

    for (RPackage aPackage : getInstance().getPackages()) {
      Function function = aPackage.getFunction(funName);

      if (function != null) {
        funList.add(function);
      }
    }

    return funList;
  }
  public static List<Function> getFunctionByName(
      String funName, @Nullable Collection<RPackage> importedPackages) {

    RPackageService packageService = RPackageService.getInstance();

    if (packageService == null) return Collections.emptyList();

    // if the user didn't import anything do a global search todo: does this make sense???
    if (importedPackages == null) {
      importedPackages = packageService.getPackages();
    } else {
      importedPackages = addImportDependencies(importedPackages);
    }

    List<Function> funs = new ArrayList<Function>();
    for (RPackage importedPackage : importedPackages) {
      if (importedPackage.hasFunction(funName)) funs.add(importedPackage.getFunction(funName));
    }

    return funs;
  }
  @NotNull
  public Collection<RPackage> getDependencies(RPackage somePckge) {
    Collection<RPackage> deps = new HashSet<RPackage>();

    for (String dep : somePckge.getDependencyNames()) {
      RPackage depPckg = getByName(dep);
      if (depPckg == null) continue;

      deps.add(depPckg);
    }

    return deps;
  }
 public Set<RPackage> getImporting(@NotNull RPackage rPackage) {
   return allPackages
       .stream()
       .filter(p -> p.getImports().contains(rPackage.getName()))
       .collect(Collectors.toSet());
 }
  /**
   * @param packageNames Packages to be reindexd if version has changed or package has been
   *     installed since last indexing run.If non are provided all packages will be refreshed.
   */
  public boolean refreshIndex(String... packageNames) {
    RHelperUtil.runHelperWithArgs(RHelperUtil.R_HELPER_INSTALL_TIDYVERSE);

    Set<RPackage> installedPackages = LocalRUtil.getInstalledPackages();

    // remove packages from index that are no longer present
    if (packageNames.length == 0) {
      Sets.SetView<RPackage> noLongerInstalled = Sets.difference(allPackages, installedPackages);
      allPackages.removeAll(noLongerInstalled);
    }

    // cut down packges to be refreshed to speed up calculations
    //        if(packageNames.length>0){
    //            installedPackages = installedPackages.stream().
    //                    filter(p -> Arrays.asList(packageNames).contains(p.getName())).
    //                    collect(Collectors.toSet());
    //        }

    ExecutorService executorService = Executors.newFixedThreadPool(8);

    final boolean[] hasChanged = {false};

    for (final RPackage rPackage : installedPackages) {
      final RPackage indexPackage = getByName(rPackage.getName());

      if (indexPackage != null
          && ObjectUtils.equals(indexPackage.getVersion(), rPackage.getVersion())) {
        continue;
      }

      executorService.submit(
          new Runnable() {
            @Override
            public void run() {
              reindexPackage(rPackage, indexPackage);
              hasChanged[0] = true;
            }
          });
    }

    executorService.shutdown();
    try {
      executorService.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    //        allPackages.clear();
    //        allPackages.addAll(installedPackages);

    if (hasChanged[0]) {
      if (ApplicationManager.getApplication() != null) {
        Project[] projects = ProjectManager.getInstance().getOpenProjects();
        for (Project project : projects) {
          if (project.isInitialized() && project.isOpen() && !project.isDefault()) {
            SpellCheckerManager spellCheckerManager = SpellCheckerManager.getInstance(project);
            EditableDictionary dictionary = spellCheckerManager.getUserDictionary();

            for (RPackage rPackage : RPackageService.getInstance().allPackages) {
              dictionary.addToDictionary(rPackage.getName());
              dictionary.addToDictionary(rPackage.getFunctionNames());
            }

            DaemonCodeAnalyzer.getInstance(project).restart();
          }
        }
      }
    }

    return hasChanged[0];
  }