WEB-20533 FileWatcher not respecting scope
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Fri, 26 Feb 2016 19:27:29 +0000 (22:27 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Fri, 26 Feb 2016 19:27:29 +0000 (22:27 +0300)
platform/lang-impl/src/com/intellij/ide/util/scopeChooser/ScopeChooserUtils.java

index 322f8e6d859c51702e34452c374a4ea6678cc34b..fb5a9bde7ab2a74ce11b2cbfcc8eaa72d44c5177 100644 (file)
  */
 package com.intellij.ide.util.scopeChooser;
 
+import com.intellij.ide.IdeBundle;
+import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.packageDependencies.ChangeListsScopesProvider;
 import com.intellij.psi.search.GlobalSearchScope;
+import com.intellij.psi.search.GlobalSearchScopes;
 import com.intellij.psi.search.GlobalSearchScopesCore;
 import com.intellij.psi.search.ProjectScope;
 import com.intellij.psi.search.scope.ProjectFilesScope;
 import com.intellij.psi.search.scope.ProjectProductionScope;
 import com.intellij.psi.search.scope.packageSet.NamedScope;
 import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
+import com.intellij.util.ArrayUtil;
+import com.intellij.util.containers.ContainerUtil;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import java.util.List;
+
 public class ScopeChooserUtils {
+
+  private static final String OPEN_FILES_SCOPE_NAME = "Open Files";
+  private static final String CURRENT_FILE_SCOPE_NAME = IdeBundle.message("scope.current.file");
+
   private ScopeChooserUtils() {
   }
 
@@ -37,14 +49,27 @@ public class ScopeChooserUtils {
     if (namedScope == null) {
       namedScope = NamedScopesHolder.getScope(project, scopeName);
     }
+    if (namedScope == null && OPEN_FILES_SCOPE_NAME.equals(scopeName)) {
+      return intersectWithContentScope(project, GlobalSearchScopes.openFilesScope(project));
+    }
+    if (namedScope == null && CURRENT_FILE_SCOPE_NAME.equals(scopeName)) {
+      VirtualFile[] array = FileEditorManager.getInstance(project).getSelectedFiles();
+      List<VirtualFile> files = ContainerUtil.createMaybeSingletonList(ArrayUtil.getFirstElement(array));
+      GlobalSearchScope scope = GlobalSearchScope.filesScope(project, files, CURRENT_FILE_SCOPE_NAME);
+      return intersectWithContentScope(project, scope);
+    }
     if (namedScope == null) {
       namedScope = new ProjectFilesScope();
     }
     GlobalSearchScope scope = GlobalSearchScopesCore.filterScope(project, namedScope);
-    boolean restrictedToProject = namedScope instanceof ProjectFilesScope || namedScope instanceof ProjectProductionScope;
-    if (!restrictedToProject) {
-      scope = scope.intersectWith(ProjectScope.getContentScope(project));
+    if (namedScope instanceof ProjectFilesScope || namedScope instanceof ProjectProductionScope) {
+      return scope;
     }
-    return scope;
+    return intersectWithContentScope(project, scope);
+  }
+
+  @NotNull
+  private static GlobalSearchScope intersectWithContentScope(@NotNull Project project, @NotNull GlobalSearchScope scope) {
+    return scope.intersectWith(ProjectScope.getContentScope(project));
   }
 }