use file's directory as context root when resolving references in FileIncludeProvider...
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Thu, 24 Mar 2016 18:57:02 +0000 (21:57 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Thu, 24 Mar 2016 18:57:02 +0000 (21:57 +0300)
platform/lang-impl/src/com/intellij/psi/impl/include/FileIncludeManagerImpl.java

index a79e85e91f4d87eafae6dd042f069e74bf0647d1..98e327097cf2fcfde960f2c3ee4f2feb25b64673 100644 (file)
@@ -30,6 +30,7 @@ import com.intellij.psi.PsiFileFactory;
 import com.intellij.psi.PsiFileSystemItem;
 import com.intellij.psi.PsiManager;
 import com.intellij.psi.impl.source.PsiFileImpl;
+import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceHelper;
 import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet;
 import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.psi.util.CachedValueProvider;
@@ -181,9 +182,35 @@ public class FileIncludeManagerImpl extends FileIncludeManager {
       protected boolean useIncludingFileAsContext() {
         return false;
       }
+
+      @NotNull
+      @Override
+      public Collection<PsiFileSystemItem> computeDefaultContexts() {
+        Collection<PsiFileSystemItem> contexts = super.computeDefaultContexts();
+        if (!isAbsolutePathReference()) {
+          return addFileDirectoryToContexts(contexts, context);
+        }
+        return contexts;
+      }
     }.resolve();
   }
 
+  @NotNull
+  private static Collection<PsiFileSystemItem> addFileDirectoryToContexts(@NotNull Collection<PsiFileSystemItem> contexts,
+                                                                          @NotNull PsiFile context) {
+    VirtualFile file = context.getOriginalFile().getVirtualFile();
+    VirtualFile dir = file == null ? null : file.getParent();
+    if (dir != null) {
+      PsiFileSystemItem item = FileReferenceHelper.getPsiFileSystemItem(context.getManager(), dir);
+      if (item != null && !contexts.contains(item)) {
+        List<PsiFileSystemItem> result = ContainerUtil.newArrayList(contexts);
+        result.add(item);
+        return result;
+      }
+    }
+    return contexts;
+  }
+
   private abstract class IncludeCacheHolder {
 
     private final Key<ParameterizedCachedValue<VirtualFile[], PsiFile>> COMPILE_TIME_KEY;