Search in sources :

Example 91 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.

the class ResolveTestCase method configureByFileText.

protected PsiReference configureByFileText(String fileText, String fileName, @Nullable VirtualFile parentDir) throws Exception {
    int offset = fileText.indexOf(MARKER);
    assertTrue(offset >= 0);
    fileText = fileText.substring(0, offset) + fileText.substring(offset + MARKER.length());
    if (parentDir == null) {
        myFile = createFile(myModule, fileName, fileText);
    } else {
        VirtualFile existing = parentDir.findChild(fileName);
        if (existing != null) {
            myDocument = FileDocumentManager.getInstance().getDocument(existing);
            assertNotNull(myDocument);
            final String finalFileText = fileText;
            ApplicationManager.getApplication().runWriteAction(() -> myDocument.setText(finalFileText));
            myFile = PsiManager.getInstance(getProject()).findFile(existing);
            assertNotNull(myFile);
            assertEquals(fileText, myFile.getText());
        } else {
            myFile = createFile(myModule, parentDir, fileName, fileText);
        }
    }
    PsiReference ref = myFile.findReferenceAt(offset);
    assertNotNull(ref);
    return ref;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiReference(com.intellij.psi.PsiReference)

Example 92 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-plugins by JetBrains.

the class CfmlFileReferenceInspection method buildVisitor.

@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new PsiElementVisitor() {

        public void visitElement(final PsiElement element) {
            PsiElement tagParent = PsiTreeUtil.getParentOfType((element), CfmlTag.class);
            if ((element.getNode().getElementType() == CfmlTokenTypes.STRING_TEXT)) {
                if ((tagParent == null || (!((CfmlTag) tagParent).getTagName().equalsIgnoreCase("cfinclude") && !((CfmlTag) tagParent).getTagName().equalsIgnoreCase("cfmodule")))) {
                    PsiElement superParent = element.getParent() != null ? element.getParent().getParent() : null;
                    ASTNode superParentNode = superParent != null ? superParent.getNode() : null;
                    if ((superParentNode == null || superParentNode.getElementType() != CfmlElementTypes.INCLUDEEXPRESSION)) {
                        return;
                    }
                }
                final PsiReference[] refs = element.getParent().getReferences();
                for (int i = 0, refsLength = refs.length; i < refsLength; i++) {
                    PsiReference ref = refs[i];
                    if (!(ref instanceof FileReference))
                        continue;
                    if (ref.resolve() == null) {
                        PsiDirectory dir;
                        if (i > 0) {
                            final PsiElement target = refs[i - 1].resolve();
                            dir = target instanceof PsiDirectory ? (PsiDirectory) target : null;
                        } else {
                            dir = element.getContainingFile().getParent();
                        }
                        holder.registerProblem(ref.getElement(), ref.getRangeInElement(), isOnTheFly ? "Path '" + ref.getCanonicalText() + "' not found" : "Path not found", isOnTheFly && dir != null ? new LocalQuickFix[] { new CreateFileFix(i < refs.length - 1, ref.getCanonicalText(), dir) } : LocalQuickFix.EMPTY_ARRAY);
                        //                       ProblemHighlightType.ERROR);
                        break;
                    }
                }
            }
        }
    };
}
Also used : CreateFileFix(com.intellij.codeInsight.daemon.quickFix.CreateFileFix) PsiDirectory(com.intellij.psi.PsiDirectory) ASTNode(com.intellij.lang.ASTNode) PsiReference(com.intellij.psi.PsiReference) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) CfmlTag(com.intellij.coldFusion.model.psi.CfmlTag) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 93 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-plugins by JetBrains.

the class DartControlFlow method analyze.

