Java Code Examples for com.intellij.psi.PsiReference#resolve()

The following examples show how to use com.intellij.psi.PsiReference#resolve() . 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: HaskellGoToSymbolTest.java    From intellij-haskforce with Apache License 2.0 7 votes vote down vote up
public void testGoToSymbolFunction_QualifiedImport() {
    PsiFile[] psiFiles = myFixture.configureByFiles(
            "QualifiedImport/Usage.hs",
            "QualifiedImport/Definition.hs"
    );
    PsiFile usage = psiFiles[0];
    PsiFile definition = psiFiles[1];
    String textOfFile = definition.getText();
    int expectedStartOffset = textOfFile.indexOf("seven ::");
    PsiElement psiElement = usage
            .findElementAt(myFixture.getCaretOffset()).getParent();
    HaskellVarid varId = (HaskellVarid) psiElement;
    PsiReference reference = varId.getReference();
    HaskellVarid referencedElement = (HaskellVarid) reference.resolve();
    assertNotSame(psiElement, referencedElement);
    assertEquals(expectedStartOffset, referencedElement.getTextRange().getStartOffset());
}
 
Example 2
Source File: LazyGetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static boolean isInitializedInConstructors(@NotNull PsiElement element) {
  if (!(element instanceof PsiIdentifier)) {
    return false;
  }
  PsiElement parent = element.getParent();
  if (!(parent instanceof PsiReferenceExpression)) {
    return false;
  }
  PsiElement qualifier = ((PsiReferenceExpression) parent).getQualifier();
  if (qualifier == null) {
    return false;
  }
  PsiReference reference = qualifier.getReference();
  if (reference == null) {
    return false;
  }
  PsiElement field = reference.resolve();
  if (!(field instanceof PsiField)) {
    return false;
  }
  PsiClass containingClass = ((PsiField) field).getContainingClass();
  if (containingClass == null) {
    return false;
  }
  return isInitializedInConstructors((PsiField) field, containingClass);
}
 
Example 3
Source File: HaskellGoToSymbolTest.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
public void testGoToSymbolFunction_QualifiedImport_MultipleImportSameQualifiedName2() {
    PsiFile[] psiFiles = myFixture.configureByFiles(
            "QualifiedImport_MultipleImportSameQualifiedName2/Usage/Usage.hs",
            "QualifiedImport_MultipleImportSameQualifiedName2/Definition/Definition.hs",
            "QualifiedImport_MultipleImportSameQualifiedName2/Other/Definition.hs"
    );
    PsiFile usage = psiFiles[0];
    String textOfFile = usage.getText();
    int expectedStartOffset = textOfFile.indexOf("qualified Other.Definition as Definition") + 30;
    PsiElement psiElement = usage
            .findElementAt(myFixture.getCaretOffset()).getParent();
    HaskellConid conId = (HaskellConid) psiElement;
    PsiReference reference = conId.getReference();
    HaskellConid referencedElement = (HaskellConid) reference.resolve();
    assertNotSame(psiElement, referencedElement);
    assertEquals(expectedStartOffset, referencedElement.getTextRange().getStartOffset());
}
 
Example 4
Source File: HaskellGoToSymbolTest.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
public void testGoToSymbolFunction_QualifiedImportQualifierresolvesMultipleCons_Cons1_NoAs() {
    PsiFile[] psiFiles = myFixture.configureByFiles(
            "QualifiedImport_QualifierResolvesMultipleCons_Cons1_NoAs/Usage.hs",
            "QualifiedImport_QualifierResolvesMultipleCons_Cons1_NoAs/Definition.hs"
    );
    PsiFile usage = psiFiles[0];
    String textOfFile = usage.getText();
    int expectedStartOffset = textOfFile.indexOf("qualified Definition.Definition") + 10;
    PsiElement psiElement = usage
            .findElementAt(myFixture.getCaretOffset()).getParent();
    HaskellConid conId = (HaskellConid) psiElement;
    PsiReference reference = conId.getReference();
    HaskellConid referencedElement = (HaskellConid) reference.resolve();
    assertNotSame(psiElement, referencedElement);
    assertEquals(expectedStartOffset, referencedElement.getTextRange().getStartOffset());
}
 
Example 5
Source File: HaskellGoToSymbolTest.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
public void testGoToSymbolFunction_QualifiedImportQualifierresolves() {
    PsiFile[] psiFiles = myFixture.configureByFiles(
            "QualifiedImport_QualifierResolves/Usage.hs",
            "QualifiedImport_QualifierResolves/Definition.hs"
    );
    PsiFile usage = psiFiles[0];
    String textOfFile = usage.getText();
    int expectedStartOffset = textOfFile.indexOf("as Def") + 3;
    PsiElement psiElement = usage
            .findElementAt(myFixture.getCaretOffset()).getParent();
    HaskellConid conId = (HaskellConid) psiElement;
    PsiReference reference = conId.getReference();
    HaskellConid referencedElement = (HaskellConid) reference.resolve();
    assertNotSame(psiElement, referencedElement);
    assertEquals(expectedStartOffset, referencedElement.getTextRange().getStartOffset());
}
 
