package mobi.hsz.idea.gitignore.reference;

import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElementResolveResult;
import com.intellij.psi.PsiFileSystemItem;
import com.intellij.psi.PsiManager;
import com.intellij.psi.ResolveResult;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference;
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.util.containers.ContainerUtil;
import mobi.hsz.idea.gitignore.psi.GitignoreEntry;
import mobi.hsz.idea.gitignore.util.Utils;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;

public class GitReferenceSet extends FileReferenceSet {
    public GitReferenceSet(@NotNull GitignoreEntry element) {
        super(element);
    }

    @Override
    public FileReference createFileReference(TextRange range, int index, String text) {
        return new GitReference(this, range, index, text);
    }

    private class GitReference extends FileReference {
        public GitReference(@NotNull FileReferenceSet fileReferenceSet, TextRange range, int index, String text) {
            super(fileReferenceSet, range, index, text);
        }

        @NotNull
        @Override
        protected ResolveResult[] innerResolve(boolean caseSensitive) {
            LinkedList<ResolveResult> result = ContainerUtil.newLinkedList();
            PsiManager psiManager = PsiManager.getInstance(getElement().getProject());
            for (PsiFileSystemItem context : getContexts()) {
                VirtualFile contextVirtualFile = context.getVirtualFile();
                if (contextVirtualFile != null) {
                    String regexFromGlob = Utils.createRegexFromGlob(getCanonicalText());
                    for (VirtualFile file : contextVirtualFile.getChildren()) {
                        if (file.getName().matches(regexFromGlob)) {
                            result.add(new PsiElementResolveResult(FileReferenceHelper.getPsiFileSystemItem(psiManager, file)));
                        }
                    }
                }
            }
            return result.isEmpty()
                    ? super.innerResolve(caseSensitive)
                    : result.toArray(new ResolveResult[result.size()]);
        }
    }
}

@@ -154,149 +154,9 @@
 psi.
-PsiElementResolveResult;%0Aimport com.intellij.psi.PsiFileSystemItem;%0Aimport com.intellij.psi.PsiManager;%0Aimport com.intellij.psi.ResolveResult
+*
 ;%0Aim
@@ -602,16 +602,75 @@
 tNull;%0A%0A
+import java.util.Collection;%0Aimport java.util.Collections;%0A
 import j
@@ -1004,24 +1004,499 @@
 xt);%0A    %7D%0A%0A
+    @Override%0A    public boolean isEndingSlashNotAllowed() %7B%0A        return false;%0A    %7D%0A%0A    @NotNull%0A    @Override%0A    public Collection%3CPsiFileSystemItem%3E computeDefaultContexts() %7B%0A        PsiFile containingFile = getElement().getContainingFile();%0A        PsiDirectory containingDirectory = containingFile.getParent();%0A        return containingDirectory != null ? Collections.%3CPsiFileSystemItem%3EsingletonList(containingDirectory) : super.computeDefaultContexts();%0A    %7D%0A%0A
     private 
@@ -2451,47 +2451,46 @@
     
-result.add(new PsiElementResolveResult(
+PsiFileSystemItem psiFileSystemItem = 
 File
@@ -2543,19 +2543,200 @@
 r, file)
-));
+;%0A                            if (psiFileSystemItem != null) %7B%0A                                result.add(new PsiElementResolveResult(psiFileSystemItem));%0A                            %7D
 %0A       

Allow only parent directory of current file as a default context