'add library to dependencies' fix migrated to use new ProjectModelModificationService

This commit is contained in:
nik
2015-08-07 18:47:18 +03:00
parent 42a7ca6bad
commit d7e4afab08
9 changed files with 118 additions and 50 deletions
@@ -20,6 +20,7 @@ import com.intellij.ide.projectView.PresentationData;
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;
@@ -97,12 +98,7 @@ public class LibrarySourceItem extends PackagingSourceItem {
@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
@@ -0,0 +1,76 @@
/*
* 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);
}
}
}
@@ -16,7 +16,6 @@
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;
@@ -30,7 +29,6 @@ import com.intellij.openapi.module.Module;
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;
@@ -178,47 +176,9 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
!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);
}
}
}
@@ -18,6 +18,7 @@ package com.intellij.openapi.roots;
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;
@@ -47,4 +48,6 @@ public abstract class ProjectModelModificationService {
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);
}
@@ -17,6 +17,7 @@ package com.intellij.openapi.roots;
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;
@@ -32,4 +33,6 @@ public abstract class ProjectModelModifier {
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);
}
@@ -84,4 +84,10 @@ public class IdeaProjectModelModifier extends ProjectModelModifier {
}
return true;
}
@Override
public boolean addLibraryDependency(@NotNull Module from, @NotNull Library library, @NotNull DependencyScope scope) {
OrderEntryUtil.addLibraryToRoots(from, library);
return true;
}
}
@@ -21,6 +21,7 @@ import com.intellij.openapi.roots.DependencyScope;
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;
@@ -53,6 +54,15 @@ public class ProjectModelModificationServiceImpl extends ProjectModelModificatio
}
}
@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);
@@ -136,10 +136,14 @@ public class OrderEntryUtil {
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()) {
@@ -162,4 +162,14 @@ public class LibraryUtil {
}
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";
}
}