@Nullable
  public static PsiDirectory findOrCreateDirectoryForPackage(@NotNull final Project project,
                                                             @Nullable Module module,
                                                             GlobalSearchScope scope,
                                                             String packageName,
                                                             PsiDirectory baseDir,
                                                             boolean askUserToCreate,
                                                             ThreeState chooseFlag) throws IncorrectOperationException {
    PsiDirectory psiDirectory = null;
    if (chooseFlag == ThreeState.UNSURE && StringUtil.isNotEmpty(packageName)) {
      String rootPackage = findLongestExistingPackage(project, packageName, scope);
      if (rootPackage != null) {
        int beginIndex = rootPackage.length() + 1;
        packageName = beginIndex < packageName.length() ? packageName.substring(beginIndex) : "";
        String postfixToShow = packageName.replace('.', File.separatorChar);
        if (packageName.length() > 0) {
          postfixToShow = File.separatorChar + postfixToShow;
        }
        psiDirectory =
          DirectoryChooserUtil.selectDirectory(project, getPackageDirectories(project, rootPackage, scope), baseDir, postfixToShow);
        if (psiDirectory == null) return null;
      }
    }

    if (psiDirectory == null) {
      if (chooseFlag == ThreeState.NO && baseDir != null) {
        VirtualFile sourceRoot = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(baseDir.getVirtualFile());
        psiDirectory = sourceRoot != null ? PsiManager.getInstance(project).findDirectory(sourceRoot) : null;
      }
      else {
        if (module != null && !checkSourceRootsConfigured(module)) return null;
        final GlobalSearchScope scope_ = scope;
        List<PsiDirectory> dirs =
          ContainerUtil
            .mapNotNull(ProjectRootManager.getInstance(project).getContentSourceRoots(),
                        virtualFile -> scope_.contains(virtualFile) ? PsiManager.getInstance(project).findDirectory(virtualFile) : null);
        psiDirectory = DirectoryChooserUtil.selectDirectory(project, dirs.toArray(PsiDirectory.EMPTY_ARRAY), baseDir,
                                                            File.separatorChar + packageName.replace('.', File.separatorChar));
        if (psiDirectory == null) return null;
        final VirtualFile sourceRoot = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(psiDirectory.getVirtualFile());
        psiDirectory = sourceRoot != null ? PsiManager.getInstance(project).findDirectory(sourceRoot) : null;
      }
    }

    String restOfName = packageName;
    boolean askedToCreate = false;
    while (restOfName.length() > 0) {
      final String name = getLeftPart(restOfName);
      PsiDirectory foundExistingDirectory = psiDirectory != null ? psiDirectory.findSubdirectory(name) : null;
      if (foundExistingDirectory == null) {
        if (!askedToCreate && askUserToCreate) {
          if (!ApplicationManager.getApplication().isUnitTestMode()) {
            int toCreate = Messages.showYesNoDialog(project,
                                                    IdeBundle.message("prompt.create.non.existing.package", packageName),
                                                    IdeBundle.message("title.package.not.found"),
                                                    Messages.getQuestionIcon());
            if (toCreate != Messages.YES) {
              return null;
            }
          }
          askedToCreate = true;
        }

        final PsiDirectory psiDirectory_ = psiDirectory;
        try {
          psiDirectory = WriteAction.compute(() -> psiDirectory_ != null ? psiDirectory_.createSubdirectory(name) : null);
        }
        catch (IncorrectOperationException e) {
          throw e;
        }
        catch (Exception e) {
          LOG.error(e);
        }
      }
      else {
        psiDirectory = foundExistingDirectory;
      }
      restOfName = cutLeftPart(restOfName);
    }
    return psiDirectory;
  }
@Nullable
  public static PsiDirectory findOrCreateDirectoryForPackage(@NotNull final Project project,
                                                             @Nullable Module module,
                                                             GlobalSearchScope scope,
                                                             String packageName,
                                                             PsiDirectory baseDir,
                                                             boolean askUserToCreate,
                                                             ThreeState chooseFlag)