From e310487092e1e9251302ec518c186a587d8fccb4 Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Mon, 27 Sep 2010 19:32:36 +0400 Subject: [PATCH] find project extension by instance class --- .../analysis/PackagesScopesProvider.java | 91 ++++--------------- .../openapi/extensions/Extensions.java | 10 ++ .../search/scope}/NonProjectFilesScope.java | 34 +++---- .../search/scope/ProjectProductionScope.java | 46 ++++++++++ .../intellij/psi/search/scope/TestsScope.java | 47 ++++++++++ .../scope/packageSet/AbstractPackageSet.java | 57 ++++++++++++ .../DefaultScopesProvider.java | 78 ++++++---------- 7 files changed, 218 insertions(+), 145 deletions(-) rename platform/{lang-impl/src/com/intellij/packageDependencies => lang-api/src/com/intellij/psi/search/scope}/NonProjectFilesScope.java (61%) create mode 100644 platform/lang-api/src/com/intellij/psi/search/scope/ProjectProductionScope.java create mode 100644 platform/lang-api/src/com/intellij/psi/search/scope/TestsScope.java create mode 100644 platform/lang-api/src/com/intellij/psi/search/scope/packageSet/AbstractPackageSet.java diff --git a/java/java-impl/src/com/intellij/analysis/PackagesScopesProvider.java b/java/java-impl/src/com/intellij/analysis/PackagesScopesProvider.java index 015b7f395441..ef82fd7bf46f 100644 --- a/java/java-impl/src/com/intellij/analysis/PackagesScopesProvider.java +++ b/java/java-impl/src/com/intellij/analysis/PackagesScopesProvider.java @@ -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 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 getCustomScopes() { - final List list = new ArrayList(); - 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; } - - } \ No newline at end of file diff --git a/platform/extensions/src/com/intellij/openapi/extensions/Extensions.java b/platform/extensions/src/com/intellij/openapi/extensions/Extensions.java index 921afea556b2..4e9b6f4a697d 100644 --- a/platform/extensions/src/com/intellij/openapi/extensions/Extensions.java +++ b/platform/extensions/src/com/intellij/openapi/extensions/Extensions.java @@ -109,6 +109,16 @@ public class Extensions { throw new IllegalArgumentException("could not find extension implementation " + extClass); } + public static U findExtension(ExtensionPointName extensionPointName, AreaInstance areaInstance, Class 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); diff --git a/platform/lang-impl/src/com/intellij/packageDependencies/NonProjectFilesScope.java b/platform/lang-api/src/com/intellij/psi/search/scope/NonProjectFilesScope.java similarity index 61% rename from platform/lang-impl/src/com/intellij/packageDependencies/NonProjectFilesScope.java rename to platform/lang-api/src/com/intellij/psi/search/scope/NonProjectFilesScope.java index fdeade98c894..2d7870534ac4 100644 --- a/platform/lang-impl/src/com/intellij/packageDependencies/NonProjectFilesScope.java +++ b/platform/lang-api/src/com/intellij/psi/search/scope/NonProjectFilesScope.java @@ -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); + } + }); } } diff --git a/platform/lang-api/src/com/intellij/psi/search/scope/ProjectProductionScope.java b/platform/lang-api/src/com/intellij/psi/search/scope/ProjectProductionScope.java new file mode 100644 index 000000000000..6dbd6c5eb51e --- /dev/null +++ b/platform/lang-api/src/com/intellij/psi/search/scope/ProjectProductionScope.java @@ -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); + } + }); + } +} diff --git a/platform/lang-api/src/com/intellij/psi/search/scope/TestsScope.java b/platform/lang-api/src/com/intellij/psi/search/scope/TestsScope.java new file mode 100644 index 000000000000..a84c87c4ce9d --- /dev/null +++ b/platform/lang-api/src/com/intellij/psi/search/scope/TestsScope.java @@ -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); + } + }); + } +} diff --git a/platform/lang-api/src/com/intellij/psi/search/scope/packageSet/AbstractPackageSet.java b/platform/lang-api/src/com/intellij/psi/search/scope/packageSet/AbstractPackageSet.java new file mode 100644 index 000000000000..a149ddb4f1e6 --- /dev/null +++ b/platform/lang-api/src/com/intellij/psi/search/scope/packageSet/AbstractPackageSet.java @@ -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; + } +} diff --git a/platform/lang-impl/src/com/intellij/packageDependencies/DefaultScopesProvider.java b/platform/lang-impl/src/com/intellij/packageDependencies/DefaultScopesProvider.java index 31af86f27019..99c50b8096b2 100644 --- a/platform/lang-impl/src/com/intellij/packageDependencies/DefaultScopesProvider.java +++ b/platform/lang-impl/src/com/intellij/packageDependencies/DefaultScopesProvider.java @@ -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 getCustomScopes() { - final List list = new ArrayList(); - 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 getAllCustomScopes() { + final List scopes = new ArrayList(); + for (CustomScopesProvider provider : Extensions.getExtensions(CUSTOM_SCOPES_PROVIDER, myProject)) { + scopes.addAll(provider.getCustomScopes()); } - return myNonProjectFilesScope; + return scopes; } }