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;
  }
 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];
  }