2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ide.util.scopeChooser;
18 import com.intellij.ide.IdeBundle;
19 import com.intellij.openapi.fileEditor.FileEditorManager;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.vfs.VirtualFile;
22 import com.intellij.packageDependencies.ChangeListsScopesProvider;
23 import com.intellij.psi.search.GlobalSearchScope;
24 import com.intellij.psi.search.GlobalSearchScopes;
25 import com.intellij.psi.search.GlobalSearchScopesCore;
26 import com.intellij.psi.search.ProjectScope;
27 import com.intellij.psi.search.scope.ProjectFilesScope;
28 import com.intellij.psi.search.scope.ProjectProductionScope;
29 import com.intellij.psi.search.scope.packageSet.NamedScope;
30 import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
31 import com.intellij.util.ArrayUtil;
32 import com.intellij.util.containers.ContainerUtil;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
36 import java.util.List;
38 public class ScopeChooserUtils {
40 private static final String CURRENT_FILE_SCOPE_NAME = IdeBundle.message("scope.current.file");
42 private ScopeChooserUtils() {
46 public static GlobalSearchScope findScopeByName(@NotNull Project project, @Nullable String scopeName) {
47 NamedScope namedScope = scopeName == null ? null : ChangeListsScopesProvider.getInstance(project).getCustomScope(scopeName);
48 if (namedScope == null) {
49 namedScope = NamedScopesHolder.getScope(project, scopeName);
51 if (namedScope == null && GlobalSearchScopes.OPEN_FILES_SCOPE_NAME.equals(scopeName)) {
52 return intersectWithContentScope(project, GlobalSearchScopes.openFilesScope(project));
54 if (namedScope == null && CURRENT_FILE_SCOPE_NAME.equals(scopeName)) {
55 VirtualFile[] array = FileEditorManager.getInstance(project).getSelectedFiles();
56 List<VirtualFile> files = ContainerUtil.createMaybeSingletonList(ArrayUtil.getFirstElement(array));
57 GlobalSearchScope scope = GlobalSearchScope.filesScope(project, files, CURRENT_FILE_SCOPE_NAME);
58 return intersectWithContentScope(project, scope);
60 if (namedScope == null) {
61 namedScope = new ProjectFilesScope();
63 GlobalSearchScope scope = GlobalSearchScopesCore.filterScope(project, namedScope);
64 if (namedScope instanceof ProjectFilesScope || namedScope instanceof ProjectProductionScope) {
67 return intersectWithContentScope(project, scope);
71 private static GlobalSearchScope intersectWithContentScope(@NotNull Project project, @NotNull GlobalSearchScope scope) {
72 return scope.intersectWith(ProjectScope.getContentScope(project));