IDEA-160131 Consider adding roots provided by AdditionalLibraryRootsProvider to External Libraries

This commit is contained in:
Sergey Simonchik
2017-01-31 19:19:55 +03:00
parent d2d4e87765
commit 5ae1e907d7
8 changed files with 237 additions and 21 deletions
@@ -158,8 +158,10 @@ public class DirectoryIndexTest extends IdeaTestCase {
PlatformTestUtil.registerExtension(AdditionalLibraryRootsProvider.EP_NAME, new AdditionalLibraryRootsProvider() {
@NotNull
@Override
public Collection<VirtualFile> getAdditionalProjectLibrarySourceRoots(@NotNull Project project) {
return myProject == project ? Collections.singletonList(myLibAdditionalSrcDir) : Collections.emptyList();
public Collection<SyntheticLibrary> getAdditionalProjectLibraries(@NotNull Project project) {
return myProject == project ? Collections.singletonList(
SyntheticLibrary.newFixedLibrary(null, Collections.singletonList(myLibAdditionalSrcDir))
) : Collections.emptyList();
}
}, getTestRootDisposable());
@@ -38,10 +38,7 @@ import com.intellij.util.PlatformIcons;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.*;
public class ExternalLibrariesNode extends ProjectViewNode<String> {
public ExternalLibrariesNode(Project project, ViewSettings viewSettings) {
@@ -50,7 +47,11 @@ public class ExternalLibrariesNode extends ProjectViewNode<String> {
@Override
public boolean contains(@NotNull VirtualFile file) {
ProjectFileIndex index = ProjectRootManager.getInstance(getProject()).getFileIndex();
Project project = getProject();
if (project == null) {
return false;
}
ProjectFileIndex index = ProjectFileIndex.getInstance(project);
if (!index.isInLibrarySource(file) && !index.isInLibraryClasses(file)) return false;
return someChildContainsFile(file, false);
@@ -59,9 +60,13 @@ public class ExternalLibrariesNode extends ProjectViewNode<String> {
@NotNull
@Override
public Collection<? extends AbstractTreeNode> getChildren() {
final List<AbstractTreeNode> children = new ArrayList<>();
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(getProject()).getFileIndex();
Module[] modules = ModuleManager.getInstance(getProject()).getModules();
Project project = getProject();
if (project == null) {
return Collections.emptyList();
}
List<AbstractTreeNode> children = new ArrayList<>();
ProjectFileIndex fileIndex = ProjectFileIndex.getInstance(project);
Module[] modules = ModuleManager.getInstance(project).getModules();
Set<Library> processedLibraries = new THashSet<>();
Set<Sdk> processedSdk = new THashSet<>();
@@ -80,10 +85,10 @@ public class ExternalLibrariesNode extends ProjectViewNode<String> {
final String libraryName = library.getName();
if (libraryName == null || libraryName.length() == 0) {
addLibraryChildren(libraryOrderEntry, children, getProject(), this);
addLibraryChildren(libraryOrderEntry, children, project, this);
}
else {
children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(null, libraryOrderEntry), getSettings()));
children.add(new NamedLibraryElementNode(project, new NamedLibraryElement(null, libraryOrderEntry), getSettings()));
}
}
else if (orderEntry instanceof JdkOrderEntry) {
@@ -92,11 +97,19 @@ public class ExternalLibrariesNode extends ProjectViewNode<String> {
if (jdk != null) {
if (processedSdk.contains(jdk)) continue;
processedSdk.add(jdk);
children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(null, jdkOrderEntry), getSettings()));
children.add(new NamedLibraryElementNode(project, new NamedLibraryElement(null, jdkOrderEntry), getSettings()));
}
}
}
}
for (AdditionalLibraryRootsProvider provider : AdditionalLibraryRootsProvider.EP_NAME.getExtensions()) {
Collection<SyntheticLibrary> libraries = provider.getAdditionalProjectLibraries(project);
for (SyntheticLibrary library : libraries) {
if (library.getName() != null) {
children.add(new SyntheticLibraryElementNode(project, library, getSettings()));
}
}
}
return children;
}
@@ -0,0 +1,92 @@
/*
* Copyright 2000-2017 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.ide.projectView.impl.nodes;
import com.intellij.icons.AllIcons;
import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.SyntheticLibrary;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
public class SyntheticLibraryElementNode extends ProjectViewNode<SyntheticLibrary> {
public SyntheticLibraryElementNode(@NotNull Project project, @NotNull SyntheticLibrary library, ViewSettings settings) {
super(project, library, settings);
}
@Override
public boolean contains(@NotNull VirtualFile file) {
SyntheticLibrary library = getLibrary();
return VfsUtil.contains(file, library.getSourceRoots());
}
@NotNull
@Override
public Collection<AbstractTreeNode> getChildren() {
List<AbstractTreeNode> children = new ArrayList<>();
SyntheticLibrary library = getLibrary();
Project project = getProject();
if (project != null) {
PsiManager psiManager = PsiManager.getInstance(project);
for (VirtualFile file : library.getSourceRoots()) {
if (!file.isValid()) continue;
if (file.isDirectory()) {
PsiDirectory psiDir = psiManager.findDirectory(file);
if (psiDir != null) {
children.add(new PsiDirectoryNode(project, psiDir, getSettings()));
}
}
else {
PsiFile psiFile = psiManager.findFile(file);
if (psiFile != null) {
children.add(new PsiFileNode(project, psiFile, getSettings()));
}
}
}
}
return children;
}
@Override
public String getName() {
SyntheticLibrary library = getLibrary();
return StringUtil.notNullize(library.getName());
}
@NotNull
private SyntheticLibrary getLibrary() {
return Objects.requireNonNull(getValue());
}
@Override
protected void update(PresentationData presentation) {
presentation.setPresentableText(getName());
presentation.setIcon(AllIcons.Nodes.PpLibFolder);
}
}
@@ -87,7 +87,10 @@ public class FileBasedIndexScanRunnableCollectorImpl extends FileBasedIndexScanR
if (myProject.isDisposed()) {
return tasks;
}
contributedRoots = contributedRoots.append(provider.getAdditionalProjectLibrarySourceRoots(myProject));
contributedRoots = contributedRoots.append(
provider.getAdditionalProjectLibraries(myProject),
descriptor -> descriptor.getSourceRoots()
);
}
for (VirtualFile root : contributedRoots) {
if (visitedRoots.add(root)) {
@@ -151,6 +151,33 @@ public class VfsUtil extends VfsUtilCore {
return toVirtualFileArray(ancestorsList);
}
public static boolean contains(@NotNull VirtualFile file, @NotNull Collection<VirtualFile> roots) {
Set<VirtualFile> dirs = null;
for (VirtualFile root : roots) {
if (file.getFileSystem().equals(root.getFileSystem())) {
if (root.isDirectory()) {
if (dirs == null) {
dirs = new HashSet<>();
}
dirs.add(root);
}
else if (root.equals(file)) {
return true;
}
}
}
if (dirs != null) {
VirtualFile f = file;
while (f != null) {
if (dirs.contains(f)) {
return true;
}
f = f.getParent();
}
}
return false;
}
/**
* Gets the common ancestor for passed files, or {@code null} if the files do not have common ancestors.
*/
@@ -38,17 +38,28 @@ public abstract class AdditionalLibraryRootsProvider {
public static final ExtensionPointName<AdditionalLibraryRootsProvider> EP_NAME = ExtensionPointName.create("com.intellij.additionalLibraryRootsProvider");
/**
* Returns library source roots (analogous to {@code library.getFiles(OrderRootType.SOURCES)} for a given project.
* Returns of collection of {@link SyntheticLibrary}.
* This method is suitable when it's easier to collect all additional library roots associated with {@code Project},
* instead of {@code Module}. E.g. JavaScript libraries can be associated with files or folders allowing more
* fine-grained control.
* Files contained in the returned roots are considered as library source files:
* Files contained in the returned libraries roots are considered as library source files:
* {@link ProjectFileIndex#isInLibrarySource(VirtualFile)} should return {@code true} for them.
*
* @param project Project instance
* @return a collection of library source roots
* @return a collection of {@link SyntheticLibrary}
*/
@NotNull
public Collection<SyntheticLibrary> getAdditionalProjectLibraries(@NotNull Project project) {
//noinspection deprecation
Collection<VirtualFile> roots = getAdditionalProjectLibrarySourceRoots(project);
if (roots.isEmpty()) {
return Collections.emptyList();
}
return Collections.singletonList(SyntheticLibrary.newFixedLibrary(null, roots));
}
@SuppressWarnings("DeprecatedIsStillUsed")
@NotNull
public Collection<VirtualFile> getAdditionalProjectLibrarySourceRoots(@NotNull Project project) {
return Collections.emptyList();
}
@@ -0,0 +1,65 @@
/*
* Copyright 2000-2017 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.openapi.roots;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
/**
* A lightweight library definition comparing to {@link com.intellij.openapi.roots.libraries.Library}.
* When provided by {@link AdditionalLibraryRootsProvider}, a library of this type contributes the followings:
* <ul>
* <li>Source libraries roots ({@link #getSourceRoots()}) extends {@link com.intellij.psi.search.GlobalSearchScope#allScope(Project)}
* (in UI, "Project and Libraries" scope).
* Files contained in the returned roots are considered as library source files:
* {@link ProjectFileIndex#isInLibrarySource(VirtualFile)} returns {@code true} for them.
* <br>
* Unlike to {@code library.getFiles(OrderRootType.SOURCES)}, these source roots are not indexed and
* are not included in the classpath.</li>
* <li>An item in "External Libraries" in Project view if {@link #getName()} is not-null</li>
* </ul>
* @see AdditionalLibraryRootsProvider
*/
@ApiStatus.Experimental
public abstract class SyntheticLibrary {
@Nullable
public abstract String getName();
@NotNull
public abstract Collection<VirtualFile> getSourceRoots();
@NotNull
public static SyntheticLibrary newFixedLibrary(@Nullable String name, @NotNull Collection<VirtualFile> sourceRoots) {
return new SyntheticLibrary() {
@Nullable
@Override
public String getName() {
return name;
}
@NotNull
@Override
public Collection<VirtualFile> getSourceRoots() {
return sourceRoots;
}
};
}
}
@@ -168,10 +168,13 @@ public class RootIndex {
}
for (AdditionalLibraryRootsProvider provider : Extensions.getExtensions(AdditionalLibraryRootsProvider.EP_NAME)) {
Collection<VirtualFile> roots = ContainerUtil.filter(provider.getAdditionalProjectLibrarySourceRoots(project),
file -> ensureValid(file, provider));
info.libraryOrSdkSources.addAll(roots);
info.classAndSourceRoots.addAll(roots);
Collection<SyntheticLibrary> libraries = provider.getAdditionalProjectLibraries(project);
for (SyntheticLibrary descriptor : libraries) {
Collection<VirtualFile> roots = ContainerUtil.filter(descriptor.getSourceRoots(),
file -> ensureValid(file, project));
info.libraryOrSdkSources.addAll(roots);
info.classAndSourceRoots.addAll(roots);
}
}
for (DirectoryIndexExcludePolicy policy : Extensions.getExtensions(DirectoryIndexExcludePolicy.EP_NAME, project)) {
info.excludedFromProject.addAll(ContainerUtil.filter(policy.getExcludeRootsForProject(), file -> ensureValid(file, policy)));