package mobi.hsz.idea.gitignore.reference;

import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference;
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)) {
                            PsiDirectory psiDirectory = psiManager.findDirectory(file);
                            if (psiDirectory != null) {
                                result.add(new PsiElementResolveResult(psiDirectory));
                            }
                            PsiFile psiFile = psiManager.findFile(file);
                            if (psiFile != null) {
                                result.add(new PsiElementResolveResult(psiFile));
                            }
                        }
                    }
                }
            }
            return result.isEmpty()
                    ? super.innerResolve(caseSensitive)
                    : result.toArray(new ResolveResult[result.size()]);
        }
    }
}

@@ -154,93 +154,323 @@
 psi.
-*;%0Aimport com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference
+PsiElementResolveResult;%0Aimport com.intellij.psi.PsiFileSystemItem;%0Aimport com.intellij.psi.PsiManager;%0Aimport com.intellij.psi.ResolveResult;%0Aimport com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference;%0Aimport com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceHelper
 ;%0Aim
@@ -2057,476 +2057,108 @@
     
-PsiDirectory psiDirectory = psiManager.findDirectory(file);%0A                            if (psiDirectory != null) %7B%0A                                result.add(new PsiElementResolveResult(psiDirectory));%0A                            %7D%0A                            PsiFile psiFile = psiManager.findFile(file);%0A                            if (psiFile != null) %7B%0A                                result.add(new PsiElementResolveResult(psiFile));%0A                            %7D
+result.add(new PsiElementResolveResult(FileReferenceHelper.getPsiFileSystemItem(psiManager, file)));
 %0A   

Simplify retrieving psiFileItem