Java Code Examples for com.intellij.psi.PsiElement#getReferences()

The following examples show how to use com.intellij.psi.PsiElement#getReferences() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PsiViewerDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiElement resolve(int index) {
  final PsiElement element = getPsiElement();
  if (element == null) return null;
  @SuppressWarnings("unchecked")
  Map<PsiElement, PsiElement[]> map = (Map<PsiElement, PsiElement[]>)myRefs.getClientProperty(REFS_CACHE);
  if (map == null) {
    myRefs.putClientProperty(REFS_CACHE, map = new HashMap<PsiElement, PsiElement[]>());
  }
  PsiElement[] cache = map.get(element);
  if (cache == null) {
    final PsiReference[] references = element.getReferences();
    cache = new PsiElement[references.length];
    for (int i = 0; i < references.length; i++) {
      cache[i] = references[i].resolve();
    }
    map.put(element, cache);
  }
  return index >= cache.length ? null : cache[index];
}
 
Example 2
Source File: ControllerActionReferenceTest.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public void testReferenceCanResolveDefinition() {
    PsiFile file = myFixture.configureByText(PhpFileType.INSTANCE, "<?php \n" +
            "class ActionController {" +
            "public function action() {" +
            "  $this->forward('<caret>baz');" +
            "}" +
            "}"
    );

    PsiElement elementAtCaret = file.findElementAt(myFixture.getCaretOffset()).getParent();
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference reference : references) {
        if (reference instanceof ControllerActionReference) {
            ResolveResult[] resolveResults = ((ControllerActionReference) reference).multiResolve(false);
            for (ResolveResult resolveResult : resolveResults) {
                assertInstanceOf(resolveResult.getElement(), Method.class);

                return;
            }
        }
    }

    fail("No TranslationReference found");
}
 
Example 3
Source File: IconReferenceProviderTest.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public void testIconReferenceIsCreatedOnGetIcon() {
    myFixture.addFileToProject("foo/ext_emconf.php", "");
    myFixture.configureByFile("IconRegistry9.php");
    myFixture.configureByFile("icon_provider_test.php");

    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();

    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference ref : references) {
        if (ref instanceof IconReference) {
            return;
        }
    }

    fail("No icon reference found");
}
 
Example 4
Source File: StandardOptionReferenceTest.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
private void checkReferenceToField(String typeReference, String fieldName, String... file) {
    myFixture.configureByFiles(file);
    ProtoPsiFileRoot proto = (ProtoPsiFileRoot) myFixture.getFile();
    PsiElement elementAtCaret = proto
            .findElementAt(myFixture.getCaretOffset());
    while (elementAtCaret != null
            && elementAtCaret.getReferences().length == 0) {
        elementAtCaret = elementAtCaret.getParent();
    }
    assertNotNull(elementAtCaret);
    PsiElement target = elementAtCaret.getReferences()[0].resolve();
    Assert.assertTrue(String.valueOf(target), target instanceof FieldNode);
    FieldNode field = (FieldNode) target;
    Assert.assertEquals(fieldName, field.getFieldName());
    Assert.assertEquals(typeReference, ((DataType) field.getParent()).getQualifiedName());
}
 
Example 5
Source File: ResourceReferenceContributorTest.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public void testResourceReferencesAreCreated() {
    myFixture.addFileToProject("foo/ext_emconf.php", "");
    myFixture.addFileToProject("foo/Configuration/RTE/Processing.yaml", "");

    myFixture.configureByText(YAMLFileType.YML, "imports:\n" +
            "  - { resource: \"EXT:foo/Configuration/RTE/<caret>Processing.yaml\" }';");

    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();

    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference ref : references) {
        if (ref instanceof ResourceReference) {
            return;
        }
    }

    fail("No resource reference.");
}
 
Example 6
Source File: ResourceReferenceContributorTest.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public void testResourceReferencesAreCreated() {
    myFixture.addFileToProject("foo/ext_emconf.php", "");

    myFixture.configureByText(PhpFileType.INSTANCE, "<?php \n" +
            "'EXT:foo/ext_emconf.php<caret>';");

    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();

    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference ref : references) {
        if (ref instanceof ResourceReference) {
            return;
        }
    }

    fail("No resource reference.");
}
 