public static DartControlFlow analyze(PsiElement[] elements) {
    final PsiElement scope = PsiTreeUtil.getTopmostParentOfType(elements[0], DartExecutionScope.class);
    final PsiElement lastElement = elements[elements.length - 1];
    final int lastElementEndOffset = lastElement.getTextRange().getEndOffset();
    final int firstElementStartOffset = elements[0].getTextRange().getStartOffset();
    // find out params
    assert scope != null;
    final LocalSearchScope localSearchScope = new LocalSearchScope(scope);
    final List<DartComponentName> outDeclarations = ContainerUtil.filter(DartControlFlowUtil.getSimpleDeclarations(elements, null, false), componentName -> {
        for (PsiReference usage : ReferencesSearch.search(componentName, localSearchScope, false).findAll()) {
            if (usage.getElement().getTextRange().getStartOffset() > lastElementEndOffset) {
                return true;
            }
        }
        return false;
    });
    // find params
    final DartReferenceVisitor dartReferenceVisitor = new DartReferenceVisitor();
    for (PsiElement element : elements) {
        element.accept(dartReferenceVisitor);
    }
    final List<DartComponentName> inComponentNames = ContainerUtil.filter(dartReferenceVisitor.getComponentNames(), componentName -> {
        final int offset = componentName.getTextRange().getStartOffset();
        final boolean declarationInElements = firstElementStartOffset <= offset && offset < lastElementEndOffset;
        return !declarationInElements;
    });
    return new DartControlFlow(inComponentNames, outDeclarations);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 94 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-plugins by JetBrains.

the class FlexCssNavigationTest method findTargetElements.

@NotNull
private PsiElement[] findTargetElements(@NotNull String... filenames) throws Exception {
    String[] fileNamesWithBasePath = new String[filenames.length];
    for (int i = 0, filenamesLength = filenames.length; i < filenamesLength; i++) {
        fileNamesWithBasePath[i] = BASE_PATH + filenames[i];
    }
    configureByFiles(null, fileNamesWithBasePath);
    Collection<PsiElement> targets;
    PsiReference reference = TargetElementUtil.findReference(myEditor);
    if (reference == null) {
        reference = JSTestUtils.findReferenceFromInjected(myEditor, myFile);
    }
    assertNotNull(reference);
    if (reference instanceof PsiMultiReference) {
        targets = new ArrayList<>();
        for (PsiReference ref : ((PsiMultiReference) reference).getReferences()) {
            targets.addAll(TargetElementUtil.getInstance().getTargetCandidates(ref));
        }
    } else {
        targets = TargetElementUtil.getInstance().getTargetCandidates(reference);
    }
    assertTrue("Target elements not found", targets.size() > 0);
    return PsiUtilCore.toPsiElementArray(targets);
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 95 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-plugins by JetBrains.

the class ActionScriptResolveTest method testResolve26.

@JSTestOptions({ JSTestOption.WithIndex })
public void testResolve26() throws Exception {
    String fileText = "dynamic class Object {}\n" + "package {\n" + "\timport flash.display.Sprite;\n" + "\tpublic class DoubleInterfaceResolve extends Sprite {\n" + "\t}\n" + "}\n" + "import flash.display.Sprite;\n" + "class FooView extends Sprite {\n" + "}\n" + "\n" + "interface ItfA {\n" + "    function f():It<ref>fB;\n" + "}\n" + "interface ItfB {}";
    PsiReference ref = configureByFileText(fileText, "sample.js2");
    PsiElement resolved = ref.resolve();
    assertTrue(resolved instanceof JSClass);
}
Also used : PsiReference(com.intellij.psi.PsiReference) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) JSTestOptions(com.intellij.lang.javascript.JSTestOptions)

Aggregations

PsiReference (com.intellij.psi.PsiReference)564 PsiElement (com.intellij.psi.PsiElement)327 NotNull (org.jetbrains.annotations.NotNull)97 Nullable (org.jetbrains.annotations.Nullable)55 TextRange (com.intellij.openapi.util.TextRange)54 PsiFile (com.intellij.psi.PsiFile)52 ArrayList (java.util.ArrayList)46 Test (org.junit.Test)40 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)36 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)32 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)25 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)23 XmlTag (com.intellij.psi.xml.XmlTag)22 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)20 PsiClass (com.intellij.psi.PsiClass)17 XmlAttribute (com.intellij.psi.xml.XmlAttribute)17 LinkedList (java.util.LinkedList)17 LookupElement (com.intellij.codeInsight.lookup.LookupElement)16 Project (com.intellij.openapi.project.Project)16