mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-04 17:20:55 +07:00
[java, quickfix] Single-step Dependency Addition for Maven or Android Projects along with jigsaw modules. IDEA-341660
GitOrigin-RevId: e92f26e071b7781b26ea9b6905f9082c16742baf
This commit is contained in:
committed by
intellij-monorepo-bot
parent
23e6424faa
commit
d5da0ada2b
@@ -1,13 +1,11 @@
|
|||||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||||
package com.intellij.openapi.roots.impl;
|
package com.intellij.openapi.roots.impl;
|
||||||
|
|
||||||
import com.intellij.codeInsight.daemon.impl.analysis.JavaModuleGraphUtil;
|
|
||||||
import com.intellij.codeInsight.daemon.impl.quickfix.LocateLibraryDialog;
|
import com.intellij.codeInsight.daemon.impl.quickfix.LocateLibraryDialog;
|
||||||
import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix;
|
import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix;
|
||||||
import com.intellij.ide.JavaUiBundle;
|
import com.intellij.ide.JavaUiBundle;
|
||||||
import com.intellij.jarRepository.JarRepositoryManager;
|
import com.intellij.jarRepository.JarRepositoryManager;
|
||||||
import com.intellij.jarRepository.RepositoryAttachDialog;
|
import com.intellij.jarRepository.RepositoryAttachDialog;
|
||||||
import com.intellij.openapi.application.ReadAction;
|
|
||||||
import com.intellij.openapi.application.WriteAction;
|
import com.intellij.openapi.application.WriteAction;
|
||||||
import com.intellij.openapi.diagnostic.Logger;
|
import com.intellij.openapi.diagnostic.Logger;
|
||||||
import com.intellij.openapi.module.LanguageLevelUtil;
|
import com.intellij.openapi.module.LanguageLevelUtil;
|
||||||
@@ -24,11 +22,9 @@ import com.intellij.openapi.roots.libraries.ui.OrderRoot;
|
|||||||
import com.intellij.openapi.ui.Messages;
|
import com.intellij.openapi.ui.Messages;
|
||||||
import com.intellij.openapi.util.EmptyRunnable;
|
import com.intellij.openapi.util.EmptyRunnable;
|
||||||
import com.intellij.pom.java.LanguageLevel;
|
import com.intellij.pom.java.LanguageLevel;
|
||||||
import com.intellij.psi.PsiJavaModule;
|
|
||||||
import com.intellij.util.PathUtil;
|
import com.intellij.util.PathUtil;
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
import com.intellij.util.containers.ContainerUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.concurrency.Promise;
|
import org.jetbrains.concurrency.Promise;
|
||||||
import org.jetbrains.concurrency.Promises;
|
import org.jetbrains.concurrency.Promises;
|
||||||
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryDescription;
|
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryDescription;
|
||||||
@@ -51,22 +47,12 @@ public class IdeaProjectModelModifier extends JavaProjectModelModifier {
|
|||||||
@Override
|
@Override
|
||||||
public Promise<Void> addModuleDependency(@NotNull Module from, @NotNull Module to, @NotNull DependencyScope scope, boolean exported) {
|
public Promise<Void> addModuleDependency(@NotNull Module from, @NotNull Module to, @NotNull DependencyScope scope, boolean exported) {
|
||||||
ModuleRootModificationUtil.addDependency(from, to, scope, exported);
|
ModuleRootModificationUtil.addDependency(from, to, scope, exported);
|
||||||
|
|
||||||
PsiJavaModule fromModule = ReadAction.compute(() -> JavaModuleGraphUtil.findDescriptorByModule(from, scope == DependencyScope.TEST));
|
|
||||||
PsiJavaModule toModule = ReadAction.compute(() -> JavaModuleGraphUtil.findDescriptorByModule(to, scope == DependencyScope.TEST));
|
|
||||||
addJigsawModule(fromModule, toModule, scope, exported);
|
|
||||||
|
|
||||||
return Promises.resolvedPromise(null);
|
return Promises.resolvedPromise(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Promise<Void> addLibraryDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope, boolean exported) {
|
public Promise<Void> addLibraryDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope, boolean exported) {
|
||||||
WriteAction.run(() -> OrderEntryUtil.addLibraryToRoots(from, library, scope, exported));
|
WriteAction.run(() -> OrderEntryUtil.addLibraryToRoots(from, library, scope, exported));
|
||||||
|
|
||||||
PsiJavaModule fromModule = ReadAction.compute(() -> JavaModuleGraphUtil.findDescriptorByModule(from, scope == DependencyScope.TEST));
|
|
||||||
PsiJavaModule toLibrary = ReadAction.compute(() -> JavaModuleGraphUtil.findDescriptorByLibrary(library, from.getProject()));
|
|
||||||
addJigsawModule(fromModule, toLibrary, scope, exported);
|
|
||||||
|
|
||||||
return Promises.resolvedPromise(null);
|
return Promises.resolvedPromise(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,19 +95,6 @@ public class IdeaProjectModelModifier extends JavaProjectModelModifier {
|
|||||||
final List<String> urls = OrderEntryFix.refreshAndConvertToUrls(classesRoots);
|
final List<String> urls = OrderEntryFix.refreshAndConvertToUrls(classesRoots);
|
||||||
if (modules.size() == 1) {
|
if (modules.size() == 1) {
|
||||||
ModuleRootModificationUtil.addModuleLibrary(firstModule, libraryName, urls, Collections.emptyList(), scope);
|
ModuleRootModificationUtil.addModuleLibrary(firstModule, libraryName, urls, Collections.emptyList(), scope);
|
||||||
|
|
||||||
if (libraryName != null) {
|
|
||||||
ReadAction.run(() -> {
|
|
||||||
Library library = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject).getLibraryByName(libraryName);
|
|
||||||
if (library == null) return;
|
|
||||||
|
|
||||||
for (Module module : modules) {
|
|
||||||
PsiJavaModule fromModule = JavaModuleGraphUtil.findDescriptorByModule(module, scope == DependencyScope.TEST);
|
|
||||||
PsiJavaModule toLibrary = JavaModuleGraphUtil.findDescriptorByLibrary(library, module.getProject());
|
|
||||||
addJigsawModule(fromModule, toLibrary, scope, false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
WriteAction.run(() -> {
|
WriteAction.run(() -> {
|
||||||
@@ -134,12 +107,6 @@ public class IdeaProjectModelModifier extends JavaProjectModelModifier {
|
|||||||
model.commit();
|
model.commit();
|
||||||
for (Module module : modules) {
|
for (Module module : modules) {
|
||||||
ModuleRootModificationUtil.addDependency(module, library, scope, false);
|
ModuleRootModificationUtil.addDependency(module, library, scope, false);
|
||||||
|
|
||||||
ReadAction.run(() -> {
|
|
||||||
PsiJavaModule fromModule = JavaModuleGraphUtil.findDescriptorByModule(module, scope == DependencyScope.TEST);
|
|
||||||
PsiJavaModule toLibrary = JavaModuleGraphUtil.findDescriptorByLibrary(library, module.getProject());
|
|
||||||
addJigsawModule(fromModule, toLibrary, scope, false);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -161,13 +128,4 @@ public class IdeaProjectModelModifier extends JavaProjectModelModifier {
|
|||||||
}
|
}
|
||||||
return Promises.resolvedPromise(null);
|
return Promises.resolvedPromise(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void addJigsawModule(@Nullable PsiJavaModule from,
|
|
||||||
@Nullable PsiJavaModule to,
|
|
||||||
@NotNull DependencyScope scope,
|
|
||||||
boolean exported) {
|
|
||||||
if (from == null || to == null) return;
|
|
||||||
WriteAction.run(() -> JavaModuleGraphUtil.addDependency(from, to.getName(), scope, exported));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||||
package com.intellij.openapi.roots.impl;
|
package com.intellij.openapi.roots.impl;
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.daemon.impl.analysis.JavaModuleGraphUtil;
|
||||||
|
import com.intellij.openapi.application.ReadAction;
|
||||||
|
import com.intellij.openapi.application.WriteAction;
|
||||||
import com.intellij.openapi.module.LanguageLevelUtil;
|
import com.intellij.openapi.module.LanguageLevelUtil;
|
||||||
import com.intellij.openapi.module.Module;
|
import com.intellij.openapi.module.Module;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
@@ -9,9 +12,12 @@ import com.intellij.openapi.projectRoots.ex.JavaSdkUtil;
|
|||||||
import com.intellij.openapi.roots.*;
|
import com.intellij.openapi.roots.*;
|
||||||
import com.intellij.openapi.roots.ex.ProjectRootManagerEx;
|
import com.intellij.openapi.roots.ex.ProjectRootManagerEx;
|
||||||
import com.intellij.openapi.roots.libraries.Library;
|
import com.intellij.openapi.roots.libraries.Library;
|
||||||
|
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
|
||||||
import com.intellij.openapi.util.EmptyRunnable;
|
import com.intellij.openapi.util.EmptyRunnable;
|
||||||
import com.intellij.pom.java.LanguageLevel;
|
import com.intellij.pom.java.LanguageLevel;
|
||||||
|
import com.intellij.psi.PsiJavaModule;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.concurrency.Promise;
|
import org.jetbrains.concurrency.Promise;
|
||||||
import org.jetbrains.concurrency.Promises;
|
import org.jetbrains.concurrency.Promises;
|
||||||
|
|
||||||
@@ -27,35 +33,44 @@ public final class JavaProjectModelModificationServiceImpl extends JavaProjectMo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Promise<Void> addDependency(@NotNull Module from, @NotNull Module to, @NotNull DependencyScope scope, boolean exported) {
|
public Promise<Void> addDependency(@NotNull Module from, @NotNull Module to, @NotNull DependencyScope scope, boolean exported) {
|
||||||
for (JavaProjectModelModifier modifier : getModelModifiers()) {
|
Promise<Void> promise = null;
|
||||||
Promise<Void> promise = modifier.addModuleDependency(from, to, scope, exported);
|
List<JavaProjectModelModifier> modifiers = getModelModifiers();
|
||||||
if (promise != null) {
|
for (int i = 0; i < modifiers.size() && promise == null; i++) {
|
||||||
return promise;
|
promise = modifiers.get(i).addModuleDependency(from, to, scope, exported);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return Promises.rejectedPromise();
|
promise = promise == null ? Promises.rejectedPromise() : promise;
|
||||||
|
return promise.onSuccess(v -> addJigsawModule(from, to, scope, exported));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Promise<Void> addDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope, boolean exported) {
|
public Promise<Void> addDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope, boolean exported) {
|
||||||
for (JavaProjectModelModifier modifier : getModelModifiers()) {
|
Promise<Void> promise = null;
|
||||||
Promise<Void> promise = modifier.addLibraryDependency(from, library, scope, exported);
|
List<JavaProjectModelModifier> modifiers = getModelModifiers();
|
||||||
if (promise != null) {
|
for (int i = 0; i < modifiers.size() && promise == null; i++) {
|
||||||
return promise;
|
promise = modifiers.get(i).addLibraryDependency(from, library, scope, exported);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return Promises.rejectedPromise();
|
promise = promise == null ? Promises.rejectedPromise() : promise;
|
||||||
|
return promise.onSuccess(v -> addJigsawModule(from, library, scope, exported));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Promise<Void> addDependency(@NotNull Collection<? extends Module> from, @NotNull ExternalLibraryDescriptor libraryDescriptor, @NotNull DependencyScope scope) {
|
public Promise<Void> addDependency(@NotNull Collection<? extends Module> from,
|
||||||
for (JavaProjectModelModifier modifier : getModelModifiers()) {
|
@NotNull ExternalLibraryDescriptor libraryDescriptor,
|
||||||
Promise<Void> promise = modifier.addExternalLibraryDependency(from, libraryDescriptor, scope);
|
@NotNull DependencyScope scope) {
|
||||||
if (promise != null) {
|
Promise<Void> promise = null;
|
||||||
return promise;
|
List<JavaProjectModelModifier> modifiers = getModelModifiers();
|
||||||
|
for (int i = 0; i < modifiers.size() && promise == null; i++) {
|
||||||
|
promise = modifiers.get(i).addExternalLibraryDependency(from, libraryDescriptor, scope);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return Promises.rejectedPromise();
|
promise = promise == null ? Promises.rejectedPromise() : promise;
|
||||||
|
return promise.onSuccess(v -> {
|
||||||
|
Library library = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject)
|
||||||
|
.getLibraryByName(libraryDescriptor.getPresentableName());
|
||||||
|
from.forEach(m -> addJigsawModule(m, library, scope, false));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -88,4 +103,31 @@ public final class JavaProjectModelModificationServiceImpl extends JavaProjectMo
|
|||||||
private List<JavaProjectModelModifier> getModelModifiers() {
|
private List<JavaProjectModelModifier> getModelModifiers() {
|
||||||
return JavaProjectModelModifier.EP_NAME.getExtensionList(myProject);
|
return JavaProjectModelModifier.EP_NAME.getExtensionList(myProject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void addJigsawModule(@NotNull Module from,
|
||||||
|
@NotNull Module to,
|
||||||
|
@NotNull DependencyScope scope,
|
||||||
|
boolean exported) {
|
||||||
|
PsiJavaModule toModule = ReadAction.compute(() -> JavaModuleGraphUtil.findDescriptorByModule(to, scope == DependencyScope.TEST));
|
||||||
|
addJigsawModule(from, toModule, scope, exported);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addJigsawModule(@NotNull Module from,
|
||||||
|
@Nullable Library library,
|
||||||
|
@NotNull DependencyScope scope,
|
||||||
|
boolean exported) {
|
||||||
|
if (library == null) return;
|
||||||
|
PsiJavaModule toModule = ReadAction.compute(() -> JavaModuleGraphUtil.findDescriptorByLibrary(library, from.getProject()));
|
||||||
|
addJigsawModule(from, toModule, scope, exported);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addJigsawModule(@NotNull Module from,
|
||||||
|
@Nullable PsiJavaModule to,
|
||||||
|
@NotNull DependencyScope scope,
|
||||||
|
boolean exported) {
|
||||||
|
if (to == null) return;
|
||||||
|
PsiJavaModule fromModule = ReadAction.compute(() -> JavaModuleGraphUtil.findDescriptorByModule(from, scope == DependencyScope.TEST));
|
||||||
|
if (fromModule == null) return;
|
||||||
|
WriteAction.run(() -> JavaModuleGraphUtil.addDependency(fromModule, to.getName(), scope, exported));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user