private static PsiPackage findLongestExistingPackage(@NotNull Module module, @NotNull String packageName) {
    String nameToMatch = packageName;
    while (true) {
      Query<VirtualFile> vFiles = ModulePackageIndex.getInstance(module).getDirsByPackageName(nameToMatch, false);
      PsiDirectory directory = getWritableModuleDirectory(module, vFiles);
      if (directory != null) return JavaDirectoryService.getInstance().getPackage(directory);

      int lastDotIndex = nameToMatch.lastIndexOf('.');
      if (lastDotIndex >= 0) {
        nameToMatch = nameToMatch.substring(0, lastDotIndex);
      }
      else {
        return null;
      }
    }
  }
private static PsiPackage findLongestExistingPackage(@NotNull Module module, @NotNull String packageName)