public static PsiExpression findDefinition(@NotNull PsiReferenceExpression referenceExpression, @Nullable PsiVariable variable) {
    if (variable == null) {
      final PsiElement target = referenceExpression.resolve();
      if (!(target instanceof PsiVariable)) {
        return null;
      }
      variable = (PsiVariable)target;
    }
    final PsiCodeBlock block = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class);
    if (block == null) {
      return null;
    }
    final PsiElement[] defs = DefUseUtil.getDefs(block, variable, referenceExpression);
    if (defs.length != 1) {
      return null;
    }
    final PsiElement def = defs[0];
    if (def instanceof PsiVariable target) {
      return PsiUtil.skipParenthesizedExprDown(target.getInitializer());
    }
    else if (def instanceof PsiReferenceExpression) {
      if (!(def.getParent() instanceof PsiAssignmentExpression assignmentExpression) ||
          assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) {
        return null;
      }
      return PsiUtil.skipParenthesizedExprDown(assignmentExpression.getRExpression());
    }
    return null;
  }
public static PsiExpression findDefinition(@NotNull PsiReferenceExpression referenceExpression, @Nullable PsiVariable variable)