Search in sources :

Example 1 with FileReferenceSet

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet in project intellij-community by JetBrains.

the class MavenPathReferenceConverter method createReferences.

public static PsiReference[] createReferences(final DomElement genericDomValue, PsiElement element, @NotNull final Condition<PsiFileSystemItem> fileFilter, boolean isAbsolutePath) {
    ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
    TextRange range = manipulator.getRangeInElement(element);
    String text = range.substring(element.getText());
    FileReferenceSet set = new FileReferenceSet(text, element, range.getStartOffset(), null, SystemInfo.isFileSystemCaseSensitive, false) {

        private MavenDomProjectModel model;

        @Override
        protected Condition<PsiFileSystemItem> getReferenceCompletionFilter() {
            return fileFilter;
        }

        @Override
        protected boolean isSoft() {
            return true;
        }

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

                @Override
                protected void innerResolveInContext(@NotNull String text, @NotNull PsiFileSystemItem context, Collection<ResolveResult> result, boolean caseSensitive) {
                    if (model == null) {
                        DomElement rootElement = DomUtil.getFileElement(genericDomValue).getRootElement();
                        if (rootElement instanceof MavenDomProjectModel) {
                            model = (MavenDomProjectModel) rootElement;
                        }
                    }
                    String resolvedText = model == null ? text : MavenPropertyResolver.resolve(text, model);
                    if (resolvedText.equals(text)) {
                        if (getIndex() == 0 && resolvedText.length() == 2 && resolvedText.charAt(1) == ':') {
                            // it's root on windows, e.g. "C:"
                            VirtualFile file = LocalFileSystem.getInstance().findFileByPath(resolvedText + '/');
                            if (file != null) {
                                PsiDirectory psiDirectory = context.getManager().findDirectory(file);
                                if (psiDirectory != null) {
                                    result.add(new PsiElementResolveResult(psiDirectory));
                                }
                            }
                        } else {
                            super.innerResolveInContext(resolvedText, context, result, caseSensitive);
                        }
                    } else {
                        VirtualFile contextFile = context.getVirtualFile();
                        if (contextFile == null)
                            return;
                        VirtualFile file = null;
                        if (getIndex() == 0) {
                            file = LocalFileSystem.getInstance().findFileByPath(resolvedText);
                        }
                        if (file == null) {
                            file = LocalFileSystem.getInstance().findFileByPath(contextFile.getPath() + '/' + resolvedText);
                        }
                        if (file != null) {
                            PsiFileSystemItem res = file.isDirectory() ? context.getManager().findDirectory(file) : context.getManager().findFile(file);
                            if (res != null) {
                                result.add(new PsiElementResolveResult(res));
                            }
                        }
                    }
                }
            };
        }
    };
    if (isAbsolutePath) {
        set.addCustomization(FileReferenceSet.DEFAULT_PATH_EVALUATOR_OPTION, file -> {
            VirtualFile virtualFile = file.getVirtualFile();
            if (virtualFile == null) {
                return FileReferenceSet.ABSOLUTE_TOP_LEVEL.fun(file);
            }
            virtualFile = VfsUtil.getRootFile(virtualFile);
            PsiDirectory root = file.getManager().findDirectory(virtualFile);
            if (root == null) {
                return FileReferenceSet.ABSOLUTE_TOP_LEVEL.fun(file);
            }
            return Collections.<PsiFileSystemItem>singletonList(root);
        });
    }
    return set.getAllReferences();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) DomElement(com.intellij.util.xml.DomElement) Collection(java.util.Collection) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)

Example 2 with FileReferenceSet

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet in project intellij-community by JetBrains.

the class RngHrefConverter method createReferences.

