com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference Java Examples

The following examples show how to use com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference. 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: RequirejsTestCase.java    From WebStormRequireJsPlugin with MIT License 6 votes vote down vote up
protected void assertReference(PsiReference reference, String expectedText, String expectedFileName) {
    assertNotNull("wrong cursor position", reference);
    if (reference instanceof PsiMultiReference) {
        for (PsiReference ref : ((PsiMultiReference)reference).getReferences()) {
            if (ref instanceof RequirejsReference) {
                reference = ref;
                break;
            }
        }
    }

    if (!expectedText.startsWith("'")) {
        expectedText = "'".concat(expectedText).concat("'");
    }

    assertInstanceOf(reference, RequirejsReference.class);
    assertEquals(expectedText, reference.getCanonicalText());
    PsiElement referenceElement = reference.resolve();
    if (null == expectedFileName) {
        assertNull(referenceElement);
    } else {
        assertNotNull("Not resolved", referenceElement);
        assertInstanceOf(referenceElement, PsiFile.class);
        assertEquals(expectedFileName, ((PsiFile) referenceElement).getName());
    }
}
 
Example #2
Source File: FileReference.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
public static FileReference findFileReference(@Nonnull final PsiReference original) {
  if (original instanceof PsiMultiReference) {
    final PsiMultiReference multiReference = (PsiMultiReference)original;
    for (PsiReference reference : multiReference.getReferences()) {
      if (reference instanceof FileReference) {
        return (FileReference)reference;
      }
    }
  }
  else if (original instanceof FileReferenceOwner) {
    final PsiFileReference fileReference = ((FileReferenceOwner)original).getLastFileReference();
    if (fileReference instanceof FileReference) {
      return (FileReference)fileReference;
    }
  }

  return null;
}
 
Example #3
Source File: SharedPsiElementImplUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
@RequiredReadAction
public static PsiReference findReferenceAt(PsiElement thisElement, int offset, @Nullable Language lang) {
  if (thisElement == null) return null;
  PsiElement element = lang != null ? thisElement.getContainingFile().getViewProvider().findElementAt(offset, lang) :
                       thisElement.findElementAt(offset);
  if (element == null || element instanceof OuterLanguageElement) return null;
  offset = thisElement.getTextRange().getStartOffset() + offset - element.getTextRange().getStartOffset();

  List<PsiReference> referencesList = new ArrayList<PsiReference>();
  while (element != null) {
    addReferences(offset, element, referencesList);
    if (element instanceof PsiFile) break;
    if (element instanceof HintedReferenceHost &&
        !((HintedReferenceHost)element).shouldAskParentForReferences(new PsiReferenceService.Hints(null, offset))) {
      break;
    }
    offset = element.getStartOffsetInParent() + offset;
    element = element.getParent();
  }

  if (referencesList.isEmpty()) return null;
  if (referencesList.size() == 1) return referencesList.get(0);
  return new PsiMultiReference(referencesList.toArray(new PsiReference[referencesList.size()]),
                               referencesList.get(referencesList.size() - 1).getElement());
}
 
Example #4
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void addReferenceAtCaret(Collection<PsiReference> refs) {
  PsiFile myEditorFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
  // Note, that myEditorFile can be different from myElement.getContainingFile() e.g. in injections: myElement declaration in one
  // file / usage in another !
  final PsiReference reference = (myEditorFile != null ?
                                  myEditorFile : myElementToRename.getContainingFile())
    .findReferenceAt(myEditor.getCaretModel().getOffset());
  if (reference instanceof PsiMultiReference) {
    final PsiReference[] references = ((PsiMultiReference)reference).getReferences();
    for (PsiReference ref : references) {
      addReferenceIfNeeded(refs, ref);
    }
  }
  else {
    addReferenceIfNeeded(refs, reference);
  }
}
 
Example #5
Source File: BuildTargetElementEvaluator.java    From intellij with Apache License 2.0 5 votes vote down vote up
/** Returns null in the cases where we're happy with the default behavior. */
@Nullable
@Override
public PsiElement getElementByReference(PsiReference ref, int flags) {
  if (!(ref instanceof PsiMultiReference) || !(ref.getElement() instanceof StringLiteral)) {
    return null;
  }
  // choose the outer-most reference
  PsiReference[] refs = ((PsiMultiReference) ref).getReferences().clone();
  Arrays.sort(refs, COMPARATOR);
  return refs[0].resolve();
}
 
Example #6
Source File: HaxeHierarchyUtils.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
/**
 * Given a PSI id element, find out if it -- or one of its parents --
 * references a class, and, if so, returns the PSI element for the class.
 *
 * @param id A PSI element for an identifier (e.g. variable name).
 * @return A PSI class element, or null if not found.
 */
@Nullable
public static HaxeClass findReferencedClassForId(@NotNull LeafPsiElement id) {
  if (null == id) {
    return null;
  }

  PsiReference found = id.findReferenceAt(0);
  PsiElement resolved = null;
  if (found instanceof PsiMultiReference) {
    for (PsiReference ref : ((PsiMultiReference)found).getReferences()) {
      PsiElement target = ref.resolve();
      if (null != target && target instanceof PsiClass) {
        resolved = target;
        break;
      }
    }
  }
  else {
    resolved = found.resolve();
  }

  if (LOG.isDebugEnabled()) {
    LOG.debug("findReferencedClassForID found " + resolved);
  }

  return ((resolved instanceof HaxeClass) ? ((HaxeClass) resolved) : null);
}
 
Example #7
Source File: CompletionData.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected static PsiReference[] getReferences(final PsiMultiReference multiReference) {
  final PsiReference[] references = multiReference.getReferences();
  final List<PsiReference> hard = ContainerUtil.findAll(references, object -> !object.isSoft());
  if (!hard.isEmpty()) {
    return hard.toArray(new PsiReference[hard.size()]);
  }
  return references;
}
 
