@Nullable static PsiExpression getDefToInline(final PsiVariable local, final PsiElement refExpr, final PsiCodeBlock block) { if (refExpr != null) { PsiElement def; if (refExpr instanceof PsiReferenceExpression && PsiUtil.isAccessedForWriting((PsiExpression) refExpr)) { def = refExpr; } else { final PsiElement[] defs = DefUseUtil.getDefs(block, local, refExpr); if (defs.length == 1) { def = defs[0]; } else { return null; } } if (def instanceof PsiReferenceExpression && def.getParent() instanceof PsiAssignmentExpression) { final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)def.getParent(); if (assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) return null; final PsiExpression rExpr = assignmentExpression.getRExpression(); if (rExpr != null) return rExpr; } } return local.getInitializer(); }
private static void addListenerRef(final DefaultActionGroup actionGroup, final String eventName, final PsiExpression listenerArg) { final PsiType type = listenerArg.getType(); if (type instanceof PsiClassType) { PsiClass listenerClass = ((PsiClassType) type).resolve(); if (listenerClass != null) { if (!isAbstractOrInterface(listenerClass)) { actionGroup.add(new MyNavigateAction(eventName + ": " + ClassPresentationUtil.getNameForClass(listenerClass, false), listenerClass)); return; } else if (listenerArg instanceof PsiReferenceExpression) { final PsiElement psiElement = ((PsiReferenceExpression)listenerArg).resolve(); if (psiElement instanceof PsiVariable) { PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(listenerArg, PsiCodeBlock.class); final PsiElement[] defs = DefUseUtil.getDefs(codeBlock, (PsiVariable)psiElement, listenerArg); if (defs.length == 1) { final PsiElement def = defs[0]; if (def instanceof PsiVariable) { PsiVariable var = (PsiVariable) def; if (var.getInitializer() != listenerArg) { addListenerRef(actionGroup, eventName, var.getInitializer()); return; } } else if (def.getParent() instanceof PsiAssignmentExpression) { final PsiAssignmentExpression assignmentExpr = (PsiAssignmentExpression)def.getParent(); if (def.equals(assignmentExpr.getLExpression())) { addListenerRef(actionGroup, eventName, assignmentExpr.getRExpression()); return; } } } } } } } actionGroup.add(new MyNavigateAction(eventName + ": " + listenerArg.getText(), listenerArg)); }
@Nullable private static PsiExpression getDefToInline(final PsiLocalVariable local, final PsiElement refExpr, final PsiCodeBlock block) { if (refExpr != null) { PsiElement def; if (refExpr instanceof PsiReferenceExpression && PsiUtil.isAccessedForWriting((PsiExpression) refExpr)) { def = refExpr; } else { final PsiElement[] defs = DefUseUtil.getDefs(block, local, refExpr); if (defs.length == 1) { def = defs[0]; } else { return null; } } if (def instanceof PsiReferenceExpression && def.getParent() instanceof PsiAssignmentExpression) { final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)def.getParent(); if (assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) return null; final PsiExpression rExpr = assignmentExpression.getRExpression(); if (rExpr != null) return rExpr; } } return local.getInitializer(); }