package uk.co.reecedunn.intellij.plugin.xquery.resolve.reference;

import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryStringLiteral;
import uk.co.reecedunn.intellij.plugin.xquery.psi.impl.xquery.XQueryUriLiteralPsiImpl;

public class XQueryUriLiteralReference extends PsiReferenceBase<XQueryUriLiteralPsiImpl> {
    public XQueryUriLiteralReference(XQueryUriLiteralPsiImpl element, TextRange range) {
        super(element, range);
    }

    @Nullable
    @Override
    public PsiElement resolve() {
        PsiElement element = getElement();
        final String path = ((XQueryStringLiteral)element).getAtomicValue().toString();
        if (path.contains("://")) {
            return null;
        }
        return resolveFileByPath(element.getContainingFile().getParent(), path);
    }

    @NotNull
    @Override
    public Object[] getVariants() {
        return new Object[0];
    }

    private PsiFile resolveFileByPath(PsiDirectory parent, String path) {
        if (parent == null) {
            return null;
        }

        VirtualFile file = parent.getVirtualFile().findFileByRelativePath(path);
        if (file != null) {
            return PsiManager.getInstance(parent.getProject()).findFile(file);
        }

        return resolveFileByPath(parent.getParentDirectory(), path);
    }
}

@@ -60,16 +60,61 @@
 rence;%0A%0A
+import com.intellij.openapi.project.Project;%0A
 import c
@@ -954,24 +954,25 @@
       %7D%0A
+%0A
         
 return r
@@ -967,33 +967,27 @@
     
-return resolveFileByPath(
+VirtualFile file = 
 elem
@@ -1013,21 +1013,87 @@
 le().get
-Paren
+VirtualFile();%0A        return resolveFileByPath(file, element.getProjec
 t(), pat
@@ -1245,26 +1245,42 @@
 ath(
-PsiDirectory paren
+VirtualFile parent, Project projec
 t, S
@@ -1397,25 +1397,8 @@
 ent.
-getVirtualFile().
 find
@@ -1498,26 +1498,14 @@
 ce(p
-arent.getP
 roject
-()
 ).fi
@@ -1582,19 +1582,19 @@
 rent
-Directory()
+(), project
 , pa

Use VirtualFile to resolve the file path.