int (@NotNull final Document document, @NotNull final PsiFile psiFile, final int start, final int end) { PsiJavaToken elementAtStartLineEnd = tryCast(psiFile.findElementAt(start), PsiJavaToken.class); if (elementAtStartLineEnd == null || !elementAtStartLineEnd.getTokenType().equals(JavaTokenType.SEMICOLON)) return CANNOT_JOIN; PsiElement firstElement = elementAtStartLineEnd.getParent(); PsiExpression firstValue = null; PsiVariable variable = null; if (firstElement instanceof PsiExpressionStatement firstStatement) { PsiAssignmentExpression firstAssignment = ExpressionUtils.getAssignment(firstStatement); if (firstAssignment == null) return CANNOT_JOIN; PsiReferenceExpression ref = tryCast(PsiUtil.skipParenthesizedExprDown(firstAssignment.getLExpression()), PsiReferenceExpression.class); if (ref == null) return CANNOT_JOIN; variable = tryCast(ref.resolve(), PsiVariable.class); firstValue = firstAssignment.getRExpression(); } else if (firstElement instanceof PsiLocalVariable) { variable = (PsiLocalVariable)firstElement; firstValue = variable.getInitializer(); } if (firstValue == null || !(variable instanceof PsiLocalVariable) && !(variable instanceof PsiParameter)) return CANNOT_JOIN; PsiExpressionStatement secondStatement = PsiTreeUtil.getParentOfType(psiFile.findElementAt(end), PsiExpressionStatement.class); PsiExpression secondValue = ExpressionUtils.getAssignmentTo(secondStatement, variable); if (!(secondValue instanceof PsiMethodCallExpression)) return CANNOT_JOIN; PsiExpression qualifier = ChainCallJoinLinesHandler.getDeepQualifier((PsiMethodCallExpression)secondValue); if (!ExpressionUtils.isReferenceTo(qualifier, variable)) return CANNOT_JOIN; if (ReferencesSearch.search(variable, new LocalSearchScope(secondValue)).findAll().size() > 1) return CANNOT_JOIN; qualifier.replace(firstValue); firstValue.replace(secondValue); secondStatement.delete(); return firstElement.getTextRange().getEndOffset(); }