AppendChain (@NotNull PsiVariable target, @Nullable PsiElement element) { if (!(element instanceof PsiExpressionStatement exprStatement)) return null; if (!(exprStatement.getExpression() instanceof PsiMethodCallExpression topCall)) return null; PsiMethodCallExpression call = topCall; while (true) { if (!APPEND.test(call)) return null; PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); if (qualifier instanceof PsiMethodCallExpression) { call = (PsiMethodCallExpression)qualifier; } else if (ExpressionUtils.isReferenceTo(qualifier, target)) { return new AppendChain(call, topCall); } else { return null; } } }