@Nullable public static FileReference findFileReference(@NotNull 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; }
@Nullable 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); offset = element.getStartOffsetInParent() + offset; if (element instanceof PsiFile) break; 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()); }
@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) { return Pair.create((FileReference) reference, false); } } } else if (original instanceof FileReferenceOwner) { final PsiFileReference fileReference = ((FileReferenceOwner)original).getLastFileReference(); if (fileReference instanceof FileReference) { return Pair.create((FileReference) fileReference, true); } } return null; }
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); } }
/** * Overrides the findReferenceAt() logic in order to provide a resolve context with origin file for returned references. * The findReferenceAt() is usually invoked from UI operations, and it helps to be able to do deeper analysis in the * current file for such operations. * * @param offset the offset to find the reference at * @return the reference or null. */ @Override public PsiReference findReferenceAt(int offset) { // copy/paste from SharedPsiElementImplUtil PsiElement element = findElementAt(offset); if (element == null || element instanceof OuterLanguageElement) return null; offset = getTextRange().getStartOffset() + offset - element.getTextRange().getStartOffset(); List<PsiReference> referencesList = new ArrayList<PsiReference>(); final PsiFile file = element.getContainingFile(); final PyResolveContext resolveContext = file != null ? PyResolveContext.defaultContext().withTypeEvalContext(TypeEvalContext.codeAnalysis(file.getProject(), file)) : PyResolveContext.defaultContext(); while (element != null) { addReferences(offset, element, referencesList, resolveContext); offset = element.getStartOffsetInParent() + offset; if (element instanceof PsiFile) break; 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()); }
@Nullable private static FileReference findFileReference(@NotNull 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; }
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()); } }
@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; }
@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()); }
@Nullable private static PsiJavaReference findJavaReference(final PsiFile file, final int offset) { PsiReference reference = file.findReferenceAt(offset); if(reference instanceof PsiMultiReference) { for(final PsiReference psiReference : ((PsiMultiReference) reference).getReferences()) { if(psiReference instanceof PsiJavaReference) { return (PsiJavaReference) psiReference; } } } return reference instanceof PsiJavaReference ? (PsiJavaReference) reference : null; }
@Nullable private static PsiJavaReference findJavaReference(final PsiFile file, final int offset) { PsiReference reference = file.findReferenceAt(offset); if (reference instanceof PsiMultiReference) { for (final PsiReference psiReference : ((PsiMultiReference)reference).getReferences()) { if (psiReference instanceof PsiJavaReference) { return (PsiJavaReference)psiReference; } } } return reference instanceof PsiJavaReference ? (PsiJavaReference)reference : null; }
@Nullable public static JavaClassReference findJavaClassReference(final PsiFile file, final int offset) { PsiReference reference = file.findReferenceAt(offset); if (reference instanceof PsiMultiReference) { for (final PsiReference psiReference : ((PsiMultiReference)reference).getReferences()) { if (psiReference instanceof JavaClassReference) { return (JavaClassReference)psiReference; } } } return reference instanceof JavaClassReference ? (JavaClassReference)reference : null; }
protected static PsiReference[] getReferences(final PsiMultiReference multiReference) { final PsiReference[] references = multiReference.getReferences(); final List<PsiReference> hard = ContainerUtil.findAll(references, new Condition<PsiReference>() { @Override public boolean value(final PsiReference object) { return !object.isSoft(); } }); if (!hard.isEmpty()) { return hard.toArray(new PsiReference[hard.size()]); } return references; }
protected void assertNoReferences(VirtualFile file, Class refClass) throws IOException { PsiReference ref = getReferenceAtCaret(file); if (ref == null) return; PsiReference[] refs = ref instanceof PsiMultiReference ? ((PsiMultiReference)ref).getReferences() : new PsiReference[]{ref}; for (PsiReference each : refs) { assertFalse(each.toString(), refClass.isInstance(each)); } }
@Nullable @Override public PsiReference getReference() { final PsiReference[] references = getReferences(); if (references.length == 1) { return references[0]; } if (references.length > 1) { return new PsiMultiReference(references, this); } return null; }
/** 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(); }
/** * 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); }
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; }
@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; }
@NotNull private PsiPolyVariantReference getRealReference() { PsiReference[] otherReferences = ReferenceProvidersRegistry.getReferencesFromProviders(this, PsiReferenceService.Hints.NO_HINTS); PsiPolyVariantReference reference = getReferenceFromNamedArgumentProviders(); if (otherReferences.length == 0) { if (reference != null) { return reference; } else { return new PsiPolyVariantReferenceBase<PsiElement>(this) { @NotNull @Override public Object[] getVariants() { return ArrayUtil.EMPTY_OBJECT_ARRAY; } @NotNull @Override public ResolveResult[] multiResolve(boolean incompleteCode) { return ResolveResult.EMPTY_ARRAY; } }; } } else { if (reference != null) { PsiReference[] refs = new PsiReference[otherReferences.length + 1]; refs[0] = reference; //noinspection ManualArrayCopy for (int i = 0; i < otherReferences.length; i++) { refs[i + 1] = otherReferences[i]; } otherReferences = refs; } return new PsiMultiReference(otherReferences, this); } }