Example 6
Source File: FunctionResolveTestCase.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@NotNull
private PsiElement doCheckFunctionReference(boolean dumbMode) throws Exception {
    boolean oldDumb = DumbService.isDumb(myProject);

    DumbServiceImpl.getInstance(myProject).setDumb(dumbMode);
    try {
        PsiReference psiReference = configure();
        Assert.assertTrue(psiReference.getElement() instanceof BashCommand);
        BashCommand commandElement = (BashCommand) psiReference.getElement();

        Assert.assertTrue(psiReference.resolve() instanceof BashFunctionDef);
        Assert.assertTrue(commandElement.isFunctionCall());
        Assert.assertFalse(commandElement.isVarDefCommand());
        Assert.assertFalse(commandElement.isExternalCommand());
        Assert.assertTrue(commandElement.getReference().isReferenceTo(psiReference.resolve()));

        return psiReference.resolve();
    } finally {
        DumbServiceImpl.getInstance(myProject).setDumb(oldDumb);
    }
}
 
Example 7
Source File: HaskellGoToSymbolTest.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
public void testGoToSymbolFunction_ImportResolvesToModule() {
    PsiFile[] psiFiles = myFixture.configureByFiles(
            "ImportResolvesToModule/Usage.hs",
            "ImportResolvesToModule/Definition.hs"
    );
    PsiFile usage = psiFiles[0];
    PsiFile definition = psiFiles[1];
    String textOfFile = definition.getText();
    int expectedStartOffset = textOfFile.indexOf("module Definition") + 7;
    PsiElement psiElement = usage
            .findElementAt(myFixture.getCaretOffset()).getParent();
    HaskellConid conId = (HaskellConid) psiElement;
    PsiReference reference = conId.getReference();
    HaskellConid referencedElement = (HaskellConid) reference.resolve();
    assertNotSame(psiElement, referencedElement);
    assertEquals(expectedStartOffset, referencedElement.getTextRange().getStartOffset());
}
 
Example 8
Source File: HaskellGoToSymbolTest.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public void testGoToSymbolFunction_CanReferenceOtherFunction(){
    myFixture.configureByFile(getTestName(false)+".hs");
    PsiFile file = myFixture.getFile();
    String textOfFile = file.getText();
    int expectedStartOffset= textOfFile.indexOf("test2 ::");
    PsiElement psiElement = file
            .findElementAt(myFixture.getCaretOffset()).getParent();
    HaskellVarid varId = (HaskellVarid) psiElement;
    PsiReference reference = varId.getReference();
    HaskellVarid referencedElement = (HaskellVarid)reference.resolve();
    assertNotSame(psiElement, referencedElement);
    assertEquals(expectedStartOffset,referencedElement.getTextRange().getStartOffset());
}
 
Example 9
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 10
Source File: BlueprintBeanReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
private Collection<UsageInfo> findBeanUsages(PsiElement element) {
    PsiReference reference = element.getReference();
    assertNotNull(reference);
    assertEquals(BeanSelfReference.class, reference.getClass());
    PsiElement target = reference.resolve();
    assertNotNull(target);
    assertEquals(ReferenceableIdPsiElement.class, target.getClass());
    return myFixture.findUsages(target);
}
 
Example 11
Source File: CustomOptionReferenceTest.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
public void testCustomOptionReference() {
    PsiReference reference = myFixture.getReferenceAtCaretPositionWithAssertion(
            "reference/options/custom/SimpleExtensionField.proto"
    );
    PsiElement target = reference.resolve();
    Assert.assertNotNull(target);
    Assert.assertTrue(target instanceof FieldNode);
    FieldNode field = (FieldNode) target;
    Assert.assertEquals("bar", field.getFieldName());
    Assert.assertTrue(field.getParent() instanceof ExtendEntryNode);
}
 
Example 12
Source File: ReferenceTest.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
public void testFix_import_empty_proto() {
    String[] file = new String[]{"reference/Fix_import_empty_proto.proto"};
    myFixture.configureByFiles(file);
    PsiReference referenceAtCaretPosition = myFixture.getReferenceAtCaretPositionWithAssertion(file);
    PsiElement target = referenceAtCaretPosition.resolve();
    assertNotNull(target);
    assertTrue(target instanceof ProtoPsiFileRoot);
    ProtoPsiFileRoot protoPsiFileRoot = (ProtoPsiFileRoot) target;
    assertEquals("empty.proto", protoPsiFileRoot.getName());
}
 
Example 13
Source File: ProtoErrorsAnnotator.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
private void checkReference(PsiElement element) {
    PsiReference ref = element.getReference();
    if (ref == null || ref.isSoft()) {
        return;
    }
    if (ref.resolve() == null) {
        String message = message("error.unresolved.reference");
        markError(element.getNode(), null, message);
    }
}
 
