Search in sources :

Example 16 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project android by JetBrains.

the class AndroidGotoRelatedProvider method collectRelatedJavaFiles.

@Nullable
private static Computable<List<GotoRelatedItem>> collectRelatedJavaFiles(@NotNull final XmlFile file, @NotNull final AndroidFacet facet) {
    final String resType = ResourceType.LAYOUT.getName();
    final String resourceName = AndroidCommonUtils.getResourceName(resType, file.getName());
    final PsiField[] fields = AndroidResourceUtil.findResourceFields(facet, resType, resourceName, true);
    if (fields.length == 0 || fields.length > 1) {
        return null;
    }
    final PsiField field = fields[0];
    final Module module = facet.getModule();
    final GlobalSearchScope scope = module.getModuleScope(false);
    return new Computable<List<GotoRelatedItem>>() {

        @Override
        public List<GotoRelatedItem> compute() {
            final JavaPsiFacade facade = JavaPsiFacade.getInstance(module.getProject());
            final List<PsiClass> psiContextClasses = new ArrayList<PsiClass>();
            // Explicitly chosen in the layout/menu file with a tools:context attribute?
            PsiClass declared = AndroidPsiUtils.getContextClass(module, file);
            if (declared != null) {
                return Collections.singletonList(new GotoRelatedItem(declared, "JAVA"));
            }
            for (String contextClassName : CONTEXT_CLASSES) {
                final PsiClass contextClass = facade.findClass(contextClassName, module.getModuleWithDependenciesAndLibrariesScope(false));
                if (contextClass != null) {
                    psiContextClasses.add(contextClass);
                }
            }
            if (psiContextClasses.isEmpty()) {
                return Collections.emptyList();
            }
            final List<GotoRelatedItem> result = new ArrayList<GotoRelatedItem>();
            ReferencesSearch.search(field, scope).forEach(new Processor<PsiReference>() {

                @Override
                public boolean process(PsiReference reference) {
                    PsiElement element = reference.getElement();
                    if (!(element instanceof PsiReferenceExpression)) {
                        return true;
                    }
                    element = element.getParent();
                    if (!(element instanceof PsiExpressionList)) {
                        return true;
                    }
                    element = element.getParent();
                    if (!(element instanceof PsiMethodCallExpression)) {
                        return true;
                    }
                    final String methodName = ((PsiMethodCallExpression) element).getMethodExpression().getReferenceName();
                    if ("setContentView".equals(methodName) || "inflate".equals(methodName)) {
                        final PsiClass relatedClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
                        if (relatedClass != null && isInheritorOfOne(relatedClass, psiContextClasses)) {
                            result.add(new GotoRelatedItem(relatedClass, "JAVA"));
                        }
                    }
                    return true;
                }
            });
            return result;
        }
    };
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) Computable(com.intellij.openapi.util.Computable) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project android by JetBrains.

the class AndroidMissingOnClickHandlerInspection method findRelatedActivities.

@NotNull
private static Collection<PsiClass> findRelatedActivities(@NotNull XmlFile file, @NotNull AndroidFacet facet, @NotNull DomFileDescription<?> description) {
    if (description instanceof LayoutDomFileDescription) {
        final Computable<List<GotoRelatedItem>> computable = AndroidGotoRelatedProvider.getLazyItemsForXmlFile(file, facet);
        if (computable == null) {
            return Collections.emptyList();
        }
        final List<GotoRelatedItem> items = computable.compute();
        if (items.isEmpty()) {
            return Collections.emptyList();
        }
        final PsiClass activityClass = findActivityClass(facet.getModule());
        if (activityClass == null) {
            return Collections.emptyList();
        }
        final List<PsiClass> result = new ArrayList<PsiClass>();
        for (GotoRelatedItem item : items) {
            final PsiElement element = item.getElement();
            if (element instanceof PsiClass) {
                final PsiClass aClass = (PsiClass) element;
                if (aClass.isInheritor(activityClass, true)) {
                    result.add(aClass);
                }
            }
        }
        return result;
    } else {
        return findRelatedActivitiesForMenu(file, facet);
    }
}
Also used : LayoutDomFileDescription(org.jetbrains.android.dom.layout.LayoutDomFileDescription) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project android by JetBrains.

