Search in sources :

Example 1 with FileReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference 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 FileReference

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

the class PyStringLiteralFileReferenceSet method getFileReferences.

@NotNull
private List<FileReference> getFileReferences(@NotNull PyStringLiteralExpression expression) {
    final String value = expression.getStringValue();
    final Matcher matcher = DELIMITERS.matcher(value);
    int start = 0;
    int index = 0;
    final List<FileReference> results = new ArrayList<>();
    while (matcher.find()) {
        final String s = value.substring(start, matcher.start());
        if (!s.isEmpty()) {
            final TextRange range = TextRange.create(expression.valueOffsetToTextOffset(start), expression.valueOffsetToTextOffset(matcher.start()));
            results.add(createFileReference(range, index++, s));
        }
        start = matcher.end();
    }
    final String s = value.substring(start);
    if (!s.isEmpty()) {
        final TextRange range = TextRange.create(expression.valueOffsetToTextOffset(start), expression.valueOffsetToTextOffset(value.length()));
        results.add(createFileReference(range, index, s));
    }
    return results;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with FileReference

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

the class HtmlFileImpl method resolveFileReference.

@Nullable
@Override
public PsiFileSystemItem resolveFileReference(@NotNull FileReference reference, @NotNull String name) {
    VirtualFile file = getVirtualFile();
    if (!(file instanceof HttpVirtualFile)) {
        return null;
    }
    VirtualFile parent = file;
    if (!parent.isDirectory()) {
        parent = parent.getParent();
        if (parent == null) {
            parent = file;
        }
    }
    VirtualFile childFile = parent.findChild(name);
    HttpFileSystem fileSystem = (HttpFileSystem) parent.getFileSystem();
    if (childFile == null) {
        childFile = fileSystem.createChild(parent, name, !reference.isLast());
    }
    if (childFile.isDirectory()) {
        // pre create children
        VirtualFile childParent = childFile;
        FileReference[] references = reference.getFileReferenceSet().getAllReferences();
        for (int i = reference.getIndex() + 1, n = references.length; i < n; i++) {
            FileReference childReference = references[i];
            childParent = fileSystem.createChild(childParent, childReference.decode(childReference.getText()), i != (n - 1));
        }
        return getManager().findDirectory(childFile);
    } else {
        return getManager().findFile(childFile);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HttpVirtualFile(com.intellij.openapi.vfs.impl.http.HttpVirtualFile) HttpVirtualFile(com.intellij.openapi.vfs.impl.http.HttpVirtualFile) HttpFileSystem(com.intellij.openapi.vfs.ex.http.HttpFileSystem) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with FileReference

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

the class MoveFilesOrDirectoriesProcessor method retargetUsages.

protected void retargetUsages(UsageInfo[] usages, Map<PsiElement, PsiElement> oldToNewMap) {
    final List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usageInfo : usages) {
        if (usageInfo instanceof MyUsageInfo) {
            final MyUsageInfo info = (MyUsageInfo) usageInfo;
            final PsiElement element = myElementsToMove[info.myIndex];
            if (info.getReference() instanceof FileReference || info.getReference() instanceof PsiDynaReference) {
                final PsiElement usageElement = info.getElement();
                if (usageElement != null) {
                    final PsiFile usageFile = usageElement.getContainingFile();
                    final PsiFile psiFile = usageFile.getViewProvider().getPsi(usageFile.getViewProvider().getBaseLanguage());
                    if (psiFile != null && psiFile.equals(element)) {
                        // already processed in MoveFilesOrDirectoriesUtil.doMoveFile
                        continue;
                    }
                }
            }
            final PsiElement refElement = info.myReference.getElement();
            if (refElement != null && refElement.isValid()) {
                info.myReference.bindToElement(element);
            }
        } else if (usageInfo instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usageInfo);
        }
    }
    for (PsiFile movedFile : myFoundUsages.keySet()) {
        MoveFileHandler.forElement(movedFile).retargetUsages(myFoundUsages.get(movedFile), oldToNewMap);
    }
    myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
}
Also used : PsiDynaReference(com.intellij.openapi.paths.PsiDynaReference) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) UsageInfo(com.intellij.usageView.UsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 5 with FileReference

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

the class ProjectIconsAccessor method resolveIconFile.

@Nullable
public VirtualFile resolveIconFile(PsiType type, @Nullable PsiExpression initializer) {
    if (initializer == null || !initializer.isValid() || !isIconClassType(type)) {
        return null;
    }
    final List<FileReference> refs = new ArrayList<>();
    initializer.accept(new JavaRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof PsiLiteralExpression) {
                for (PsiReference ref : element.getReferences()) {
                    if (ref instanceof FileReference) {
                        refs.add((FileReference) ref);
                    }
                }
            }
            super.visitElement(element);
        }
    });
    for (FileReference ref : refs) {
        final PsiFileSystemItem psiFileSystemItem = ref.resolve();
        VirtualFile file = null;
        if (psiFileSystemItem == null) {
            final ResolveResult[] results = ref.multiResolve(false);
            for (ResolveResult result : results) {
                final PsiElement element = result.getElement();
                if (element instanceof PsiBinaryFile) {
                    file = ((PsiFile) element).getVirtualFile();
                    break;
                }
            }
        } else {
            file = psiFileSystemItem.getVirtualFile();
        }
        if (file == null || file.isDirectory() || !isIconFileExtension(file.getExtension()) || file.getLength() > ICON_MAX_SIZE) {
            continue;
        }
        return file;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)17 NotNull (org.jetbrains.annotations.NotNull)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 PsiElement (com.intellij.psi.PsiElement)5 PsiReference (com.intellij.psi.PsiReference)5 ArrayList (java.util.ArrayList)4 FileReferenceInjector (org.intellij.plugins.intelliLang.references.FileReferenceInjector)4 TextRange (com.intellij.openapi.util.TextRange)3 FileReferenceSet (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet)3 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)2 Nullable (org.jetbrains.annotations.Nullable)2 GoReference (com.goide.psi.impl.GoReference)1 CreateFileFix (com.intellij.codeInsight.daemon.quickFix.CreateFileFix)1 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)1 CfmlMappingsConfig (com.intellij.coldFusion.UI.config.CfmlMappingsConfig)1 CfmlProjectConfiguration (com.intellij.coldFusion.UI.config.CfmlProjectConfiguration)1 CfmlTag (com.intellij.coldFusion.model.psi.CfmlTag)1 ASTNode (com.intellij.lang.ASTNode)1 Module (com.intellij.openapi.module.Module)1 PsiDynaReference (com.intellij.openapi.paths.PsiDynaReference)1