private Promise<Void> addDependency(@NotNull Collection<? extends Module> fromModules,
                                      @NotNull final MavenId mavenId,
                                      @Nullable String minVersion,
                                      @Nullable String maxVersion,
                                      @Nullable String preferredVersion, @NotNull final DependencyScope scope) {
    final List<Trinity<MavenDomProjectModel, MavenId, String>> models = new ArrayList<>(fromModules.size());
    List<XmlFile> files = new ArrayList<>(fromModules.size());
    List<MavenProject> projectToUpdate = new ArrayList<>(fromModules.size());
    final String mavenScope = getMavenScope(scope);
    for (Module from : fromModules) {
      if (!myProjectsManager.isMavenizedModule(from)) return null;
      MavenProject fromProject = myProjectsManager.findProject(from);
      if (fromProject == null) return null;

      final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, fromProject.getFile());
      if (model == null) return null;

      String scopeToSet = null;
      String version = null;
      if (mavenId.getGroupId() != null && mavenId.getArtifactId() != null) {
        MavenDomDependency managedDependency =
          MavenDependencyCompletionUtil.findManagedDependency(model, myProject, mavenId.getGroupId(), mavenId.getArtifactId());
        if (managedDependency != null) {
          String managedScope = StringUtil.nullize(managedDependency.getScope().getStringValue(), true);
          scopeToSet = (managedScope == null && MavenConstants.SCOPE_COMPILE.equals(mavenScope)) ||
                       StringUtil.equals(managedScope, mavenScope)
                       ? null : mavenScope;
        }

        if (managedDependency == null || StringUtil.isEmpty(managedDependency.getVersion().getStringValue())) {
          version = selectVersion(mavenId, minVersion, maxVersion, preferredVersion);
          scopeToSet = mavenScope;
        }
      }

      models.add(Trinity.create(model, new MavenId(mavenId.getGroupId(), mavenId.getArtifactId(), version), scopeToSet));
      files.add(DomUtil.getFile(model));
      projectToUpdate.add(fromProject);
    }

    WriteCommandAction.writeCommandAction(myProject, PsiUtilCore.toPsiFileArray(files)).withName(MavenDomBundle.message("fix.add.dependency")).run(() -> {
                                                                                                 PsiDocumentManager pdm = PsiDocumentManager.getInstance(myProject);
      for (Trinity<MavenDomProjectModel, MavenId, String> trinity : models) {
        final MavenDomProjectModel model = trinity.first;
        MavenDomDependency dependency = MavenDomUtil.createDomDependency(model, null, trinity.second);
        String ms = trinity.third;
        if (ms != null) {
          dependency.getScope().setStringValue(ms);
        }
        Document document = pdm.getDocument(DomUtil.getFile(model));
        if (document != null) {
          pdm.doPostponedOperationsAndUnblockDocument(document);
          FileDocumentManager.getInstance().saveDocument(document);
        }
      }
    });
    return myProjectsManager.forceUpdateProjects(projectToUpdate);
  }
private Promise<Void> addDependency(@NotNull Collection<? extends Module> fromModules,
                                      @NotNull final MavenId mavenId,
                                      @Nullable String minVersion,
                                      @Nullable String maxVersion,
                                      @Nullable String preferredVersion, @NotNull final DependencyScope scope)