the class AndroidGotoRelatedTest method doCheckItems.

private static void doCheckItems(List<VirtualFile> expectedTargetFiles, List<GotoRelatedItem> items, Class<?> targetElementClass) {
    final Set<VirtualFile> targetFiles = new HashSet<>();
    for (GotoRelatedItem item : items) {
        final PsiElement element = item.getElement();
        assertInstanceOf(element, targetElementClass);
        final VirtualFile targetFile = element.getContainingFile().getVirtualFile();
        assertNotNull(targetFile);
        targetFiles.add(targetFile);
    }
    assertEquals(new HashSet<>(expectedTargetFiles), targetFiles);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 19 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project android by JetBrains.

the class AndroidGotoRelatedTest method testActivityToAndManifest.

public void testActivityToAndManifest() throws Exception {
    final VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + "Manifest.xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    final VirtualFile activityFile = myFixture.copyFileToProject(BASE_PATH + "Activity1.java", "src/p1/p2/MyActivity.java");
    AndroidGotoRelatedProvider.ourAddDeclarationToManifest = true;
    final List<GotoRelatedItem> items;
    try {
        items = doGotoRelatedFile(activityFile);
    } finally {
        AndroidGotoRelatedProvider.ourAddDeclarationToManifest = false;
    }
    assertEquals(1, items.size());
    final GotoRelatedItem item = items.get(0);
    final PsiElement element = item.getElement();
    assertInstanceOf(element, XmlAttributeValue.class);
    assertEquals(manifestFile, element.getContainingFile().getVirtualFile());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement)

Example 20 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project android by JetBrains.

the class AndroidGotoRelatedTest method testActivityToLayoutAndManifest.

public void testActivityToLayoutAndManifest() throws Exception {
    final VirtualFile layout = myFixture.copyFileToProject(BASE_PATH + "layout1.xml", "res/layout/layout.xml");
    final VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + "Manifest.xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    final VirtualFile activityFile = myFixture.copyFileToProject(BASE_PATH + "Activity1.java", "src/p1/p2/MyActivity.java");
    AndroidGotoRelatedProvider.ourAddDeclarationToManifest = true;
    final List<GotoRelatedItem> items;
    try {
        items = doGotoRelatedFile(activityFile);
    } finally {
        AndroidGotoRelatedProvider.ourAddDeclarationToManifest = false;
    }
    assertEquals(2, items.size());
    XmlAttributeValue manifestDeclarationTarget = null;
    PsiFile psiFileTarget = null;
    for (GotoRelatedItem item : items) {
        final PsiElement element = item.getElement();
        if (element instanceof PsiFile) {
            psiFileTarget = (PsiFile) element;
        } else if (element instanceof XmlAttributeValue) {
            manifestDeclarationTarget = (XmlAttributeValue) element;
        } else {
            fail("Unexpected element: " + element);
        }
    }
    assertEquals(layout, psiFileTarget.getVirtualFile());
    assertEquals(manifestFile, manifestDeclarationTarget.getContainingFile().getVirtualFile());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)21 PsiElement (com.intellij.psi.PsiElement)13 NotNull (org.jetbrains.annotations.NotNull)11 PsiFile (com.intellij.psi.PsiFile)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ArrayList (java.util.ArrayList)3 ResourceBundle (com.intellij.lang.properties.ResourceBundle)2 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)2 GotoRelatedProvider (com.intellij.navigation.GotoRelatedProvider)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 Project (com.intellij.openapi.project.Project)2 Computable (com.intellij.openapi.util.Computable)2 PsiClass (com.intellij.psi.PsiClass)2 LineMarkerProvider (com.intellij.codeInsight.daemon.LineMarkerProvider)1 RelatedItemLineMarkerInfo (com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo)1 RelatedItemLineMarkerProvider (com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider)1 DomGotoRelatedItem (com.intellij.codeInsight.navigation.DomGotoRelatedItem)1 Location (com.intellij.execution.Location)1 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)1