Search in sources :

Example 1 with TargetElementUtil

use of com.intellij.codeInsight.TargetElementUtil in project intellij-community by JetBrains.

the class GotoImplementationHandler method getSourceAndTargetElements.

@Override
@Nullable
public GotoData getSourceAndTargetElements(@NotNull Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement source = TargetElementUtil.getInstance().findTargetElement(editor, ImplementationSearcher.getFlags(), offset);
    if (source == null)
        return null;
    final PsiReference reference = TargetElementUtil.findReference(editor, offset);
    final TargetElementUtil instance = TargetElementUtil.getInstance();
    PsiElement[] targets = new ImplementationSearcher.FirstImplementationsSearcher() {

        @Override
        protected boolean accept(PsiElement element) {
            return instance.acceptImplementationForReference(reference, element);
        }

        @Override
        protected boolean canShowPopupWithOneItem(PsiElement element) {
            return false;
        }
    }.searchImplementations(editor, source, offset);
    if (targets == null)
        return null;
    GotoData gotoData = new GotoData(source, targets, Collections.emptyList());
    gotoData.listUpdaterTask = new ImplementationsUpdaterTask(gotoData, editor, offset, reference) {

        @Override
        public void onFinished() {
            super.onFinished();
            PsiElement oneElement = getTheOnlyOneElement();
            if (oneElement != null && navigateToElement(oneElement)) {
                myPopup.cancel();
            }
        }
    };
    return gotoData;
}
Also used : TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with TargetElementUtil

use of com.intellij.codeInsight.TargetElementUtil in project intellij-community by JetBrains.

the class GotoDeclarationTest method testMultipleConstructors.

public void testMultipleConstructors() throws Exception {
    String name = getTestName(false);
    configureByFile("/codeInsight/gotoDeclaration/" + name + ".java");
    final int offset = getEditor().getCaretModel().getOffset();
    final PsiElement[] elements = GotoDeclarationAction.findAllTargetElements(getProject(), getEditor(), offset);
    assertEquals(Arrays.asList(elements).toString(), 0, elements.length);
    final TargetElementUtil elementUtilBase = TargetElementUtil.getInstance();
    final PsiReference reference = getFile().findReferenceAt(offset);
    assertNotNull(reference);
    final Collection<PsiElement> candidates = elementUtilBase.getTargetCandidates(reference);
    assertEquals(candidates.toString(), 2, candidates.size());
}
Also used : TargetElementUtil(com.intellij.codeInsight.TargetElementUtil)

Example 3 with TargetElementUtil

use of com.intellij.codeInsight.TargetElementUtil in project intellij-community by JetBrains.

the class ImplementationSearcher method searchImplementations.

@Nullable
PsiElement[] searchImplementations(Editor editor, PsiElement element, int offset) {
    TargetElementUtil targetElementUtil = TargetElementUtil.getInstance();
    boolean onRef = ReadAction.compute(() -> targetElementUtil.findTargetElement(editor, getFlags() & ~(TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil.LOOKUP_ITEM_ACCEPTED), offset) == null);
    return searchImplementations(element, editor, onRef && ReadAction.compute(() -> element == null || targetElementUtil.includeSelfInGotoImplementation(element)), onRef);
}
Also used : TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with TargetElementUtil

use of com.intellij.codeInsight.TargetElementUtil in project intellij-community by JetBrains.

the class GroovyGotoTest method doTest.

private void doTest(Condition<PsiElement> verifier, String... files) {
    for (String file : files) {
        myFixture.configureByFile(file);
    }
    final TargetElementUtil targetUtil = TargetElementUtil.getInstance();
    final PsiElement target = TargetElementUtil.findTargetElement(myFixture.getEditor(), targetUtil.getReferenceSearchFlags());
    assertTrue(verifier.value(target));
}
Also used : TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) PsiElement(com.intellij.psi.PsiElement)

Example 5 with TargetElementUtil

use of com.intellij.codeInsight.TargetElementUtil in project intellij-community by JetBrains.

the class DocumentationManager method findTargetElementUnsafe.

/**
   * in case index is not ready will throw IndexNotReadyException
   */
@Nullable
private PsiElement findTargetElementUnsafe(final Editor editor, int offset, @Nullable final PsiFile file, PsiElement contextElement) {
    TargetElementUtil util = TargetElementUtil.getInstance();
    PsiElement element = assertSameProject(getElementFromLookup(editor, file));
    if (element == null && file != null) {
        final DocumentationProvider documentationProvider = getProviderFromElement(file);
        if (documentationProvider instanceof DocumentationProviderEx) {
            element = assertSameProject(((DocumentationProviderEx) documentationProvider).getCustomDocumentationElement(editor, file, contextElement));
        }
    }
    if (element == null) {
        element = assertSameProject(util.findTargetElement(editor, myTargetElementUtil.getAllAccepted(), offset));
        // Allow context doc over xml tag content
        if (element != null || contextElement != null) {
            final PsiElement adjusted = assertSameProject(util.adjustElement(editor, myTargetElementUtil.getAllAccepted(), element, contextElement));
            if (adjusted != null) {
                element = adjusted;
            }
        }
    }
    if (element == null) {
        final PsiReference ref = TargetElementUtil.findReference(editor, offset);
        if (ref != null) {
            element = assertSameProject(util.adjustReference(ref));
            if (ref instanceof PsiPolyVariantReference) {
                element = assertSameProject(ref.getElement());
            }
        }
    }
    storeOriginalElement(myProject, contextElement, element);
    return element;
}
Also used : TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)5 Nullable (org.jetbrains.annotations.Nullable)3 PsiElement (com.intellij.psi.PsiElement)2 PsiReference (com.intellij.psi.PsiReference)1