find project extension by instance class

This commit is contained in:
Konstantin Bulenkov
2010-09-28 17:13:46 +04:00
parent 2f3a88f7fc
commit e310487092
7 changed files with 218 additions and 145 deletions
@@ -20,99 +20,48 @@
*/
package com.intellij.analysis;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.scope.packageSet.*;
import com.intellij.psi.search.scope.NonProjectFilesScope;
import com.intellij.psi.search.scope.ProjectProductionScope;
import com.intellij.psi.search.scope.TestsScope;
import com.intellij.psi.search.scope.packageSet.CustomScopesProvider;
import com.intellij.psi.search.scope.packageSet.NamedScope;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Konstantin Bulenkov
*/
public class PackagesScopesProvider implements CustomScopesProvider {
private NamedScope myProjectTestScope;
private NamedScope myProjectProductionScope;
private final Project myProject;
private final NamedScope myProjectTestScope;
private final NamedScope myProjectProductionScope;
private final NamedScope myNonProjectScope;
private final List<NamedScope> myScopes;
public static PackagesScopesProvider getInstance(Project project) {
for (CustomScopesProvider provider : Extensions.getExtensions(CUSTOM_SCOPES_PROVIDER, project)) {
if (provider instanceof PackagesScopesProvider) return (PackagesScopesProvider)provider;
}
return null;
return Extensions.findExtension(CUSTOM_SCOPES_PROVIDER, project, PackagesScopesProvider.class);
}
public PackagesScopesProvider(Project project) {
myProject = project;
myProjectTestScope = new TestsScope(project);
myProjectProductionScope = new ProjectProductionScope(project);
myNonProjectScope = new NonProjectFilesScope(project);
myScopes = Arrays.asList(myProjectProductionScope, myNonProjectScope, myProjectTestScope);
}
@NotNull
public List<NamedScope> getCustomScopes() {
final List<NamedScope> list = new ArrayList<NamedScope>();
list.add(getProjectProductionScope());
list.add(getProjectTestScope());
return list;
return myScopes;
}
public NamedScope getProjectTestScope() {
if (myProjectTestScope == null) {
final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
myProjectTestScope = new NamedScope(IdeBundle.message("predefined.scope.tests.name"), new PackageSet() {
public boolean contains(PsiFile file, NamedScopesHolder holder) {
final VirtualFile virtualFile = file.getVirtualFile();
return file.getProject() == myProject && virtualFile != null && index.isInTestSourceContent(virtualFile);
}
public PackageSet createCopy() {
return this;
}
public String getText() {
return PatternPackageSet.SCOPE_TEST+":*..*";
}
public int getNodePriority() {
return 0;
}
});
}
public NamedScope getProjectTestScope() {
return myProjectTestScope;
}
public NamedScope getProjectProductionScope() {
if (myProjectProductionScope == null) {
final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
myProjectProductionScope = new NamedScope(IdeBundle.message("predefined.scope.production.name"), new PackageSet() {
public boolean contains(PsiFile file, NamedScopesHolder holder) {
final VirtualFile virtualFile = file.getVirtualFile();
return file.getProject() == myProject
&& virtualFile != null
&& !index.isInTestSourceContent(virtualFile)
&& !index.isInLibraryClasses(virtualFile)
&& !index.isInLibrarySource(virtualFile)
;
}
public PackageSet createCopy() {
return this;
}
public String getText() {
return PatternPackageSet.SCOPE_SOURCE+":*..*";
}
public int getNodePriority() {
return 0;
}
});
}
return myProjectProductionScope;
}
}
@@ -109,6 +109,16 @@ public class Extensions {
throw new IllegalArgumentException("could not find extension implementation " + extClass);
}
public static <T, U extends T> U findExtension(ExtensionPointName<T> extensionPointName, AreaInstance areaInstance, Class<U> extClass) {
for (T t : getExtensions(extensionPointName, areaInstance)) {
if (extClass.isInstance(t)) {
//noinspection unchecked
return (U) t;
}
}
throw new IllegalArgumentException("could not find extension implementation " + extClass);
}
public static void instantiateArea(@NonNls @NotNull String areaClass, AreaInstance areaInstance, AreaInstance parentAreaInstance) {
if (!ourAreaClass2Configuration.containsKey(areaClass)) {
throw new IllegalArgumentException("Area class is not registered: " + areaClass);
@@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.packageDependencies;
package com.intellij.psi.search.scope;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.scope.packageSet.AbstractPackageSet;
import com.intellij.psi.search.scope.packageSet.NamedScope;
import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
import com.intellij.psi.search.scope.packageSet.PackageSet;
import com.intellij.ui.Colored;
import org.intellij.lang.annotations.RegExp;
@@ -35,26 +35,14 @@ public class NonProjectFilesScope extends NamedScope {
@RegExp(prefix = "[0-9a-f]{6}")
public static final String DEFAULT_COLOR = "ffffe4";
public NonProjectFilesScope(final Project project) {
super(NAME, new PackageSet() {
public boolean contains(PsiFile psiFile, NamedScopesHolder holder) {
final VirtualFile file = psiFile.getVirtualFile();
if (file == null) return true;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
return !(project.isInitialized() && !fileIndex.isIgnored(file) && fileIndex.getContentRootForFile(file) != null);
}
public PackageSet createCopy() {
return this;
}
public String getText() {
return "NonProject";
}
public int getNodePriority() {
return 0;
}
});
public NonProjectFilesScope(Project project) {
super(NAME, new AbstractPackageSet("NonProject", project) {
public boolean contains(PsiFile psiFile, NamedScopesHolder holder) {
final VirtualFile file = psiFile.getVirtualFile();
if (file == null) return true;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(getProject()).getFileIndex();
return !(getProject().isInitialized() && !fileIndex.isIgnored(file) && fileIndex.getContentRootForFile(file) != null);
}
});
}
}
@@ -0,0 +1,46 @@
/*
* Copyright 2000-2010 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.psi.search.scope;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.scope.packageSet.AbstractPackageSet;
import com.intellij.psi.search.scope.packageSet.NamedScope;
import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
import org.jetbrains.annotations.NotNull;
/**
* @author Konstantin Bulenkov
*/
public class ProjectProductionScope extends NamedScope {
public ProjectProductionScope(@NotNull Project project) {
super(IdeBundle.message("predefined.scope.production.name"), new AbstractPackageSet("src:*..*", project) {
public boolean contains(PsiFile file, NamedScopesHolder holder) {
final ProjectFileIndex index = ProjectRootManager.getInstance(getProject()).getFileIndex();
final VirtualFile virtualFile = file.getVirtualFile();
return file.getProject() == getProject()
&& virtualFile != null
&& !index.isInTestSourceContent(virtualFile)
&& !index.isInLibraryClasses(virtualFile)
&& !index.isInLibrarySource(virtualFile);
}
});
}
}
@@ -0,0 +1,47 @@
/*
* Copyright 2000-2010 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.psi.search.scope;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.scope.packageSet.AbstractPackageSet;
import com.intellij.psi.search.scope.packageSet.NamedScope;
import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
import com.intellij.ui.Colored;
import org.jetbrains.annotations.NotNull;
/**
* @author Konstantin Bulenkov
*/
@Colored(color = "ff0000")
public class TestsScope extends NamedScope {
public static final String NAME = IdeBundle.message("predefined.scope.tests.name");
public TestsScope(@NotNull Project project) {
super(NAME, new AbstractPackageSet("test:*..*", project) {
public boolean contains(PsiFile file, NamedScopesHolder holder) {
final ProjectFileIndex index = ProjectRootManager.getInstance(getProject()).getFileIndex();
final VirtualFile virtualFile = file.getVirtualFile();
return file.getProject() == getProject()
&& virtualFile != null
&& index.isInTestSourceContent(virtualFile);
}
});
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 2000-2010 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.psi.search.scope.packageSet;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Konstantin Bulenkov
*/
public abstract class AbstractPackageSet implements PackageSet {
private final String myText;
private final Project myProject;
private final int myPriority;
public AbstractPackageSet(@NotNull String text, @Nullable Project project) {
this(text, project, 1);
}
public AbstractPackageSet(@NotNull String text, @Nullable Project project, int priority) {
myText = text;
myProject = project;
myPriority = priority;
}
@Nullable
public Project getProject() {
return myProject;
}
public PackageSet createCopy() {
return this;
}
public int getNodePriority() {
return myPriority;
}
@Override
public String getText() {
return myText;
}
}
@@ -29,84 +29,60 @@ import com.intellij.psi.search.scope.packageSet.*;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author anna
* @author Konstantin Bulenkov
*/
public class DefaultScopesProvider implements CustomScopesProvider {
private NamedScope myProblemsScope;
private NamedScope myNonProjectFilesScope;
private final NamedScope myProblemsScope;
private final Project myProject;
public static DefaultScopesProvider getInstance(Project project) {
for (CustomScopesProvider provider : Extensions.getExtensions(CUSTOM_SCOPES_PROVIDER, project)) {
if (provider instanceof DefaultScopesProvider) return (DefaultScopesProvider)provider;
}
return null;
return Extensions.findExtension(CUSTOM_SCOPES_PROVIDER, project, DefaultScopesProvider.class);
}
public DefaultScopesProvider(Project project) {
myProject = project;
final String text = FilePatternPackageSet.SCOPE_FILE + ":*//*";
myProblemsScope = new NamedScope(IdeBundle.message("predefined.scope.problems.name"), new AbstractPackageSet(text, myProject) {
public boolean contains(PsiFile file, NamedScopesHolder holder) {
return file.getProject() == myProject
&& WolfTheProblemSolver.getInstance(myProject).isProblemFile(file.getVirtualFile());
}
});
}
@NotNull
public List<NamedScope> getCustomScopes() {
final List<NamedScope> list = new ArrayList<NamedScope>();
list.add(getProblemsScope());
list.add(getAllScope());
list.add(getNonProjectFilesScope());
return list;
return Arrays.asList(getProblemsScope(), getAllScope());
}
private static class NamedScopeHolder {
private static final NamedScope myAllScope = new NamedScope("All", new PackageSet() {
public boolean contains(final PsiFile file, final NamedScopesHolder holder) {
@SuppressWarnings({"UtilityClassWithoutPrivateConstructor"})
private static class AllScopeHolder {
private static final String TEXT = FilePatternPackageSet.SCOPE_FILE + ":*//*";
private static final NamedScope ALL = new NamedScope("All", new AbstractPackageSet(TEXT, null, 0) {
public boolean contains(final PsiFile file, final NamedScopesHolder scopesHolder) {
return true;
}
public PackageSet createCopy() {
return this;
}
public String getText() {
return FilePatternPackageSet.SCOPE_FILE + ":*//*";
}
public int getNodePriority() {
return 0;
}
});
}
public static NamedScope getAllScope() {
return NamedScopeHolder.myAllScope;
return AllScopeHolder.ALL;
}
public NamedScope getProblemsScope() {
if (myProblemsScope == null) {
myProblemsScope = new NamedScope(IdeBundle.message("predefined.scope.problems.name"), new PackageSet() {
public boolean contains(PsiFile file, NamedScopesHolder holder) {
return file.getProject() == myProject && WolfTheProblemSolver.getInstance(myProject).isProblemFile(file.getVirtualFile());
}
public PackageSet createCopy() {
return this;
}
public String getText() {
return FilePatternPackageSet.SCOPE_FILE + ":*//*";
}
public int getNodePriority() {
return 1;
}
});
}
public NamedScope getProblemsScope() {
return myProblemsScope;
}
public NamedScope getNonProjectFilesScope() {
if (myNonProjectFilesScope == null) {
myNonProjectFilesScope = new NonProjectFilesScope(myProject);
public List<NamedScope> getAllCustomScopes() {
final List<NamedScope> scopes = new ArrayList<NamedScope>();
for (CustomScopesProvider provider : Extensions.getExtensions(CUSTOM_SCOPES_PROVIDER, myProject)) {
scopes.addAll(provider.getCustomScopes());
}
return myNonProjectFilesScope;
return scopes;
}
}