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;
}
};
}
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);
}
}
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);
}
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());
}
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());
}
Aggregations