Example 7
Source File: UserFuncReferenceContributorTest.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public void testCanCreateUserFuncReferencesOnConcatenationExpressionsInPHPArrays() {
    PsiFile file = myFixture.configureByText(
            "foo.php",
            "<?php \n$foo = ['userFunc' => \\Foo\\Bar::class . '->coun<caret>t'];"
    );

    PsiElement elementAtCaret = file.findElementAt(myFixture.getCaretOffset()).getParent();
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference reference: references) {
        if (reference instanceof UserFuncReference) {
            return;
        }
    }

    fail("No UserFuncReference found");
}
 
Example 8
Source File: UserFuncReferenceContributorTest.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public void testCanCreateUserFuncReferencesOnPHPArrays() {
    PsiFile file = myFixture.configureByText(
            "foo.php",
            "<?php $foo = ['userFunc' => 'coun<caret>t'];"
    );

    PsiElement elementAtCaret = file.findElementAt(myFixture.getCaretOffset()).getParent();
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference reference: references) {
        if (reference instanceof UserFuncReference) {
            return;
        }
    }

    fail("No UserFuncReference found");
}
 
Example 9
Source File: ResourceReferenceContributorTest.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public void testResourceReferenceResolves() {
    PsiFile psiFile = myFixture.addFileToProject("foo/ext_emconf.php", "");
    myFixture.addFileToProject("foo/bar.txt", "");

    myFixture.configureByText(PhpFileType.INSTANCE, "<?php \n" +
            "'EXT:foo/ext_emconf.php<caret>';");

    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();

    ResourceReference target = null;
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference ref : references) {
        if (ref instanceof ResourceReference) {
            target = (ResourceReference) ref;
        }
    }

    if (target != null) {
        for (ResolveResult o : target.multiResolve(true)) {
            if (o.getElement().isEquivalentTo(psiFile)) {
                return;
            }
        }
    }

    fail("No resource reference.");
}
 
Example 10
Source File: SoyReferenceContributor.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(
    @NotNull PsiElement element,
    @NotNull ProcessingContext context) {
  return element.getReferences();
}
 
Example 11
Source File: ImagePreviewComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static JComponent getPreviewComponent(@Nullable final PsiElement parent) {
  if (parent == null) {
    return null;
  }
  final PsiReference[] references = parent.getReferences();
  for (final PsiReference reference : references) {
    final PsiElement fileItem = reference.resolve();
    if (fileItem instanceof PsiFileSystemItem) {
      final PsiFileSystemItem item = (PsiFileSystemItem)fileItem;
      if (!item.isDirectory()) {
        final VirtualFile file = item.getVirtualFile();
        if (file != null && supportedExtensions.contains(file.getExtension())) {
          try {
            refresh(file);
            SoftReference<BufferedImage> imageRef = file.getUserData(BUFFERED_IMAGE_REF_KEY);
            final BufferedImage image = SoftReference.dereference(imageRef);
            if (image != null) {
              return new ImagePreviewComponent(image, file.getLength());
            }
          }
          catch (IOException ignored) {
            // nothing
          }
        }
      }
    }
  }

  return null;
}
 
Example 12
Source File: TranslationReferenceProviderTest.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public void testCanCreateTranslationReferencesOnPhpStrings() {
    PsiFile file = myFixture.configureByText(PhpFileType.INSTANCE, "<?php \n" +
            "\"LLL:EXT:foo/sample.xlf:sys_<caret>language.language_isocode.ab\";");

    PsiElement elementAtCaret = file.findElementAt(myFixture.getCaretOffset()).getParent();
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference reference : references) {
        if (reference instanceof TranslationReference) {
            return;
        }
    }

    fail("No TranslationReference found");
}
 
Example 13
Source File: FileReferenceUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiFileReference findFileReference(@Nonnull PsiElement element) {
  final PsiReference[] references = element.getReferences();
  for (int i = references.length - 1; i >= 0; i--) {
    PsiReference ref = references[i];
    if (ref instanceof FileReferenceOwner && !(ref instanceof PsiFileReference)) {
      ref = ((FileReferenceOwner)ref).getLastFileReference();
    }
    if (ref instanceof PsiFileReference) {
      return (PsiFileReference)references[i];
    }
  }
  return null;
}
 