Example 14
Source File: HaskellGoToSymbolTest.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public void testGoToSymbolFunction_Records(){
    myFixture.configureByFile(getTestName(false)+".hs");
    PsiFile file = myFixture.getFile();
    String textOfFile = file.getText();
    int expectedStartOffset= textOfFile.indexOf("Pool maybeA") + 5;
    PsiElement psiElement = file
            .findElementAt(myFixture.getCaretOffset()).getParent();
    HaskellVarid varId = (HaskellVarid) psiElement;
    PsiReference reference = varId.getReference();
    HaskellVarid referencedElement = (HaskellVarid)reference.resolve();
    assertNotSame(psiElement, referencedElement);
    assertEquals(expectedStartOffset,referencedElement.getTextRange().getStartOffset());
}
 
Example 15
Source File: PhpElementsUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Nullable
private static String getStringValue(@Nullable PsiElement psiElement, int depth) {
    if(psiElement == null || ++depth > 5) {
        return null;
    }

    if(psiElement instanceof StringLiteralExpression) {
        String resolvedString = ((StringLiteralExpression) psiElement).getContents();
        if(StringUtils.isEmpty(resolvedString)) {
            return null;
        }

        return resolvedString;
    } else if(psiElement instanceof Field) {
        return getStringValue(((Field) psiElement).getDefaultValue(), depth);
    } else if(psiElement instanceof ClassConstantReference && "class".equals(((ClassConstantReference) psiElement).getName())) {
        // Foobar::class
        return getClassConstantPhpFqn((ClassConstantReference) psiElement);
    } else if(psiElement instanceof PhpReference) {
        PsiReference psiReference = psiElement.getReference();
        if(psiReference == null) {
            return null;
        }

        PsiElement ref = psiReference.resolve();
        if(ref instanceof PhpReference) {
            return getStringValue(psiElement, depth);
        }

        if(ref instanceof Field) {
            return getStringValue(((Field) ref).getDefaultValue());
        }
    }

    return null;
}
 
Example 16
Source File: ReferenceTest.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
private void checkReferenceToDataType(String typeReference, String... file) {
    myFixture.configureByFiles(file);
    PsiReference referenceAtCaretPosition = myFixture.getReferenceAtCaretPositionWithAssertion(file);
    PsiElement target = referenceAtCaretPosition.resolve();
    if (typeReference != null) {
        assertNotNull(target);
        assertTrue(target instanceof DataType);
        DataType dataType = (DataType) target;
        assertEquals(typeReference, dataType.getQualifiedName());
    }
}
 
Example 17
Source File: VarResolveTestCase.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoResolveVarWithTwoLocalDefs() throws Exception {
    PsiReference psiReference = configure();

    //must not resolve because the definition is local due to the previous definition
    PsiElement varDef = psiReference.resolve();
    Assert.assertNull("The vardef should not be found, because it is local.", varDef);
}
 
Example 18
Source File: HaskellGoToSymbolTest.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public void testGoToSymbolFunction_CaretOnFunctionInstance(){
    myFixture.configureByFile(getTestName(false)+".hs");
    PsiFile file = myFixture.getFile();
    String textOfFile = file.getText();
    int expectedStartOffset= textOfFile.indexOf("test ::");
    PsiElement psiElement = file
            .findElementAt(myFixture.getCaretOffset()).getParent();
    HaskellVarid varId = (HaskellVarid) psiElement;
    PsiReference reference = varId.getReference();
    HaskellVarid referencedElement = (HaskellVarid)reference.resolve();
    assertNotSame(psiElement, referencedElement);
    assertEquals(expectedStartOffset, referencedElement.getTextRange().getStartOffset());
}
 
Example 19
Source File: PhpElementsUtil.java    From idea-php-advanced-autocomplete with MIT License 4 votes vote down vote up
private static String getFuncName(PsiElement caller) {

        PsiReference psiReference = caller.getReference();

        if (psiReference == null) {
            return null;
        }

        PsiElement resolvedReference = psiReference.resolve();

        if (!(resolvedReference instanceof Function)) {
            return null;
        }

        Function function = (Function)resolvedReference;

        return function.getName();
    }
 
Example 20
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 4 votes vote down vote up
@Nullable
private static String getStringValue(@Nullable PsiElement psiElement, int depth) {

    if(psiElement == null || ++depth > 5) {
        return null;
    }

    if(psiElement instanceof StringLiteralExpression) {
        String resolvedString = ((StringLiteralExpression) psiElement).getContents();
        if(StringUtils.isEmpty(resolvedString)) {
            return null;
        }

        return resolvedString;
    }

    if(psiElement instanceof Field) {
        return getStringValue(((Field) psiElement).getDefaultValue(), depth);
    }

    if(psiElement instanceof PhpReference) {

        PsiReference psiReference = psiElement.getReference();
        if(psiReference == null) {
            return null;
        }

        PsiElement ref = psiReference.resolve();
        if(ref instanceof PhpReference) {
            return getStringValue(psiElement, depth);
        }

        if(ref instanceof Field) {
            PsiElement resolved = ((Field) ref).getDefaultValue();

            if(resolved instanceof StringLiteralExpression) {
                return ((StringLiteralExpression) resolved).getContents();
            }
        }

    }

    return null;

}