Example #8
Source File: FilePathCompletionContributor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Pair<FileReference, Boolean> getReference(final PsiReference original) {
  if (original == null) {
    return null;
  }

  if (original instanceof PsiMultiReference) {
    final PsiMultiReference multiReference = (PsiMultiReference)original;
    for (PsiReference reference : multiReference.getReferences()) {
      if (reference instanceof FileReference) {
        if (((FileReference)reference).getFileReferenceSet().supportsExtendedCompletion()) {
          return Pair.create((FileReference)reference, false);
        }
      }
    }
  }
  else if (original instanceof FileReferenceOwner) {
    final PsiFileReference fileReference = ((FileReferenceOwner)original).getLastFileReference();
    if (fileReference instanceof FileReference) {
      if (((FileReference)fileReference).getFileReferenceSet().supportsExtendedCompletion()) {
        return Pair.create((FileReference)fileReference, true);
      }
    }
  }

  return null;
}
 
Example #9
Source File: CppParsingTest.java    From CppTools with Apache License 2.0 5 votes vote down vote up
public void test() throws Throwable {
  doParseTest("SimpleParse");
  String docContent = myFixture.getEditor().getDocument().getCharsSequence().toString();
  String marker = "warn";

  PsiReference psiReference = myFixture.getFile().findReferenceAt(docContent.indexOf(marker) + marker.length());
  assertNotNull(psiReference);
  assertTrue(!(psiReference instanceof PsiMultiReference));

  marker = "operator ==";
  int offset = docContent.indexOf(marker) + marker.length() - 1;
  psiReference = myFixture.getFile().findReferenceAt(offset);
  assertNotNull(psiReference);
  assertTrue(!(psiReference instanceof PsiMultiReference));

  PsiElement psiElement = TargetElementUtil.getInstance().findTargetElement(myFixture.getEditor(), TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED |
    TargetElementUtil.ELEMENT_NAME_ACCEPTED, offset);

  assertNotNull(psiElement);

  marker = "operator=";
  offset = docContent.indexOf(marker) + marker.length() - 1;
  psiReference = myFixture.getFile().findReferenceAt(offset);
  assertNotNull(psiReference);
  assertTrue(!(psiReference instanceof PsiMultiReference));
  psiElement = TargetElementUtil.getInstance().findTargetElement(myFixture.getEditor(), TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED |
    TargetElementUtil.ELEMENT_NAME_ACCEPTED, offset);
  assertNotNull(psiElement);
}
 
Example #10
Source File: MakefileParsingTest.java    From CppTools with Apache License 2.0 5 votes vote down vote up
public void testCheckRefResolve3() throws Throwable {
  myFixture.testHighlighting(getTestName() + ".mk");
  PsiReference psiReference = myFixture.getFile().findReferenceAt(myFixture.getEditor().getCaretModel().getOffset());
  assertTrue(psiReference instanceof PsiMultiReference);
  ResolveResult[] resolveResults = ((PsiMultiReference) psiReference).multiResolve(false);
  assertEquals(2, resolveResults.length);
  
  assertNotNull(resolveResults[0].getElement());
  assertNotNull(resolveResults[1].getElement());

  // TODO
  //assertEquals(2, ReferencesSearch.search(resolveResults[0].getElement()).findAll().size());
  //assertEquals(2, ReferencesSearch.search(resolveResults[1].getElement()).findAll().size());
}
 
Example #11
Source File: ReferenceTest.java    From WebStormRequireJsPlugin with MIT License 4 votes vote down vote up
public void testReference()
{
    PsiReference reference;
    PsiElement referenceElement;

    // referenceNotFound
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(1, 40));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'app/as'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);

    // referenceTrue
    myFixture
            .getEditor()
            .getCaretModel()
            .moveToLogicalPosition(new LogicalPosition(2, 40));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'blocks/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());

    // referenceNotFound2
    myFixture
            .getEditor()
            .getCaretModel()
            .moveToLogicalPosition(new LogicalPosition(3, 40));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof PsiMultiReference);
    reference = ((PsiMultiReference) reference).getReferences()[1];
    assertEquals("'bl'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);

    // referenceDirectory
    myFixture
            .getEditor()
            .getCaretModel()
            .moveToLogicalPosition(new LogicalPosition(4, 40));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'blocks/childBlocks'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertNull(referenceElement);

    // referenceWithTwoDot
    myFixture
            .getEditor()
            .getCaretModel()
            .moveToLogicalPosition(new LogicalPosition(5, 46));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'../blocks/block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());

    // referenceWithTwoDotTwoDir
    myFixture
            .getEditor()
            .getCaretModel()
            .moveToLogicalPosition(new LogicalPosition(6, 49));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'../blocks/childBlocks/childBlock'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("childBlock.js", ((JSFile) referenceElement).getName());

    // referenceWithOneDot
    myFixture
            .getEditor()
            .getCaretModel()
            .moveToLogicalPosition(new LogicalPosition(7, 44));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'./block'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("block.js", ((JSFile) referenceElement).getName());

    // referenceWithOneDotTwoDir
    myFixture
            .getEditor()
            .getCaretModel()
            .moveToLogicalPosition(new LogicalPosition(8, 50));
    reference = myFixture.getReferenceAtCaretPosition();
    assertTrue(reference instanceof RequirejsReference);
    assertEquals("'./childBlocks/childBlock'", reference.getCanonicalText());
    referenceElement = reference.resolve();
    assertTrue(referenceElement instanceof JSFile);
    assertEquals("childBlock.js", ((JSFile) referenceElement).getName());
}