@Override
@NotNull
public PsiReference[] createReferences(GenericDomValue<XmlFile> genericDomValue, PsiElement element, ConvertContext context) {
    final String s = genericDomValue.getStringValue();
    if (s == null || element == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final FileReferenceSet set = FileReferenceSet.createSet(element, false, false, false);
    if (set != null) {
        return FileReferenceUtil.restrict(set, FileReferenceUtil.byNamespace(ApplicationLoader.RNG_NAMESPACE), true);
    } else {
        return PsiReference.EMPTY_ARRAY;
    }
}
Also used : FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with FileReferenceSet

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet in project intellij-community by JetBrains.

the class RncFileReferenceImpl method getReferences.

@NotNull
@Override
@SuppressWarnings({ "SSBasedInspection" })
public PsiReference[] getReferences() {
    final ASTNode literal = getNode().findChildByType(RncTokenTypes.LITERAL);
    if (literal == null)
        return PsiReference.EMPTY_ARRAY;
    final String s = literal.getText();
    final FileReferenceSet set = new FileReferenceSet(s.substring(1, s.length() - 1), this, getReferenceRange().getStartOffset(), null, true, false);
    return FileReferenceUtil.restrict(set, FileReferenceUtil.byType(RncFileType.getInstance()));
}
Also used : ASTNode(com.intellij.lang.ASTNode) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with FileReferenceSet

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet in project intellij-community by JetBrains.

the class JavaFxLocationReferenceProvider method collectRefs.

private PsiReference[] collectRefs(@NotNull PsiElement element, String value, int startInElement) {
    final int atSignIndex = value.indexOf('@');
    if (atSignIndex >= 0 && (atSignIndex == 0 || StringUtil.trimLeading(value).startsWith("@"))) {
        value = value.substring(atSignIndex + 1);
        startInElement += atSignIndex + 1;
    }
    final FileReferenceSet set = new FileReferenceSet(value, element, startInElement, null, true) {

        @Override
        protected Condition<PsiFileSystemItem> getReferenceCompletionFilter() {
            return item -> {
                if (item instanceof PsiDirectory)
                    return true;
                final VirtualFile virtualFile = item.getVirtualFile();
                if (virtualFile == null)
                    return false;
                final FileType fileType = virtualFile.getFileType();
                return myAcceptedFileTypes.contains(fileType);
            };
        }
    };
    if (value.startsWith("/")) {
        set.addCustomization(FileReferenceSet.DEFAULT_PATH_EVALUATOR_OPTION, FileReferenceSet.ABSOLUTE_TOP_LEVEL);
    }
    return set.getAllReferences();
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) FileTypeManager(com.intellij.openapi.fileTypes.FileTypeManager) StringUtil(com.intellij.openapi.util.text.StringUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Set(java.util.Set) FileType(com.intellij.openapi.fileTypes.FileType) ContainerUtil(com.intellij.util.containers.ContainerUtil) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) ArrayList(java.util.ArrayList) List(java.util.List) com.intellij.psi(com.intellij.psi) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Condition(com.intellij.openapi.util.Condition) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileType(com.intellij.openapi.fileTypes.FileType) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet)

Example 5 with FileReferenceSet

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet in project intellij-community by JetBrains.

the class FileIncludeManagerImpl method doResolve.

@Nullable
private PsiFileSystemItem doResolve(@NotNull final FileIncludeInfo info, @NotNull final PsiFile context) {
    if (info instanceof FileIncludeInfoImpl) {
        String id = ((FileIncludeInfoImpl) info).providerId;
        FileIncludeProvider provider = id == null ? null : myProviderMap.get(id);
        final PsiFileSystemItem resolvedByProvider = provider == null ? null : provider.resolveIncludedFile(info, context);
        if (resolvedByProvider != null) {
            return resolvedByProvider;
        }
    }
    PsiFileImpl psiFile = (PsiFileImpl) myPsiFileFactory.createFileFromText("dummy.txt", FileTypes.PLAIN_TEXT, info.path);
    psiFile.setOriginalFile(context);
    return new FileReferenceSet(psiFile) {

        @Override
        protected boolean useIncludingFileAsContext() {
            return false;
        }
    }.resolve();
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileReferenceSet (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet)16 NotNull (org.jetbrains.annotations.NotNull)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 WebFacet (com.intellij.javaee.web.facet.WebFacet)3 TextRange (com.intellij.openapi.util.TextRange)3 FileReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 ProcessingContext (com.intellij.util.ProcessingContext)3 ArrayList (java.util.ArrayList)3 FileType (com.intellij.openapi.fileTypes.FileType)2 Module (com.intellij.openapi.module.Module)2 Condition (com.intellij.openapi.util.Condition)2 com.intellij.psi (com.intellij.psi)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 GoGetPackageFix (com.goide.codeInsight.imports.GoGetPackageFix)1 GoDeleteImportQuickFix (com.goide.quickfix.GoDeleteImportQuickFix)1 RenameFileFix (com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix)1 CreateFileFix (com.intellij.codeInsight.daemon.quickFix.CreateFileFix)1