Example 14
Source File: PsiViewerDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void valueChanged(ListSelectionEvent e) {
  clearSelection();
  updateVersionsCombo(null);
  updateExtensionsCombo();
  final int ind = myRefs.getSelectedIndex();
  final PsiElement element = getPsiElement();
  if (ind > -1 && element != null) {
    final PsiReference[] references = element.getReferences();
    if (ind < references.length) {
      final TextRange textRange = references[ind].getRangeInElement();
      TextRange range = InjectedLanguageManager.getInstance(myProject).injectedToHost(element, element.getTextRange());
      int start = range.getStartOffset();
      int end = range.getEndOffset();
      final ViewerTreeStructure treeStructure = (ViewerTreeStructure)myPsiTreeBuilder.getTreeStructure();
      PsiElement rootPsiElement = treeStructure.getRootPsiElement();
      if (rootPsiElement != null) {
        int baseOffset = rootPsiElement.getTextRange().getStartOffset();
        start -= baseOffset;
        end -= baseOffset;
      }

      start += textRange.getStartOffset();
      end = start + textRange.getLength();
      myListenerHighlighter = myEditor.getMarkupModel()
        .addRangeHighlighter(start, end, HighlighterLayer.FIRST + 1, myAttributes, HighlighterTargetArea.EXACT_RANGE);
    }
  }
}
 
Example 15
Source File: TranslationUtil.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public static boolean hasTranslationReference(@NotNull PsiElement element) {

        for (PsiReference reference : element.getReferences()) {
            if (reference instanceof TranslationReference) {
                return true;
            }
        }

        return false;
    }
 
Example 16
Source File: ResourceReferenceContributorTest.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public void testResourceReferenceHasVariants() {
    myFixture.addFileToProject("foo/ext_emconf.php", "");
    myFixture.addFileToProject("foo/bar.txt", "");

    myFixture.configureByText(PhpFileType.INSTANCE, "<?php \n" +
            "'EXT:foo/ext_emconf.php<caret>';");

    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();

    ResourceReference target = null;
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference ref : references) {
        if (ref instanceof ResourceReference) {
            target = (ResourceReference) ref;
        }
    }

    if (target != null) {
        for (Object o : target.getVariants()) {
            if (o instanceof ResourceLookupElement && ((ResourceLookupElement) o).getLookupString().contains("EXT:foo/bar.txt")) {
                return;
            }
        }
    }

    fail("No resource reference.");
}
 
Example 17
Source File: ResourceReferenceContributorTest.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public void testResourceReferenceResolves() {
    myFixture.addFileToProject("foo/ext_emconf.php", "");
    myFixture.addFileToProject("foo/bar.txt", "");

    PsiFile psiFile = myFixture.addFileToProject("foo/Configuration/RTE/Processing.yaml", "");

    myFixture.configureByText(YAMLFileType.YML, "imports:\n" +
            "  - { resource: \"EXT:foo<caret>/Configuration/RTE/Processing.yaml\" }';");

    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();

    ResourceReference target = null;
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference ref : references) {
        if (ref instanceof ResourceReference) {
            target = (ResourceReference) ref;
        }
    }

    if (target != null) {
        for (ResolveResult o : target.multiResolve(true)) {
            if (o.getElement().isEquivalentTo(psiFile)) {
                return;
            }
        }
    }

    fail("No resource reference.");
}
 
Example 18
Source File: CodeInsightFixtureTestCase.java    From silex-idea-plugin with MIT License 5 votes vote down vote up
protected void assertReferenceContains(LanguageFileType languageFileType, String configureByText, String classFqn) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement element = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();

    if ((element.getReferences().length > 0) && ((PhpClass) element.getReferences()[0].resolve()) != null) {
        assertEquals(classFqn, ((PhpClass) element.getReferences()[0].resolve()).getFQN());
    } else {
        fail("Cannot resolve PhpReference.");
    }
}
 
Example 19
Source File: ControllerActionReferenceTest.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public void testReferenceCanResolveVariants() {
    PsiFile file = myFixture.configureByText(PhpFileType.INSTANCE, "<?php \n" +
            "class ActionController {" +
            "public function action() {" +
            "  $this->forward('<caret>baz');" +
            "}" +
            "}"
    );

    PsiElement elementAtCaret = file.findElementAt(myFixture.getCaretOffset()).getParent();
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference reference : references) {
        if (reference instanceof ControllerActionReference) {
            Object[] variants = reference.getVariants();
            for (Object variant : variants) {
                if (variant instanceof LookupElement) {
                    String lookupString = ((LookupElement) variant).getLookupString();
                    assertEquals("foo", lookupString);
                    return;
                }
            }

        }
    }

    fail("No ControllerActionReference found");
}
 
Example 20
Source File: TranslationUtil.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public static TranslationReference getTranslationReference(@NotNull PsiElement element) {

        for (PsiReference reference : element.getReferences()) {
            if (reference instanceof TranslationReference) {
                return (TranslationReference) reference;
            }
        }

        return null;
    }