Interface PsiReference


public interface PsiReference
A reference to a PSI element. For example, the variable name used in an expression. The "Go to Declaration" action can be used to go from a reference to the element it references. Generally returned from PsiElement#getReferences() and PsiReferenceService#getReferences, but may be contributed to some elements by third party plugins via PsiReferenceContributor.
See Also:
  • PsiPolyVariantReference
  • PsiElement.getReference()
  • PsiElement#getReferences()
  • PsiReferenceService#getReferences(PsiElement, PsiReferenceService.Hints)
  • PsiReferenceBase
  • PsiReferenceContributor
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    static final PsiReference[]
     
  • Method Summary

    Modifier and Type
    Method
    Description
    bindToElement(@NotNull PsiElement element)
    Changes the reference so that it starts to point to the specified element.
    @NotNull String
    Returns the name of the reference target element which does not depend on import statements and other context (for example, the full-qualified name of the class if the reference targets a Java class).
    @NotNull PsiElement
    Returns the underlying (referencing) element of the reference.
    @NotNull com.intellij.openapi.util.TextRange
    Returns the part of the underlying element which serves as a reference, or the complete text range of the element if the entire element is a reference.
    handleElementRename(@NotNull String newElementName)
    Called when the reference target element has been renamed, in order to change the reference text according to the new name.
    boolean
    isReferenceTo(@NotNull PsiElement element)
    Checks if the reference targets the specified element.
    boolean
    Returns false if the underlying element is guaranteed to be a reference, or true if the underlying element is a possible reference which should not be reported as an error if it fails to resolve.
    @Nullable PsiElement
    Returns the element which is the target of the reference.
  • Field Details

  • Method Details

    • getElement

      @NotNull @NotNull PsiElement getElement()
      Returns the underlying (referencing) element of the reference.
      Returns:
      the underlying element of the reference.
    • getRangeInElement

      @NotNull @NotNull com.intellij.openapi.util.TextRange getRangeInElement()
      Returns the part of the underlying element which serves as a reference, or the complete text range of the element if the entire element is a reference.

      Sample: PsiElement representing a fully qualified name with multiple dedicated PsiReferences, each bound to the range it resolves to (skipping the '.' separator).

       PsiElement text: qualified.LongName
       PsiReferences:   [Ref1---]X[Ref2--]
       
      where Ref1 would resolve to a "namespace" and Ref2 to an "element".
      Returns:
      Relative range in element
    • resolve

      @Nullable @Nullable PsiElement resolve()
      Returns the element which is the target of the reference.
      Returns:
      the target element, or null if it was not possible to resolve the reference to a valid target.
      See Also:
      • PsiPolyVariantReference#multiResolve(boolean)
    • getCanonicalText

      @NotNull @NotNull String getCanonicalText()
      Returns the name of the reference target element which does not depend on import statements and other context (for example, the full-qualified name of the class if the reference targets a Java class).
      Returns:
      the canonical text of the reference.
    • handleElementRename

      PsiElement handleElementRename(@NotNull @NotNull String newElementName) throws IncorrectOperationException
      Called when the reference target element has been renamed, in order to change the reference text according to the new name.
      Parameters:
      newElementName - the new name of the target element.
      Returns:
      the new underlying element of the reference.
      Throws:
      IncorrectOperationException - if the rename cannot be handled for some reason.
    • bindToElement

      PsiElement bindToElement(@NotNull @NotNull PsiElement element) throws IncorrectOperationException
      Changes the reference so that it starts to point to the specified element. This is called, for example, by the "Create Class from New" quickfix, to bind the (invalid) reference on which the quickfix was called to the newly created class.
      Parameters:
      element - the element which should become the target of the reference.
      Returns:
      the new underlying element of the reference.
      Throws:
      IncorrectOperationException - if the rebind cannot be handled for some reason.
    • isReferenceTo

      boolean isReferenceTo(@NotNull @NotNull PsiElement element)
      Checks if the reference targets the specified element.
      Parameters:
      element - the element to check target for.
      Returns:
      true if the reference targets that element, false otherwise.
    • isSoft

      boolean isSoft()
      Returns false if the underlying element is guaranteed to be a reference, or true if the underlying element is a possible reference which should not be reported as an error if it fails to resolve. For example, a text in an XML file which looks like a full-qualified Java class name is a soft reference.
      Returns:
      true if the reference is soft, false otherwise.