ask each intermediate context file for a forced resolve scope (IDEA-136304)
authorpeter <peter@jetbrains.com>
Tue, 10 Feb 2015 20:39:58 +0000 (21:39 +0100)
committerpeter <peter@jetbrains.com>
Tue, 10 Feb 2015 20:45:10 +0000 (21:45 +0100)
java/java-tests/testSrc/com/intellij/psi/resolve/ResolveInCodeFragmentTest.java
platform/indexing-impl/src/com/intellij/psi/impl/file/impl/ResolveScopeManagerImpl.java

index b934877c48bfd3ec2009d8d90278eee0b945ad67..f108ad67742bef13df7be1600598518db18ca694 100644 (file)
@@ -1,6 +1,7 @@
 package com.intellij.psi.resolve;
 
 import com.intellij.psi.*;
+import com.intellij.psi.impl.search.JavaSourceFilterScope;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.testFramework.ResolveTestCase;
 
@@ -45,4 +46,15 @@ public class ResolveInCodeFragmentTest extends ResolveTestCase {
   private PsiReference configure() throws Exception {
     return configureByFile("codeFragment/" + getTestName(false) + ".java");
   }
+
+  public void testResolveScopeWithFragmentContext() throws Exception {
+    PsiElement physical = configureByFile("codeFragment/LocalVariable.java").getElement();
+    JavaCodeFragment fragment = JavaCodeFragmentFactory.getInstance(myProject)
+      .createExpressionCodeFragment("ref", physical, null, true);
+    fragment.forceResolveScope(new JavaSourceFilterScope(physical.getResolveScope()));
+    assertFalse(fragment.getResolveScope().equals(physical.getResolveScope()));
+
+    PsiExpression lightExpr = JavaPsiFacade.getElementFactory(myProject).createExpressionFromText("xxx.xxx", fragment);
+    assertEquals(lightExpr.getResolveScope(), fragment.getResolveScope());
+  }
 }
index f3a9c183874d86ce66b438568bcd35d4bec9dedc..c60d3073985261c42c1f9a4d32d79b128ee521d7 100644 (file)
@@ -26,7 +26,6 @@ import com.intellij.openapi.vfs.VirtualFileWithId;
 import com.intellij.psi.*;
 import com.intellij.psi.impl.PsiManagerImpl;
 import com.intellij.psi.impl.ResolveScopeManager;
-import com.intellij.psi.impl.source.resolve.FileContextUtil;
 import com.intellij.psi.search.DelegatingGlobalSearchScope;
 import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.psi.search.SearchScope;
@@ -145,15 +144,17 @@ public class ResolveScopeManagerImpl extends ResolveScopeManager {
         if (forcedScope != null) {
           return forcedScope;
         }
-        final PsiElement context = containingFile.getContext();
-        if (context == null) {
-          return GlobalSearchScope.allScope(myProject);
+      }
+
+      if (containingFile != null) {
+        PsiElement context = containingFile.getContext();
+        if (context != null) {
+          return getResolveScope(context);
         }
-        return getResolveScope(context);
       }
 
-      contextFile = containingFile != null ? FileContextUtil.getContextFile(containingFile) : null;
-      if (contextFile == null) {
+      contextFile = containingFile;
+      if (containingFile == null) {
         return GlobalSearchScope.allScope(myProject);
       }
       else if (contextFile instanceof FileResolveScopeProvider) {