import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.libraries.LibraryUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.packaging.elements.PackagingElement;
import com.intellij.packaging.elements.PackagingElementFactory;
@Override
public String getPresentableName() {
- final String name = myLibrary.getName();
- if (name != null) {
- return name;
- }
- final VirtualFile[] files = myLibrary.getFiles(OrderRootType.CLASSES);
- return files.length > 0 ? files[0].getName() : "Empty Library";
+ return LibraryUtil.getPresentableName(myLibrary);
}
@Override
--- /dev/null
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.codeInsight.daemon.impl.quickfix;
+
+import com.intellij.codeInsight.daemon.QuickFixBundle;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.module.Module;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.roots.DependencyScope;
+import com.intellij.openapi.roots.ProjectModelModificationService;
+import com.intellij.openapi.roots.impl.libraries.LibraryEx;
+import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.libraries.LibraryUtil;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.PsiReference;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * @author nik
+ */
+class AddLibraryToDependenciesFix extends OrderEntryFix {
+ private final Library myLibrary;
+ private final Module myCurrentModule;
+ private final PsiReference myReference;
+ private final String myQualifiedClassName;
+
+ public AddLibraryToDependenciesFix(@NotNull Module currentModule,
+ @NotNull Library library,
+ @NotNull PsiReference reference,
+ @Nullable String qualifiedClassName) {
+ myLibrary = library;
+ myCurrentModule = currentModule;
+ myReference = reference;
+ myQualifiedClassName = qualifiedClassName;
+ }
+
+ @Override
+ @NotNull
+ public String getText() {
+ return QuickFixBundle.message("orderEntry.fix.add.library.to.classpath", LibraryUtil.getPresentableName(myLibrary));
+ }
+
+ @Override
+ @NotNull
+ public String getFamilyName() {
+ return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath");
+ }
+
+ @Override
+ public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
+ return !project.isDisposed() && !myCurrentModule.isDisposed() && !((LibraryEx)myLibrary).isDisposed();
+ }
+
+ @Override
+ public void invoke(@NotNull final Project project, @Nullable final Editor editor, PsiFile file) {
+ DependencyScope scope = suggestScopeByLocation(myCurrentModule, myReference.getElement());
+ ProjectModelModificationService.getInstance(project).addDependency(myCurrentModule, myLibrary, scope);
+ if (editor != null) {
+ importClass(myCurrentModule, editor, myReference, myQualifiedClassName);
+ }
+ }
+}
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.actions.AddImportAction;
import com.intellij.openapi.roots.ExternalLibraryDescriptor;
import com.intellij.codeInsight.daemon.quickFix.ExternalLibraryResolver;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.*;
-import com.intellij.openapi.roots.impl.OrderEntryUtil;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
!ModuleRootManager.getInstance(currentModule).getFileIndex().isInTestSourceContent(classVFile))) {
continue;
}
- final OrderEntryFix platformFix = new OrderEntryFix() {
- @Override
- @NotNull
- public String getText() {
- return QuickFixBundle.message("orderEntry.fix.add.library.to.classpath", libraryEntry.getPresentableName());
- }
-
- @Override
- @NotNull
- public String getFamilyName() {
- return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath");
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return !project.isDisposed() && !currentModule.isDisposed() && libraryEntry.isValid();
- }
-
- @Override
- public void invoke(@NotNull final Project project, @Nullable final Editor editor, PsiFile file) {
- OrderEntryUtil.addLibraryToRoots(libraryEntry, currentModule);
- if (editor != null) {
- DumbService.getInstance(project).withAlternativeResolveEnabled(new Runnable() {
- @Override
- public void run() {
- new AddImportAction(project, reference, editor, aClass).execute();
- }
- });
- }
- }
- };
-
- final OrderEntryFix providedFix = provideFix(new Function<MissingDependencyFixProvider, OrderEntryFix>() {
- @Override
- public OrderEntryFix fun(MissingDependencyFixProvider provider) {
- return provider.getAddLibraryToClasspathFix(reference, platformFix, currentModule, libraryEntry, aClass);
- }
- });
- final OrderEntryFix fix = ObjectUtils.notNull(providedFix, platformFix);
- registrar.register(fix);
- result.add(fix);
+ final OrderEntryFix platformFix = new AddLibraryToDependenciesFix(currentModule, library, reference, aClass.getQualifiedName());
+ registrar.register(platformFix);
+ result.add(platformFix);
}
}
}
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
+import com.intellij.openapi.roots.libraries.Library;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public abstract void addDependency(@NotNull Collection<Module> from, @NotNull ExternalLibraryDescriptor libraryDescriptor,
@NotNull DependencyScope scope);
+
+ public abstract void addDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope);
}
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.module.Module;
+import com.intellij.openapi.roots.libraries.Library;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public abstract boolean addExternalLibraryDependency(@NotNull Collection<Module> modules,
@NotNull ExternalLibraryDescriptor descriptor,
@NotNull DependencyScope scope);
+
+ public abstract boolean addLibraryDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope);
}
}
return true;
}
+
+ @Override
+ public boolean addLibraryDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope) {
+ OrderEntryUtil.addLibraryToRoots(from, library);
+ return true;
+ }
}
import com.intellij.openapi.roots.ExternalLibraryDescriptor;
import com.intellij.openapi.roots.ProjectModelModificationService;
import com.intellij.openapi.roots.ProjectModelModifier;
+import com.intellij.openapi.roots.libraries.Library;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
}
}
+ @Override
+ public void addDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope) {
+ for (ProjectModelModifier modifier : getModelModifiers()) {
+ if (modifier.addLibraryDependency(from, library, scope)) {
+ return;
+ }
+ }
+ }
+
@NotNull
private ProjectModelModifier[] getModelModifiers() {
return ProjectModelModifier.EP_NAME.getExtensions(myProject);
public static void addLibraryToRoots(final LibraryOrderEntry libraryOrderEntry, final Module module) {
Library library = libraryOrderEntry.getLibrary();
if (library == null) return;
+ addLibraryToRoots(module, library);
+ }
+
+ public static void addLibraryToRoots(@NotNull Module module, @NotNull Library library) {
final ModuleRootManager manager = ModuleRootManager.getInstance(module);
final ModifiableRootModel rootModel = manager.getModifiableModel();
- if (libraryOrderEntry.isModuleLevel()) {
+ if (library.getTable() == null) {
final Library jarLibrary = rootModel.getModuleLibraryTable().createLibrary();
final Library.ModifiableModel libraryModel = jarLibrary.getModifiableModel();
for (OrderRootType orderRootType : OrderRootType.getAllTypes()) {
}
return null;
}
+
+ @NotNull
+ public static String getPresentableName(@NotNull Library library) {
+ final String name = library.getName();
+ if (name != null) {
+ return name;
+ }
+ final VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
+ return files.length > 0 ? files[0].getName() : "Empty Library";
+ }
}